-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGovernance.sol
547 lines (427 loc) · 19.9 KB
/
Governance.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.7.6;
pragma experimental ABIEncoderV2;
import "./interfaces/IBarn.sol";
import "./Bridge.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
contract Governance is Bridge {
using SafeMath for uint256;
enum ProposalState {
WarmUp,
Active,
Canceled,
Failed,
Accepted,
Queued,
Grace,
Expired,
Executed,
Abrogated
}
struct Receipt {
// Whether or not a vote has been cast
bool hasVoted;
// The number of votes the voter had, which were cast
uint256 votes;
// support
bool support;
}
struct AbrogationProposal {
address creator;
uint256 createTime;
string description;
uint256 forVotes;
uint256 againstVotes;
mapping(address => Receipt) receipts;
}
struct ProposalParameters {
uint256 warmUpDuration;
uint256 activeDuration;
uint256 queueDuration;
uint256 gracePeriodDuration;
uint256 acceptanceThreshold;
uint256 minQuorum;
}
struct Proposal {
// proposal identifiers
// unique id
uint256 id;
// Creator of the proposal
address proposer;
// proposal description
string description;
string title;
// proposal technical details
// ordered list of target addresses to be made
address[] targets;
// The ordered list of values (i.e. msg.value) to be passed to the calls to be made
uint256[] values;
// The ordered list of function signatures to be called
string[] signatures;
// The ordered list of calldata to be passed to each call
bytes[] calldatas;
// proposal creation time - 1
uint256 createTime;
// votes status
// The timestamp that the proposal will be available for execution, set once the vote succeeds
uint256 eta;
// Current number of votes in favor of this proposal
uint256 forVotes;
// Current number of votes in opposition to this proposal
uint256 againstVotes;
bool canceled;
bool executed;
// Receipts of ballots for the entire set of voters
mapping(address => Receipt) receipts;
ProposalParameters parameters;
}
uint256 public lastProposalId;
mapping(uint256 => Proposal) public proposals;
mapping(uint256 => AbrogationProposal) public abrogationProposals;
mapping(address => uint256) public latestProposalIds;
IBarn barn;
bool isInitialized;
bool public isActive;
event ProposalCreated(uint256 indexed proposalId);
event Vote(uint256 indexed proposalId, address indexed user, bool support, uint256 power);
event VoteCanceled(uint256 indexed proposalId, address indexed user);
event ProposalQueued(uint256 indexed proposalId, address caller, uint256 eta);
event ProposalExecuted(uint256 indexed proposalId, address caller);
event ProposalCanceled(uint256 indexed proposalId, address caller);
event AbrogationProposalStarted(uint256 indexed proposalId, address caller);
event AbrogationProposalExecuted(uint256 indexed proposalId, address caller);
event AbrogationProposalVote(uint256 indexed proposalId, address indexed user, bool support, uint256 power);
event AbrogationProposalVoteCancelled(uint256 indexed proposalId, address indexed user);
receive() external payable {}
// executed only once
function initialize(address barnAddr) public {
require(isInitialized == false, "Contract already initialized.");
require(barnAddr != address(0), "barn must not be 0x0");
barn = IBarn(barnAddr);
isInitialized = true;
}
function activate() public {
require(!isActive, "DAO already active");
require(barn.bondStaked() >= ACTIVATION_THRESHOLD, "Threshold not met yet");
isActive = true;
}
function propose(
address[] memory targets,
uint256[] memory values,
string[] memory signatures,
bytes[] memory calldatas,
string memory description,
string memory title
)
public returns (uint256)
{
if (!isActive) {
require(barn.bondStaked() >= ACTIVATION_THRESHOLD, "DAO not yet active");
isActive = true;
}
require(
barn.votingPowerAtTs(msg.sender, block.timestamp - 1) >= _getCreationThreshold(),
"Creation threshold not met"
);
require(
targets.length == values.length && targets.length == signatures.length && targets.length == calldatas.length,
"Proposal function information arity mismatch"
);
require(targets.length != 0, "Must provide actions");
require(targets.length <= PROPOSAL_MAX_ACTIONS, "Too many actions on a vote");
require(bytes(title).length > 0, "title can't be empty");
require(bytes(description).length > 0, "description can't be empty");
// check if user has another running vote
uint256 previousProposalId = latestProposalIds[msg.sender];
if (previousProposalId != 0) {
require(_isLiveState(previousProposalId) == false, "One live proposal per proposer");
}
uint256 newProposalId = lastProposalId + 1;
Proposal storage newProposal = proposals[newProposalId];
newProposal.id = newProposalId;
newProposal.proposer = msg.sender;
newProposal.description = description;
newProposal.title = title;
newProposal.targets = targets;
newProposal.values = values;
newProposal.signatures = signatures;
newProposal.calldatas = calldatas;
newProposal.createTime = block.timestamp - 1;
newProposal.parameters.warmUpDuration = warmUpDuration;
newProposal.parameters.activeDuration = activeDuration;
newProposal.parameters.queueDuration = queueDuration;
newProposal.parameters.gracePeriodDuration = gracePeriodDuration;
newProposal.parameters.acceptanceThreshold = acceptanceThreshold;
newProposal.parameters.minQuorum = minQuorum;
lastProposalId = newProposalId;
latestProposalIds[msg.sender] = newProposalId;
emit ProposalCreated(newProposalId);
return newProposalId;
}
function queue(uint256 proposalId) public {
require(state(proposalId) == ProposalState.Accepted, "Proposal can only be queued if it is succeeded");
Proposal storage proposal = proposals[proposalId];
uint256 eta = proposal.createTime + proposal.parameters.warmUpDuration + proposal.parameters.activeDuration + proposal.parameters.queueDuration;
proposal.eta = eta;
for (uint256 i = 0; i < proposal.targets.length; i++) {
require(
!queuedTransactions[_getTxHash(proposal.targets[i], proposal.values[i], proposal.signatures[i], proposal.calldatas[i], eta)],
"proposal action already queued at eta"
);
queueTransaction(proposal.targets[i], proposal.values[i], proposal.signatures[i], proposal.calldatas[i], eta);
}
emit ProposalQueued(proposalId, msg.sender, eta);
}
function execute(uint256 proposalId) public payable {
require(_canBeExecuted(proposalId), "Cannot be executed");
Proposal storage proposal = proposals[proposalId];
proposal.executed = true;
for (uint256 i = 0; i < proposal.targets.length; i++) {
executeTransaction(proposal.targets[i], proposal.values[i], proposal.signatures[i], proposal.calldatas[i], proposal.eta);
}
emit ProposalExecuted(proposalId, msg.sender);
}
function cancelProposal(uint256 proposalId) public {
require(_isCancellableState(proposalId), "Proposal in state that does not allow cancellation");
require(_canCancelProposal(proposalId), "Cancellation requirements not met");
Proposal storage proposal = proposals[proposalId];
proposal.canceled = true;
for (uint256 i = 0; i < proposal.targets.length; i++) {
cancelTransaction(proposal.targets[i], proposal.values[i], proposal.signatures[i], proposal.calldatas[i], proposal.eta);
}
emit ProposalCanceled(proposalId, msg.sender);
}
function castVote(uint256 proposalId, bool support) public {
require(state(proposalId) == ProposalState.Active, "Voting is closed");
Proposal storage proposal = proposals[proposalId];
Receipt storage receipt = proposal.receipts[msg.sender];
// exit if user already voted
require(receipt.hasVoted == false || receipt.hasVoted && receipt.support != support, "Already voted this option");
uint256 votes = barn.votingPowerAtTs(msg.sender, _getSnapshotTimestamp(proposal));
require(votes > 0, "no voting power");
// means it changed its vote
if (receipt.hasVoted) {
if (receipt.support) {
proposal.forVotes = proposal.forVotes.sub(receipt.votes);
} else {
proposal.againstVotes = proposal.againstVotes.sub(receipt.votes);
}
}
if (support) {
proposal.forVotes = proposal.forVotes.add(votes);
} else {
proposal.againstVotes = proposal.againstVotes.add(votes);
}
receipt.hasVoted = true;
receipt.votes = votes;
receipt.support = support;
emit Vote(proposalId, msg.sender, support, votes);
}
function cancelVote(uint256 proposalId) public {
require(state(proposalId) == ProposalState.Active, "Voting is closed");
Proposal storage proposal = proposals[proposalId];
Receipt storage receipt = proposal.receipts[msg.sender];
uint256 votes = barn.votingPowerAtTs(msg.sender, _getSnapshotTimestamp(proposal));
require(receipt.hasVoted, "Cannot cancel if not voted yet");
if (receipt.support) {
proposal.forVotes = proposal.forVotes.sub(votes);
} else {
proposal.againstVotes = proposal.againstVotes.sub(votes);
}
receipt.hasVoted = false;
receipt.votes = 0;
receipt.support = false;
emit VoteCanceled(proposalId, msg.sender);
}
// ======================================================================================================
// Abrogation proposal methods
// ======================================================================================================
// the Abrogation Proposal is a mechanism for the DAO participants to veto the execution of a proposal that was already
// accepted and it is currently queued. For the Abrogation Proposal to pass, 50% + 1 of the vBOND holders
// must vote FOR the Abrogation Proposal
function startAbrogationProposal(uint256 proposalId, string memory description) public {
require(state(proposalId) == ProposalState.Queued, "Proposal must be in queue");
require(
barn.votingPowerAtTs(msg.sender, block.timestamp - 1) >= _getCreationThreshold(),
"Creation threshold not met"
);
AbrogationProposal storage ap = abrogationProposals[proposalId];
require(ap.createTime == 0, "Abrogation proposal already exists");
require(bytes(description).length > 0, "description can't be empty");
ap.createTime = block.timestamp;
ap.creator = msg.sender;
ap.description = description;
emit AbrogationProposalStarted(proposalId, msg.sender);
}
// abrogateProposal cancels a proposal if there's an Abrogation Proposal that passed
function abrogateProposal(uint256 proposalId) public {
require(state(proposalId) == ProposalState.Abrogated, "Cannot be abrogated");
Proposal storage proposal = proposals[proposalId];
require(proposal.canceled == false, "Cannot be abrogated");
proposal.canceled = true;
for (uint256 i = 0; i < proposal.targets.length; i++) {
cancelTransaction(proposal.targets[i], proposal.values[i], proposal.signatures[i], proposal.calldatas[i], proposal.eta);
}
emit AbrogationProposalExecuted(proposalId, msg.sender);
}
function abrogationProposal_castVote(uint256 proposalId, bool support) public {
require(0 < proposalId && proposalId <= lastProposalId, "invalid proposal id");
AbrogationProposal storage abrogationProposal = abrogationProposals[proposalId];
require(
state(proposalId) == ProposalState.Queued && abrogationProposal.createTime != 0,
"Abrogation Proposal not active"
);
Receipt storage receipt = abrogationProposal.receipts[msg.sender];
require(
receipt.hasVoted == false || receipt.hasVoted && receipt.support != support,
"Already voted this option"
);
uint256 votes = barn.votingPowerAtTs(msg.sender, abrogationProposal.createTime - 1);
require(votes > 0, "no voting power");
// means it changed its vote
if (receipt.hasVoted) {
if (receipt.support) {
abrogationProposal.forVotes = abrogationProposal.forVotes.sub(receipt.votes);
} else {
abrogationProposal.againstVotes = abrogationProposal.againstVotes.sub(receipt.votes);
}
}
if (support) {
abrogationProposal.forVotes = abrogationProposal.forVotes.add(votes);
} else {
abrogationProposal.againstVotes = abrogationProposal.againstVotes.add(votes);
}
receipt.hasVoted = true;
receipt.votes = votes;
receipt.support = support;
emit AbrogationProposalVote(proposalId, msg.sender, support, votes);
}
function abrogationProposal_cancelVote(uint256 proposalId) public {
require(0 < proposalId && proposalId <= lastProposalId, "invalid proposal id");
AbrogationProposal storage abrogationProposal = abrogationProposals[proposalId];
Receipt storage receipt = abrogationProposal.receipts[msg.sender];
require(
state(proposalId) == ProposalState.Queued && abrogationProposal.createTime != 0,
"Abrogation Proposal not active"
);
uint256 votes = barn.votingPowerAtTs(msg.sender, abrogationProposal.createTime - 1);
require(receipt.hasVoted, "Cannot cancel if not voted yet");
if (receipt.support) {
abrogationProposal.forVotes = abrogationProposal.forVotes.sub(votes);
} else {
abrogationProposal.againstVotes = abrogationProposal.againstVotes.sub(votes);
}
receipt.hasVoted = false;
receipt.votes = 0;
receipt.support = false;
emit AbrogationProposalVoteCancelled(proposalId, msg.sender);
}
// ======================================================================================================
// views
// ======================================================================================================
function state(uint256 proposalId) public view returns (ProposalState) {
require(0 < proposalId && proposalId <= lastProposalId, "invalid proposal id");
Proposal storage proposal = proposals[proposalId];
if (proposal.canceled) {
return ProposalState.Canceled;
}
if (proposal.executed) {
return ProposalState.Executed;
}
if (block.timestamp <= proposal.createTime + proposal.parameters.warmUpDuration) {
return ProposalState.WarmUp;
}
if (block.timestamp <= proposal.createTime + proposal.parameters.warmUpDuration + proposal.parameters.activeDuration) {
return ProposalState.Active;
}
if ((proposal.forVotes + proposal.againstVotes) < _getQuorum(proposal) ||
(proposal.forVotes < _getMinForVotes(proposal))) {
return ProposalState.Failed;
}
if (proposal.eta == 0) {
return ProposalState.Accepted;
}
if (block.timestamp < proposal.eta) {
return ProposalState.Queued;
}
if (_proposalAbrogated(proposalId)) {
return ProposalState.Abrogated;
}
if (block.timestamp <= proposal.eta + proposal.parameters.gracePeriodDuration) {
return ProposalState.Grace;
}
return ProposalState.Expired;
}
function getReceipt(uint256 proposalId, address voter) public view returns (Receipt memory) {
return proposals[proposalId].receipts[voter];
}
function getProposalParameters(uint256 proposalId) public view returns (ProposalParameters memory) {
return proposals[proposalId].parameters;
}
function getAbrogationProposalReceipt(uint256 proposalId, address voter) public view returns (Receipt memory) {
return abrogationProposals[proposalId].receipts[voter];
}
function getActions(uint256 proposalId) public view returns (
address[] memory targets,
uint256[] memory values,
string[] memory signatures,
bytes[] memory calldatas
) {
Proposal storage p = proposals[proposalId];
return (p.targets, p.values, p.signatures, p.calldatas);
}
function getProposalQuorum(uint256 proposalId) public view returns (uint256) {
require(0 < proposalId && proposalId <= lastProposalId, "invalid proposal id");
return _getQuorum(proposals[proposalId]);
}
// ======================================================================================================
// internal methods
// ======================================================================================================
function _canCancelProposal(uint256 proposalId) internal view returns (bool){
Proposal storage proposal = proposals[proposalId];
if (msg.sender == proposal.proposer ||
barn.votingPower(proposal.proposer) < _getCreationThreshold()
) {
return true;
}
return false;
}
function _isCancellableState(uint256 proposalId) internal view returns (bool) {
ProposalState s = state(proposalId);
return s == ProposalState.WarmUp || s == ProposalState.Active;
}
function _isLiveState(uint256 proposalId) internal view returns (bool) {
ProposalState s = state(proposalId);
return s == ProposalState.WarmUp ||
s == ProposalState.Active ||
s == ProposalState.Accepted ||
s == ProposalState.Queued ||
s == ProposalState.Grace;
}
function _canBeExecuted(uint256 proposalId) internal view returns (bool) {
return state(proposalId) == ProposalState.Grace;
}
function _getMinForVotes(Proposal storage proposal) internal view returns (uint256) {
return (proposal.forVotes + proposal.againstVotes).mul(proposal.parameters.acceptanceThreshold).div(100);
}
function _getCreationThreshold() internal view returns (uint256) {
return barn.bondStaked().div(100);
}
// Returns the timestamp of the snapshot for a given proposal
// If the current block's timestamp is equal to `proposal.createTime + warmUpDuration` then the state function
// will return WarmUp as state which will prevent any vote to be cast which will gracefully avoid any flashloan attack
function _getSnapshotTimestamp(Proposal storage proposal) internal view returns (uint256) {
return proposal.createTime + proposal.parameters.warmUpDuration;
}
function _getQuorum(Proposal storage proposal) internal view returns (uint256) {
return barn.bondStakedAtTs(_getSnapshotTimestamp(proposal)).mul(proposal.parameters.minQuorum).div(100);
}
function _proposalAbrogated(uint256 proposalId) internal view returns (bool) {
Proposal storage p = proposals[proposalId];
AbrogationProposal storage cp = abrogationProposals[proposalId];
if (cp.createTime == 0 || block.timestamp < p.eta) {
return false;
}
return cp.forVotes >= barn.bondStakedAtTs(cp.createTime - 1).div(2);
}
}