Building the contract
Now it is time to build our contract. We can use a traditional cargo build pipeline for local
testing purposes: cargo build
for compiling it and cargo test
for running all tests (which we
don't have yet, but we will work on that soon).
However, we need to create a Wasm binary to upload the contract to blockchain. We can do it by passing an additional argument to the build command:
cargo build --target wasm32-unknown-unknown --release
The --target
argument tells cargo to perform cross-compilation for a given target instead of
building a native binary for an operating system it is running on, in this case, wasm32-unknown-unknown
,
which is a fancy name for the Wasm target.
Additionally, we have passed the --release
argument to the command. It is not required, but in most cases,
debug information is not very useful while running on-chain. It is crucial to reduce the uploaded binary size
for gas cost minimization. It is worth knowing that there is a CosmWasm Rust Optimizer tool that takes care
of building even smaller binaries. For production, all the contracts should be compiled using this tool,
but now, for learning purposes, it is not an essential thing to do.
Aliasing the build command
Now I can imagine you are disappointed in building your contracts with some overcomplicated command
instead of simple cargo build
. Fortunately, it is not the case. The common practice is to alias the building
command to make it as simple as building a native app.
Let's create the .cargo/config.toml
file in your contract project directory with the following content:
[alias]
wasm = "build --target wasm32-unknown-unknown --release"
wasm-debug = "build --target wasm32-unknown-unknown"
Now, building your Wasm binary is as easy as executing cargo wasm
.
We also added the additional wasm-debug
command for very rare cases when you might want
to build the Wasm binary with debug information.
Checking contract validity
Once the contract is built, the last step to ensure it is a valid CosmWasm contract is by calling cosmwasm-check
on it:
cargo wasm && cosmwasm-check ./target/wasm32-unknown-unknown/release/contract.wasm
Available capabilities: {"cosmwasm_1_2", "cosmwasm_1_4", "stargate", "cosmwasm_1_1", "cosmwasm_2_1", "cosmwasm_1_3", "cosmwasm_2_0", "staking", "iterator"}
./target/wasm32-unknown-unknown/release/contract.wasm: pass
All contracts (1) passed checks!