Deploy Your Stylus Smart Contracts
To deploy your Stylus smart contracts to Arbitrum networks, there are a few things you need to adjust.
1. Configure your networkโ
Scaffold-Stylus supports Arbitrum networks by default. You can configure your deployment network using environment variables.
Local Network Setupโ
For local development, you need to start a local Stylus-compatible network first:
yarn chain
This command starts a local Stylus-compatible network using the Nitro dev node script (./nitro-devnode/run-dev-node.sh
). The network runs on your local machine and can be used for testing and development. You can customize the Nitro dev node configuration in the nitro-devnode
submodule.
Available Networksโ
This template supports Arbitrum networks only. You can test which networks are available and their RPC URLs:
yarn test:networks
This will show you all supported networks and their corresponding RPC endpoints.
For testnet information, including faucets and RPC endpoints, see the Arbitrum Stylus testnet documentation.
2. Deploy your Stylus smart contract(s)โ
Deploy to Local Networkโ
The simplest way to deploy your Stylus smart contract is using:
yarn deploy
This command deploys a test smart contract to the local network. The contract is located in packages/stylus/your-contract/src
and can be modified to suit your needs. The yarn deploy
command uses the deploy script located in packages/stylus/scripts
to deploy the contract to the network. You can also customize the deploy script.
Create Your Own Contractโ
Scaffold-Stylus enables you to create and deploy multiple contracts within a single project. Follow the steps below to create and deploy your own contracts.
Step 1: Generate New Contractโ
Use the following command to create a new contract and customize it as needed:
yarn new-module <contract-name>
The generated contract will be located in packages/stylus/<contract-name>
.
Step 2: Deploy Your Contractโ
yarn deploy [...options]
This command runs the deploy.ts
script located in packages/stylus/scripts
. You can customize this script with your deployment logic.
Available Options:
--network <network>
: Specify which network to deploy to--estimate-gas
: Only perform gas estimation without deploying--max-fee=<maxFee>
: Set maximum fee per gas in gwei
Note: Deployment information is automatically saved in packages/stylus/deployments
by default.
Check Your Programโ
Before deploying to live networks, you can check that your program compiles to valid WASM for Stylus and will succeed a deployment onchain without transacting:
cargo stylus check
If successful, you should see:
Finished release [optimized] target(s) in 1.88s
Reading WASM file at target/wasm32-unknown-unknown/release/your-program.wasm
Compressed WASM size: 8.9 KB
Program succeeded Stylus onchain activation checks with Stylus version: 1
You can edit your deployment scripts in packages/stylus/scripts/deploy.ts
to customize the deployment process.
3. Export ABI for your smart contractโ
If you are using yarn deploy
, the script will automatically export the ABI to packages/nextjs/contracts/deployedContracts.ts
. You can manually export the ABI for your Stylus program by using:
yarn export-abi
This outputs a Solidity interface that represents your Rust program:
/**
* 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).
*/
// SPDX-License-Identifier: MIT-OR-APACHE-2.0
pragma solidity ^0.8.23;
interface ICounter {
function number() external view returns (uint256);
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"]
4. Deploy to Other Networksโ
To deploy your contracts to other networks (other than the default local Nitro dev node), you'll need to configure your RPC endpoint and wallet credentials.
Prerequisitesโ
Set the RPC URL
Configure your target network's RPC endpoint using the proper
RPC_URL_<network>
environment variable. You can set this in your shell or create a.env
file (see.env.example
for reference):RPC_URL_SEPOLIA=https://your-network-rpc-url
Note: If RPC URL is not provided, the system will use the default public RPC URL from that network.
Set the Private Key
For real deployments, you must provide your own wallet's private key. Set the
PRIVATE_KEY_<network>
environment variable:PRIVATE_KEY_SEPOLIA=your_private_key_here
Security Note: A development key is used by default when running the Nitro dev node locally, but for external deployments, you must provide your own private key.
Set the Account Address
Set the
ACCOUNT_ADDRESS_<network>
ACCOUNT_ADDRESS_SEPOLIA=your_account_address_here
Update Frontend Configuration
Open
packages/nextjs/scaffold.config.ts
and update thetargetNetworks
array to include your target chain. This ensures your frontend connects to the correct network and generates the proper ABI indeployedContracts.ts
:import * as chains from "viem/chains";
// ...
targetNetworks: [chains.arbitrumSepolia],
Deploy to Other Networkโ
Once configured, deploy to your target network:
yarn deploy --network <network>
You can also estimate gas costs before deploying:
yarn deploy --network <network> --estimate-gas
- The values in
.env.example
provide a template for required environment variables - Always keep your private key secure and never commit it to version control
- Consider using environment variable management tools for production deployments :::