How to Create and Deploy a Smart Contract on NEAR
A NEAR smart contract is a Rust or JavaScript program compiled to WebAssembly and deployed to a NEAR account. Setup to a live testnet contract, step by step.
On this page
Short answer
A smart contract on NEAR is a program stored on the NEAR blockchain that runs its rules automatically when it receives a signed call. You write it in Rust or JavaScript, compile it to WebAssembly, and deploy it to a NEAR account with the NEAR command-line tool.
What is a smart contract on NEAR?
A smart contract on NEAR is a program that runs on the NEAR blockchain and carries out rules automatically when it receives a signed call. NEAR is a proof-of-stake layer 1 blockchain that splits work across shards with its Nightshade design, so contracts run on a network built for many parallel transactions.
Unlike Ethereum, where contracts are written in Solidity, NEAR contracts are written in Rust or JavaScript and TypeScript, then compiled to WebAssembly. The Rust route uses near-sdk-rs and has the most examples and tooling. The JavaScript route uses near-sdk-js and suits web developers who already know the language.
Every contract lives at a NEAR account, and account names are readable, such as myapp.testnet on testnet. Calls to a contract come in two kinds: view calls read state and cost nothing, while change calls alter state, need a signature and cost gas. For how this compares with Ethereum, see deploying smart contracts on Ethereum.
What do you need before you start?
You need a small set of tools before you write any contract code, and most of them take under an hour to install.
| Tool | What it does | How to get it |
|---|---|---|
| Rust toolchain | Compiles Rust contracts to WebAssembly | Install from rustup, then add the wasm32-unknown-unknown target |
| Node.js and npm | Runs the NEAR command-line tool | Install a current long-term version |
| near-cli | Deploys contracts and calls their methods | Install with npm install -g near-cli |
| NEAR wallet | Holds your testnet account and keys | Create one in the browser wallet |
| Testnet account | A free account for testing | Create one with the testnet faucet |
Start on testnet, never on mainnet. Testnet tokens are free from the faucet, mistakes cost nothing, and the steps are the same as mainnet. Only move to mainnet when the contract has passed its tests and a review.
Should you write the contract in Rust or JavaScript?
Pick Rust if you want the most examples, the best tooling and the strongest type checking, because most NEAR documentation and templates use Rust.
| Factor | Rust | JavaScript / TypeScript |
|---|---|---|
| SDK | near-sdk-rs | near-sdk-js |
| Examples | Most NEAR guides and templates | Fewer, but growing |
| Learning curve | Steeper if you are new to Rust | Lower for web developers |
| Testing | Unit tests with the SDK test utilities | Unit tests in the JavaScript toolchain |
Both languages compile to the same WebAssembly format, so the deployed contract works the same way either way. If your team already writes Rust, that is the obvious choice. If your team is a JavaScript shop, the JavaScript SDK gets you to a working contract faster, and you can switch later if you need Rust features.
How to create and deploy the contract, step by step
Creating and deploying a NEAR contract follows the same sequence for a first contract and a production one, so learn it once and repeat it.
- Install Rust and the WebAssembly target. Install Rust with rustup, then run rustup target add wasm32-unknown-unknown.
- Install near-cli. Run npm install -g near-cli and check it with near --version.
- Create the project. Start from a template with cargo generate or npx create-near-app, which gives you a contract folder, tests and a frontend.
- Write the contract. Define the stored state and the methods. Mark which methods change state and which only read it.
- Build it. Run cargo near build, or cargo build --target wasm32-unknown-unknown --release, which produces a .wasm file.
- Create a testnet account. Use the faucet to create an account such as myapp.testnet.
- Log in with the CLI. Run near login and approve the request in the browser, which saves a key locally.
- Deploy. Run near deploy with your account name and the path to the .wasm file.
- Initialise the contract. Call the init method once, for example near call myapp.testnet new '{}' --accountId myapp.testnet.
- Test it. Call a view method with near view to read state, then call a change method and read the state again.
For the general shape of a contract project, see how to create a smart contract with Remix, which covers the same ideas on Ethereum tooling.
How do you test a NEAR contract?
Test a NEAR contract in three layers: unit tests, a throwaway dev account and the shared testnet, because each layer catches different problems.
Unit tests run inside the Rust or JavaScript toolchain and use the SDK test utilities to set up a fake blockchain context. They are fast and catch logic errors, such as a method that updates the wrong field. Write them for every method that changes state.
Next, deploy to a dev account with near dev-deploy, which creates a fresh account each time. This tests the real deploy path, the init call and the gas each method uses, without touching an account you care about.
Finally, deploy to your named testnet account and test with real wallet signing. Check that view calls return what you expect, that change calls fail cleanly when given bad input, and that the gas each call uses stays well under the 300 TGas limit per transaction. A call that uses 30 TGas, for example 30000000000000 gas units, leaves plenty of room.
Example (hypothetical): deploying a counter contract
Example (hypothetical): a team builds a counter contract with one stored number, a method to read it and a method to add one. The contract starts at zero.
The team deploys to testnet and calls the increment method 200 times during testing. Each call uses 30 TGas, so the 200 calls use 200 x 30 = 6,000 TGas in total, and each single call stays well under the 300 TGas limit for one transaction.
After the 200 calls, a view call returns 200, which matches the count exactly. The team then tests a bad call with no signature and confirms it fails, and a call with a negative step confirms the contract rejects it. Only after all three checks pass does the team plan a mainnet deploy.
What are the common mistakes?
The most common mistakes are deploying to mainnet first, forgetting the init call, and leaving change methods open to any caller.
- Testing on mainnet. Mainnet tokens cost real money. Test on testnet until the contract is reviewed.
- Skipping the init call. A contract that is never initialised has no starting state, and its methods fail or behave oddly.
- No access control. Any account can call a change method unless the code checks the caller, so guard admin methods.
- Hardcoding account names. A contract that only works with one account name cannot move between testnet and mainnet.
- Ignoring storage costs. Storing data on NEAR costs tokens, so a contract that stores large records gets expensive.
- No unit tests. A contract without tests is a contract you have not checked, and fixes after deploy are costly.
Key takeaways
- NEAR contracts are written in Rust or JavaScript and compiled to WebAssembly
- Deploy with near-cli to a testnet account first, then to mainnet
- View calls are free; change calls need a signature and cost gas
- Test in three layers: unit tests, a dev account and testnet
- Guard change methods so only the right accounts can call them
Frequently asked questions
Do I need to know Rust to build on NEAR?
No. You can write NEAR contracts in JavaScript or TypeScript with near-sdk-js, which suits web developers. Rust with near-sdk-rs has more examples and tooling, so pick Rust if your team already knows it, and JavaScript if speed of learning matters more.
What is near-cli?
near-cli is the command-line tool for NEAR. It creates accounts, deploys compiled contracts, calls contract methods and reads state. You install it with npm, log in with near login, and then run commands such as near deploy and near call against testnet or mainnet.
How much does it cost to deploy a contract on NEAR?
Deployment costs gas, which is paid in NEAR tokens, plus storage costs for the data the contract holds. On testnet the tokens are free from the faucet, so testing costs nothing. On mainnet the cost depends on the contract size and the data it stores, so measure both on testnet first.
What is the difference between a view call and a change call?
A view call only reads state, needs no signature and costs nothing. A change call alters state, must be signed by an account and costs gas. Reading a counter is a view call; adding one to it is a change call.
Can I redeploy a contract after it is live?
Yes, the account that owns the contract can deploy a new version over it, but the stored state stays as it is, so the new code must work with the old data. Plan upgrades before launch, and consider a migration method if the state shape changes.
What is the 300 TGas limit?
Each NEAR transaction can use at most 300 TGas of gas, where 1 TGas is one trillion gas units. Most simple calls use far less, such as 30 TGas. If a call needs more than the limit, split the work across several calls or simplify the method.