This example demonstrates a complex storage pattern in a Soroban smart contract, designed to showcase the capabilities of the soroban-debugger.
For information on how to contribute to this project, please see the CONTRIBUTING.md.
The contract implements a simple voting system where proposals can be created, voted on, and tallied. It uses persistent storage to track proposals and a map of votes for each proposal.
create_proposal(creator: Address, id: u32, title: String): Creates a new proposal.vote(voter: Address, proposal_id: u32, support: bool): Casts a vote (yay or nay).tally(proposal_id: u32) -> (u32, u32): Returns the current vote count.close(creator: Address, proposal_id: u32): Closes the proposal for further voting.
This section provides a step-by-step guide on using soroban-debug to inspect the contract's execution and state.
First, compile the contract to WASM:
cargo build --target wasm32-unknown-unknown --releaseThe WASM file will be located at target/wasm32-unknown-unknown/release/voting_contract.wasm.
Execute the create_proposal function and observe the initial state setup:
soroban-debug run \
--contract target/wasm32-unknown-unknown/release/voting_contract.wasm \
--function create_proposal \
--args '["GDG7...123", 1, "Upgrade Protocol"]' \
--verboseAfter voting, use the debugger to see how the Votes map changes in storage. This is particularly useful for debugging complex data structures.
soroban-debug run \
--contract target/wasm32-unknown-unknown/release/voting_contract.wasm \
--function vote \
--args '["GBK2...456", 1, true]' \
--storage '{"Proposal(1)": {...}, "Votes(1)": {}}'Use the interactive mode to step through the tally function and watch the vote count increment:
soroban-debug interactive --contract target/wasm32-unknown-unknown/release/voting_contract.wasmOnce in the interactive TUI:
- Use
break tallyto set a breakpoint at the start of the tally function. - Use
stepto execute instructions line by line. - Use
inspectto view the values ofyaysandnaysas they update.
- Persistent Storage: Proposals and vote maps are stored persistently.
- Nested Maps: The
Voteskey points to aMap<Address, bool>, demonstrating how the debugger handles nested data structures. - Access Control: The
closefunction demonstrates howrequire_auth()works and how to debug authentication failures.