What Are We Going To Build
In this lesson, you will be building a smart contract that allows the owner to create and manage proposals that other users can vote on.
This is a simple voting system that can be used for various purposes, such as governance, decision making, or feedback.
You will learn how to use structs, mappings, arrays, modifiers, and libraries to implement the logic of the proposal system. By the end of this tutorial, you will have a working proposal contract that you can customize and extend for your own needs.
What does Proposal Contract do:
Starting to the Contract
Let's start with defining our contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract ProposalContract {
// Our contract code
}
Now that you have the basis for our contract, you can create our proposal structure.
To store the proposals, first you need to have a structure that has the necessary fields for our Proposal.
struct Proposal {
string description; // Description of the proposal
uint256 approve; // Number of approve votes
uint256 reject; // Number of reject votes
uint256 pass; // Number of pass votes
uint256 total_vote_to_end; // When the total votes in the proposal reaches this limit, proposal ends
bool current_state; // This shows the current state of the proposal, meaning whether if passes of fails
bool is_active; // This shows if others can vote to our contract
}
Now that you have a necessary structure for our proposal but the question arises, how can you store them? The answer is using mapping.
mapping(uint256 => Proposal) proposal_history; // Recordings of previous proposals
In this mapping you have uint256 as the key and the proposal as the value. So, you will be able to get the proposal based on its key uint256 value, which will be the id of the proposal.
So far, your contract looks like this:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract ProposalContract {
struct Proposal {
string description; // Description of the proposal
uint256 approve; // Number of approve votes
uint256 reject; // Number of reject votes
uint256 pass; // Number of pass votes
uint256 total_vote_to_end; // When the total votes in the proposal reaches this limit, proposal ends
bool current_state; // This shows the current state of the proposal, meaning whether if passes of fails
bool is_active; // This shows if others can vote to our contract
}
mapping(uint256 => Proposal) proposal_history; // Recordings of previous proposals
}
You have planned that our proposals will have ids to identify them in the mapping. For that you need some system so that you can track the ids of the proposals. So, what would that system be like?
The next part will be answering this question.
Swap insights and ask questions about “Learn everything about Solidity”.
Ask a question or share your thoughts about this lesson.