|
| 1 | +/** |
| 2 | + * @file ballot.sol |
| 3 | + */ |
| 4 | + |
| 5 | +pragma solidity ^0.5.0; |
| 6 | + |
| 7 | +contract Ballot { |
| 8 | + |
| 9 | + struct vote{ |
| 10 | + address voterAddress; |
| 11 | + bool choice; |
| 12 | + } |
| 13 | + |
| 14 | + struct voter{ |
| 15 | + string voterName; |
| 16 | + bool voted; |
| 17 | + } |
| 18 | + |
| 19 | + uint private countResult = 0; |
| 20 | + uint public finalResult = 0; |
| 21 | + uint public totalVoter = 0; |
| 22 | + uint public totalVote = 0; |
| 23 | + address public ballotOfficialAddress; |
| 24 | + string public ballotOfficialName; |
| 25 | + string public proposal; |
| 26 | + |
| 27 | + mapping(uint => vote) private votes; |
| 28 | + mapping(address => voter) public voterRegister; |
| 29 | + |
| 30 | + enum State { Created, Voting, Ended } |
| 31 | + State public state; |
| 32 | + |
| 33 | + //creates a new ballot contract |
| 34 | + constructor( |
| 35 | + string memory _ballotOfficialName, |
| 36 | + string memory _proposal) public { |
| 37 | + ballotOfficialAddress = msg.sender; |
| 38 | + ballotOfficialName = _ballotOfficialName; |
| 39 | + proposal = _proposal; |
| 40 | + |
| 41 | + state = State.Created; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + modifier condition(bool _condition) { |
| 46 | + require(_condition); |
| 47 | + _; |
| 48 | + } |
| 49 | + |
| 50 | + modifier onlyOfficial() { |
| 51 | + require(msg.sender ==ballotOfficialAddress); |
| 52 | + _; |
| 53 | + } |
| 54 | + |
| 55 | + modifier inState(State _state) { |
| 56 | + require(state == _state); |
| 57 | + _; |
| 58 | + } |
| 59 | + |
| 60 | + event voterAdded(address voter); |
| 61 | + event voteStarted(); |
| 62 | + event voteEnded(uint finalResult); |
| 63 | + event voteDone(address voter); |
| 64 | + |
| 65 | + //add voter |
| 66 | + function addVoter(address _voterAddress, string memory _voterName) |
| 67 | + public |
| 68 | + inState(State.Created) |
| 69 | + onlyOfficial |
| 70 | + { |
| 71 | + voter memory v; |
| 72 | + v.voterName = _voterName; |
| 73 | + v.voted = false; |
| 74 | + voterRegister[_voterAddress] = v; |
| 75 | + totalVoter++; |
| 76 | + emit voterAdded(_voterAddress); |
| 77 | + } |
| 78 | + |
| 79 | + //declare voting starts now |
| 80 | + function startVote() |
| 81 | + public |
| 82 | + inState(State.Created) |
| 83 | + onlyOfficial |
| 84 | + { |
| 85 | + state = State.Voting; |
| 86 | + emit voteStarted(); |
| 87 | + } |
| 88 | + |
| 89 | + //voters vote by indicating their choice (true/false) |
| 90 | + function doVote(bool _choice) |
| 91 | + public |
| 92 | + inState(State.Voting) |
| 93 | + returns (bool voted) |
| 94 | + { |
| 95 | + bool found = false; |
| 96 | + |
| 97 | + if (bytes(voterRegister[msg.sender].voterName).length != 0 |
| 98 | + && !voterRegister[msg.sender].voted){ |
| 99 | + voterRegister[msg.sender].voted = true; |
| 100 | + vote memory v; |
| 101 | + v.voterAddress = msg.sender; |
| 102 | + v.choice = _choice; |
| 103 | + if (_choice){ |
| 104 | + countResult++; //counting on the go |
| 105 | + } |
| 106 | + votes[totalVote] = v; |
| 107 | + totalVote++; |
| 108 | + found = true; |
| 109 | + } |
| 110 | + emit voteDone(msg.sender); |
| 111 | + return found; |
| 112 | + } |
| 113 | + |
| 114 | + //end votes |
| 115 | + function endVote() |
| 116 | + public |
| 117 | + inState(State.Voting) |
| 118 | + onlyOfficial |
| 119 | + { |
| 120 | + state = State.Ended; |
| 121 | + finalResult = countResult; //move result from private countResult to public finalResult |
| 122 | + emit voteEnded(finalResult); |
| 123 | + } |
| 124 | +} |
0 commit comments