Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prettyirrelevant/1871d0344261888257a142a76610ba4d to your computer and use it in GitHub Desktop.
Save prettyirrelevant/1871d0344261888257a142a76610ba4d 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.4.26+commit.4563c3fc.js&optimize=false&runs=200&gist=
pragma solidity ^0.4.0;
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
uint voteCount;
}
enum Stage {Init,Reg, Vote, Done}
Stage public stage = Stage.Init;
address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;
event votingCompleted();
uint startTime;
//modifiers
modifier validStage(Stage reqStage){
require(stage == reqStage);
_;
}
/// Create a new ballot with $(_numProposals) different proposals.
constructor (uint8 _numProposals) public {
chairperson = msg.sender;
voters[chairperson].weight = 2; // weight is 2 for testing purposes
proposals.length = _numProposals;
stage = Stage.Reg;
startTime = now;
}
/// Give $(toVoter) the right to vote on this ballot.
/// May only be called by $(chairperson).
function register(address toVoter) public validStage(Stage.Reg) {
//if (stage != Stage.Reg) {return;}
if (msg.sender != chairperson || voters[toVoter].voted) return;
voters[toVoter].weight = 1;
voters[toVoter].voted = false;
if (now > (startTime+ 30 seconds)) {stage = Stage.Vote; }
}
/// Give a single vote to proposal $(toProposal).
function vote(uint8 toProposal) public validStage(Stage.Vote) {
// 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+ 30 seconds)) {
stage = Stage.Done;
emit votingCompleted();
}
}
function winningProposal() public validStage(Stage.Done) 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;
}
assert (winningVoteCount > 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment