Compiling a smart contract in Rust

When compiling a Rust contract one wants to reduce the resulting binary size to the bare minimum possible to save on deployment fees, since that metric is tied only to the size of the deployed binary. As a good rule of thumb is to always strip all removable metadata (e.g.: DWARF, symbol table and debug info).

The QVM compiler enables presets which help to achieve an extremely small static binary as a result.

Compiling Rust smart contract to static binary with the QVM compiler

We already supply a simple compiler container which will perform the most optimized build of turning above sample contract to a static binary which can be run by QVM.

Since you saved the previous sample contract as in your current directory, you can run the compiler like this:

docker run --rm -v $(pwd):/ws qanplatform/qvm-compiler-rust

Afterwards you will end up with a statically linked linux ELF binary called “contract” in your current directory.

# checking output size of QVM compiler
ls -alh contract
-rwxr-xr-x  1 user  group    30K May 4 17:34 contract

Last updated