The VotingContract focuses on the individual voter's interaction with elections, enabling them to cast their ranked votes for candidates in specific elections. It interfaces with the BallotContract to submit these votes, ensuring that each voter's preferences are recorded accurately and securely.
This contract includes functionalities to verify if a voter has already participated in an election, to add their votes, and to retrieve election results, candidates, and the voter's own voting status and choices. It essentially acts as the bridge between voters and the election management system established by the BallotContract, facilitating a seamless and transparent voting process.
// SPDX-License-Identifier: MITpragmasolidity 0.8.22;/* Imports */import"./IBallotContract.sol";/* Events */// Event to record when a vote is countedeventVoteCast(addressindexed voter,uint8[] rankedChoices );/** * @title VotingContract * @dev Contract to manage individual votes, recording voters' ranked choices for candidates. * Also store votes and interact with the BallotContract */contract VotingContract {/* State Variables */addressprivate owner;// BallotContract instance IBallotContract private ballotContract;boolprivate hasVoted;/* Constructor */// Set the owner of the contractconstructor(address_ballotContractAddress) { owner = msg.sender; ballotContract =IBallotContract(_ballotContractAddress); }// Mapping to store a voters voting statusmapping(uint=>mapping(address=>bool)) public voted;/* Functions *//* Function to vote on a ballot * @param _votes The ranked choices of the voter */functionaddVotes(uint8[] memory_votes,uint_electionId ) public {// require voter has not votedrequire( voted[_electionId][msg.sender] ==false,"Voter has already voted" );// Add the votes to the BallotContract ballotContract.checkElection(_votes, _electionId);// Update the voter's choices in the BallotContract ballotContract.updateVoterChoices(_votes[0],_votes[1], _votes[2], _electionId, msg.sender);// Add the votes to the BallotContract ballotContract.addVotesToCount(_votes[0],_votes[1], _votes[2], _electionId); hasVoted =true;// Set the voter's vote status to true voted[_electionId][msg.sender] = hasVoted;// Emit the VoteCast eventemitVoteCast(msg.sender, _votes); }/* Getter Functions */// Function to get the owner of the contractfunctiongetOwner() publicviewreturns (address) {return owner; }// Function to get the election statusfunctiongetElectionStatus(uint_electionId) publicviewreturns (bool) {return ballotContract.getElectionStatus(_electionId); }// Function to get election candidatesfunctiongetElectionCandidates(uint_electionId)externalviewreturns (string[] memory) {return ballotContract.getElectionCandidates(_electionId); }// Function to get a voter's vote statusfunctiongetVoterStatus(address_voter,uint_electionId)publicviewreturns (bool) {return ballotContract.getVoterStatus(_voter, _electionId); }// Function to get a voter's ranked choicesfunctiongetVoterChoices(address_voter,uint_electionId ) publicviewreturns (uint8,uint8,uint8) {require( voted[_electionId][_voter] ==true,"Voter has not voted" );return ballotContract.getVoterChoices(_voter, _electionId); }}