-
Notifications
You must be signed in to change notification settings - Fork 7
/
GovernorAlpha.sol
559 lines (485 loc) · 18.3 KB
/
GovernorAlpha.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
548
549
550
551
552
553
554
555
556
557
558
559
pragma solidity 0.5.16;
pragma experimental ABIEncoderV2;
contract GovernorAlpha {
/// @notice The name of this contract
string public constant name = "TDrop Governor Alpha";
/// @notice The number of votes in support of a proposal required in order for a quorum to be reached and for a vote to succeed
function quorumVotes() public view returns (uint256) {
return quorumThresholdInWei;
}
/// @notice The number of votes required in order for a voter to become a proposer
function proposalThreshold() public view returns (uint256) {
return proposalThresholdInWei;
}
/// @notice The maximum number of actions that can be included in a proposal
function proposalMaxOperations() public pure returns (uint256) {
return 10;
} // 10 actions
/// @notice The super admin address
address public superAdmin;
/// @notice The admin address
address public admin;
/// @notice The The number of votes in support of a proposal required in order for a quorum to be reached and for a vote to succeed
uint256 quorumThresholdInWei = 1_000_000_000e18;
/// @notice The number of votes required in order for a voter to become a proposer
uint256 proposalThresholdInWei = 5_000_000e18;
/// @notice The delay before voting on a proposal may take place, once proposed
uint256 public votingDelay = 1; // 1 block
/// @notice The duration of voting on a proposal, in blocks
uint256 public votingPeriod = 100_800; // ~7 days in blocks (assuming 6s blocks)
/// @notice The address of the TDrop Protocol Timelock
TimelockInterface public timelock;
/// @notice The address of the Tdrop governance token
TDropStakingInterface public tdropStaking;
/// @notice The total number of proposals
uint256 public proposalCount;
struct Proposal {
/// @notice Unique id for looking up a proposal
uint256 id;
/// @notice Creator of the proposal
address proposer;
/// @notice The timestamp that the proposal will be available for execution, set once the vote succeeds
uint256 eta;
/// @notice the ordered list of target addresses for calls to be made
address[] targets;
/// @notice The ordered list of values (i.e. msg.value) to be passed to the calls to be made
uint256[] values;
/// @notice The ordered list of function signatures to be called
string[] signatures;
/// @notice The ordered list of calldata to be passed to each call
bytes[] calldatas;
/// @notice The block at which voting begins: holders must delegate their votes prior to this block
uint256 startBlock;
/// @notice The block at which voting ends: votes must be cast prior to this block
uint256 endBlock;
/// @notice Current number of votes in favor of this proposal
uint256 forVotes;
/// @notice Current number of votes in opposition to this proposal
uint256 againstVotes;
/// @notice Flag marking whether the proposal has been canceled
bool canceled;
/// @notice Flag marking whether the proposal has been executed
bool executed;
/// @notice Receipts of ballots for the entire set of voters
mapping(address => Receipt) receipts;
}
/// @notice Ballot receipt record for a voter
struct Receipt {
/// @notice Whether or not a vote has been cast
bool hasVoted;
/// @notice Whether or not the voter supports the proposal
bool support;
/// @notice The number of votes the voter had, which were cast
uint96 votes;
}
/// @notice Possible states that a proposal may be in
enum ProposalState {
Pending,
Active,
Canceled,
Defeated,
Succeeded,
Queued,
Expired,
Executed
}
/// @notice The official record of all proposals ever proposed
mapping(uint256 => Proposal) public proposals;
/// @notice The latest proposal for each proposer
mapping(address => uint256) public latestProposalIds;
/// @notice The EIP-712 typehash for the contract's domain
bytes32 public constant DOMAIN_TYPEHASH =
keccak256(
"EIP712Domain(string name,uint256 chainId,address verifyingContract)"
);
/// @notice The EIP-712 typehash for the ballot struct used by the contract
bytes32 public constant BALLOT_TYPEHASH =
keccak256("Ballot(uint256 proposalId,bool support)");
/// @notice An event emitted when a new proposal is created
event ProposalCreated(
uint256 id,
address proposer,
address[] targets,
uint256[] values,
string[] signatures,
bytes[] calldatas,
uint256 startBlock,
uint256 endBlock,
string description
);
/// @notice An event emitted when a vote has been cast on a proposal
event VoteCast(
address voter,
uint256 proposalId,
bool support,
uint256 votes
);
/// @notice An event emitted when a proposal has been canceled
event ProposalCanceled(uint256 id);
/// @notice An event emitted when a proposal has been queued in the Timelock
event ProposalQueued(uint256 id, uint256 eta);
/// @notice An event emitted when a proposal has been executed in the Timelock
event ProposalExecuted(uint256 id);
/// @notice An event thats emitted when the super admin address is changed
event SuperAdminChanged(address superAdmin, address newSuperAdmin);
/// @notice An event thats emitted when the admin address is changed
event AdminChanged(address admin, address newAdmin);
event QuorumThresholdChanged(uint256 quorumThresholdInWei, uint256 newQuorumThresholdInWei);
event ProposalThresholdChanged(uint256 proposalThresholdInWei, uint256 newProposalThresholdInWei);
event VotingDelayChanged(uint256 votingDelay, uint256 newVotingDelay);
event VotingPeriodChanged(uint256 votingPeriod, uint256 newVotingPeriod);
constructor(address superAdmin_, address admin_, address timelock_, address tdropStaking_) public {
superAdmin = superAdmin_;
emit SuperAdminChanged(address(0), superAdmin);
admin = admin_;
emit AdminChanged(address(0), admin);
timelock = TimelockInterface(timelock_);
tdropStaking = TDropStakingInterface(tdropStaking_);
}
/**
* @notice Change the admin address
* @param superAdmin_ The address of the new super admin
*/
function setSuperAdmin(address superAdmin_) onlySuperAdmin external {
emit SuperAdminChanged(superAdmin, superAdmin_);
superAdmin = superAdmin_;
}
/**
* @notice Change the admin address
* @param admin_ The address of the new admin
*/
function setAdmin(address admin_) onlySuperAdmin external {
emit AdminChanged(admin, admin_);
admin = admin_;
}
function setQuorumThreshold(uint256 quorumThresholdInWei_) onlyAdmin external {
emit QuorumThresholdChanged(quorumThresholdInWei, quorumThresholdInWei_);
quorumThresholdInWei = quorumThresholdInWei_;
}
function setProposalThreshold(uint256 proposalThreshold_) onlyAdmin external {
emit ProposalThresholdChanged(proposalThresholdInWei, proposalThreshold_);
proposalThresholdInWei = proposalThreshold_;
}
function setVotingDelay(uint256 votingDelay_) onlyAdmin external {
emit VotingDelayChanged(votingDelay, votingDelay_);
votingDelay = votingDelay_;
}
function setVotingPeriod(uint256 votingPeriod_) onlyAdmin external {
emit VotingPeriodChanged(votingPeriod, votingPeriod_);
votingPeriod = votingPeriod_;
}
function propose(
address[] memory targets,
uint256[] memory values,
string[] memory signatures,
bytes[] memory calldatas,
string memory description
) public returns (uint256) {
require(
tdropStaking.getPriorVotes(msg.sender, sub256(block.number, 1)) >
proposalThreshold(),
"GovernorAlpha::propose: proposer votes below proposal threshold"
);
require(
targets.length == values.length &&
targets.length == signatures.length &&
targets.length == calldatas.length,
"GovernorAlpha::propose: proposal function information arity mismatch"
);
require(
targets.length != 0,
"GovernorAlpha::propose: must provide actions"
);
require(
targets.length <= proposalMaxOperations(),
"GovernorAlpha::propose: too many actions"
);
uint256 latestProposalId = latestProposalIds[msg.sender];
if (latestProposalId != 0) {
ProposalState proposersLatestProposalState = state(
latestProposalId
);
require(
proposersLatestProposalState != ProposalState.Active,
"GovernorAlpha::propose: one live proposal per proposer, found an already active proposal"
);
require(
proposersLatestProposalState != ProposalState.Pending,
"GovernorAlpha::propose: one live proposal per proposer, found an already pending proposal"
);
}
uint256 startBlock = add256(block.number, votingDelay);
uint256 endBlock = add256(startBlock, votingPeriod);
proposalCount++;
Proposal memory newProposal = Proposal({
id: proposalCount,
proposer: msg.sender,
eta: 0,
targets: targets,
values: values,
signatures: signatures,
calldatas: calldatas,
startBlock: startBlock,
endBlock: endBlock,
forVotes: 0,
againstVotes: 0,
canceled: false,
executed: false
});
proposals[newProposal.id] = newProposal;
latestProposalIds[newProposal.proposer] = newProposal.id;
emit ProposalCreated(
newProposal.id,
msg.sender,
targets,
values,
signatures,
calldatas,
startBlock,
endBlock,
description
);
return newProposal.id;
}
function queue(uint256 proposalId) public {
require(
state(proposalId) == ProposalState.Succeeded,
"GovernorAlpha::queue: proposal can only be queued if it is succeeded"
);
Proposal storage proposal = proposals[proposalId];
uint256 eta = add256(block.timestamp, timelock.delay());
for (uint256 i = 0; i < proposal.targets.length; i++) {
_queueOrRevert(
proposal.targets[i],
proposal.values[i],
proposal.signatures[i],
proposal.calldatas[i],
eta
);
}
proposal.eta = eta;
emit ProposalQueued(proposalId, eta);
}
function _queueOrRevert(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 eta
) internal {
require(
!timelock.queuedTransactions(
keccak256(abi.encode(target, value, signature, data, eta))
),
"GovernorAlpha::_queueOrRevert: proposal action already queued at eta"
);
timelock.queueTransaction(target, value, signature, data, eta);
}
function execute(uint256 proposalId) public payable {
require(
state(proposalId) == ProposalState.Queued,
"GovernorAlpha::execute: proposal can only be executed if it is queued"
);
Proposal storage proposal = proposals[proposalId];
proposal.executed = true;
for (uint256 i = 0; i < proposal.targets.length; i++) {
timelock.executeTransaction.value(proposal.values[i])(
proposal.targets[i],
proposal.values[i],
proposal.signatures[i],
proposal.calldatas[i],
proposal.eta
);
}
emit ProposalExecuted(proposalId);
}
function cancel(uint256 proposalId) public {
ProposalState state = state(proposalId);
require(
state != ProposalState.Executed,
"GovernorAlpha::cancel: cannot cancel executed proposal"
);
Proposal storage proposal = proposals[proposalId];
require(
tdropStaking.getPriorVotes(
proposal.proposer,
sub256(block.number, 1)
) < proposalThreshold(),
"GovernorAlpha::cancel: proposer above threshold"
);
proposal.canceled = true;
for (uint256 i = 0; i < proposal.targets.length; i++) {
timelock.cancelTransaction(
proposal.targets[i],
proposal.values[i],
proposal.signatures[i],
proposal.calldatas[i],
proposal.eta
);
}
emit ProposalCanceled(proposalId);
}
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 getReceipt(uint256 proposalId, address voter)
public
view
returns (Receipt memory)
{
return proposals[proposalId].receipts[voter];
}
function state(uint256 proposalId) public view returns (ProposalState) {
require(
proposalCount >= proposalId && proposalId > 0,
"GovernorAlpha::state: invalid proposal id"
);
Proposal storage proposal = proposals[proposalId];
if (proposal.canceled) {
return ProposalState.Canceled;
} else if (block.number <= proposal.startBlock) {
return ProposalState.Pending;
} else if (block.number <= proposal.endBlock) {
return ProposalState.Active;
} else if (
proposal.forVotes <= proposal.againstVotes ||
proposal.forVotes < quorumVotes()
) {
return ProposalState.Defeated;
} else if (proposal.eta == 0) {
return ProposalState.Succeeded;
} else if (proposal.executed) {
return ProposalState.Executed;
} else if (
block.timestamp >= add256(proposal.eta, timelock.GRACE_PERIOD())
) {
return ProposalState.Expired;
} else {
return ProposalState.Queued;
}
}
function castVote(uint256 proposalId, bool support) public {
return _castVote(msg.sender, proposalId, support);
}
function castVoteBySig(
uint256 proposalId,
bool support,
uint8 v,
bytes32 r,
bytes32 s
) public {
bytes32 domainSeparator = keccak256(
abi.encode(
DOMAIN_TYPEHASH,
keccak256(bytes(name)),
getChainId(),
address(this)
)
);
bytes32 structHash = keccak256(
abi.encode(BALLOT_TYPEHASH, proposalId, support)
);
bytes32 digest = keccak256(
abi.encodePacked("\x19\x01", domainSeparator, structHash)
);
address signatory = ecrecover(digest, v, r, s);
require(
signatory != address(0),
"GovernorAlpha::castVoteBySig: invalid signature"
);
return _castVote(signatory, proposalId, support);
}
function _castVote(
address voter,
uint256 proposalId,
bool support
) internal {
require(
state(proposalId) == ProposalState.Active,
"GovernorAlpha::_castVote: voting is closed"
);
Proposal storage proposal = proposals[proposalId];
Receipt storage receipt = proposal.receipts[voter];
require(
receipt.hasVoted == false,
"GovernorAlpha::_castVote: voter already voted"
);
uint96 votes = tdropStaking.getPriorVotes(voter, proposal.startBlock);
if (support) {
proposal.forVotes = add256(proposal.forVotes, votes);
} else {
proposal.againstVotes = add256(proposal.againstVotes, votes);
}
receipt.hasVoted = true;
receipt.support = support;
receipt.votes = votes;
emit VoteCast(voter, proposalId, support, votes);
}
function add256(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "addition overflow");
return c;
}
function sub256(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "subtraction underflow");
return a - b;
}
function getChainId() internal pure returns (uint256) {
uint256 chainId;
assembly {
chainId := chainid()
}
return chainId;
}
modifier onlySuperAdmin {
require(msg.sender == superAdmin, "TDropStaking::onlySuperAdmin: only the super admin can perform this action");
_;
}
modifier onlyAdmin {
require(msg.sender == admin, "TDropStaking::onlyAdmin: only the admin can perform this action");
_;
}
}
interface TimelockInterface {
function delay() external view returns (uint256);
function GRACE_PERIOD() external view returns (uint256);
function acceptAdmin() external;
function queuedTransactions(bytes32 hash) external view returns (bool);
function queueTransaction(
address target,
uint256 value,
string calldata signature,
bytes calldata data,
uint256 eta
) external returns (bytes32);
function cancelTransaction(
address target,
uint256 value,
string calldata signature,
bytes calldata data,
uint256 eta
) external;
function executeTransaction(
address target,
uint256 value,
string calldata signature,
bytes calldata data,
uint256 eta
) external payable returns (bytes memory);
}
interface TDropStakingInterface {
function getPriorVotes(address account, uint256 blockNumber)
external
view
returns (uint96);
}