NEAR Smart Contracts · 2 min read
Deploying to Testnet
Create a faucet-funded testnet account, build Wasm, deploy, and call view and change methods with near-cli-rs.
Testnet uses play NEAR from a faucet. Nothing here costs real money. Official tooling is cargo-near to build and near-cli-rs (the near binary) to create accounts and deploy. The older Node near-cli package is not what current docs recommend.
Install
bash
# Wasm target for Rust contracts
rustup target add wasm32-unknown-unknown
# cargo-near (build)
curl --proto '=https' --tlsv1.2 -LsSf \
https://github.com/near/cargo-near/releases/latest/download/cargo-near-installer.sh | sh
# near-cli-rs (the `near` command)
curl --proto '=https' --tlsv1.2 -LsSf \
https://github.com/near/near-cli-rs/releases/latest/download/near-cli-rs-installer.sh | sh
# or: npm install -g near-cli-rs@latestnear defaults to testnet. Use --networkId testnet anyway so the command is explicit.
Build, create, deploy
bash
cargo near build non-reproducible-wasm
# Wasm lands in ./target/near/<crate>.wasm
CONTRACT_ID=hello-r0.yourname.testnet
near create-account "$CONTRACT_ID" --useFaucet --networkId testnet
near deploy "$CONTRACT_ID" ./target/near/hello_near.wasm --networkId testnet--useFaucet sponsors the new account on testnet. If the contract has an #[init] method and no Default, initialize in the same transaction (full near contract deploy ... with-init-call form in the quickstart) or call it right after:
bash
near call "$CONTRACT_ID" new '{"owner":"yourname.testnet"}' \
--useAccount "$CONTRACT_ID" --networkId testnetYou can also build and deploy in one step: cargo near deploy build-non-reproducible-wasm "$CONTRACT_ID".
Call it
bash
# Free read
near view "$CONTRACT_ID" get_greeting '{}' --networkId testnet
# Paid write, signed by your account
near call "$CONTRACT_ID" set_greeting '{"greeting":"Howdy"}' \
--useAccount yourname.testnet --networkId testnet
# Payable method
near call "$CONTRACT_ID" donate '{}' \
--deposit 0.01 --useAccount yourname.testnet --networkId testnetRedeploying to the same account keeps the state and replaces the Wasm. If you changed the state layout, add a migration method or use a fresh account. Mainnet is the same commands with --networkId mainnet and real NEAR — do not use the faucet there.
<h1>view_account</h1>
<p>Look up any testnet account over RPC, the same way the CLI does.</p>
<input id="acc" value="hello.near-examples.testnet" aria-label="Account id" />
<button type="button" id="go">Lookup</button>
<pre id="out">Press the button.</pre>Edit this example in the Playground.
Exercise
Deploy your greeting contract to a new *.testnet account (or reuse one). Call get_greeting with near view, then set_greeting with near call, and paste both outputs into your notes.
Saved in this browser.