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:
Example | Explanation |
---|---|
BTreeMap | Alternative to HashMap in a no-std env |
BTreeSet | Alternative to HashSet in a no-std env |
Common host functions
Function | Explanation |
---|---|
dusk_core::abi::emit(“EVENT_NAME”, data) | Emit a contract event |
Common Dependencies
- dusk-core, with
ABI
feature enabled
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
Example Makefile to compile to WASM
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.