Full working code: The complete source code for this example is available in
example-contracts/token/WIT Interface
Exports functions for a fungible token with minting, burning, transfers, and balance queries:Rust Implementation
The token contract uses a Map to store account balances, with helper functions for validation and a total supply tracker:- Storage accessed via
ctx.model(), not a separatestorage()function - Map operations:
ledger.get(&key)andledger.set(key, value)- no extra ctx parameter - Errors use
Error::Message(...to_string())variant - Uses checked arithmetic (
.add(n)?,.sub(n)?) for overflow safety - Helper validation function (
assert_gt_zero) - Total supply tracking with
try_update_*closures
Testing
The test demonstrates minting, transferring, and error handling with proper use of the double?? operator for functions returning Result:
- Uses
interface!to generate bindings, notimport! #[testlib::test]auto-injectsruntimevariableruntime.identity()creates test usersruntime.publish()deploys the contract- Functions returning
Resultuse??to unwrap both the runtime Result and contract Result - Integers created with
.into()orInteger::from()