Write a smart contract in Rust using Stylus
Rust toolchain
Follow the instructions on Rust Lang’s installation page to get a full Rust toolchain installed on your system. Make sure after installation that you have access to the programsrustup, rustc, and cargo from your preferred command line terminal (programs should be added to your system’s PATH, more instructions available on Rust’s website)
VS Code
We recommend VSCode as the IDE of choice for developing Stylus contracts for its excellent Rust support. See code.visualstudio.com to install. Feel free to use another text editor or IDE if you’re comfortable with those. Some helpful VS Code extensions for Rust development:Testnet ETH for deployment
You’ll need some testnet ETH for deploying your Rust contract for live testing. Explained below in further detail.Developer wallet / account
When deploying on and interacting with a testnet chain, it’s important to use a fresh wallet that does not contain any real assets. You’ll often be including private keys as CLI arguments to execute transactions programmatically, so avoid using personal accounts for development. If you’re using MetaMask, simply click the dropdown at the top middle of the plugin and then click “Add Account” to create a fresh account. It can be helpful to label the account as a dev wallet or “Stylus” for this purpose. You’ll need this newly created account’s private key (as well as some Sepolia ETH) for deploying a smart contract. Follow the instructions on MetaMask’s website to obtain your key.
Testnet ETH
The Stylus testnet settles directly to the Arbitrum Sepolia testnet. Follow these steps to acquire testnet ETH on the Stylus testnet:- Navigate to https://bwarelabs.com/faucets/arbitrum-stylus-testnet.
- Enter your wallet address into the text field.
- Click
Claimand optionally follow the second step to receive extra testnet tokens. - You should now have Sepolia ETH on the Stylus testnet.
Creating a Stylus project
cargo-stylus is our CLI tool for assisting with building, verifying, and deploying Arbitrum Stylus programs in Rust. This is available as a plugin to the standard Cargo tool used for developing Rust programs, integrating easily into common Rust workflows. Once Rust has been installed on your system, install the Stylus CLI tool by running the following command:
Overview
The cargo stylus command comes with useful commands such asnew, check and deploy, and export-abi for developing and deploying Stylus programs to Arbitrum chains. Here’s a common workflow:
Start a new Stylus project with
Counter smart contract example. See the README of stylus-hello-world for more details. Alternatively, you can use cargo stylus new --minimal <YOUR_PROJECT_NAME> to create a more barebones example with a Stylus entrypoint locally, useful for projects that don’t need all the Solidity plumbing.
Then, develop your Rust program normally and take advantage of all the features the stylus-sdk has to offer.
Checking your Stylus project is valid
To check whether or not your program will successfully deploy and activate onchain, use thecargo stylus check subcommand:
cargo stylus check --help for more options.
If the command above fails, you’ll see detailed information about why your WASM will be rejected:
cargo stylus deploy subcommand as follows. First, we can estimate the gas required to perform our deployment with:
cargo stylus deploy --help for more details.
Deploying non-Rust WASM projects
The Stylus CLI tool can also deploy non-Rust, WASM projects to Stylus by specifying the WASM file directly with the--wasm-file-path flag to any of the cargo stylus commands.
Even WebAssembly Text (WAT) files are supported. This means projects that are just individual WASM files can be deployed onchain without needing to have been compiled by Rust. WASMs produced by other languages, such as C, can be used with the tool this way.
For example:
add.wat and used as cargo stylus check --wasm-file-path=add.wat or cargo stylus deploy --priv-key-path=<YOUR PRIV KEY FILE PATH> --wasm-file-path=add.wat
Exporting Solidity ABIs
Stylus Rust projects that use the stylus-sdk have the option of exporting Solidity ABIs. The cargo stylus tool also makes this easy with theexport-abi command:
cargo stylus export-abi
