Building Smart Contracts with Rust
Khushboo Gehi, MSc
Doctoral Researcher at SnT, Interdisciplinary Center for Security, Reliability and Trust
Smart contracts are event-driven programs stored on a blockchain that trigger action when certain conditions are met. They are used to automate & fast-track the execution of agreements between participants without involving intermediaries. Smart contracts can be accessed by anyone on a blockchain, once published. They are smaller and simpler than other programs due to the expenses spent in deploying them. Being transparent and immutable, they make reliable and impartial third parties. Their execution is serverless and decentralized/not controlled by a single entity.
Smart contracts are written in various programming languages depending on the blockchain they are run on. For example, Solidity is used on Ethereum and other EVM compatible blockchains, Cadence on Flow and Rust on Solana, Near, Substrate and so on.
Rust is a general-purpose and Turing complete programming language designed for performance and safety with syntax like C++. Though it wasn’t really designed for blockchain like the other Smart contract languages specific to their blockchain networks, Rust has become increasingly popular over the past five years and its usage is expected to grow even further in the coming years.
Rust enforces memory management rules at compile time, it uses minimal runtime resources, which reduces overheads, is safe and efficient. It comes with the cargo and crates ecosystem that make the development process more productive. Smart contracts written in Rust are compiled to wasm or web assembly. The WebAssembly System Interface, WASI is an initiative to implement development standards in blockchain with smart contracts being one of its modules. Its implementation would enable developers to run smart contracts universally on blockchains supported by WASI. Web Assembly satisfies requirements of a virtual machine required to run smart contracts and overcomes the limitations of Ethereum Virtual Machine (EVM). Some of the Rust APIs include near_bindgen on NEAR, smart_contract for Wavelet, EWASM for Ethereum like blockchains, Ink!, Mantle and so on.
Ink! and Mantle are embedded Domain Specific Language (eDSL) based APIs that require developers to follow certain rules or patterns to turn their Rust code into smart contracts whereas near_bingden and wavelet do not require much modifying or refactoring of code as a contract interface.?Their APIs automatically generate and inject code using macros and even allow developers to generate eDSL style API smart contracts.
All of them have a common architecture comprising of a wasm executor in which Rust contracts are executed, and the interfaces between contract and blockchain comprising methods that facilitate interaction between the two. The client side programs, or web applications communicate using RPC interface, APIs specific to each blockchain sharing a common purpose of communicating with the blockchain. For example, Solana uses JSON RPC and Near uses NEAR RPC API to interact between smart contract and client application. The RPC interface allow applications to broadcast signed transactions to nodes, as well as query the current state of the blockchain (account balances, access keys, view function calls, etc.)
领英推荐
Some resources and motivation to get started building smart contracts with Rust-
-?????????https://developer.concordium.software/en/mainnet/smart-contracts/general/contract-module.html
-?????????Hello World: https://soldev.app/course/hello-world-program
-?????????Note-Taking Application: https://soldev.app/course/deserialize-instruction-data