Modules, Types & Structs
When writing smart contracts you will leverage common programming concepts such:
Modules
Modules help you to organize your code and reuse third-party libraries.
The main module you will use in your contract will be the NEAR SDK, which: gives you access to the execution environment, allows you to call other contracts, transfer tokens, and much more.
- 🌐 JavaScript
 - 🦀 Rust
 - 🚀 AssemblyScript
 
loading...
loading...
loading...
As a general rule of thumb for Rust, anything that supports wasm32-unknown-unknown will be compatible with your smart contract.
However, we do have a size limit for a compiled contract binary which is ~4.19 MB, so it is possible that certain large libraries will not be compatible.
Native Types
When writing contracts you have access to all the language's native types.
- 🌐 JavaScript
 - 🦀 Rust
 - 🚀 AssemblyScript
 
number, bigint, string, [], {} ...
u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, Vec<T>, HashMap<K,V> ...
u8, u16, u32, u64, i8, i16, i32, i64, Array<T>, Map<K,V> ...
Always prefer native types in the contract's interface. The only exception is values larger than 52 bytes (such as u64 and u128), for which string-like alternatives are preferred.
Always make sure to check for underflow and overflow errors. For Rust, simply add overflow-checks=true in your Cargo.
SDK Collections
Besides the native types, the NEAR SDK implements collections such as Vector and UnorderedMap
to help you store complex data in the contract's state.
- 🌐 JavaScript
 - 🦀 Rust
 - 🚀 AssemblyScript
 
loading...
loading...
loading...
Always prefer SDK collections over native ones in the contract's attributes (state).
Internal Structures
You can define and instantiate complex objects through classes and structures.
- 🌐 JavaScript
 - 🦀 Rust
 - 🚀 AssemblyScript
 
loading...
loading...
🦀 Notice that the class is decorated with multiple macros:
BorshDeserialize&BorshSerializeallow the structure to be read and written into the contract's stateSerialize&Deserializeallow the structure to be used as an input type and return type of the contract's methods.mẹoIf you are curious on why the (de)serialization is needed read our serialization documentation
loading...