Smart Contract Layout
Structure of NEAR smart contract
Simple Counter Contract
#[near_bindgen]
tells the program generate any necessary boilerplate to expose the necessary functions.
Any
pub
functions will be callable externally from any account/contract.For more information, see public methods
self
can be used in multiple ways to control the mutability of the contract:Functions that take
&self
orself
will be read-only and do not write the updated state to storageFunctions that take
&mut self
allow for mutating state, and state will always be written back at the end of the function call
Exposed functions can omit reading and writing to state if
self
is not included in the function paramsThis can be useful for some static functionality or returning data embedded in the contract code
If the function has a return value, it will be serialized and attached as a result through
env::value_return
Initialisation
By default, the
Default::default()
implementation of a contract will be used to initialize a contract. There can be a custom initialization function which takes parameters or performs custom logic using the#[init]
annotation.
All contracts are expected to implement
Default
. If you would like to prohibit the default implementation from being used, thePanicOnDefault
derive macro can be used:
More on Rust’s procedural macros:
Last updated