import Tabs from ‘@theme/Tabs’;
import TabItem from ‘@theme/TabItem’;
import {CodeTabs, Language, Github} from "@site/components/codetabs"
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.
:::info 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.
“`ts
number, bigint, string, [], {} …
“`
“`rust
u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, Vec
“`
“`ts
u8, u16, u32, u64, i8, i16, i32, i64, Array
“`
- :::tip
- Always prefer native types in the contract’s interface. The only exception is values larger than
52 bytes
(such asu64
andu128
), for which string-like alternatives are preferred. - ::
-
::warning
Always make sure to check for underflow and overflow errors. For Rust, simply addoverflow-checks=true
in yourCargo
. - ::
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.
- :::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.
🦀 Notice that the class is decorated with multiple macros:
– `BorshDeserialize` & `BorshSerialize` allow the structure to be read and
written into the contract’s state
– `Serialize` & `Deserialize` allow the structure to be used as an input type and
return type of the contract’s methods.
:::tip
If you are curious on why the (de)serialization is needed read our [serialization documentation](./serialization.md)
:::