Skip to main content

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.

contract/src/contract.ts
loading...
Using external libraries

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.

number, bigint, string, [], {} ...
tip

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.

danger

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.

storage-js/src/index.ts
loading...
tip

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.

contract/src/model.ts
loading...