Compile a Rust smart contract module#
This guide will show you how to compile smart contract module written in Rust to a Wasm module.
Make sure to have Rust and Cargo installed and the
target, together with
cargo-concordium and the Rust source code for a smart
contract module, you wish to compile.
For instructions on how to install the developer tools see Install tools for development.
Compiling to Wasm#
In order to build a smart contract for deployment, run:
$cargo concordium build --verifiable docker.io/concordium/verifiable-sc:1.70.0 --out contract.wasm.v1
This uses Cargo for building, but runs further optimizations on the result.
--verifiable option is available on
cargo-concordium version 3.1 or newer. It will cause
cargo-concordium to build the sources
in a fixed container to make the build reproducible. It will additionally produce
tar file with packaged sources that were used for the build. The list of
available docker images can be found on DockerHub.
tar archive should be uploaded and made publicly available, and its
link should be embedded into the deployed module using the
cargo concordium edit-build-info
command. For example
$cargo concordium edit-build-info --module module.wasm.v1 --source-link https://link.to/module.wasm.v1.tar --verify
--out option is required for verifiable builds, and instructs
cargo-concordium to output the build artifact at the specified location.
Verifiable builds require a container runtime such as Docker to be installed.
Additional information about verifiable builds can be found on Github.
If you receive an error similiar to the one below, you need to update your
cargo-concordium to version 3.0 or later.
Error: Could not build smart contract.
0: Could not build module schema.
1: Could not generate module schema from Wasm module.
2: Unsupported instruction 0xc0
If you want a smaller code footprint, enable the wee_alloc feature by using:
$cargo concordium build --verifiable docker.io/concordium/verifiable-sc:1.70.0 --out contract.wasm.v1 -- --features wee_alloc
For building the schema for a smart contract module, some further
preparation is required.
Building and embedding the schema can and should be combined with a verifiable build using the
It is also possible to compile using Cargo directly by running:
$cargo build --target=wasm32-unknown-unknown [--release]
Note that even with
--release set, the produced Wasm module includes
--verifiable option from the build command will make a build on the host machine.
This is typically not verifiable since the Rust toolchain is affected by the build environment.
However, for local development, non-verifiable builds can be useful.
By default, running the
cargo concordium build command will produce a smart contract module which can be found
relative to your project root folder in
--out option can be supplied also for non-verifiable builds.
For example, running the following command will output your smart contract module into the root folder of your project in a file called
$cargo concordium build --out ./my_module.wasm.v1
cargo-concordium produces several smart contract modules with different suffixes. The suffix corresponds
to the smart contract version, i.e.
my_module.wasm/my_module.wasm.v0 for V0 contracts and
for V1 contracts. Concordium recommends using the wasm module with the
(the most-up-to date smart contract version).
my_module.wasm.v1 will be used when deploying a smart contract on-chain.