Near Learn

Deploy to testnet

Deploy the crowdfunding contract to NEAR testnet: create accounts with near-cli-rs, cargo near deploy with an init call, then pledge and query campaigns.

Beginner6 min read3-question check

Tests pass; time to put the contract on a public network. On NEAR a contract does not get a fresh address at deploy time: you deploy to an account you control, and that account’s name becomes the contract’s address. The usual layout is a personal account plus a sub-account for each contract.

AccountRoleCreated with
you.testnetYour personal account; pays for the sub-accountFaucet-sponsored create-account
crowdfund.you.testnetHolds the contract code and all its statecreate-account fund-myself, signed by you.testnet
alice.testnetA second account to act as a backerFaucet-sponsored create-account
The accounts we use

Create the accounts#

Use your own names: testnet account ids are first come, first served
Shell
# a personal account, funded by the testnet faucet; the key goes to your keychain
near account create-account sponsor-by-faucet-service you.testnet \
  autogenerate-new-keypair save-to-keychain network-config testnet create

# a sub-account for the contract, funded with 5 NEAR from you.testnet
near account create-account fund-myself crowdfund.you.testnet '5 NEAR' \
  autogenerate-new-keypair save-to-keychain \
  sign-as you.testnet network-config testnet sign-with-keychain send

# a backer to test with
near account create-account sponsor-by-faucet-service alice.testnet \
  autogenerate-new-keypair save-to-keychain network-config testnet create

Build, deploy and initialize in one command#

cargo near deploy builds the wasm, deploys it and calls new() in the same transaction
Shell
cargo near deploy build-non-reproducible-wasm crowdfund.you.testnet \
  with-init-call new json-args '{}' \
  prepaid-gas '100.0 Tgas' attached-deposit '0 NEAR' \
  network-config testnet sign-with-keychain send

Deploying and initializing in one transaction matters: if you deployed first and called new later, anyone could front-run the init. Our new takes no arguments, so a front-run would be harmless here — but make it a habit for contracts whose new sets an owner.

Running cargo near deploy with no arguments starts an interactive mode that asks for each part and prints the full command at the end, which is handy the first time.

Use it#

Create a 1-hour campaign with a 10 NEAR goal, back it, read it
Shell
# create: goal is a U128 string in yoctoNEAR (10 NEAR), duration in ms (1 hour)
near contract call-function as-transaction crowdfund.you.testnet create_campaign \
  json-args '{"title": "Community garden", "goal": "10000000000000000000000000", "duration_ms": 3600000, "token": null}' \
  prepaid-gas '30 Tgas' attached-deposit '0.1 NEAR' \
  sign-as you.testnet network-config testnet sign-with-keychain send

# pledge 2 NEAR as alice
near contract call-function as-transaction crowdfund.you.testnet pledge \
  json-args '{"campaign_id": 0}' \
  prepaid-gas '30 Tgas' attached-deposit '2 NEAR' \
  sign-as alice.testnet network-config testnet sign-with-keychain send

# read (free)
near contract call-function as-read-only crowdfund.you.testnet get_campaign \
  json-args '{"campaign_id": 0}' network-config testnet now

# after the deadline: refund (goal missed) — or claim, if you pledged 10+ NEAR
near contract call-function as-transaction crowdfund.you.testnet refund \
  json-args '{"campaign_id": 0}' \
  prepaid-gas '50 Tgas' attached-deposit '0 NEAR' \
  sign-as alice.testnet network-config testnet sign-with-keychain send

# inspect the contract account: balance, locked storage, code hash
near account view-account-summary crowdfund.you.testnet network-config testnet now

Check yourself

3 questions · progress saved in this browser

  1. 1.What is the address of a NEAR contract?
  2. 2.Why use with-init-call new instead of deploying and calling new in a separate transaction?
  3. 3.A 300 KB wasm is deployed to an account. Roughly how much NEAR does the code itself lock as storage?