-
Notifications
You must be signed in to change notification settings - Fork 0
/
Referendum.sol
61 lines (51 loc) · 1.33 KB
/
Referendum.sol
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
pragma solidity ^0.5.16;
contract Referendum {
struct Result {
uint256 yes;
uint256 no;
uint256 againstAll;
uint256 totalVotes;
}
struct Vote {
bool hasVoted;
uint256 answer;
}
mapping (address => Vote) votes;
Result result;
constructor() public {
result = Result(0, 0, 0, 0);
}
function vote(uint8 answer) private returns(bool) {
address voter = msg.sender;
if(votes[voter].hasVoted) {
return false;
}
else {
if(answer == 0) {
result.yes++;
}
else if(answer == 1) {
result.no++;
}
else if(answer == 2) {
result.againstAll++;
}
result.totalVotes++;
votes[voter].answer = answer;
votes[voter].hasVoted = true;
return true;
}
}
function voteYes() public returns(bool) {
return vote(0);
}
function voteNo() public returns(bool) {
return vote(1);
}
function voteAgainstAll() public returns(bool) {
return vote(2);
}
function getResult() public view returns(uint256, uint256, uint256, uint256) {
return (result.yes, result.no, result.againstAll, result.totalVotes);
}
}