Skip to content

Instantly share code, notes, and snippets.

@PanosJee
Created October 20, 2018 21:27
Show Gist options
  • Save PanosJee/b84d6933b4b614f163ceb8cd264f24f3 to your computer and use it in GitHub Desktop.
Save PanosJee/b84d6933b4b614f163ceb8cd264f24f3 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.25+commit.59dbf8f1.js&optimize=true&gist=
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
{
"VM:-": {
"linkReferences": {},
"autoDeployLib": true
},
"main:1": {
"linkReferences": {},
"autoDeployLib": true
},
"ropsten:3": {
"linkReferences": {},
"autoDeployLib": true
},
"rinkeby:4": {
"linkReferences": {},
"autoDeployLib": true
},
"kovan:42": {
"linkReferences": {},
"autoDeployLib": true
},
"Custom": {
"linkReferences": {},
"autoDeployLib": true
}
}
pragma solidity ^0.4.0;
contract Voting {
enum Vote {ZERO, ONE, TWO}
enum VoterStatus {Verified, Unverified}
mapping(address => VoterStatus) private voters;
Vote[] private votes;
address chairperson;
address[] board_members;
/// Create a new voting with $(_board_members) different board members.
function Voting() public {
chairperson = msg.sender;
// board_members = [];
}
function _canEdit()
private
constant
returns (bool is_member)
{
is_member = msg.sender==chairperson;
for (uint8 i=0; i < board_members.length; i++){
if (is_member==false && board_members[i]==msg.sender) {
is_member = true;
break;
}
}
}
/// Allow the board to verify addreses and allow them to vote again
modifier onlyBoardMember() { // Modifier
require(
_canEdit() == true,
"Only contract owners & board members can call this."
);
_;
}
/// Only addresses already approved by the board can vote
modifier onlyVerifiedVoter(){
require(
voters[msg.sender] == VoterStatus.Verified,
"Only verfierd addresses can vote."
);
_;
}
/// Give $(addr) the right to vote on this ballot.
/// May only be called by $(chairperson or board member).
function verify(address addr) public onlyBoardMember {
voters[addr] = VoterStatus.Verified;
}
/// Give a single vote to proposal $(toProposal).
function vote(uint8 toVote) public onlyVerifiedVoter {
VoterStatus status = voters[msg.sender];
if (status == VoterStatus.Verified){
Vote v;
if (toVote ==1) v = Vote.ONE;
else if (toVote == 2) v = Vote.TWO;
else if (toVote == 0) v = Vote.ZERO;
else revert("Invalid vote");
votes.push(v);
voters[msg.sender] = VoterStatus.Unverified;
}
}
function winningProposal() public constant returns (Vote _winningProposal) {
uint8 ZEROVoteCount = 0;
uint8 ONEVoteCount = 0;
uint8 TWOVoteCount = 0;
for (uint8 i = 0; i < votes.length; i++){
Vote v = votes[i];
if (v == Vote.ZERO) ZEROVoteCount +=1;
else if (v == Vote.ONE) ONEVoteCount +=1;
else TWOVoteCount += 1;
}
// what about equal outcome??
if (ZEROVoteCount > ONEVoteCount && ZEROVoteCount > TWOVoteCount)
_winningProposal = Vote.ZERO;
else if (ONEVoteCount > ZEROVoteCount && ONEVoteCount > TWOVoteCount)
_winningProposal = Vote.ONE;
else
_winningProposal = Vote.TWO;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment