-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleBallot (basic)
More file actions
65 lines (56 loc) · 1.9 KB
/
ExampleBallot (basic)
File metadata and controls
65 lines (56 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pragma solidity ^0.5.1;
contract Ballot {
// chairperson
struct Voter {
uint weight;
bool voted;
uint8 vote;
// address delegate;
}
//modifer
modifier onlyOwner () {
require(msg.sender == chairperson);
_;
}
/* struct Proposal {
uint voteCount; // could add other data about proposal
} */
address public chairperson;
mapping(address => Voter) public voters;
uint[4] public proposals;
// Create a new ballot with 4 different proposals.
constructor() public {
chairperson = msg.sender;
voters[chairperson].weight = 2;
}
/// Give $(toVoter) the right to vote on this ballot.
/// May only be called by $(chairperson).
function register(address toVoter) public onlyOwner{
if(voters[toVoter].weight != 0) revert();
voters[toVoter].weight = 1;
voters[toVoter].voted = false;
}
/// Give a single vote to proposal $(toProposal).
/// chairperson has a vote weight of 2 other voters have weight of 1
function vote(uint8 toProposal) public {
Voter storage sender = voters[msg.sender];
if (sender.voted || toProposal >= 4 || sender.weight == 0) revert();
sender.voted = true;
sender.vote = toProposal;
proposals[toProposal] += sender.weight;
}
// This function returns the winning proposal but in case of draw
// it returns the proposal with lowest index
function winningProposal() public view returns (uint8 _winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 prop = 0; prop < 4; prop++)
if (proposals[prop] > winningVoteCount) {
winningVoteCount = proposals[prop];
_winningProposal = prop;
}
}
// Returns Proposal value array
function getCount() public view returns (uint[4] memory) {
return proposals;
}
}