Let’s start by adding the winningProposal
function to our contract. This function will iterate over all the proposals and return the one with the most votes. Additionally, we’ll create a function winnerName
to return the name of the winning proposal. Here is the final version of our contract:
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;
}
function winningProposal() public view returns (uint winningProposal_) {
uint winningVoteCount = 0;
for (uint p = 0; p < proposals.length; p++) {
if (proposals[p].voteCount > winningVoteCount) {
winningVoteCount = proposals[p].voteCount;
winningProposal_ = p;
}
}
}
function winnerName() public view returns (string memory winnerName_) {
winnerName_ = proposals[winningProposal()].name;
}
}
Let’s explain the new additions:
Winning Proposal Function: This function iterates over all the proposals and finds the one with the highest vote count. It returns the index of this proposal in the proposals
array. It’s a view
function, meaning it doesn’t modify the state of the contract and only reads the data.
Winner Name Function: This function calls the winningProposal
function to get the index of the winning proposal and then returns the name of this proposal.
Compile your contract by clicking on the Solidity compiler icon on the left sidebar and then clicking on the “Compile” button.
Deploy your contract in the “Deploy & Run Transactions” tab and interact with it. Register some voters, add a few proposals, cast your votes, and finally, check the winner.
In the next lesson, we’ll discuss contract interactions and event logging. We’ll learn how contracts can interact with each other and how to log and monitor activities on blockchain. Congratulations on building your first decentralized voting system on Ethereum!
Let’s start by adding the winningProposal
function to our contract. This function will iterate over all the proposals and return the one with the most votes. Additionally, we’ll create a function winnerName
to return the name of the winning proposal. Here is the final version of our contract:
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;
}
function winningProposal() public view returns (uint winningProposal_) {
uint winningVoteCount = 0;
for (uint p = 0; p < proposals.length; p++) {
if (proposals[p].voteCount > winningVoteCount) {
winningVoteCount = proposals[p].voteCount;
winningProposal_ = p;
}
}
}
function winnerName() public view returns (string memory winnerName_) {
winnerName_ = proposals[winningProposal()].name;
}
}
Let’s explain the new additions:
Winning Proposal Function: This function iterates over all the proposals and finds the one with the highest vote count. It returns the index of this proposal in the proposals
array. It’s a view
function, meaning it doesn’t modify the state of the contract and only reads the data.
Winner Name Function: This function calls the winningProposal
function to get the index of the winning proposal and then returns the name of this proposal.
Compile your contract by clicking on the Solidity compiler icon on the left sidebar and then clicking on the “Compile” button.
Deploy your contract in the “Deploy & Run Transactions” tab and interact with it. Register some voters, add a few proposals, cast your votes, and finally, check the winner.
In the next lesson, we’ll discuss contract interactions and event logging. We’ll learn how contracts can interact with each other and how to log and monitor activities on blockchain. Congratulations on building your first decentralized voting system on Ethereum!