Rise In Logo

Build on Internet Computer with ICP Rust CDK

Creating Smart Contract for Final Project

Creating Smart Contract for Final Project

Building a Voting App: Structs, Serialization, and Memory Management

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.

Initial Setup and Imports

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.

Defining the Proposal Struct

The project involves two primary structs:

  1. Proposal Struct – Contains all data related to a proposal.
  2. Function Parameter Struct – Used exclusively for functions such as creating or editing proposals.

For the Proposal struct:

  • It includes fields such as:
  • description (String)
  • approve, reject, and pass (u32) – These are counters to record votes.
  • is_active (bool) – Indicates whether the proposal can be voted on.
  • voted (Vec<Principal>) – Stores principals who have already voted.
  • owner (Principal) – Identifies the creator of the proposal.

These structs are marked as CandidType and Deserialize to allow conversion for the Candid interface.

Creating the Function Parameter Struct

The second struct, used for function parameters like creating or editing proposals, contains only:

  • description (String)
  • is_active (bool)

This struct is kept minimal to avoid unnecessary manipulation of sensitive data like vote counts or voter identities, which are managed internally.

Implementing the Storable Trait

We implement the Storable trait for the Proposal struct to enable serialization and deserialization:

  • to_bytes method serializes the struct for stable memory storage.
  • from_bytes method deserializes the byte data back into a usable format.

These are essential for storing and retrieving proposal data efficiently in a persistent environment.

Configuring Memory Management

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:

  • Key: u64
  • Value: Proposal

This map is initialized inside the memory manager and uses virtual index 0 for organization.

Summary

In this lesson, we:

  • Defined two essential structs for proposals and function parameters.
  • Implemented serialization and deserialization using the Storable trait.
  • Set up memory management using a virtualized memory manager.
  • Created a StableBTreeMap to store and access proposal data.

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.

Comments

You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!

Disclaimer: The information /programs / events provided on https://patika.dev and https://risein.com are strictly for upskilling and networking purposes related to the technical infrastructure of blockchain platforms. We do not provide financial or investment advice and do not make any representations regarding the value, profitability, or future price of any blockchain or cryptocurrency. Users are encouraged to conduct their own research and consult with licensed financial professionals before engaging in any investment activities. https://patika.dev and https://risein.com disclaim any responsibility for financial decisions made by users based on information provided here.