Created
February 7, 2023 16:58
-
-
Save ArslanKathia/44c27284a4733f3076ec8817148222ba to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.17+commit.8df45f5f.js&optimize=false&runs=200&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >= 0.7.0 < 0.8.18; | |
contract Ballot{ | |
struct Voter{ | |
uint weight; | |
bool voted; | |
address delegate; | |
uint vote; | |
} | |
struct Proposal{ | |
bytes32 name; | |
uint voteCount; | |
} | |
address public chairperson; | |
mapping(address => Voter) public voters; | |
Proposal[] public proposals; | |
constructor(bytes32[] memory proposalNames){ | |
chairperson = msg.sender; | |
voters[chairperson].weight = 1; | |
//thechariperson is also a voter and his weight is assigned 1 | |
for(uint i=0;i<proposalNames.length;i++){ | |
proposals.push(Proposal({ | |
name: proposalNames[i], | |
voteCount:0 | |
})); | |
} | |
} | |
function giveRightToVote(address voter) external{ | |
require( | |
msg.sender == chairperson, | |
"Only Chairperson can give right to vote" | |
); | |
require( | |
!voters[voter].voted, | |
"The voter already voted." | |
); | |
require(voters[voter].weight == 0); | |
voters[voter].weight = 1; | |
} | |
function delegate(address to) external{ | |
Voter storage sender = voters[msg.sender]; | |
require(sender.weight !=0 ,"You have no right to vote"); | |
require(!sender.voted,"You already voted."); | |
require(to != msg.sender,"Self-delefation is disallowed"); | |
while(voters[to].delegate !=address(0)){ | |
to = voters[to].delegate; | |
require(to!=msg.sender,"Found loop in delegation"); | |
} | |
Voter storage delegate_ = voters[to]; | |
require(delegate_.weight >=1); | |
sender.voted = true; | |
sender.delegate = to; | |
if(delegate_.voted){ | |
proposals[delegate_.vote].voteCount +=sender.weight; | |
}else{ | |
delegate_.weight +=sender.weight; | |
} | |
} | |
function vote(uint proposal) external{ | |
Voter storage sender = voters[msg.sender]; | |
require(sender.weight!=0,"Has no right to vote"); | |
require(!sender.voted,"Already Voted"); | |
sender.voted = true; | |
sender.vote = proposal; | |
proposals[proposal].voteCount +=sender.weight; | |
} | |
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() external view returns(bytes32 winnerName_){ | |
winnerName_ = proposals[winningProposal()].name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment