Full working code: The complete source code for this example is available in
example-contracts/shared-account/WIT Interface
Exports functions for multi-tenant account management with token deposits and withdrawals:Rust Implementation
- The
interface!macro generates an interface for cross-contract calls to thetokencontract with a dynamic address (passed as a parameter). Useimport!when the dependency contract has been previously published and the contract address is known at compile time. - The
StorageRootmacro, used for the root storage type, and theStoragemacro, used for nested storage types in theAccountstruct, enable persistent storage. other_tenantsusesMap<String, bool>because the storage layer does not currently support list types, andMapprovides a limited interface with thekeysmethod for iteration. Even with aListtype using aMaphere could make sense. Instead ofboolanenumorstructthat defines the their “role” in the account could be written.authorizedVerifies procedure permissionsopen: Verifies token balance, generates an ID usingcrypto::generate_id(), sets the account, and transfers tokens toctx.contract_signer()deposit/withdraw: Authorize the caller, verify balances, update storage, and call the token contract following CEI patternbalance/tenants: Query the storage for account details
- Uses
interface!notimport!for dynamic token contract address ctx.model()for storage access- Map operations:
accounts().get(id)returnsOption<AccountModel> - Nested struct updates:
account.update_balance(|b| b + n) - Error helpers return
Error::Message(...to_string()) authorized()function takes&AccountModelnot&AccountWrapperctx.generate_id()creates unique account IDs- Calls
token::balance()andtoken::transfer()with contract addresses