example-contracts/. Each is a self-contained example that builds on concepts introduced in previous ones, progressing from a minimal contract to production-grade DeFi applications. The examples demonstrate how to develop blockchain smart contracts with seamless Rust-like testing and rapid feedback in a local development environment.
Suggested Learning Order
- hello-world → Minimal contract structure, basic functions
- token → Map-based ledger, Integer type, error handling
- shared-account → Nested Maps, authorization patterns
- proxy → Fallback function, delegation patterns
- pool → DeFi math, liquidity pools
- amm → Multi-pool AMM, production patterns
Example Contracts
hello-world
Minimal contract demonstrating basic contract structure, view functions, and simple state management. View Example →token (59 lines)
Fungible token with Map-based ledger, arbitrary precision Integer arithmetic, balance transfers, and error handling for insufficient funds. View Example →shared-account
Multi-tenant account management with two-level nested Maps, authorization patterns (owner and tenants), helper functions for access control, and contract receiving tokens viacontract_signer().
View Example →
proxy
Generic contract delegation using the fallback function, FallContext, andforeign::call for dynamic forwarding. Demonstrates proxy/upgrade patterns.
View Example →
pool (200+ lines)
Single liquidity pool implementation with constant product AMM math, LP token management, swap calculations, and slippage protection using Integer arithmetic. View Example →amm (350+ lines)
Multi-pool automated market maker managing multiple token pairs with Map of Pool structs, dynamic token handling, pool creation, and production-grade validation logic. View Example →Project Structure
Each contract directory contains:contract/- Rust code (src/lib.rs), WIT interface (wit/contract.wit), andwit/deps/(symlink to built-in types)test/- Tests (src/lib.rs),build.rs(compiles, optimizes, compresses contract), and dependencies- Root
Cargo.toml- Workspace configuration
Running Examples
To test any example contract:target/ after compilation. This provides fast iteration like standard Rust development. Each example assumes familiarity with prior ones, focusing on new concepts.
Finding the Code
All example contracts are in the repository underexample-contracts/: