Before starting, make sure you have rustup along with a
recent rustc
and cargo
version installed. Currently, we are testing on 1.48
.
And you need to have the wasm32-unknown-unknown
target installed as well.
You can check that via:
rustc --version
cargo --version
rustup target list --installed
# if wasm32 is not listed above, run this
rustup target add wasm32-unknown-unknown
While the Wasm calls (init
, handle
, query
) accept JSON, this is not enough
information to use it. We need to expose the schema for the expected messages to the
clients. You can generate this schema by calling cargo schema
(from within a contract's folder), which will output
4 files in ./schema
, corresponding to the 3 message types the contract accepts,
as well as the internal State
.
These files are in standard json-schema format, which should be usable by various client side tools, either to auto-generate codecs, or just to validate incoming json wrt. the defined schema.
Before we upload it to a chain, we need to ensure the smallest output size possible, as this will be included in the body of a transaction. We also want to have a reproducible build process, so third parties can verify that the uploaded Wasm code did indeed come from the claimed rust code.
To solve both these issues, we use the rust-optimizer provided by Cosmwasm team, a docker image to produce an extremely small build output in a consistent manner. The suggest way to run it is this:
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.5
We must mount the contract code to /code
. You can use a absolute path instead
of $(pwd)
if you don't want to cd
to the directory first.
This produces an artifacts
directory with a PROJECT_NAME.wasm
, as well as
checksums.txt
, containing the Sha256 hash of the wasm file.
The wasm file is compiled deterministically (anyone else running the same
docker on the same git commit should get the identical file with the same Sha256 hash).
It is also stripped and minimized for upload to a blockchain.
Once you've finished to build (and test) your contract, it's time to publish it into crates.io.
cargo package
cargo publish