Activation
Stylus contracts undergo a two-step process to become executable on Arbitrum chains: deployment and activation. This guide explains both steps, the distinction between them, and how to manage the activation process.
Overview
Unlike traditional EVM contracts that become immediately executable after deployment, Stylus contracts require an additional activation step:
- Deployment: Stores the compressed WASM bytecode on-chain at a contract address
- Activation: Converts the bytecode into an executable Stylus program by registering it with the ArbWasm precompile
Why two steps?
- Gas optimization: Activation involves one-time processing and caching that would be expensive to repeat on every call
- Code reuse: Multiple contracts can share the same activated codehash, reducing activation costs
- Version management: Allows the chain to track which Stylus protocol version a contract targets
Deployment vs Activation
| Aspect | Deployment | Activation |
|---|---|---|
| Purpose | Store compressed WASM on-chain | Register program with ArbWasm |
| Transaction count | 1 transaction | 1 transaction (separate) |
| Cost type | Standard EVM deployment gas | Data fee (WASM-specific cost) |
| When required | Always - stores the code | Always - makes code executable |
| Reversible | No | No (but can expire) |
| Who can call | Anyone with funds | Anyone (after deployment) |
| Can be skipped | No | No (unless already activated) |
Contract States
A Stylus contract can be in one of these states:
pub enum ContractStatus {
/// Contract already exists on-chain and is activated
Active { code: Vec<u8> },
/// Contract is deployed but not yet activated
/// Ready to activate with the given data fee
Ready { code: Vec<u8>, fee: U256 },
}
The Activation Process
Step 1: Build and Process WASM
Before deployment, your Rust contract is compiled and processed:
cargo stylus check
This performs:
- Compile Rust to WASM: Using
wasm32-unknown-unknowntarget - Process WASM binary:
- Remove dangling references
- Add project hash metadata
- Strip unnecessary custom sections
- Brotli compression: Maximum compression (level 11)
- Add EOF prefix:
EFF00000(identifies Stylus programs) - Size validation: Compressed code must be ≤ 24KB
WASM Processing Pipeline:
Raw WASM Binary
↓
Remove dangling references
↓
Add project_hash metadata
↓
Strip user custom sections
↓
Brotli compress (level 11)
↓
Add EOF prefix (EFF00000)
↓
Final compressed code (≤ 24KB)
Step 2: Deploy the Contract
Deployment creates a transaction that stores your processed WASM on-chain:
cargo stylus deploy \
--private-key-path=key.txt \
--endpoint="https://sepolia-rollup.arbitrum.io/rpc"
What happens during deployment:
- Generate deployment bytecode: Create EVM initcode with embedded compressed WASM
- Estimate gas: Calculate deployment transaction gas cost
- Send deployment transaction: To Stylus deployer contract
- Extract contract address: From transaction receipt
Deployment Bytecode Structure:
EVM Initcode Prelude (43 bytes):
┌─────────────────────────────────────┐
│ 0x7f PUSH32 <code_len> │ Push code length
│ 0x80 DUP1 │ Duplicate length
│ 0x60 PUSH1 <prelude_length> │ Push prelude length
│ 0x60 PUSH1 0x00 │ Push 0
│ 0x39 CODECOPY │ Copy code to memory
│ 0x60 PUSH1 0x00 │ Push 0
│ 0xf3 RETURN │ Return code
│ 0x00 <version_byte> │ Stylus version
└─────────────────────────────────────┘
↓
<compressed_wasm_code>
Step 3: Calculate Activation Fee
Before activating, the data fee must be calculated:
// Simulated via state overrides (no transaction sent)
let data_fee = calculate_activation_fee(contract_address);
// Apply bump percentage for safety (default: 20%)
let final_fee = data_fee * (1 + bump_percent / 100);
Data fee calculation:
- Uses state override simulation to estimate fee
- No actual transaction sent during estimation
- Configurable bump percentage protects against variance (default: 20%)
- Fee is paid in ETH when activating
Step 4: Activate the Contract
Activation registers your contract with the ArbWasm precompile:
# Automatic activation (default)
cargo stylus deploy --private-key-path=key.txt
# Or manual activation
cargo stylus activate \
--address=0x1234... \
--private-key-path=key.txt
What happens during activation:
- Call ArbWasm precompile: At address
0x0000000000000000000000000000000000000071 - Send activation transaction:
ArbWasm.activateProgram{value: dataFee}(contractAddress) - ArbWasm processes the code:
- Validates WASM format
- Checks against protocol version
- Stores activation metadata
- Emits
ProgramActivatedevent
- Returns activation info:
returns (uint16 version, uint256 actualDataFee)
Using cargo-stylus
The cargo-stylus CLI tool simplifies the deployment and activation workflow.
Basic Deployment (Automatic Activation)
By default, cargo stylus deploy handles both steps:
cargo stylus deploy \
--private-key-path=wallet.txt \
--endpoint="https://sepolia-rollup.arbitrum.io/rpc"
Output:
Building contract...
Compressing WASM...
Deploying contract to 0x1234567890abcdef...
Deployment transaction: 0xabcd...
Contract deployed at: 0x1234567890abcdef
Activating contract...
Activation transaction: 0xef12...
Contract activated successfully!
Deploy Without Activation
To deploy but skip activation:
cargo stylus deploy \
--private-key-path=wallet.txt \
--no-activate
This is useful when:
- You want to inspect the contract before activating
- Someone else will handle activation
- You're testing deployment workflows
Manual Activation
Activate a previously deployed contract:
cargo stylus activate \
--address=0x1234567890abcdef \
--private-key-path=wallet.txt \
--endpoint="https://sepolia-rollup.arbitrum.io/rpc"
Check Contract Status
Before deploying, check if a contract with the same code is already activated:
cargo stylus check \
--endpoint="https://sepolia-rollup.arbitrum.io/rpc"
Possible outcomes:
- Code already activated: You can reuse the existing deployment
- Ready to activate: Shows estimated data fee
- Validation errors: Displays issues that must be fixed
Deployment with Constructors
If your contract has a constructor, provide arguments during deployment:
#[public]
impl MyContract {
#[constructor]
pub fn constructor(&mut self, initial_value: U256, owner: Address) {
self.value.set(initial_value);
self.owner.set(owner);
}
}
Deploy with constructor arguments:
cargo stylus deploy \
--private-key-path=wallet.txt \
--constructor-args 42 0x1234567890abcdef1234567890abcdef12345678
With payable constructor:
#[constructor]
#[payable]
pub fn constructor(&mut self) {
let value = self.vm().msg_value();
self.initial_balance.set(value);
}
cargo stylus deploy \
--private-key-path=wallet.txt \
--constructor-value=1000000000000000000 # 1 ETH in wei
The ArbWasm Precompile
Activation is handled by the ArbWasm precompile at address 0x0000000000000000000000000000000000000071.
Key Functions
activateProgram
Activates a deployed Stylus contract:
function activateProgram(
address program
) external payable returns (uint16 version, uint256 dataFee);
Parameters:
program: Contract address containing WASM bytecode
Payment:
- Must send
valueequal to the calculated data fee (in wei)
Returns:
version: Stylus protocol version the program was activated againstdataFee: Actual fee paid for activation
Example (via cast):
cast send 0x0000000000000000000000000000000000000071 \
"activateProgram(address)" \
0x1234567890abcdef \
--value 100000000000000000 \
--private-key=$PRIVATE_KEY
codehashVersion
Check if a codehash is activated and get its version:
function codehashVersion(bytes32 codehash) external view returns (uint16 version);
Reverts if:
- Code is not activated
- Program needs upgrade
- Program has expired
programTimeLeft
Get remaining time before a program expires:
function programTimeLeft(address program) external view returns (uint64 timeLeft);
Returns seconds until expiration (default: ~1 year from activation).
codehashKeepalive
Extend a program's expiration time:
function codehashKeepalive(bytes32 codehash) external payable returns (uint64 expirySeconds);
Resets the expiration timer, preventing program deactivation.
ArbWasm Errors
Activation can fail with these errors:
error ProgramNotWasm();
// The deployed bytecode is not valid WASM
error ProgramNotActivated();
// Contract exists but hasn't been activated
error ProgramNeedsUpgrade(uint16 version, uint16 stylusVersion);
// Program version incompatible with current Stylus version
error ProgramExpired(uint64 ageInSeconds);
// Program has expired and must be reactivated
error ProgramInsufficientValue(uint256 have, uint256 want);
// Sent data fee is less than required