Smart contracts expose functions so users can interact with them. There are different types of functions including 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.
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:
“`rust
pub trait MyTrait {
fn trait_method(&mut self);
}
#[near]
impl MyTrait for MyContractStructure {
fn trait_method(&mut self) {
// .. method logic here
}
}
“`
### 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](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./storage.md).
#### `@initialize({ privateFunction: true })`
The initialization function is marked with the `@initialize` decorator.
#### `#[init]`
The initialization function is marked with the `#[init]` macro.
### State Changing Functions
The functions that modify the [state](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./storage.md) or perform [actions](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./actions.md) need to be called by a user with a NEAR account, since a transaction is required to execute them.
#### `@call`
State changing functions are marked with the `@call` decorator.
#### `mut &self`
State changing functions are those that take a **mutable** reference to `self` in Rust.
The SDK provides [contextual information](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./environment.md), such as which account is calling the function, or what time it is.
### 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.
#### `@view`
Read-only functions are marked with the `@view` decorator in TS/JS.
#### `&self`
Read-only functions are those that take an **immutable** reference to `self` in Rust.
### 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](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./crosscontract.md) should always be `private`.
These functions are marked as `private` in the contract’s code, and can only be called by the contract itself.
#### `decorator({privateFunction: true})`
Private functions are marked by setting `privateFunction: true` in the `@call` or `@initialization` decorators.
#### [#private]
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 as `payable`.
Within the function, the user will have access to the [attached deposit](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./environment.md).
#### `@call({payableFunction: true})`
Payable functions are marked by setting `payableFunction: true` in the `@call` decorator.
#### [#payable]
Payable functions are marked using the `#[payable]` macro in Rust.
### 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 JS contract, simply omit the `@view` and `@call` decorators.
### 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
Another way of not exporting methods is by having a separate `impl Contract` section, that is not marked with `#[near]`.
“`rust
#[near]
impl Contract {
pub fn increment(&mut self) {
self.internal_increment();
}
}
impl Contract {
/// This methods is still not exported.
pub fn internal_increment(&mut self) {
self.counter += 1;
}
}
“`
### Pure Functions
Pure functions are a special kind of function that do not require to access data from the state.
They are useful to return hardcoded values on the contract.
“`js
@NearBindgen({})
class Contract {
helper_function(params… ){
// this function cannot be called from the outside
}
@view({})
interface_view(params…){
// this function can be called from outside
}
@call({privateFunction: true}){
// this function can be called from outside, but
// only by the contract’s account
}
}
“`
“`rs
const SOME_VALUE: u64 = 8;
#[near]
impl MyContractStructure {
fn internal_helper(mut &self, params… ){
// this function cannot be called from the outside
}
pub fn public_log(/* Parameters here */) {
near_sdk::log!(“inside log message”);
}
pub fn return_static_u64() -> u64 {
SOME_VALUE
}
}
“`