Created
May 10, 2020 00:16
-
-
Save uneeb123/8dcdffed63fed42d4f27594eedd90192 to your computer and use it in GitHub Desktop.
This file contains 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
contract SimpleYesNoVotingContract { | |
event voteStarted(); | |
event voted(address voter); | |
event voteEnded(uint yesVotes, uint noVotes) | |
address public officialAddress; | |
enum State { Created, Voting, Ended } | |
State public state; | |
modifier inState(State _state) { | |
require(state == _state); | |
_; | |
} | |
modifier onlyOfficial() { | |
require(msg.sender == officialAddress); | |
_; | |
} | |
modifier notAlreadyVoted() { | |
require(!voteRegister(msg.sender)); | |
_; | |
} | |
constructor() public { | |
officialAddress = msg.sender; | |
state = State.Created; | |
} | |
mapping(address => bool) public voteRegister; | |
uint public yesVotes = 0; | |
uint public noVotes = 0; | |
function startVote() | |
public | |
inState(State.Created) | |
onlyOfficial | |
{ | |
state = State.Voting; | |
emit voteStarted(); | |
} | |
function vote(bool yes) | |
public | |
inState(State.Voting) | |
notAlreadyVoted | |
returns (bool voted) | |
{ | |
if (yes){ | |
yesVotes++; | |
} else { | |
noVotes++; | |
} | |
voteRegister[msg.sender] = true; | |
emit voted(msg.sender); | |
} | |
function endVote() | |
public | |
inState(State.Voting) | |
onlyOfficial | |
{ | |
state = State.Ended; | |
emit voteEnded(yesVotes, noVotes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment