Rust
Building in Rustβ
To benefit maximally from the efficiency of WASM based smart contracts, you can use the Fluentbase SDK to build your Rust contracts.
For Rust smart contracts, any no_std
compatible crate (e.g., rand
, alloc
) can be used. Standard library crates are not supported. Verified libraries can be found on crates.io.
Before getting started, make sure to install either of the following:
-
Clang and CMake: Install these if they are not already on your system.
-
For macOS users, LLVM can be installed via Homebrew:
brew install llvm
-
gblend
: find the installation instructions here
Start a new Projectβ
To create a project, run the following in your terminal:
gblend init my-project
This will install a simple template that expands Foundry's counter default example. The project when using the command above, has the following structure:
my-project/
βββ src/
β βββ BlendedCounter.sol # Solidity contract
β βββ power-calculator/ # Rust/WASM module
β βββ Cargo.toml
β βββ src/
β βββ lib.rs
βββ test/ # Forge tests
βββ script/ # Deployment scripts
β ββ foundry.toml # Configuration
In this guide, you'll only be focusing on Rust contracts so you can remove:
rm src/BlendedCounter.sol
rm script/BlendedCounter.s.sol
rm test/BlendedCounter.t.sol
and rename power-calculator/
to something more appropriate in this case,greeting/
.
Also rename the package name in Cargo.toml
to greeting
.
The Main Contract File: lib.rs
β
Replace the lib.rs
file located in the src/greeting/src
folder with the following (representative of the structure for any Rust smart contract):
lib.rs
// 1. Set the target to wasm32 and enable no_std for compatibility.
// no_std is required since WebAssembly's minimal runtime lacks support for Rustβs standard library.
#![cfg_attr(target_arch = "wasm32", no_std)]
// 2. Import FluentBase SDK
extern crate fluentbase_sdk;
use fluentbase_sdk::{basic_entrypoint, derive::Contract, SharedAPI};
// 3. Define Contract Struct with SDK: Acts as the core of the contractβs logic.
#[derive(Contract)]
struct GREETING<SDK> {
sdk: SDK,
}
impl<SDK: SharedAPI> GREETING<SDK> {
// 4. Deployment Logic: Placeholder for setup during deployment.
fn deploy(&mut self) {
// Add any custom deployment logic here
}
// 5. Core Contract Logic: writes "Hello, World" message to output.
fn main(&mut self) {
self.sdk.write("Hello, World".as_bytes());
}
}
// 6. Set Entry Point: Connects GREETING struct to the WASM runtime.
basic_entrypoint!(GREETING);
This snippet shows how simple it is to interact with the Fluent VM: just call the SDK, initialize your contract, add the functions, and, if needed, define a custom deployment process.
Compiling the Rust Smart Contractβ
Understanding the deployment process starts with comprehending the role of gblend
, which compiles Rust code into rWasm and generates the necessary binaries that will be embedded in a tx for deployment.
gblend build
Note: the first build might take a few minutes and should take a few seconds after the first run.
Executing this command compiles the code and generates build artifacts in the out
directory.
Update Rust crate fluentbase-sdk
in the contract package if there are issues:
# from src/greeting/
cargo clean
cargo update -p fluentbase-sdk
Deploying the Contractβ
To deploy contract, use gblend cli:
- Testnet
- Devnet
gblend create Greeting.wasm \
--rpc-url https://rpc.testnet.fluent.xyz \
--private-key $PRIVATE_KEY \
--broadcast \
--verify \
--wasm \
--verifier blockscout \
--verifier-url https://testnet.fluentscan.xyz/api/
gblend create Greeting.wasm \
--rpc-url https://rpc.devnet.fluent.xyz \
--private-key $PRIVATE_KEY \
--broadcast \
--verify \
--wasm \
--verifier blockscout \
--verifier-url https://devnet.fluentscan.xyz/api/
Upon successful deployment, the receipt of your deployment transaction will be displayed, confirming the smart contract deployment on Fluent using the Fluentbasse SDK.
To view your deployed Fluent contract, navigate to the Fluent block explorer. From there, you can input your contract address to explore your deployed contract.
When building Rust contracts with Fluentbase SDK, you can use the gblend
CLI to build, deploy and verify your contracts.