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, run:
$cargo concordium build
This uses Cargo for building, but runs further optimizations on the result.
For building the schema for a smart contract module, some further preparation is required.
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
Removing host information from build#
The compiled Wasm module can contain information from the host machine building
the binary; information such as the absolute path of the
For most people this is not sensitive information, but it is important to be aware of it.
On Linux the paths can be inspected by running:
strings contract.wasm | grep /home/
The ideal solution would be to remove this path entirely, but that is unfortunately not a trivial task in general.
It is possible to work around the issue by using the
flag when compiling the contract.
On Unix-like systems the flag can be passed directly to the
invocation using the
RUSTFLAGS environment variable:
$RUSTFLAGS="--remap-path-prefix=$HOME=" cargo concordium build
Which will replace the users home path with the empty string. Other paths could
be mapped in a similar way. In general using
to at the beginning of any embedded path.
The flag can also be set permanently in the
.cargo/config file in your
crate, under the build section:
[build] rustflags = ["--remap-path-prefix=/home/<user>="]
where <user> should be replaced with the user building the wasm module.
The above will likely not fix the issue if the
rust-src component is
installed for the Rust toolchain. This component is needed by some Rust tools
such as the rust-analyzer.
An issue reporting the problem with