read-only, private and payable.
Contract’s Interface
All public functions in the contract are part of its interface. They can be called by anyone, and are the only way to interact with the contract.lib.rs
Exposing trait implementations
Exposing trait implementations
Functions can also be exposed through trait implementations. This can be useful if implementing a shared interface or standard for a contract. This code generation is handled very similarly to basic
pub functions, but the #[near] macro only needs to be attached to the trait implementation, not the trait itself:Initialization Functions
A contract can opt to have an initialization function. If present, this function must be called before any other to initialize the contract. The initialization function is marked with the#[init] macro.
lib.rs
State Changing Functions
The functions that modify the state or perform actions need to be called by a user with a NEAR account, since a transaction is required to execute them. State changing functions are those that take a mutable reference toself in Rust.
lib.rs
lib.rs
Read-Only Functions
Contract’s functions can be read-only, meaning they don’t modify the state. Calling them is free for everyone, and does not require to have a NEAR account. Read-only functions are those that take an immutable reference toself in Rust.
lib.rs
Private Functions
Many times you will want to have functions that are exposed as part of the contract’s interface, but should not be called directly by users. Besides initialization functions, callbacks from cross-contract calls should always beprivate.
These functions are marked as private in the contract’s code, and can only be called by the contract itself.
Private functions are marked using the #[private] macro in Rust.
Payable Functions
By default, functions will panic if the user attaches NEAR Tokens to the call. Functions that accept NEAR Tokens must be marked aspayable.
Within the function, the user will have access to the attached deposit.
Payable functions are marked using the #[payable] macro in Rust.
lib.rs
Internal Functions
All the functions we covered so far are part of the interface, meaning they can be called by an external actor. However, contracts can also have private internal functions - such as helper or utility functions - that are not exposed to the outside world. To create internal private methods in a Rust contract, do not declare them as public (pub fn).
Separate impl block
Separate impl block
Another way of not exporting methods is by having a separate
impl Contract section, that is not marked with #[near].SDK types
Smart contracts can receive, store, and return data using native Rust types such as strings, integers, booleans, maps, and vectors. The SDK also provides types for values that need special handling at the contract boundary.Large integers
Contracts can storeu64 and u128 values directly, but JSON consumers cannot always represent them without losing precision. Use the SDK’s U64 and U128 wrapper types for public inputs and outputs that need to be serialized as strings.
Complex objects
Contracts can receive and return complex Rust structures. Objects used as JSON inputs or outputs need JSON serialization, while structures stored in contract state need Borsh serialization.Tokens and accounts
UseNearToken to represent NEAR amounts. It provides constructors and accessors for yoctoNEAR, millinear, and NEAR values.
AccountId type validates NEAR account IDs when values enter the contract. Prefer it to a plain String whenever a value represents an account.