-
Notifications
You must be signed in to change notification settings - Fork 416
/
NounsDAOV3Proposals.sol
1018 lines (907 loc) · 39.9 KB
/
NounsDAOV3Proposals.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: GPL-3.0
/// @title Library for NounsDAOLogicV3 contract containing the proposal lifecycle code
/*********************************
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░█████████░░█████████░░░ *
* ░░░░░░██░░░████░░██░░░████░░░ *
* ░░██████░░░████████░░░████░░░ *
* ░░██░░██░░░████░░██░░░████░░░ *
* ░░██░░██░░░████░░██░░░████░░░ *
* ░░░░░░█████████░░█████████░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
*********************************/
pragma solidity ^0.8.19;
import './NounsDAOInterfaces.sol';
import { NounsDAOV3DynamicQuorum } from './NounsDAOV3DynamicQuorum.sol';
import { NounsDAOV3Fork } from './fork/NounsDAOV3Fork.sol';
import { SignatureChecker } from '../external/openzeppelin/SignatureChecker.sol';
import { ECDSA } from '../external/openzeppelin/ECDSA.sol';
import { SafeCast } from '@openzeppelin/contracts/utils/math/SafeCast.sol';
library NounsDAOV3Proposals {
using NounsDAOV3DynamicQuorum for NounsDAOStorageV3.StorageV3;
using NounsDAOV3Fork for NounsDAOStorageV3.StorageV3;
error CantCancelProposalAtFinalState();
error ProposalInfoArityMismatch();
error MustProvideActions();
error TooManyActions();
error ProposerAlreadyHasALiveProposal();
error InvalidSignature();
error SignatureExpired();
error CanOnlyEditUpdatableProposals();
error OnlyProposerCanEdit();
error SignerCountMismtach();
error ProposerCannotUpdateProposalWithSigners();
error MustProvideSignatures();
error SignatureIsCancelled();
error CannotExecuteDuringForkingPeriod();
error VetoerBurned();
error VetoerOnly();
error CantVetoExecutedProposal();
error VotesBelowProposalThreshold();
/// @notice An event emitted when a proposal has been vetoed by vetoAddress
event ProposalVetoed(uint256 id);
/// @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 new proposal is created, which includes additional information
/// @dev V3 adds `signers`, `updatePeriodEndBlock` compared to the V1/V2 event.
event ProposalCreatedWithRequirements(
uint256 id,
address proposer,
address[] signers,
address[] targets,
uint256[] values,
string[] signatures,
bytes[] calldatas,
uint256 startBlock,
uint256 endBlock,
uint256 updatePeriodEndBlock,
uint256 proposalThreshold,
uint256 quorumVotes,
string description
);
/// @notice Emitted when a proposal is created to be executed on timelockV1
event ProposalCreatedOnTimelockV1(uint256 id);
/// @notice Emitted when a proposal is updated
event ProposalUpdated(
uint256 indexed id,
address indexed proposer,
address[] targets,
uint256[] values,
string[] signatures,
bytes[] calldatas,
string description,
string updateMessage
);
/// @notice Emitted when a proposal's transactions are updated
event ProposalTransactionsUpdated(
uint256 indexed id,
address indexed proposer,
address[] targets,
uint256[] values,
string[] signatures,
bytes[] calldatas,
string updateMessage
);
/// @notice Emitted when a proposal's description is updated
event ProposalDescriptionUpdated(
uint256 indexed id,
address indexed proposer,
string description,
string updateMessage
);
/// @notice An event emitted when a proposal has been queued in the NounsDAOExecutor
event ProposalQueued(uint256 id, uint256 eta);
/// @notice An event emitted when a proposal has been executed in the NounsDAOExecutor
event ProposalExecuted(uint256 id);
/// @notice An event emitted when a proposal has been canceled
event ProposalCanceled(uint256 id);
/// @notice Emitted when someone cancels a signature
event SignatureCancelled(address indexed signer, bytes sig);
// Created to solve stack-too-deep errors
struct ProposalTxs {
address[] targets;
uint256[] values;
string[] signatures;
bytes[] calldatas;
}
/// @notice The maximum number of actions that can be included in a proposal
uint256 public constant PROPOSAL_MAX_OPERATIONS = 10; // 10 actions
bytes32 public constant DOMAIN_TYPEHASH =
keccak256('EIP712Domain(string name,uint256 chainId,address verifyingContract)');
bytes32 public constant PROPOSAL_TYPEHASH =
keccak256(
'Proposal(address proposer,address[] targets,uint256[] values,string[] signatures,bytes[] calldatas,string description,uint256 expiry)'
);
bytes32 public constant UPDATE_PROPOSAL_TYPEHASH =
keccak256(
'UpdateProposal(uint256 proposalId,address proposer,address[] targets,uint256[] values,string[] signatures,bytes[] calldatas,string description,uint256 expiry)'
);
/**
* @notice Function used to propose a new proposal. Sender must have delegates above the proposal threshold
* @param txs Target addresses, eth values, function signatures and calldatas for proposal calls
* @param description String description of the proposal
* @return Proposal id of new proposal
*/
function propose(
NounsDAOStorageV3.StorageV3 storage ds,
ProposalTxs memory txs,
string memory description
) internal returns (uint256) {
uint256 adjustedTotalSupply = ds.adjustedTotalSupply();
uint256 proposalThreshold_ = checkPropThreshold(
ds,
ds.nouns.getPriorVotes(msg.sender, block.number - 1),
adjustedTotalSupply
);
checkProposalTxs(txs);
checkNoActiveProp(ds, msg.sender);
uint256 proposalId = ds.proposalCount = ds.proposalCount + 1;
NounsDAOStorageV3.Proposal storage newProposal = createNewProposal(
ds,
proposalId,
proposalThreshold_,
adjustedTotalSupply,
txs
);
ds.latestProposalIds[msg.sender] = proposalId;
emitNewPropEvents(newProposal, new address[](0), ds.minQuorumVotes(adjustedTotalSupply), txs, description);
return proposalId;
}
/**
* @notice Function used to propose a new proposal. Sender must have delegates above the proposal threshold.
* This proposal would be executed via the timelockV1 contract. This is meant to be used in case timelockV1
* is still holding funds or has special permissions to execute on certain contracts.
* @param txs Target addresses, eth values, function signatures and calldatas for proposal calls
* @param description String description of the proposal
* @return uint256 Proposal id of new proposal
*/
function proposeOnTimelockV1(
NounsDAOStorageV3.StorageV3 storage ds,
ProposalTxs memory txs,
string memory description
) internal returns (uint256) {
uint256 newProposalId = propose(ds, txs, description);
NounsDAOStorageV3.Proposal storage newProposal = ds._proposals[newProposalId];
newProposal.executeOnTimelockV1 = true;
emit ProposalCreatedOnTimelockV1(newProposalId);
return newProposalId;
}
/**
* @notice Function used to propose a new proposal. Sender and signers must have delegates above the proposal threshold
* @param proposerSignatures Array of signers who have signed the proposal and their signatures.
* @dev The signatures follow EIP-712. See `PROPOSAL_TYPEHASH` in NounsDAOV3Proposals.sol
* @param txs Target addresses, eth values, function signatures and calldatas for proposal calls
* @param description String description of the proposal
* @return uint256 Proposal id of new proposal
*/
function proposeBySigs(
NounsDAOStorageV3.StorageV3 storage ds,
NounsDAOStorageV3.ProposerSignature[] memory proposerSignatures,
ProposalTxs memory txs,
string memory description
) external returns (uint256) {
if (proposerSignatures.length == 0) revert MustProvideSignatures();
checkProposalTxs(txs);
uint256 proposalId = ds.proposalCount = ds.proposalCount + 1;
uint256 adjustedTotalSupply = ds.adjustedTotalSupply();
uint256 propThreshold = proposalThreshold(ds, adjustedTotalSupply);
NounsDAOStorageV3.Proposal storage newProposal = createNewProposal(
ds,
proposalId,
propThreshold,
adjustedTotalSupply,
txs
);
// important that the proposal is created before the verification call in order to ensure
// the same signer is not trying to sign this proposal more than once
(uint256 votes, address[] memory signers) = verifySignersCanBackThisProposalAndCountTheirVotes(
ds,
proposerSignatures,
txs,
description,
proposalId
);
if (signers.length == 0) revert MustProvideSignatures();
if (votes <= propThreshold) revert VotesBelowProposalThreshold();
newProposal.signers = signers;
emitNewPropEvents(newProposal, signers, ds.minQuorumVotes(adjustedTotalSupply), txs, description);
return proposalId;
}
/**
* @notice Invalidates a signature that may be used for signing a proposal.
* Once a signature is canceled, the sender can no longer use it again.
* If the sender changes their mind and want to sign the proposal, they can change the expiry timestamp
* in order to produce a new signature.
* The signature will only be invalidated when used by the sender. If used by a different account, it will
* not be invalidated.
* @param sig The signature to cancel
*/
function cancelSig(NounsDAOStorageV3.StorageV3 storage ds, bytes calldata sig) external {
bytes32 sigHash = keccak256(sig);
ds.cancelledSigs[msg.sender][sigHash] = true;
emit SignatureCancelled(msg.sender, sig);
}
/**
* @notice Update a proposal transactions and description.
* Only the proposer can update it, and only during the updateable period.
* @param proposalId Proposal's id
* @param targets Updated target addresses for proposal calls
* @param values Updated eth values for proposal calls
* @param signatures Updated function signatures for proposal calls
* @param calldatas Updated calldatas for proposal calls
* @param description Updated description of the proposal
* @param updateMessage Short message to explain the update
*/
function updateProposal(
NounsDAOStorageV3.StorageV3 storage ds,
uint256 proposalId,
address[] memory targets,
uint256[] memory values,
string[] memory signatures,
bytes[] memory calldatas,
string memory description,
string memory updateMessage
) external {
updateProposalTransactionsInternal(ds, proposalId, targets, values, signatures, calldatas);
emit ProposalUpdated(
proposalId,
msg.sender,
targets,
values,
signatures,
calldatas,
description,
updateMessage
);
}
/**
* @notice Updates the proposal's transactions. Only the proposer can update it, and only during the updateable period.
* @param proposalId Proposal's id
* @param targets Updated target addresses for proposal calls
* @param values Updated eth values for proposal calls
* @param signatures Updated function signatures for proposal calls
* @param calldatas Updated calldatas for proposal calls
* @param updateMessage Short message to explain the update
*/
function updateProposalTransactions(
NounsDAOStorageV3.StorageV3 storage ds,
uint256 proposalId,
address[] memory targets,
uint256[] memory values,
string[] memory signatures,
bytes[] memory calldatas,
string memory updateMessage
) external {
updateProposalTransactionsInternal(ds, proposalId, targets, values, signatures, calldatas);
emit ProposalTransactionsUpdated(proposalId, msg.sender, targets, values, signatures, calldatas, updateMessage);
}
function updateProposalTransactionsInternal(
NounsDAOStorageV3.StorageV3 storage ds,
uint256 proposalId,
address[] memory targets,
uint256[] memory values,
string[] memory signatures,
bytes[] memory calldatas
) internal {
checkProposalTxs(ProposalTxs(targets, values, signatures, calldatas));
NounsDAOStorageV3.Proposal storage proposal = ds._proposals[proposalId];
checkProposalUpdatable(ds, proposalId, proposal);
proposal.targets = targets;
proposal.values = values;
proposal.signatures = signatures;
proposal.calldatas = calldatas;
}
/**
* @notice Updates the proposal's description. Only the proposer can update it, and only during the updateable period.
* @param proposalId Proposal's id
* @param description Updated description of the proposal
* @param updateMessage Short message to explain the update
*/
function updateProposalDescription(
NounsDAOStorageV3.StorageV3 storage ds,
uint256 proposalId,
string calldata description,
string calldata updateMessage
) external {
NounsDAOStorageV3.Proposal storage proposal = ds._proposals[proposalId];
checkProposalUpdatable(ds, proposalId, proposal);
emit ProposalDescriptionUpdated(proposalId, msg.sender, description, updateMessage);
}
/**
* @notice Update a proposal's transactions and description that was created with proposeBySigs.
* Only the proposer can update it, during the updateable period.
* Requires the original signers to sign the update.
* @param proposalId Proposal's id
* @param proposerSignatures Array of signers who have signed the proposal and their signatures.
* @dev The signatures follow EIP-712. See `UPDATE_PROPOSAL_TYPEHASH` in NounsDAOV3Proposals.sol
* @param txs Updated transactions for the proposal
* @param description Updated description of the proposal
* @param updateMessage Short message to explain the update
*/
function updateProposalBySigs(
NounsDAOStorageV3.StorageV3 storage ds,
uint256 proposalId,
NounsDAOStorageV3.ProposerSignature[] memory proposerSignatures,
ProposalTxs memory txs,
string memory description,
string memory updateMessage
) external {
checkProposalTxs(txs);
// without this check it's possible to run through this function and update a proposal without signatures
// this problem doesn't exist in the propose function because we check for prop threshold there
if (proposerSignatures.length == 0) revert MustProvideSignatures();
NounsDAOStorageV3.Proposal storage proposal = ds._proposals[proposalId];
if (stateInternal(ds, proposalId) != NounsDAOStorageV3.ProposalState.Updatable)
revert CanOnlyEditUpdatableProposals();
if (msg.sender != proposal.proposer) revert OnlyProposerCanEdit();
address[] memory signers = proposal.signers;
if (proposerSignatures.length != signers.length) revert SignerCountMismtach();
bytes memory proposalEncodeData = abi.encodePacked(
proposalId,
calcProposalEncodeData(msg.sender, txs, description)
);
for (uint256 i = 0; i < proposerSignatures.length; ++i) {
verifyProposalSignature(ds, proposalEncodeData, proposerSignatures[i], UPDATE_PROPOSAL_TYPEHASH);
// To avoid the gas cost of having to search signers in proposal.signers, we're assuming the sigs we get
// use the same amount of signers and the same order.
if (signers[i] != proposerSignatures[i].signer) revert OnlyProposerCanEdit();
}
proposal.targets = txs.targets;
proposal.values = txs.values;
proposal.signatures = txs.signatures;
proposal.calldatas = txs.calldatas;
emit ProposalUpdated(
proposalId,
msg.sender,
txs.targets,
txs.values,
txs.signatures,
txs.calldatas,
description,
updateMessage
);
}
/**
* @notice Queues a proposal of state succeeded
* @param proposalId The id of the proposal to queue
*/
function queue(NounsDAOStorageV3.StorageV3 storage ds, uint256 proposalId) external {
require(
stateInternal(ds, proposalId) == NounsDAOStorageV3.ProposalState.Succeeded,
'NounsDAO::queue: proposal can only be queued if it is succeeded'
);
NounsDAOStorageV3.Proposal storage proposal = ds._proposals[proposalId];
INounsDAOExecutor timelock = getProposalTimelock(ds, proposal);
uint256 eta = block.timestamp + timelock.delay();
for (uint256 i = 0; i < proposal.targets.length; i++) {
queueOrRevertInternal(
timelock,
proposal.targets[i],
proposal.values[i],
proposal.signatures[i],
proposal.calldatas[i],
eta
);
}
proposal.eta = eta;
emit ProposalQueued(proposalId, eta);
}
function queueOrRevertInternal(
INounsDAOExecutor timelock,
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(NounsDAOStorageV3.StorageV3 storage ds, uint256 proposalId) external {
NounsDAOStorageV3.Proposal storage proposal = ds._proposals[proposalId];
INounsDAOExecutor timelock = getProposalTimelock(ds, proposal);
executeInternal(ds, proposal, timelock);
}
/**
* @notice Executes a queued proposal on timelockV1 if eta has passed
* This is only required for proposal that were queued on timelockV1, but before the upgrade to DAO V3.
* These proposals will not have the `executeOnTimelockV1` bool turned on.
*/
function executeOnTimelockV1(NounsDAOStorageV3.StorageV3 storage ds, uint256 proposalId) external {
NounsDAOStorageV3.Proposal storage proposal = ds._proposals[proposalId];
executeInternal(ds, proposal, ds.timelockV1);
}
function executeInternal(
NounsDAOStorageV3.StorageV3 storage ds,
NounsDAOStorageV3.Proposal storage proposal,
INounsDAOExecutor timelock
) internal {
require(
stateInternal(ds, proposal.id) == NounsDAOStorageV3.ProposalState.Queued,
'NounsDAO::execute: proposal can only be executed if it is queued'
);
if (ds.isForkPeriodActive()) revert CannotExecuteDuringForkingPeriod();
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(proposal.id);
}
function getProposalTimelock(NounsDAOStorageV3.StorageV3 storage ds, NounsDAOStorageV3.Proposal storage proposal)
internal
view
returns (INounsDAOExecutor)
{
if (proposal.executeOnTimelockV1) {
return ds.timelockV1;
} else {
return ds.timelock;
}
}
/**
* @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(NounsDAOStorageV3.StorageV3 storage ds, uint256 proposalId) external {
if (ds.vetoer == address(0)) {
revert VetoerBurned();
}
if (msg.sender != ds.vetoer) {
revert VetoerOnly();
}
if (stateInternal(ds, proposalId) == NounsDAOStorageV3.ProposalState.Executed) {
revert CantVetoExecutedProposal();
}
NounsDAOStorageV3.Proposal storage proposal = ds._proposals[proposalId];
proposal.vetoed = true;
INounsDAOExecutor timelock = getProposalTimelock(ds, proposal);
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 Cancels a proposal only if sender is the proposer or a signer, or proposer & signers voting power
* dropped below proposal threshold
* @param proposalId The id of the proposal to cancel
*/
function cancel(NounsDAOStorageV3.StorageV3 storage ds, uint256 proposalId) external {
NounsDAOStorageV3.ProposalState proposalState = stateInternal(ds, proposalId);
if (
proposalState == NounsDAOStorageV3.ProposalState.Canceled ||
proposalState == NounsDAOStorageV3.ProposalState.Defeated ||
proposalState == NounsDAOStorageV3.ProposalState.Expired ||
proposalState == NounsDAOStorageV3.ProposalState.Executed ||
proposalState == NounsDAOStorageV3.ProposalState.Vetoed
) {
revert CantCancelProposalAtFinalState();
}
NounsDAOStorageV3.Proposal storage proposal = ds._proposals[proposalId];
address proposer = proposal.proposer;
NounsTokenLike nouns = ds.nouns;
uint256 votes = nouns.getPriorVotes(proposer, block.number - 1);
bool msgSenderIsProposer = proposer == msg.sender;
address[] memory signers = proposal.signers;
for (uint256 i = 0; i < signers.length; ++i) {
msgSenderIsProposer = msgSenderIsProposer || msg.sender == signers[i];
votes += nouns.getPriorVotes(signers[i], block.number - 1);
}
require(
msgSenderIsProposer || votes <= proposal.proposalThreshold,
'NounsDAO::cancel: proposer above threshold'
);
proposal.canceled = true;
INounsDAOExecutor timelock = getProposalTimelock(ds, proposal);
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 Gets the state of a proposal
* @param ds the DAO's state struct
* @param proposalId The id of the proposal
* @return Proposal state
*/
function state(NounsDAOStorageV3.StorageV3 storage ds, uint256 proposalId)
public
view
returns (NounsDAOStorageV3.ProposalState)
{
return stateInternal(ds, proposalId);
}
/**
* @notice Gets the state of a proposal
* @dev This internal function is used by other libraries to embed in compile time and save the runtime gas cost of a delegate call
* @param ds the DAO's state struct
* @param proposalId The id of the proposal
* @return Proposal state
*/
function stateInternal(NounsDAOStorageV3.StorageV3 storage ds, uint256 proposalId)
internal
view
returns (NounsDAOStorageV3.ProposalState)
{
require(ds.proposalCount >= proposalId, 'NounsDAO::state: invalid proposal id');
NounsDAOStorageV3.Proposal storage proposal = ds._proposals[proposalId];
if (proposal.vetoed) {
return NounsDAOStorageV3.ProposalState.Vetoed;
} else if (proposal.canceled) {
return NounsDAOStorageV3.ProposalState.Canceled;
} else if (block.number <= proposal.updatePeriodEndBlock) {
return NounsDAOStorageV3.ProposalState.Updatable;
} else if (block.number <= proposal.startBlock) {
return NounsDAOStorageV3.ProposalState.Pending;
} else if (block.number <= proposal.endBlock) {
return NounsDAOStorageV3.ProposalState.Active;
} else if (block.number <= proposal.objectionPeriodEndBlock) {
return NounsDAOStorageV3.ProposalState.ObjectionPeriod;
} else if (isDefeated(ds, proposal)) {
return NounsDAOStorageV3.ProposalState.Defeated;
} else if (proposal.eta == 0) {
return NounsDAOStorageV3.ProposalState.Succeeded;
} else if (proposal.executed) {
return NounsDAOStorageV3.ProposalState.Executed;
} else if (block.timestamp >= proposal.eta + getProposalTimelock(ds, proposal).GRACE_PERIOD()) {
return NounsDAOStorageV3.ProposalState.Expired;
} else {
return NounsDAOStorageV3.ProposalState.Queued;
}
}
/**
* @notice Gets actions of a proposal
* @param proposalId the id of the proposal
* @return targets
* @return values
* @return signatures
* @return calldatas
*/
function getActions(NounsDAOStorageV3.StorageV3 storage ds, uint256 proposalId)
internal
view
returns (
address[] memory targets,
uint256[] memory values,
string[] memory signatures,
bytes[] memory calldatas
)
{
NounsDAOStorageV3.Proposal storage p = ds._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(
NounsDAOStorageV3.StorageV3 storage ds,
uint256 proposalId,
address voter
) internal view returns (NounsDAOStorageV3.Receipt memory) {
return ds._proposals[proposalId].receipts[voter];
}
/**
* @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(NounsDAOStorageV3.StorageV3 storage ds, uint256 proposalId)
external
view
returns (NounsDAOStorageV2.ProposalCondensed memory)
{
NounsDAOStorageV3.Proposal storage proposal = ds._proposals[proposalId];
return
NounsDAOStorageV2.ProposalCondensed({
id: proposal.id,
proposer: proposal.proposer,
proposalThreshold: proposal.proposalThreshold,
quorumVotes: ds.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 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 proposalsV3(NounsDAOStorageV3.StorageV3 storage ds, uint256 proposalId)
external
view
returns (NounsDAOStorageV3.ProposalCondensed memory)
{
NounsDAOStorageV3.Proposal storage proposal = ds._proposals[proposalId];
return
NounsDAOStorageV3.ProposalCondensed({
id: proposal.id,
proposer: proposal.proposer,
proposalThreshold: proposal.proposalThreshold,
quorumVotes: ds.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,
signers: proposal.signers,
updatePeriodEndBlock: proposal.updatePeriodEndBlock,
objectionPeriodEndBlock: proposal.objectionPeriodEndBlock,
executeOnTimelockV1: proposal.executeOnTimelockV1
});
}
/**
* @notice Current proposal threshold using Noun Total Supply
* Differs from `GovernerBravo` which uses fixed amount
*/
function proposalThreshold(NounsDAOStorageV3.StorageV3 storage ds, uint256 adjustedTotalSupply)
internal
view
returns (uint256)
{
return bps2Uint(ds.proposalThresholdBPS, adjustedTotalSupply);
}
function isDefeated(NounsDAOStorageV3.StorageV3 storage ds, NounsDAOStorageV3.Proposal storage proposal)
internal
view
returns (bool)
{
uint256 forVotes = proposal.forVotes;
return forVotes <= proposal.againstVotes || forVotes < ds.quorumVotes(proposal.id);
}
/**
* @notice reverts if `proposer` is the proposer or signer of an active proposal.
* This is a spam protection mechanism to limit the number of proposals each noun can back.
*/
function checkNoActiveProp(NounsDAOStorageV3.StorageV3 storage ds, address proposer) internal view {
uint256 latestProposalId = ds.latestProposalIds[proposer];
if (latestProposalId != 0) {
NounsDAOStorageV3.ProposalState proposersLatestProposalState = stateInternal(ds, latestProposalId);
if (
proposersLatestProposalState == NounsDAOStorageV3.ProposalState.ObjectionPeriod ||
proposersLatestProposalState == NounsDAOStorageV3.ProposalState.Active ||
proposersLatestProposalState == NounsDAOStorageV3.ProposalState.Pending ||
proposersLatestProposalState == NounsDAOStorageV3.ProposalState.Updatable
) revert ProposerAlreadyHasALiveProposal();
}
}
/**
* @dev Extracted this function to fix the `Stack too deep` error `proposeBySigs` hit.
*/
function verifySignersCanBackThisProposalAndCountTheirVotes(
NounsDAOStorageV3.StorageV3 storage ds,
NounsDAOStorageV3.ProposerSignature[] memory proposerSignatures,
ProposalTxs memory txs,
string memory description,
uint256 proposalId
) internal returns (uint256 votes, address[] memory signers) {
NounsTokenLike nouns = ds.nouns;
bytes memory proposalEncodeData = calcProposalEncodeData(msg.sender, txs, description);
signers = new address[](proposerSignatures.length);
uint256 numSigners = 0;
for (uint256 i = 0; i < proposerSignatures.length; ++i) {
verifyProposalSignature(ds, proposalEncodeData, proposerSignatures[i], PROPOSAL_TYPEHASH);
address signer = proposerSignatures[i].signer;
checkNoActiveProp(ds, signer);
uint256 signerVotes = nouns.getPriorVotes(signer, block.number - 1);
if (signerVotes == 0) {
continue;
}
signers[numSigners++] = signer;
ds.latestProposalIds[signer] = proposalId;
votes += signerVotes;
}
if (numSigners < proposerSignatures.length) {
// this assembly trims the signer array, getting rid of unused cells
assembly {
mstore(signers, numSigners)
}
}
checkNoActiveProp(ds, msg.sender);
ds.latestProposalIds[msg.sender] = proposalId;
votes += nouns.getPriorVotes(msg.sender, block.number - 1);
}
function calcProposalEncodeData(
address proposer,
ProposalTxs memory txs,
string memory description
) internal pure returns (bytes memory) {
bytes32[] memory signatureHashes = new bytes32[](txs.signatures.length);
for (uint256 i = 0; i < txs.signatures.length; ++i) {
signatureHashes[i] = keccak256(bytes(txs.signatures[i]));
}
bytes32[] memory calldatasHashes = new bytes32[](txs.calldatas.length);
for (uint256 i = 0; i < txs.calldatas.length; ++i) {
calldatasHashes[i] = keccak256(txs.calldatas[i]);
}
return
abi.encode(
proposer,
keccak256(abi.encodePacked(txs.targets)),
keccak256(abi.encodePacked(txs.values)),
keccak256(abi.encodePacked(signatureHashes)),
keccak256(abi.encodePacked(calldatasHashes)),
keccak256(bytes(description))
);
}
function checkProposalUpdatable(
NounsDAOStorageV3.StorageV3 storage ds,
uint256 proposalId,
NounsDAOStorageV3.Proposal storage proposal
) internal view {
if (stateInternal(ds, proposalId) != NounsDAOStorageV3.ProposalState.Updatable)
revert CanOnlyEditUpdatableProposals();
if (msg.sender != proposal.proposer) revert OnlyProposerCanEdit();
if (proposal.signers.length > 0) revert ProposerCannotUpdateProposalWithSigners();
}
function createNewProposal(
NounsDAOStorageV3.StorageV3 storage ds,
uint256 proposalId,
uint256 proposalThreshold_,
uint256 adjustedTotalSupply,
ProposalTxs memory txs
) internal returns (NounsDAOStorageV3.Proposal storage newProposal) {
uint64 updatePeriodEndBlock = SafeCast.toUint64(block.number + ds.proposalUpdatablePeriodInBlocks);
uint256 startBlock = updatePeriodEndBlock + ds.votingDelay;
uint256 endBlock = startBlock + ds.votingPeriod;
newProposal = ds._proposals[proposalId];
newProposal.id = proposalId;
newProposal.proposer = msg.sender;
newProposal.proposalThreshold = proposalThreshold_;
newProposal.targets = txs.targets;
newProposal.values = txs.values;
newProposal.signatures = txs.signatures;
newProposal.calldatas = txs.calldatas;
newProposal.startBlock = startBlock;
newProposal.endBlock = endBlock;
newProposal.totalSupply = adjustedTotalSupply;
newProposal.creationBlock = SafeCast.toUint64(block.number);
newProposal.updatePeriodEndBlock = updatePeriodEndBlock;
}
function emitNewPropEvents(
NounsDAOStorageV3.Proposal storage newProposal,
address[] memory signers,
uint256 minQuorumVotes,
ProposalTxs memory txs,
string memory description
) internal {
/// @notice Maintains backwards compatibility with GovernorBravo events
emit ProposalCreated(
newProposal.id,
msg.sender,
txs.targets,
txs.values,
txs.signatures,
txs.calldatas,
newProposal.startBlock,
newProposal.endBlock,
description
);
/// @notice V1: Updated event with `proposalThreshold` and `quorumVotes` `minQuorumVotes`
/// @notice V2: `quorumVotes` changed to `minQuorumVotes`
/// @notice V3: Added signers and updatePeriodEndBlock
emit ProposalCreatedWithRequirements(
newProposal.id,
msg.sender,
signers,
txs.targets,
txs.values,
txs.signatures,
txs.calldatas,
newProposal.startBlock,
newProposal.endBlock,
newProposal.updatePeriodEndBlock,
newProposal.proposalThreshold,
minQuorumVotes,
description
);
}
function checkPropThreshold(
NounsDAOStorageV3.StorageV3 storage ds,
uint256 votes,
uint256 adjustedTotalSupply
) internal view returns (uint256 propThreshold) {
propThreshold = proposalThreshold(ds, adjustedTotalSupply);
if (votes <= propThreshold) revert VotesBelowProposalThreshold();
}
function checkProposalTxs(ProposalTxs memory txs) internal pure {
if (
txs.targets.length != txs.values.length ||
txs.targets.length != txs.signatures.length ||
txs.targets.length != txs.calldatas.length
) revert ProposalInfoArityMismatch();
if (txs.targets.length == 0) revert MustProvideActions();
if (txs.targets.length > PROPOSAL_MAX_OPERATIONS) revert TooManyActions();
}
function verifyProposalSignature(
NounsDAOStorageV3.StorageV3 storage ds,
bytes memory proposalEncodeData,
NounsDAOStorageV3.ProposerSignature memory proposerSignature,
bytes32 typehash
) internal view {
bytes32 sigHash = keccak256(proposerSignature.sig);
if (ds.cancelledSigs[proposerSignature.signer][sigHash]) revert SignatureIsCancelled();
bytes32 digest = sigDigest(typehash, proposalEncodeData, proposerSignature.expirationTimestamp, address(this));
if (!SignatureChecker.isValidSignatureNow(proposerSignature.signer, digest, proposerSignature.sig))
revert InvalidSignature();
if (block.timestamp > proposerSignature.expirationTimestamp) revert SignatureExpired();
}
/**
* @notice Generate the digest (hash) used to verify proposal signatures.
* @param typehash the EIP 712 type hash of the signed message, e.g. `PROPOSAL_TYPEHASH` or `UPDATE_PROPOSAL_TYPEHASH`.
* @param proposalEncodeData the abi encoded proposal data, identical to the output of `calcProposalEncodeData`.
* @param expirationTimestamp the signature's expiration timestamp.
* @param verifyingContract the contract verifying the signature, e.g. the DAO proxy by default.
* @return bytes32 the signature's typed data hash.
*/
function sigDigest(