near-sdk

Rust library for writing NEAR smart contracts.

Previously known as near-bindgen.

Crates.io version Download Reference Documentation Join the community on Discord Buildkite Build

Features | Pre-requisites | Writing Rust Contract | Building Rust Contract | Reference Documentation | Contributing

Release notes

Release notes and unreleased changes can be found in the CHANGELOG

Example

Wrap a struct in #[near_bindgen] and it generates a smart contract compatible with the NEAR blockchain: ```rust use nearsdk::{nearbindgen, env};

[near_bindgen]

[derive(Default, BorshDeserialize, BorshSerialize)]

pub struct StatusMessage { records: HashMap, }

[near_bindgen]

impl StatusMessage { pub fn setstatus(&mut self, message: String) { let accountid = env::signeraccountid(); self.records.insert(account_id, message); }

pub fn get_status(&self, account_id: AccountId) -> Option<String> {
    self.records.get(&account_id).cloned()
}

} ```

Features

[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]

pub struct StatusMessage { records: HashMap, } ```

To declare a payable method simply use #[payable] decorator: ```rust

[payable]

pub fn my_method(&mut self) { ... } ```

To declare a private method use #[private] decorator: ```rust

[private]

pub fn my_method(&mut self) { ... } /// Which is equivalent to

pub fn mymethod(&mut self ) { if nearsdk::env::currentaccountid() != nearsdk::env::predecessoraccountid() { nearsdk::env::panicstr("Method mymethod is private"); } ... } ```

Now, only the account of the contract itself can call this method, either directly or through a promise.

Pre-requisites

To develop Rust contracts you would need to: * Install Rustup: bash curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh * Add wasm target to your toolchain: bash rustup target add wasm32-unknown-unknown

Writing Rust Contract

You can follow the examples/status-message crate that shows a simple Rust contract.

The general workflow is the following: 1. Create a crate and configure the Cargo.toml similarly to how it is configured in examples/status-message/Cargo.toml; 2. Crate needs to have one pub struct that will represent the smart contract itself: * The struct needs to implement Default trait which NEAR will use to create the initial state of the contract upon its first usage; * The struct also needs to implement BorshSerialize and BorshDeserialize traits which NEAR will use to save/load contract's internal state;

Here is an example of a smart contract struct: ```rust use nearsdk::{nearbindgen, env};

#[near_bindgen] #[derive(Default, BorshSerialize, BorshDeserialize)] pub struct MyContract { data: HashMap } ```

  1. Define methods that NEAR will expose as smart contract methods:

    Here is an example of smart contract methods: ```rust

    [near_bindgen]

    impl MyContract { pub fn insertdata(&mut self, key: u64, value: u64) -> Option { self.data.insert(key) } pub fn getdata(&self, key: u64) -> Option { self.data.get(&key).cloned() } } ```

Building Rust Contract

We can build the contract using rustc: bash RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --release

Building with reproducible builds

Since WebAssembly compiler includes a bunch of debug information into the binary, the resulting binary might be different on different machines. To be able to compile the binary in a reproducible way, we added a Dockerfile that allows to compile the binary.

Use contract-builder

Contributing

If you are interested in contributing, please look at the contributing guidelines.