Importing the SDK
All contracts import the NEAR SDK, enabling them to access the execution environment, call other contracts, transfer tokens, and much more.- Rust
- JavaScript
Contract Structure
The contract is described through a structure:- The attributes define which data the contract stores
- The functions define its public (and private) interface
- Rust
- JavaScript
Contract Struct MacroThe
#[near(contract_state)] macro tells the SDK that this structure defines the contract’s state, so it knows:- What to fetch from storage when the contract is loaded
- What to store when the contract is done executing
#[near] macro tells the SDK which functions are exposed to the outside world.Note: Only one struct can be decorated with the #[near(contract_state)] macro.Storage (State)
We call the data stored in the contract the contract’s state. In our Hello World example, the contract stores a single string (greeting), and the state starts initialized with the default value "Hello".
- Rust
- JavaScript
Learn More About State
Deep dive into contract state management and storage
Read-Only Functions
Contract functions can be read-only, meaning they don’t modify the state. Calling them is free for everyone, and does not require having a NEAR account.- Rust
- JavaScript
In Rust, read-only functions are those that take an immutable reference to
self (&self).Learn More About Functions
Explore all function types and their use cases
State-Changing Functions
Functions that modify the state or call other contracts are considered state-changing functions. It is necessary to have a NEAR account to call them, as they require a transaction to be sent to the network.- Rust
- JavaScript
In Rust, state-changing functions are those that take a mutable reference to
self (&mut self).Contextual Information
The SDK provides contextual information about the execution environment, such as:- Who called the function -
predecessor_account_id - Current block timestamp -
block_timestamp - Attached deposit -
attached_deposit - Available gas -
prepaid_gas
- Rust
- JavaScript
Internal Functions
Contracts can also have private internal functions - such as helper or utility functions - that are not exposed to the outside world.- Rust
- JavaScript
To create internal private methods in Rust, do not declare them as public (
pub fn).Next Steps
Functions
Learn about all function types
Storage
Understand state management
Cross-Contract Calls
Call other contracts
Testing
Test your contracts