Setting up a smart contract project#

A smart contract in Rust is written as an ordinary Rust library crate. The library is then compiled to Wasm using the Rust target wasm32-unknown-unknown and, since it is just a Rust library, you can use Cargo for dependency management.

To set up a new smart contract project, first create a project directory. Inside the project directory run the following in a terminal:

$cargo init --lib

This will set up a default Rust library project by creating a few files and directories. Your directory should now contain a Cargo.toml file and a src directory and some hidden files.

To be able to build Wasm you need to tell cargo the right crate-type. This is done by adding the following in the Cargo.toml file

[lib]
crate-type = ["cdylib", "rlib"]

Adding the smart contract standard library#

The next step is to add concordium-std as a dependency. It is a library for Rust containing procedural macros and functions for writing small and efficient smart contracts.

The library is added by opening Cargo.toml and adding the line concordium-std = "*" (preferably, replace the * with the latest version of concordium-std) in the [dependencies] section:

[dependencies]
concordium-std = "0.4"

The crate documentation can be found on docs.rs.

Note

If you wish to use a modified version of this crate, you will have to clone the repository with concordium-std and have the dependency point at the directory instead, by adding the following to Cargo.toml:

[dependencies]
concordium-std = { path = "./path/to/concordium-std" }

That is it! You are now ready to develop your own smart contract.

Was this article helpful?
Legal information