Skip to content

Commit

Permalink
Merge pull request #25 from sum1t-here/feat/voting
Browse files Browse the repository at this point in the history
feat: add functionality to end election
  • Loading branch information
ishita-43 authored Oct 16, 2024
2 parents 228acf2 + ba9a671 commit ac5f19e
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Foundry_Contracts/src/Voting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct Candidate{

address public owner;
string public electionName;
bool public electionEnded = false;

mapping(address => Voter) public voters;
Candidate[] public candidates;
Expand All @@ -39,6 +40,12 @@ modifier ownerOnly(){
_;
}


modifier electionOngoing() {
require(!electionEnded, "Election has ended");
_;
}

function addCandidate(string memory _name) public ownerOnly {
// Check if a candidate with the same name already exists
for (uint i = 0; i < candidates.length; i++) {
Expand All @@ -60,17 +67,22 @@ function authorize(address _person) public ownerOnly {
voters[_person].authorized = true;
}

function vote(uint _voteIndex) public {
if (voters[msg.sender].voted) revert Voting__AlreadyVoted();
if (!voters[msg.sender].authorized) revert Voting__NotAuthorized();
if (_voteIndex >= candidates.length) revert Voting__IncorrectVoteIndex();

function vote(uint _voteIndex) public electionOngoing() {
require(!voters[msg.sender].voted, Voting__AlreadyVoted());
require(voters[msg.sender].authorized, Voting__NotAuthorized());
require(_voteIndex<candidates.length, Voting__IncorrectVoteIndex());

voters[msg.sender].vote = _voteIndex;
voters[msg.sender].voted = true;
candidates[_voteIndex].voteCount += 1;
totalVotes +=1;
}

function endElection() public ownerOnly {
electionEnded = true;
}

function getCandidates() public view returns (Candidate[] memory) {
return candidates;
}
Expand Down

0 comments on commit ac5f19e

Please sign in to comment.