Compile Contracts
This guide is from the official CosmWasm/rust-optimizer repository (opens in a new tab). This method of compiling the contract will optimize the final build so that it reduces gas consumption.
Example contracts for this can be found at https://github.com/CosmWasm/cw-examples (opens in a new tab). This repository requires this section to compile since multiple contracts are involved in a single repository.
Single Contract Repository
The easiest way is to simply use the published docker image (opens in a new tab). You must run this in the root of the smart contract repository you wish to compile. It will produce an artifacts
directory with <crate_name>.wasm
and contracts.txt
containing the hashes. This is just one file.
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/rust-optimizer:0.12.11
# If you you use an ARM machine (Ex: Mac M1), you need to use the following
# This is experimental and should not be used for production use
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/rust-optimizer-arm64:0.12.11
By running this in the root of your project, it will compile your contract into an artifacts/ folder. From here you can upload it to chain, collect the store code, and interact with it as you design
Multiple Contract Repository (Mono Repo)
Sometime you want many contracts to be related and import common functionality. This is exactly the case of cosmwasm-plus
(opens in a new tab). In such a case, we can often not just compile from root, as the compile order is not deterministic and there are feature flags shared among the repos. This has lead to issues in the past (opens in a new tab).
For this use-case there is second docker image, which will compile all the contracts/*
folders inside the workspace and do so one-by-one in alphabetical order. It will then add all the generated wasm files to an artifacts
directory with a checksum, just like the basic docker image (same output format).
To compile all contracts in the workspace deterministically, you can run:
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/workspace-optimizer:0.12.11
# If you you use an ARM machine (Ex: Mac M1), you need to use the following
# This is experimental and should not be used for production use
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/workspace-optimizer-arm64:0.12.11
NOTE: See the difference with workspace-optimizer vs rust-optimizer in the previous single contract example.\