Compile Plugin¶
The compile plugin (hardhat-arb-compile) handles Stylus contract compilation. It discovers Rust contracts, compiles them via cargo-stylus, and generates Hardhat-compatible artifacts.
Overview¶
The arb:compile task orchestrates: discovery of Stylus contracts, toolchain validation (host) or Docker image preparation (container), starting a temp node for cargo stylus check, running compilation, generating Hardhat-compatible artifacts, and cleanup.
Discovery¶
discoverStylusContractsFromSources() scans all configured Hardhat source directories (hre.config.paths.sources.solidity):
- For each source directory, recursively finds directories with
Cargo.toml(skipstarget/and hidden dirs) - Parses
Cargo.tomlwithsmol-tomlchecks ifstylus-sdkis in dependencies - Reads
rust-toolchain.tomlto get the required Rust version - Deduplicates by contract name across source directories
- Optionally filters by contract name (from
--contractsflag)
Non-Stylus Cargo projects are silently skipped. Missing rust-toolchain.toml throws an error.
Host Compilation¶
When --host is used:
- Validate toolchains Checks
rustup,cargo-stylus, required toolchains, andwasm32-unknown-unknowntargets upfront - Start temp node Calls
arb:node startwithdetach: trueand random ports - For each contract Runs
cargo +{toolchain} stylus check, thencargo +{toolchain} stylus build - Generate artifact Exports ABI and reads WASM
- Cleanup Stops the temporary node
Docker Compilation¶
When using Docker (the default):
- Ensure volumes Creates
stylus-compile-rustupandstylus-compile-cargoDocker volumes for caching - Build image Builds
stylus-compile:latestfromrust:slimwithcargo-stylus(only once, cached) - Create network A temporary Docker network lets the compile container reach the node container by hostname
- Start temp node Same as host mode, but attached to the Docker network
- For each contract Installs toolchain in container (cached in volumes), runs
cargo stylus checkandcargo stylus build - Generate artifact Exports ABI and reads WASM
- Cleanup Stops node, removes Docker network
Artifact Generation¶
After successful compilation:
- ABI export Runs
cargo stylus export-abito get a Solidity interface - Parse ABI Converts the Solidity interface to JSON ABI format
- Read WASM Reads the compiled
.wasmfile and converts to hex - Save Writes
artifacts/contracts/{name}/{name}.jsonwith formathh3-stylus-artifact-1
Artifact generation is best-effort if ABI export fails, the artifact still gets an empty ABI.
Integration Points¶
hardhat-arb-nodeProvidesarb:node startfor temporary nodes, plus temp-node lifecycle utilitieshardhat-arb-utilsProvidesDockerClientfor Docker operations,createPluginErrorfor error handling, and Stylus utilities (discoverStylusContractsFromSources,ensureVolumes,ensureCompileImage,validateAllToolchains)