Welcome to this tutorial where we begin developing a basic voting application. In this project, users will be able to vote to approve, reject, or pass a proposal. These proposals will be stored, allowing us to iterate over them for further development or voting.
We start with setting up our basic structure and necessary imports. We also define a memory setup and a MAX_VALUE_SIZE, which are foundational elements that will be reused throughout the project.
The project involves two primary structs:
For the Proposal struct:
These structs are marked as CandidType and Deserialize to allow conversion for the Candid interface.
The second struct, used for function parameters like creating or editing proposals, contains only:
This struct is kept minimal to avoid unnecessary manipulation of sensitive data like vote counts or voter identities, which are managed internally.
We implement the Storable trait for the Proposal struct to enable serialization and deserialization:
These are essential for storing and retrieving proposal data efficiently in a persistent environment.
To manage memory, we implement a memory manager using RefCell. This allows us to simulate multiple memory instances virtually. The memory manager is initialized using the default memory implementation.
We also define a proposal_map using StableBTreeMap to store our proposals:
This map is initialized inside the memory manager and uses virtual index 0 for organization.
In this lesson, we:
This setup forms the foundation of our smart contract for the voting application. In the next lesson, we will move on to implementing the functional logic that allows proposal creation and voting.
Swap insights and ask questions about “Build on Internet Computer with ICP Rust CDK”.
Ask a question or share your thoughts about this lesson.