Created
February 10, 2019 08:37
-
-
Save vidit0210/c936c540aae4a020c29bd7cff4c2b776 to your computer and use it in GitHub Desktop.
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
| pragma solidity ^0.4.0; | |
| contract Ballot { | |
| struct Voter { | |
| uint weight; | |
| bool voted; | |
| uint8 vote; | |
| } | |
| struct Proposal{ | |
| uint voteCount; | |
| } | |
| enum Stage{Init,Reg,Vote,Done} | |
| Stage public stage = Stage.Init; | |
| address chairperson; | |
| mapping(address => Voter) voters; | |
| Proposal[] proposals; | |
| uint startTime; | |
| function Ballot(uint8 _numProposals) public{ | |
| chairperson = msg.sender; | |
| voters[chairperson].weight = 2; | |
| proposals.length=_numProposals; | |
| stage = Stage.Reg; | |
| startTime = now; | |
| } | |
| //Give voter the Right to vote | |
| //Can be given only by the chairperson | |
| function register ( address toVoter){ | |
| if(stage != Stage.Reg) return; | |
| if(msg.sender != chairperson||voters[toVoter].voted) return; | |
| voters[toVoter].weight=1; | |
| voters[toVoter].voted = false; | |
| if(now > (startTime + 10 seconds)) { | |
| stage = Stage.Vote; | |
| startTime = now; | |
| } | |
| } | |
| function vote(uint8 toProposal) public { | |
| if(stage != Stage.Vote) return; | |
| Voter storage sender = voters[msg.sender]; | |
| if(sender.voted || toProposal >= proposals.length) return; | |
| sender.voted = true; | |
| sender.vote = toProposal; | |
| proposals[toProposal].voteCount+=sender.weight; | |
| if(now > (startTime + 10 seconds)) {stage = Stage.Done;} | |
| } | |
| function winningProposal() public constant returns(uint8 _winningProposal){ | |
| if(stage != Stage.Done){return;} | |
| uint256 winningVoteCount =0; | |
| for(uint8 prop =0;prop<proposals.length;prop++) | |
| { | |
| if(proposals[prop].voteCount>winningVoteCount){ | |
| winningVoteCount=proposals[prop].voteCount; | |
| _winningProposal = prop; | |
| } | |
| } | |
| } | |
| } |
shruti-kashyap877
commented
May 20, 2025
- [ ]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment