Set up a smart contract project#

This guide documents two different options (from a template or from scratch) to create a new Concordium smart contract project. The from a template option is available for cargo-concordium version 2.2.0 or greater. It provides you with some smart contract templates. Choose the template that best fits your project scope. The from scratch option guides you through the process when you want to start a new project without any boilerplate code.

Note

Concordium recommends that newcomers choose the from a template option.

Concordium maintains several smart contract templates (currently a default template and a cis2-nft template). For generating the smart contracts from the above templates, the cargo-generate crate is required. cargo-generate can be installed by running the following command:

$cargo install --locked cargo-generate

To start a new Concordium smart contract project from a template, run the command:

$cargo concordium init

The path where the project should be created can be provided with the --path option.

You can find additional information on the available templates in the README file.

Note

To compile your smart contracts, a memory allocator is used. concordium-std version <6.0.0 hard-coded the use of the wee_alloc allocator. In concordium-std version >=6.0.0, wee_alloc is a feature and needs to be explicitly enabled. When std feature is enabled the allocator provided by the Rust standard library is used by default but when the wee_alloc feature is enabled in addition, wee_alloc is used instead.

You can enable the wee_alloc feature in concordium-std version >=6.0.0 by using:

[dependencies]
concordium-std = {version = "6.0", features = ["wee_alloc"]}

Alternatively, if you want to test with and without wee_alloc enabled add a wee_alloc feature to the smart contract crate as follows:

[features]
default = ["std", "wee_alloc"]
std = ["concordium-std/std"]
wee_alloc = ["concordium-std/wee_alloc"]

The main reason for using wee_alloc instead of the default allocator is that wee_alloc has a smaller code footprint, i.e, the resulting smart contracts modules are going to be smaller. Smaller modules will be cheaper to deploy and run. Concordium nodes will load the smart contract module code into memory when executing a smart contract function on-chain. Hence, the smart contract module size has an impact on the execution cost.

Note

Wee_alloc is unmaintained currently and hence an optional feature for backward compatibility. There are other allocators available, for example dlmalloc.

See also

It is possible to build smart contracts without using Rust’s std. For more information, see Build using no_std.

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