Skip to content

Smart Contract Cheat Sheet

Collection of code snippets and information bits on smart contract development for Dusk.

Common Data Structures

Because we program for WASM in no-std some data structures are not available. Here is a list of common alternatives:

ExampleExplanation
BTreeMapAlternative to HashMap in a no-std env
BTreeSetAlternative to HashSet in a no-std env

Common host functions

FunctionExplanation
rusk_abi::emit(“EVENT_NAME”, data)Emit a contract event

Common Dependencies

  • execution-core
  • rusk_abi

No-std Crates

  • core
  • alloc

The alloc crate needs to explicitly be imported in order to use heap-allocated values in a #![no-std] environment. More information on the alloc crate can be found here.

Simple Contract Template

Counter Contract

Example Makefile to compile to WASM

Terminal window
build: ## Build contract
@RUSTFLAGS="-C link-args=-zstack-size=65536" \
cargo build \
--release \
--manifest-path=Cargo.toml \
--color=always \
-Z build-std=core,alloc \
--target wasm32-unknown-unknown
@mkdir -p target/stripped
@find target/wasm32-unknown-unknown/release -maxdepth 1 -name "*.wasm" \
| xargs -I % basename % \
| xargs -I % wasm-tools strip -a \
target/wasm32-unknown-unknown/release/% \
-o target/stripped/%
# test: contract ## Run all tests
# @cargo test \
# --manifest-path=Cargo.toml \
# --color=always
MAX_COUNTER_CONTRACT_SIZE = 8192
.PHONY: contract test

More references

The Rust Language Cheat Sheet is another great cheat sheet that can be used as a reference for Rust code.

Note: Not all examples apply due to our no-std constraints.