-
Notifications
You must be signed in to change notification settings - Fork 0
/
NounsDAOLogicV2.sol
1091 lines (957 loc) · 42.7 KB
/
NounsDAOLogicV2.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
/// @title The Nouns DAO logic version 2
/*********************************
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░█████████░░█████████░░░ *
* ░░░░░░██░░░████░░██░░░████░░░ *
* ░░██████░░░████████░░░████░░░ *
* ░░██░░██░░░████░░██░░░████░░░ *
* ░░██░░██░░░████░░██░░░████░░░ *
* ░░░░░░█████████░░█████████░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
*********************************/
// LICENSE
// NounsDAOLogicV2.sol is a modified version of Compound Lab's GovernorBravoDelegate.sol:
// https://github.com/compound-finance/compound-protocol/blob/b9b14038612d846b83f8a009a82c38974ff2dcfe/contracts/Governance/GovernorBravoDelegate.sol
//
// GovernorBravoDelegate.sol source code Copyright 2020 Compound Labs, Inc. licensed under the BSD-3-Clause license.
// With modifications by Nounders DAO.
//
// Additional conditions of BSD-3-Clause can be found here: https://opensource.org/licenses/BSD-3-Clause
//
// MODIFICATIONS
// See NounsDAOLogicV1 for initial GovernorBravoDelegate modifications.
// NounsDAOLogicV2 adds:
// - `quorumParamsCheckpoints`, which store dynamic quorum parameters checkpoints
// to be used when calculating the dynamic quorum.
// - `_setDynamicQuorumParams(DynamicQuorumParams memory params)`, which allows the
// DAO to update the dynamic quorum parameters' values.
// - `getDynamicQuorumParamsAt(uint256 blockNumber_)`
// - Individual setters of the DynamicQuorumParams members:
// - `_setMinQuorumVotesBPS(uint16 newMinQuorumVotesBPS)`
// - `_setMaxQuorumVotesBPS(uint16 newMaxQuorumVotesBPS)`
// - `_setQuorumCoefficient(uint32 newQuorumCoefficient)`
// - `minQuorumVotes` and `maxQuorumVotes`, which returns the current min and
// max quorum votes using the current Noun supply.
// - New `Proposal` struct member:
// - `totalSupply` used in dynamic quorum calculation.
// - `creationBlock` used for retrieving checkpoints of votes and dynamic quorum params. This now
// allows changing `votingDelay` without affecting the checkpoints lookup.
// - `quorumVotes(uint256 proposalId)`, which calculates and returns the dynamic
// quorum for a specific proposal.
// - `proposals(uint256 proposalId)` instead of the implicit getter, to avoid stack-too-deep error
//
// NounsDAOLogicV2 removes:
// - `quorumVotes()` has been replaced by `quorumVotes(uint256 proposalId)`.
pragma solidity ^0.8.6;
import './NounsDAOInterfaces.sol';
contract NounsDAOLogicV2 is NounsDAOStorageV2, NounsDAOEventsV2 {
/// @notice The name of this contract
string public constant name = 'Nouns DAO';
/// @notice The minimum setable proposal threshold
uint256 public constant MIN_PROPOSAL_THRESHOLD_BPS = 1; // 1 basis point or 0.01%
/// @notice The maximum setable proposal threshold
uint256 public constant MAX_PROPOSAL_THRESHOLD_BPS = 1_000; // 1,000 basis points or 10%
/// @notice The minimum setable voting period
uint256 public constant MIN_VOTING_PERIOD = 5_760; // About 24 hours
/// @notice The max setable voting period
uint256 public constant MAX_VOTING_PERIOD = 80_640; // About 2 weeks
/// @notice The min setable voting delay
uint256 public constant MIN_VOTING_DELAY = 1;
/// @notice The max setable voting delay
uint256 public constant MAX_VOTING_DELAY = 40_320; // About 1 week
/// @notice The lower bound of minimum quorum votes basis points
uint256 public constant MIN_QUORUM_VOTES_BPS_LOWER_BOUND = 200; // 200 basis points or 2%
/// @notice The upper bound of minimum quorum votes basis points
uint256 public constant MIN_QUORUM_VOTES_BPS_UPPER_BOUND = 2_000; // 2,000 basis points or 20%
/// @notice The upper bound of maximum quorum votes basis points
uint256 public constant MAX_QUORUM_VOTES_BPS_UPPER_BOUND = 6_000; // 4,000 basis points or 60%
/// @notice The maximum setable quorum votes basis points
uint256 public constant MAX_QUORUM_VOTES_BPS = 2_000; // 2,000 basis points or 20%
/// @notice The maximum number of actions that can be included in a proposal
uint256 public constant proposalMaxOperations = 10; // 10 actions
/// @notice The maximum priority fee used to cap gas refunds in `castRefundableVote`
uint256 public constant MAX_REFUND_PRIORITY_FEE = 2 gwei;
/// @notice The vote refund gas overhead, including 7K for ETH transfer and 29K for general transaction overhead
uint256 public constant REFUND_BASE_GAS = 36000;
/// @notice The maximum gas units the DAO will refund voters on; supports about 9,190 characters
uint256 public constant MAX_REFUND_GAS_USED = 200_000;
/// @notice The maximum basefee the DAO will refund voters on
uint256 public constant MAX_REFUND_BASE_FEE = 200 gwei;
/// @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,uint8 support)');
/// @dev Introduced these errors to reduce contract size, to avoid deployment failure
error AdminOnly();
error InvalidMinQuorumVotesBPS();
error InvalidMaxQuorumVotesBPS();
error MinQuorumBPSGreaterThanMaxQuorumBPS();
error UnsafeUint16Cast();
error VetoerOnly();
error PendingVetoerOnly();
error VetoerBurned();
error CantVetoExecutedProposal();
error CantCancelExecutedProposal();
/**
* @notice Used to initialize the contract during delegator contructor
* @param timelock_ The address of the NounsDAOExecutor
* @param nouns_ The address of the NOUN tokens
* @param vetoer_ The address allowed to unilaterally veto proposals
* @param votingPeriod_ The initial voting period
* @param votingDelay_ The initial voting delay
* @param proposalThresholdBPS_ The initial proposal threshold in basis points
* @param dynamicQuorumParams_ The initial dynamic quorum parameters
*/
function initialize(
address timelock_,
address nouns_,
address vetoer_,
uint256 votingPeriod_,
uint256 votingDelay_,
uint256 proposalThresholdBPS_,
DynamicQuorumParams calldata dynamicQuorumParams_
) public virtual {
require(address(timelock) == address(0), 'NounsDAO::initialize: can only initialize once');
if (msg.sender != admin) {
revert AdminOnly();
}
require(timelock_ != address(0), 'NounsDAO::initialize: invalid timelock address');
require(nouns_ != address(0), 'NounsDAO::initialize: invalid nouns address');
require(
votingPeriod_ >= MIN_VOTING_PERIOD && votingPeriod_ <= MAX_VOTING_PERIOD,
'NounsDAO::initialize: invalid voting period'
);
require(
votingDelay_ >= MIN_VOTING_DELAY && votingDelay_ <= MAX_VOTING_DELAY,
'NounsDAO::initialize: invalid voting delay'
);
require(
proposalThresholdBPS_ >= MIN_PROPOSAL_THRESHOLD_BPS && proposalThresholdBPS_ <= MAX_PROPOSAL_THRESHOLD_BPS,
'NounsDAO::initialize: invalid proposal threshold bps'
);
emit VotingPeriodSet(votingPeriod, votingPeriod_);
emit VotingDelaySet(votingDelay, votingDelay_);
emit ProposalThresholdBPSSet(proposalThresholdBPS, proposalThresholdBPS_);
timelock = INounsDAOExecutor(timelock_);
nouns = NounsTokenLike(nouns_);
vetoer = vetoer_;
votingPeriod = votingPeriod_;
votingDelay = votingDelay_;
proposalThresholdBPS = proposalThresholdBPS_;
_setDynamicQuorumParams(
dynamicQuorumParams_.minQuorumVotesBPS,
dynamicQuorumParams_.maxQuorumVotesBPS,
dynamicQuorumParams_.quorumCoefficient
);
}
struct ProposalTemp {
uint256 totalSupply;
uint256 proposalThreshold;
uint256 latestProposalId;
uint256 startBlock;
uint256 endBlock;
}
/**
* @notice Function used to propose a new proposal. Sender must have delegates above the proposal threshold
* @param targets Target addresses for proposal calls
* @param values Eth values for proposal calls
* @param signatures Function signatures for proposal calls
* @param calldatas Calldatas for proposal calls
* @param description String description of the proposal
* @return Proposal id of new proposal
*/
function propose(
address[] memory targets,
uint256[] memory values,
string[] memory signatures,
bytes[] memory calldatas,
string memory description
) public returns (uint256) {
ProposalTemp memory temp;
temp.totalSupply = nouns.totalSupply();
temp.proposalThreshold = bps2Uint(proposalThresholdBPS, temp.totalSupply);
require(
nouns.getPriorVotes(msg.sender, block.number - 1) > temp.proposalThreshold,
'NounsDAO::propose: proposer votes below proposal threshold'
);
require(
targets.length == values.length &&
targets.length == signatures.length &&
targets.length == calldatas.length,
'NounsDAO::propose: proposal function information arity mismatch'
);
require(targets.length != 0, 'NounsDAO::propose: must provide actions');
require(targets.length <= proposalMaxOperations, 'NounsDAO::propose: too many actions');
temp.latestProposalId = latestProposalIds[msg.sender];
if (temp.latestProposalId != 0) {
ProposalState proposersLatestProposalState = state(temp.latestProposalId);
require(
proposersLatestProposalState != ProposalState.Active,
'NounsDAO::propose: one live proposal per proposer, found an already active proposal'
);
require(
proposersLatestProposalState != ProposalState.Pending,
'NounsDAO::propose: one live proposal per proposer, found an already pending proposal'
);
}
temp.startBlock = block.number + votingDelay;
temp.endBlock = temp.startBlock + votingPeriod;
proposalCount++;
Proposal storage newProposal = _proposals[proposalCount];
newProposal.id = proposalCount;
newProposal.proposer = msg.sender;
newProposal.proposalThreshold = temp.proposalThreshold;
newProposal.eta = 0;
newProposal.targets = targets;
newProposal.values = values;
newProposal.signatures = signatures;
newProposal.calldatas = calldatas;
newProposal.startBlock = temp.startBlock;
newProposal.endBlock = temp.endBlock;
newProposal.forVotes = 0;
newProposal.againstVotes = 0;
newProposal.abstainVotes = 0;
newProposal.canceled = false;
newProposal.executed = false;
newProposal.vetoed = false;
newProposal.totalSupply = temp.totalSupply;
newProposal.creationBlock = block.number;
latestProposalIds[newProposal.proposer] = newProposal.id;
/// @notice Maintains backwards compatibility with GovernorBravo events
emit ProposalCreated(
newProposal.id,
msg.sender,
targets,
values,
signatures,
calldatas,
newProposal.startBlock,
newProposal.endBlock,
description
);
/// @notice Updated event with `proposalThreshold` and `minQuorumVotes`
/// @notice `minQuorumVotes` is always zero since V2 introduces dynamic quorum with checkpoints
emit ProposalCreatedWithRequirements(
newProposal.id,
msg.sender,
targets,
values,
signatures,
calldatas,
newProposal.startBlock,
newProposal.endBlock,
newProposal.proposalThreshold,
minQuorumVotes(),
description
);
return newProposal.id;
}
/**
* @notice Queues a proposal of state succeeded
* @param proposalId The id of the proposal to queue
*/
function queue(uint256 proposalId) external {
require(
state(proposalId) == ProposalState.Succeeded,
'NounsDAO::queue: proposal can only be queued if it is succeeded'
);
Proposal storage proposal = _proposals[proposalId];
uint256 eta = block.timestamp + timelock.delay();
for (uint256 i = 0; i < proposal.targets.length; i++) {
queueOrRevertInternal(
proposal.targets[i],
proposal.values[i],
proposal.signatures[i],
proposal.calldatas[i],
eta
);
}
proposal.eta = eta;
emit ProposalQueued(proposalId, eta);
}
function queueOrRevertInternal(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 eta
) internal {
require(
!timelock.queuedTransactions(keccak256(abi.encode(target, value, signature, data, eta))),
'NounsDAO::queueOrRevertInternal: identical proposal action already queued at eta'
);
timelock.queueTransaction(target, value, signature, data, eta);
}
/**
* @notice Executes a queued proposal if eta has passed
* @param proposalId The id of the proposal to execute
*/
function execute(uint256 proposalId) external {
require(
state(proposalId) == ProposalState.Queued,
'NounsDAO::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(
proposal.targets[i],
proposal.values[i],
proposal.signatures[i],
proposal.calldatas[i],
proposal.eta
);
}
emit ProposalExecuted(proposalId);
}
/**
* @notice Cancels a proposal only if sender is the proposer, or proposer delegates dropped below proposal threshold
* @param proposalId The id of the proposal to cancel
*/
function cancel(uint256 proposalId) external {
if (state(proposalId) == ProposalState.Executed) {
revert CantCancelExecutedProposal();
}
Proposal storage proposal = _proposals[proposalId];
require(
msg.sender == proposal.proposer ||
nouns.getPriorVotes(proposal.proposer, block.number - 1) <= proposal.proposalThreshold,
'NounsDAO::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);
}
/**
* @notice Vetoes a proposal only if sender is the vetoer and the proposal has not been executed.
* @param proposalId The id of the proposal to veto
*/
function veto(uint256 proposalId) external {
if (vetoer == address(0)) {
revert VetoerBurned();
}
if (msg.sender != vetoer) {
revert VetoerOnly();
}
if (state(proposalId) == ProposalState.Executed) {
revert CantVetoExecutedProposal();
}
Proposal storage proposal = _proposals[proposalId];
proposal.vetoed = 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 ProposalVetoed(proposalId);
}
/**
* @notice Gets actions of a proposal
* @param proposalId the id of the proposal
* @return targets
* @return values
* @return signatures
* @return calldatas
*/
function getActions(uint256 proposalId)
external
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);
}
/**
* @notice Gets the receipt for a voter on a given proposal
* @param proposalId the id of proposal
* @param voter The address of the voter
* @return The voting receipt
*/
function getReceipt(uint256 proposalId, address voter) external view returns (Receipt memory) {
return _proposals[proposalId].receipts[voter];
}
/**
* @notice Gets the state of a proposal
* @param proposalId The id of the proposal
* @return Proposal state
*/
function state(uint256 proposalId) public view returns (ProposalState) {
require(proposalCount >= proposalId, 'NounsDAO::state: invalid proposal id');
Proposal storage proposal = _proposals[proposalId];
if (proposal.vetoed) {
return ProposalState.Vetoed;
} else 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(proposal.id)) {
return ProposalState.Defeated;
} else if (proposal.eta == 0) {
return ProposalState.Succeeded;
} else if (proposal.executed) {
return ProposalState.Executed;
} else if (block.timestamp >= proposal.eta + timelock.GRACE_PERIOD()) {
return ProposalState.Expired;
} else {
return ProposalState.Queued;
}
}
/**
* @notice Returns the proposal details given a proposal id.
* The `quorumVotes` member holds the *current* quorum, given the current votes.
* @param proposalId the proposal id to get the data for
* @return A `ProposalCondensed` struct with the proposal data
*/
function proposals(uint256 proposalId) external view returns (ProposalCondensed memory) {
Proposal storage proposal = _proposals[proposalId];
return
ProposalCondensed({
id: proposal.id,
proposer: proposal.proposer,
proposalThreshold: proposal.proposalThreshold,
quorumVotes: quorumVotes(proposal.id),
eta: proposal.eta,
startBlock: proposal.startBlock,
endBlock: proposal.endBlock,
forVotes: proposal.forVotes,
againstVotes: proposal.againstVotes,
abstainVotes: proposal.abstainVotes,
canceled: proposal.canceled,
vetoed: proposal.vetoed,
executed: proposal.executed,
totalSupply: proposal.totalSupply,
creationBlock: proposal.creationBlock
});
}
/**
* @notice Cast a vote for a proposal
* @param proposalId The id of the proposal to vote on
* @param support The support value for the vote. 0=against, 1=for, 2=abstain
*/
function castVote(uint256 proposalId, uint8 support) external {
emit VoteCast(msg.sender, proposalId, support, castVoteInternal(msg.sender, proposalId, support), '');
}
/**
* @notice Cast a vote for a proposal, asking the DAO to refund gas costs.
* Users with > 0 votes receive refunds. Refunds are partial when using a gas priority fee higher than the DAO's cap.
* Refunds are partial when the DAO's balance is insufficient.
* No refund is sent when the DAO's balance is empty. No refund is sent to users with no votes.
* Voting takes place regardless of refund success.
* @param proposalId The id of the proposal to vote on
* @param support The support value for the vote. 0=against, 1=for, 2=abstain
* @dev Reentrancy is defended against in `castVoteInternal` at the `receipt.hasVoted == false` require statement.
*/
function castRefundableVote(uint256 proposalId, uint8 support) external {
castRefundableVoteInternal(proposalId, support, '');
}
/**
* @notice Cast a vote for a proposal, asking the DAO to refund gas costs.
* Users with > 0 votes receive refunds. Refunds are partial when using a gas priority fee higher than the DAO's cap.
* Refunds are partial when the DAO's balance is insufficient.
* No refund is sent when the DAO's balance is empty. No refund is sent to users with no votes.
* Voting takes place regardless of refund success.
* @param proposalId The id of the proposal to vote on
* @param support The support value for the vote. 0=against, 1=for, 2=abstain
* @param reason The reason given for the vote by the voter
* @dev Reentrancy is defended against in `castVoteInternal` at the `receipt.hasVoted == false` require statement.
*/
function castRefundableVoteWithReason(
uint256 proposalId,
uint8 support,
string calldata reason
) external {
castRefundableVoteInternal(proposalId, support, reason);
}
/**
* @notice Internal function that carries out refundable voting logic
* @param proposalId The id of the proposal to vote on
* @param support The support value for the vote. 0=against, 1=for, 2=abstain
* @param reason The reason given for the vote by the voter
* @dev Reentrancy is defended against in `castVoteInternal` at the `receipt.hasVoted == false` require statement.
*/
function castRefundableVoteInternal(
uint256 proposalId,
uint8 support,
string memory reason
) internal {
uint256 startGas = gasleft();
uint96 votes = castVoteInternal(msg.sender, proposalId, support);
emit VoteCast(msg.sender, proposalId, support, votes, reason);
if (votes > 0) {
_refundGas(startGas);
}
}
/**
* @notice Cast a vote for a proposal with a reason
* @param proposalId The id of the proposal to vote on
* @param support The support value for the vote. 0=against, 1=for, 2=abstain
* @param reason The reason given for the vote by the voter
*/
function castVoteWithReason(
uint256 proposalId,
uint8 support,
string calldata reason
) external {
emit VoteCast(msg.sender, proposalId, support, castVoteInternal(msg.sender, proposalId, support), reason);
}
/**
* @notice Cast a vote for a proposal by signature
* @dev External function that accepts EIP-712 signatures for voting on proposals.
*/
function castVoteBySig(
uint256 proposalId,
uint8 support,
uint8 v,
bytes32 r,
bytes32 s
) external {
bytes32 domainSeparator = keccak256(
abi.encode(DOMAIN_TYPEHASH, keccak256(bytes(name)), getChainIdInternal(), 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), 'NounsDAO::castVoteBySig: invalid signature');
emit VoteCast(signatory, proposalId, support, castVoteInternal(signatory, proposalId, support), '');
}
/**
* @notice Internal function that caries out voting logic
* @param voter The voter that is casting their vote
* @param proposalId The id of the proposal to vote on
* @param support The support value for the vote. 0=against, 1=for, 2=abstain
* @return The number of votes cast
*/
function castVoteInternal(
address voter,
uint256 proposalId,
uint8 support
) internal returns (uint96) {
require(state(proposalId) == ProposalState.Active, 'NounsDAO::castVoteInternal: voting is closed');
require(support <= 2, 'NounsDAO::castVoteInternal: invalid vote type');
Proposal storage proposal = _proposals[proposalId];
Receipt storage receipt = proposal.receipts[voter];
require(receipt.hasVoted == false, 'NounsDAO::castVoteInternal: voter already voted');
/// @notice: Unlike GovernerBravo, votes are considered from the block the proposal was created in order to normalize quorumVotes and proposalThreshold metrics
uint96 votes = nouns.getPriorVotes(voter, proposalCreationBlock(proposal));
if (support == 0) {
proposal.againstVotes = proposal.againstVotes + votes;
} else if (support == 1) {
proposal.forVotes = proposal.forVotes + votes;
} else if (support == 2) {
proposal.abstainVotes = proposal.abstainVotes + votes;
}
receipt.hasVoted = true;
receipt.support = support;
receipt.votes = votes;
return votes;
}
/**
* @notice Admin function for setting the voting delay
* @param newVotingDelay new voting delay, in blocks
*/
function _setVotingDelay(uint256 newVotingDelay) external {
if (msg.sender != admin) {
revert AdminOnly();
}
require(
newVotingDelay >= MIN_VOTING_DELAY && newVotingDelay <= MAX_VOTING_DELAY,
'NounsDAO::_setVotingDelay: invalid voting delay'
);
uint256 oldVotingDelay = votingDelay;
votingDelay = newVotingDelay;
emit VotingDelaySet(oldVotingDelay, votingDelay);
}
/**
* @notice Admin function for setting the voting period
* @param newVotingPeriod new voting period, in blocks
*/
function _setVotingPeriod(uint256 newVotingPeriod) external {
if (msg.sender != admin) {
revert AdminOnly();
}
require(
newVotingPeriod >= MIN_VOTING_PERIOD && newVotingPeriod <= MAX_VOTING_PERIOD,
'NounsDAO::_setVotingPeriod: invalid voting period'
);
uint256 oldVotingPeriod = votingPeriod;
votingPeriod = newVotingPeriod;
emit VotingPeriodSet(oldVotingPeriod, votingPeriod);
}
/**
* @notice Admin function for setting the proposal threshold basis points
* @dev newProposalThresholdBPS must be greater than the hardcoded min
* @param newProposalThresholdBPS new proposal threshold
*/
function _setProposalThresholdBPS(uint256 newProposalThresholdBPS) external {
if (msg.sender != admin) {
revert AdminOnly();
}
require(
newProposalThresholdBPS >= MIN_PROPOSAL_THRESHOLD_BPS &&
newProposalThresholdBPS <= MAX_PROPOSAL_THRESHOLD_BPS,
'NounsDAO::_setProposalThreshold: invalid proposal threshold bps'
);
uint256 oldProposalThresholdBPS = proposalThresholdBPS;
proposalThresholdBPS = newProposalThresholdBPS;
emit ProposalThresholdBPSSet(oldProposalThresholdBPS, proposalThresholdBPS);
}
/**
* @notice Admin function for setting the minimum quorum votes bps
* @param newMinQuorumVotesBPS minimum quorum votes bps
* Must be between `MIN_QUORUM_VOTES_BPS_LOWER_BOUND` and `MIN_QUORUM_VOTES_BPS_UPPER_BOUND`
* Must be lower than or equal to maxQuorumVotesBPS
*/
function _setMinQuorumVotesBPS(uint16 newMinQuorumVotesBPS) external {
if (msg.sender != admin) {
revert AdminOnly();
}
DynamicQuorumParams memory params = getDynamicQuorumParamsAt(block.number);
require(
newMinQuorumVotesBPS >= MIN_QUORUM_VOTES_BPS_LOWER_BOUND &&
newMinQuorumVotesBPS <= MIN_QUORUM_VOTES_BPS_UPPER_BOUND,
'NounsDAO::_setMinQuorumVotesBPS: invalid min quorum votes bps'
);
require(
newMinQuorumVotesBPS <= params.maxQuorumVotesBPS,
'NounsDAO::_setMinQuorumVotesBPS: min quorum votes bps greater than max'
);
uint16 oldMinQuorumVotesBPS = params.minQuorumVotesBPS;
params.minQuorumVotesBPS = newMinQuorumVotesBPS;
_writeQuorumParamsCheckpoint(params);
emit MinQuorumVotesBPSSet(oldMinQuorumVotesBPS, newMinQuorumVotesBPS);
}
/**
* @notice Admin function for setting the maximum quorum votes bps
* @param newMaxQuorumVotesBPS maximum quorum votes bps
* Must be lower than `MAX_QUORUM_VOTES_BPS_UPPER_BOUND`
* Must be higher than or equal to minQuorumVotesBPS
*/
function _setMaxQuorumVotesBPS(uint16 newMaxQuorumVotesBPS) external {
if (msg.sender != admin) {
revert AdminOnly();
}
DynamicQuorumParams memory params = getDynamicQuorumParamsAt(block.number);
require(
newMaxQuorumVotesBPS <= MAX_QUORUM_VOTES_BPS_UPPER_BOUND,
'NounsDAO::_setMaxQuorumVotesBPS: invalid max quorum votes bps'
);
require(
params.minQuorumVotesBPS <= newMaxQuorumVotesBPS,
'NounsDAO::_setMaxQuorumVotesBPS: min quorum votes bps greater than max'
);
uint16 oldMaxQuorumVotesBPS = params.maxQuorumVotesBPS;
params.maxQuorumVotesBPS = newMaxQuorumVotesBPS;
_writeQuorumParamsCheckpoint(params);
emit MaxQuorumVotesBPSSet(oldMaxQuorumVotesBPS, newMaxQuorumVotesBPS);
}
/**
* @notice Admin function for setting the dynamic quorum coefficient
* @param newQuorumCoefficient the new coefficient, as a fixed point integer with 6 decimals
*/
function _setQuorumCoefficient(uint32 newQuorumCoefficient) external {
if (msg.sender != admin) {
revert AdminOnly();
}
DynamicQuorumParams memory params = getDynamicQuorumParamsAt(block.number);
uint32 oldQuorumCoefficient = params.quorumCoefficient;
params.quorumCoefficient = newQuorumCoefficient;
_writeQuorumParamsCheckpoint(params);
emit QuorumCoefficientSet(oldQuorumCoefficient, newQuorumCoefficient);
}
/**
* @notice Admin function for setting all the dynamic quorum parameters
* @param newMinQuorumVotesBPS minimum quorum votes bps
* Must be between `MIN_QUORUM_VOTES_BPS_LOWER_BOUND` and `MIN_QUORUM_VOTES_BPS_UPPER_BOUND`
* Must be lower than or equal to maxQuorumVotesBPS
* @param newMaxQuorumVotesBPS maximum quorum votes bps
* Must be lower than `MAX_QUORUM_VOTES_BPS_UPPER_BOUND`
* Must be higher than or equal to minQuorumVotesBPS
* @param newQuorumCoefficient the new coefficient, as a fixed point integer with 6 decimals
*/
function _setDynamicQuorumParams(
uint16 newMinQuorumVotesBPS,
uint16 newMaxQuorumVotesBPS,
uint32 newQuorumCoefficient
) public {
if (msg.sender != admin) {
revert AdminOnly();
}
if (
newMinQuorumVotesBPS < MIN_QUORUM_VOTES_BPS_LOWER_BOUND ||
newMinQuorumVotesBPS > MIN_QUORUM_VOTES_BPS_UPPER_BOUND
) {
revert InvalidMinQuorumVotesBPS();
}
if (newMaxQuorumVotesBPS > MAX_QUORUM_VOTES_BPS_UPPER_BOUND) {
revert InvalidMaxQuorumVotesBPS();
}
if (newMinQuorumVotesBPS > newMaxQuorumVotesBPS) {
revert MinQuorumBPSGreaterThanMaxQuorumBPS();
}
DynamicQuorumParams memory oldParams = getDynamicQuorumParamsAt(block.number);
DynamicQuorumParams memory params = DynamicQuorumParams({
minQuorumVotesBPS: newMinQuorumVotesBPS,
maxQuorumVotesBPS: newMaxQuorumVotesBPS,
quorumCoefficient: newQuorumCoefficient
});
_writeQuorumParamsCheckpoint(params);
emit MinQuorumVotesBPSSet(oldParams.minQuorumVotesBPS, params.minQuorumVotesBPS);
emit MaxQuorumVotesBPSSet(oldParams.maxQuorumVotesBPS, params.maxQuorumVotesBPS);
emit QuorumCoefficientSet(oldParams.quorumCoefficient, params.quorumCoefficient);
}
function _withdraw() external returns (uint256, bool) {
if (msg.sender != admin) {
revert AdminOnly();
}
uint256 amount = address(this).balance;
(bool sent, ) = msg.sender.call{ value: amount }('');
emit Withdraw(amount, sent);
return (amount, sent);
}
/**
* @notice Begins transfer of admin rights. The newPendingAdmin must call `_acceptAdmin` to finalize the transfer.
* @dev Admin function to begin change of admin. The newPendingAdmin must call `_acceptAdmin` to finalize the transfer.
* @param newPendingAdmin New pending admin.
*/
function _setPendingAdmin(address newPendingAdmin) external {
// Check caller = admin
require(msg.sender == admin, 'NounsDAO::_setPendingAdmin: admin only');
// Save current value, if any, for inclusion in log
address oldPendingAdmin = pendingAdmin;
// Store pendingAdmin with value newPendingAdmin
pendingAdmin = newPendingAdmin;
// Emit NewPendingAdmin(oldPendingAdmin, newPendingAdmin)
emit NewPendingAdmin(oldPendingAdmin, newPendingAdmin);
}
/**
* @notice Accepts transfer of admin rights. msg.sender must be pendingAdmin
* @dev Admin function for pending admin to accept role and update admin
*/
function _acceptAdmin() external {
// Check caller is pendingAdmin and pendingAdmin ≠ address(0)
require(msg.sender == pendingAdmin && msg.sender != address(0), 'NounsDAO::_acceptAdmin: pending admin only');
// Save current values for inclusion in log
address oldAdmin = admin;
address oldPendingAdmin = pendingAdmin;
// Store admin with value pendingAdmin
admin = pendingAdmin;
// Clear the pending value
pendingAdmin = address(0);
emit NewAdmin(oldAdmin, admin);
emit NewPendingAdmin(oldPendingAdmin, pendingAdmin);
}
/**
* @notice Begins transition of vetoer. The newPendingVetoer must call _acceptVetoer to finalize the transfer.
* @param newPendingVetoer New Pending Vetoer
*/
function _setPendingVetoer(address newPendingVetoer) public {
if (msg.sender != vetoer) {
revert VetoerOnly();
}
emit NewPendingVetoer(pendingVetoer, newPendingVetoer);
pendingVetoer = newPendingVetoer;
}
function _acceptVetoer() external {
if (msg.sender != pendingVetoer) {
revert PendingVetoerOnly();
}
// Update vetoer
emit NewVetoer(vetoer, pendingVetoer);
vetoer = pendingVetoer;
// Clear the pending value
emit NewPendingVetoer(pendingVetoer, address(0));
pendingVetoer = address(0);
}
/**
* @notice Burns veto priviledges
* @dev Vetoer function destroying veto power forever
*/
function _burnVetoPower() public {
// Check caller is vetoer
require(msg.sender == vetoer, 'NounsDAO::_burnVetoPower: vetoer only');
// Update vetoer to 0x0
emit NewVetoer(vetoer, address(0));
vetoer = address(0);
// Clear the pending value
emit NewPendingVetoer(pendingVetoer, address(0));
pendingVetoer = address(0);
}
/**
* @notice Current proposal threshold using Noun Total Supply
* Differs from `GovernerBravo` which uses fixed amount
*/
function proposalThreshold() public view returns (uint256) {
return bps2Uint(proposalThresholdBPS, nouns.totalSupply());
}
function proposalCreationBlock(Proposal storage proposal) internal view returns (uint256) {
if (proposal.creationBlock == 0) {
return proposal.startBlock - votingDelay;
}
return proposal.creationBlock;
}
/**
* @notice Quorum votes required for a specific proposal to succeed
* Differs from `GovernerBravo` which uses fixed amount
*/
function quorumVotes(uint256 proposalId) public view returns (uint256) {
Proposal storage proposal = _proposals[proposalId];
if (proposal.totalSupply == 0) {
return proposal.quorumVotes;
}
return
dynamicQuorumVotes(
proposal.againstVotes,
proposal.totalSupply,
getDynamicQuorumParamsAt(proposal.creationBlock)
);
}
/**
* @notice Calculates the required quorum of for-votes based on the amount of against-votes
* The more against-votes there are for a proposal, the higher the required quorum is.
* The quorum BPS is between `params.minQuorumVotesBPS` and params.maxQuorumVotesBPS.
* The additional quorum is calculated as:
* quorumCoefficient * againstVotesBPS
* @dev Note the coefficient is a fixed point integer with 6 decimals
* @param againstVotes Number of against-votes in the proposal
* @param totalSupply The total supply of Nouns at the time of proposal creation
* @param params Configurable parameters for calculating the quorum based on againstVotes. See `DynamicQuorumParams` definition for additional details.
* @return quorumVotes The required quorum
*/
function dynamicQuorumVotes(
uint256 againstVotes,
uint256 totalSupply,
DynamicQuorumParams memory params
) public pure returns (uint256) {
uint256 againstVotesBPS = (10000 * againstVotes) / totalSupply;
uint256 quorumAdjustmentBPS = (params.quorumCoefficient * againstVotesBPS) / 1e6;
uint256 adjustedQuorumBPS = params.minQuorumVotesBPS + quorumAdjustmentBPS;
uint256 quorumBPS = min(params.maxQuorumVotesBPS, adjustedQuorumBPS);
return bps2Uint(quorumBPS, totalSupply);
}
/**
* @notice returns the dynamic quorum parameters values at a certain block number
* @dev The checkpoints array must not be empty, and the block number must be higher than or equal to
* the block of the first checkpoint
* @param blockNumber_ the block number to get the params at
* @return The dynamic quorum parameters that were set at the given block number
*/
function getDynamicQuorumParamsAt(uint256 blockNumber_) public view returns (DynamicQuorumParams memory) {
uint32 blockNumber = safe32(blockNumber_, 'NounsDAO::getDynamicQuorumParamsAt: block number exceeds 32 bits');
uint256 len = quorumParamsCheckpoints.length;
if (len == 0) {
return
DynamicQuorumParams({
minQuorumVotesBPS: safe16(quorumVotesBPS),
maxQuorumVotesBPS: safe16(quorumVotesBPS),
quorumCoefficient: 0
});
}
if (quorumParamsCheckpoints[len - 1].fromBlock <= blockNumber) {
return quorumParamsCheckpoints[len - 1].params;
}
if (quorumParamsCheckpoints[0].fromBlock > blockNumber) {
return
DynamicQuorumParams({