Project starter template for writing Arbitrum Stylus programs in Rust using the stylus-sdk. It includes a Rust implementation of a basic counter Ethereum smart contract:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Counter {
uint256 public number;
function setNumber(uint256 newNumber) public {
number = newNumber;
}
function increment() public {
number++;
}
}
To set up more minimal example that still uses the Stylus SDK, use cargo stylus new --minimal <YOUR_PROJECT_NAME>
under OffchainLabs/cargo-stylus.
Install Rust, and then install the Stylus CLI tool with Cargo
cargo install --force cargo-stylus cargo-stylus-check
Add the wasm32-unknown-unknown
build target to your Rust compiler:
rustup target add wasm32-unknown-unknown
You should now have it available as a Cargo subcommand:
cargo stylus --help
Then, clone the template:
git clone https://github.com/OffchainLabs/stylus-hello-world && cd stylus-hello-world
All testnet information, including faucets and RPC endpoints can be found here.
You can export the Solidity ABI for your program by using the cargo stylus
tool as follows:
cargo stylus export-abi
which outputs:
/**
* This file was automatically generated by Stylus and represents a Rust program.
* For more information, please see [The Stylus SDK](https://github.com/OffchainLabs/stylus-sdk-rs).
*/
interface Counter {
function setNumber(uint256 new_number) external;
function increment() external;
}
Exporting ABIs uses a feature that is enabled by default in your Cargo.toml:
[features]
export-abi = ["stylus-sdk/export-abi"]
You can use the cargo stylus
command to also deploy your program to the Stylus testnet. We can use the tool to first check
our program compiles to valid WASM for Stylus and will succeed a deployment onchain without transacting. By default, this will use the Stylus testnet public RPC endpoint. See here for Stylus testnet information
cargo stylus check
If successful, you should see:
Finished release [optimized] target(s) in 1.88s
Reading WASM file at stylus-hello-world/target/wasm32-unknown-unknown/release/stylus-hello-world.wasm
Compressed WASM size: 8.9 KB
Program succeeded Stylus onchain activation checks with Stylus version: 1
Next, we can estimate the gas costs to deploy and activate our program before we send our transaction. Check out the cargo-stylus README to see the different wallet options for this step:
cargo stylus deploy \
--private-key-path=<PRIVKEY_FILE_PATH> \
--estimate-gas-only
You will then see the estimated gas cost for deploying before transacting:
Deploying program to address e43a32b54e48c7ec0d3d9ed2d628783c23d65020
Estimated gas for deployment: 1874876
The above only estimates gas for the deployment tx by default. To estimate gas for activation, first deploy your program using --mode=deploy-only
, and then run cargo stylus deploy
with the --estimate-gas-only
flag, --mode=activate-only
, and specify --activate-program-address
.
Here's how to deploy:
cargo stylus deploy \
--private-key-path=<PRIVKEY_FILE_PATH>
The CLI will send 2 transactions to deploy and activate your program onchain.
Compressed WASM size: 8.9 KB
Deploying program to address 0x457b1ba688e9854bdbed2f473f7510c476a3da09
Estimated gas: 1973450
Submitting tx...
Confirmed tx 0x42db…7311, gas used 1973450
Activating program at address 0x457b1ba688e9854bdbed2f473f7510c476a3da09
Estimated gas: 14044638
Submitting tx...
Confirmed tx 0x0bdb…3307, gas used 14044638
Once both steps are successful, you can interact with your program as you would with any Ethereum smart contract.
This template includes an example of how to call and transact with your program in Rust using ethers-rs under the examples/counter.rs
. However, your programs are also Ethereum ABI equivalent if using the Stylus SDK. They can be called and transacted with using any other Ethereum tooling.
By using the program address from your deployment step above, and your wallet, you can attempt to call the counter program and increase its value in storage:
abigen!(
Counter,
r#"[
function number() external view returns (uint256)
function setNumber(uint256 number) external
function increment() external
]"#
);
let counter = Counter::new(address, client);
let num = counter.number().call().await;
println!("Counter number value = {:?}", num);
let _ = counter.increment().send().await?.await?;
println!("Successfully incremented counter via a tx");
let num = counter.number().call().await;
println!("New counter number value = {:?}", num);
To run it, set the following env vars or place them in a .env
file this project, then:
STYLUS_PROGRAM_ADDRESS=<the onchain address of your deployed program>
PRIV_KEY_PATH=<the file path for your priv key to transact with>
RPC_URL=https://stylus-testnet.arbitrum.io/rpc
Next, run:
cargo run --example counter --target=<YOUR_ARCHITECTURE>
Where you can find YOUR_ARCHITECTURE
by running rustc -vV | grep host
. For M1 Apple computers, for example, this is aarch64-apple-darwin
and for most Linux x86 it is x86_64-unknown-linux-gnu
By default, the cargo stylus tool will build your project for WASM using sensible optimizations, but you can control how this gets compiled by seeing the full README for cargo stylus. If you wish to optimize the size of your compiled WASM, see the different options available here.
The stylus-sdk contains many features for writing Stylus programs in Rust. It also provides helpful macros to make the experience for Solidity developers easier. These macros expand your code into pure Rust code that can then be compiled to WASM. If you want to see what the stylus-hello-world
boilerplate expands into, you can use cargo expand
to see the pure Rust code that will be deployed onchain.
First, run cargo install cargo-expand
if you don't have the subcommand already, then:
cargo expand --all-features --release --target=<YOUR_ARCHITECTURE>
Where you can find YOUR_ARCHITECTURE
by running rustc -vV | grep host
. For M1 Apple computers, for example, this is aarch64-apple-darwin
.
This project is fully open source, including an Apache-2.0 or MIT license at your choosing under your own copyright.