Skip to main content

Build Optimized

Building optimized contracts to be as small as possible requires some additional tools, and requires installing the nightly Rust toolchain and wasm-opt.

info

Building optimized contracts is only necessary when deploying to network or when analyzing and profiling a contract to get it as small as possible. If you're just starting out writing a contract, these steps are not necessary.

info

All contracts are optimized to some degree by the project's release profile. It is highly recommended to use the release profile outlined in Configure the Release Profile, otherwise the contract will be too large to deploy or run.

Install Rust nightly

To install the nightly Rust toolchain use rustup.

rustup install nightly
rustup target add --toolchain nightly wasm32-unknown-unknown
rustup component add --toolchain nightly rust-src

Install wasm-opt

To install wasm-opt, install binaryen. Depending on your operating system there may be different ways to install it.

brew install binaryen

Building with nightly and wasm-opt

Then build your contract using nightly, and the additional options that remove unnecessary information from the contract.

cargo +nightly build \
--target wasm32-unknown-unknown \
--release \
-Z build-std=std,panic_abort \
-Z build-std-features=panic_immediate_abort

Then additionally use wasm-opt to further minimize the size of the .wasm.

wasm-opt -Oz \
target/wasm32-unknown-unknown/release/first_project.wasm \
-o target/wasm32-unknown-unknown/release/first-project-optimized.wasm