How to Write NEAR Smart Contracts with Rust

Jun 7, 2022 | Blockchain

Welcome to a user-friendly guide on utilizing the NEAR SDK, a Rust library designed specifically for writing smart contracts on the NEAR blockchain. This guide will walk you through the steps to write, build, and deploy your first NEAR smart contract, complete with intuitive explanations and troubleshooting tips.

Getting Started

Before we dive into coding, there are a few prerequisites to ensure you have everything set up properly:

  • Install Rustup.
  • Add the WASM target to your toolchain with the command: rustup target add wasm32-unknown-unknown.

Writing Your First Rust Contract

Creating a smart contract is like crafting a recipe; you need to define the structure and the ingredients.

use near_sdk::{near, env};

#[near(contract_state)]
#[derive(Default)]
pub struct StatusMessage {
    records: HashMap,
}

#[near]
impl StatusMessage {
    pub fn set_status(mut self, message: String) {
        let account_id = env::signer_account_id();
        self.records.insert(account_id, message);
    }

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

In this analogy, think of the StatusMessage struct as a jar that holds different flavors of jam (status messages) for various users (account IDs). The set_status method is how you add a new flavor to the jar, while the get_status method helps you retrieve a specific flavor.

Building and Deploying the Contract

After writing the code, the next step is building and deploying your contract. You can make use of cargo-near, a recommended tool for building and deploying Rust contracts.

The commands to get started are:

  • cargo near build – Builds the NEAR smart contract and its ABI.
  • cargo near deploy – Guides you through the deployment process to the blockchain.

Troubleshooting Tips

If you encounter issues while writing or deploying your contract, consider the following:

  • Ensure you have the correct dependencies in your Cargo.toml file.
  • Check that your Rust and toolchain versions are compatible; the minimum supported version is 1.76.
  • If you receive compilation errors, see if there are any mismatches in syntax or missing imports.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Advanced Features

As you become more familiar with NEAR, explore advanced features such as:

  • **Unit Testing**: Easily write and run tests for your contract functions.
  • **Cross-Contract Calls**: Understand asynchronous contract interactions.
  • **Payable Methods**: Make your methods capable of accepting token transfers using the #[payable] decorator.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Conclusion

Congratulations on taking the first steps towards becoming a NEAR smart contract developer using Rust! Whether you are building basic contracts or exploring more complex features like cross-contract calls and asynchronous execution, the NEAR SDK sets you up for success. Happy coding!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox