We will add a proposal system and an option for registered voters to vote. For this, we’ll need an array to store the proposals and a new function for voting.
In the Voter
contract, add the following lines of code:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Voter {
struct Person {
bool voted;
uint vote;
}
struct Proposal {
string name;
uint voteCount;
}
Proposal[] public proposals;
mapping(address => Person) public voters;
function registerVoter() public {
voters[msg.sender].voted = false;
}
function addProposal(string memory _name) public {
proposals.push(Proposal(_name, 0));
}
function vote(uint _proposal) public {
require(_proposal < proposals.length, "Invalid proposal index."); // This is the added check
Person storage sender = voters[msg.sender];
require(!sender.voted, "Already voted.");
sender.voted = true;
sender.vote = _proposal;
proposals[_proposal].voteCount += 1;
}
}
Let’s break down the new code:
Proposal Struct: We’ve added a new struct, Proposal
, that contains the name of the proposal and the count of votes it has received.
Proposals Array: The proposals
array holds all the proposals in the voting system.
Add Proposal Function: The addProposal
function allows us to add a new proposal to the proposals
array. The proposal name is passed as a parameter, and the vote count is initially set to zero.
Vote Function: The vote
function allows a registered voter to cast their vote. It takes the index of a proposal as a parameter and increases the proposal’s vote count by one. It also checks if the voter has already voted and updates the voter’s status.
Now that we have added more features to our contract, it’s time to compile and test it. In the Remix IDE, click on the Solidity compiler icon on the left sidebar and then click on the “Compile” button.
To test your contract, go to the “Deploy & Run Transactions” tab (the one below the Solidity compiler icon) and click on the “Deploy” button. Once the contract is deployed, you can interact with it by calling its functions from this tab. Try registering a voter, adding a proposal, and casting a vote to see how it works.
In the next lesson, we will implement the functionality to tally the votes and announce the result of the voting process. Until then, feel free to explore and experiment with the contract. Solidity offers many more features that can make your contract more robust and secure. Happy coding!
We will add a proposal system and an option for registered voters to vote. For this, we’ll need an array to store the proposals and a new function for voting.
In the Voter
contract, add the following lines of code:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Voter {
struct Person {
bool voted;
uint vote;
}
struct Proposal {
string name;
uint voteCount;
}
Proposal[] public proposals;
mapping(address => Person) public voters;
function registerVoter() public {
voters[msg.sender].voted = false;
}
function addProposal(string memory _name) public {
proposals.push(Proposal(_name, 0));
}
function vote(uint _proposal) public {
require(_proposal < proposals.length, "Invalid proposal index."); // This is the added check
Person storage sender = voters[msg.sender];
require(!sender.voted, "Already voted.");
sender.voted = true;
sender.vote = _proposal;
proposals[_proposal].voteCount += 1;
}
}
Let’s break down the new code:
Proposal Struct: We’ve added a new struct, Proposal
, that contains the name of the proposal and the count of votes it has received.
Proposals Array: The proposals
array holds all the proposals in the voting system.
Add Proposal Function: The addProposal
function allows us to add a new proposal to the proposals
array. The proposal name is passed as a parameter, and the vote count is initially set to zero.
Vote Function: The vote
function allows a registered voter to cast their vote. It takes the index of a proposal as a parameter and increases the proposal’s vote count by one. It also checks if the voter has already voted and updates the voter’s status.
Now that we have added more features to our contract, it’s time to compile and test it. In the Remix IDE, click on the Solidity compiler icon on the left sidebar and then click on the “Compile” button.
To test your contract, go to the “Deploy & Run Transactions” tab (the one below the Solidity compiler icon) and click on the “Deploy” button. Once the contract is deployed, you can interact with it by calling its functions from this tab. Try registering a voter, adding a proposal, and casting a vote to see how it works.
In the next lesson, we will implement the functionality to tally the votes and announce the result of the voting process. Until then, feel free to explore and experiment with the contract. Solidity offers many more features that can make your contract more robust and secure. Happy coding!