forked from pacta-app/vote-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibVoting.sol
More file actions
126 lines (116 loc) · 3.16 KB
/
Copy pathLibVoting.sol
File metadata and controls
126 lines (116 loc) · 3.16 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
pragma solidity >=0.0;
import "./libsign.sol";
import "./TokenErc20Ifc.sol";
library LibVoting {
struct Data {
string title;
string proposal;
uint256 starttime;
uint256 endtime;
uint256 aye;
uint256 nay;
uint256 abstain;
uint256 standDown;
TokenErc20 tokenErc20;
mapping(address => bool) voters;
}
enum Vote {
Yes, /*0*/
No, /*1*/
Abstain, /*2*/
StandDown /*3*/
}
function construct(
Data storage data,
string memory title,
string memory proposal,
TokenErc20 token
) public {
require(bytes(title).length > 0, "voting title is required");
require(bytes(proposal).length > 0, "voting proposal is required");
data.title = title;
data.proposal = proposal;
data.starttime = 0;
data.endtime = 0;
data.tokenErc20 = token;
}
function setVotingTime(
Data storage data,
uint256 starttime,
uint256 endtime
) public {
if (starttime == 0 && endtime > 0) {
starttime = now;
endtime += starttime;
}
require(endtime != 0, "endttime is not defined");
require(starttime != 0, "startime is not defined");
require(endtime > starttime, "endttime is not after starttime");
require(starttime >= now, "start time must be in the future");
require(
data.starttime == 0 && data.endtime == 0,
"time is already configured"
);
data.starttime = starttime;
data.endtime = endtime;
}
function castVote(
Data storage data,
Vote vote,
address a,
uint8 v,
bytes32 r,
bytes32 s
) public returns (uint256 shares) {
address shareholder = libsign.verify(abi.encode(vote, a), v, r, s);
require(!data.voters[shareholder], "already voted");
shares = data.tokenErc20.balanceOf(shareholder);
require(shares > 0, "not a validated shareholder");
data.voters[shareholder] = true;
if (vote == Vote.Yes) {
data.aye += shares;
} else if (vote == Vote.No) {
data.nay += shares;
} else if (vote == Vote.Abstain) {
data.abstain += shares;
} else if (vote == Vote.StandDown) {
data.standDown += shares;
}
}
/* function voteYes(
Data storage data,
address a,
uint8 v,
bytes32 r,
bytes32 s
) public {
data.aye += castVote(data, a, v, r, s);
}
function voteNo(
Data storage data,
address a,
uint8 v,
bytes32 r,
bytes32 s
) public {
data.nay += castVote(data, a, v, r, s);
}
function voteAbstain(
Data storage data,
address a,
uint8 v,
bytes32 r,
bytes32 s
) public {
data.abstain += castVote(data, a, v, r, s);
}
function voteStandDown(
Data storage data,
address a,
uint8 v,
bytes32 r,
bytes32 s
) public {
data.standDown += castVote(data, a, v, r, s);
} */
}