Skip to main content
Smart contracts are pieces of executable code that live in a NEAR account. They can store data, perform transactions in the account’s name, and expose methods so other accounts can interact with them. img Smart contracts in NEAR are written in Rust and then compiled into WebAssembly. During development, you can test them locally using unit tests and NEAR Sandbox. Sandbox tests start a local blockchain and handle account creation, balances, and contract setup in code. You do not need a public account, faucet tokens, or a wallet login to run them. When you are ready to share your contract, you can deploy it to testnet and connect a frontend.
Do not worry if you don’t know how smart contract blockchains work. As a developer, it is sufficient to understand that NEAR smart contracts:
  1. Have limited computational resources.
  2. Interact with other contracts in an asynchronous way.
  3. Deal with real money, for which security must be a top concern.

Where do contracts live?

Smart contracts are deployed to NEAR accounts. Any NEAR account can hold a contract and must pay for the contract’s code and the data it stores. Once in an account, anyone can interact with the contract. Thanks to the underlying network structure, executing code from a contract is both fast (average finality of 1.2 seconds) and cheap. Read-only operations are free for everyone.
Storing 100 KB costs 1 Ⓝ, so deploying a contract generally costs only a few $NEARs.
Since protocol version 2.6, NEAR supports global contracts, which can be deployed to multiple accounts without paying for code storage each time.

What can they do?

Smart contracts have complete control over the account and can therefore perform any action on its behalf. For example, contracts can:
  • Transfer $NEAR tokens
  • Call methods on other contracts
  • Create new accounts and deploy contracts on them
  • Update their own code
In addition, smart contracts can store data in the account’s storage. This allows contracts to create almost any type of application, from simple games to complex financial systems.
What contracts cannot do
  • Smart contracts cannot access the internet, so they cannot make HTTP requests or access external data
  • Smart contracts cannot execute automatically; they must be called by an external account

A contract is not a backend

Contract state is for the data the contract needs to enforce its rules, such as balances, ownership, or the current highest bid. It is not a database for your app. Everything a transaction does is already recorded on-chain: who called which method, the arguments they passed, and the tokens they attached. Indexers and data services can read this activity back at any time. If your app only needs to display some data, such as messages, posts or history, have the contract validate the call and leave the data in the transaction, then read it with an indexer instead of storing it in the contract. Storage is also the expensive part: every byte requires staking NEAR (1 Ⓝ per 100 KB), while computation only costs gas. Never store values you can derive from other values. For example, if a message is premium when the user attaches at least 0.1 Ⓝ, store the amount or the premium flag, not both.
A guest book that stores every message in the contract pays for the storage of each message for as long as it exists. Instead, a production guest book can expose an add_message(text) method that only checks the attached deposit. The message stays in the transaction’s arguments, and the frontend lists messages by asking an indexer for every call to add_message.
If several contracts need to report the same kind of activity in a standard format, such as fungible or non-fungible token transfers, emit events. For data that only your app reads, indexing the method calls is enough.

What are they used for?

Smart contracts are used to create decentralized applications. Common examples include: For instance, you can easily create a crowdfunding contract that accepts $NEAR. If the goal is met in time, the creator can claim the funds. Otherwise, the backers are refunded.

Smart Contract SDK

near-sdk-rs is NEAR’s only official smart-contract SDK. Use it to write contracts in Rust and compile them to WebAssembly. Start with the smart-contract quickstart, or see the Rust SDK guide for reference documentation.