-
Notifications
You must be signed in to change notification settings - Fork 8
/
GitcoinGovernor.t.sol
1421 lines (1173 loc) · 50.3 KB
/
GitcoinGovernor.t.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: AGPL-3.0-only
pragma solidity ^0.8.17;
import "forge-std/Test.sol";
import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol";
import {IGovernor} from "@openzeppelin/contracts/governance/IGovernor.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {GitcoinGovernor, ICompoundTimelock} from "src/GitcoinGovernor.sol";
import {DeployInput, DeployScript} from "script/Deploy.s.sol";
import {IGovernorAlpha} from "src/interfaces/IGovernorAlpha.sol";
import {IGTC} from "src/interfaces/IGTC.sol";
import {ProposeScript} from "script/Propose.s.sol";
abstract contract GitcoinGovernorTestHelper is Test, DeployInput {
using FixedPointMathLib for uint256;
uint256 constant QUORUM = 2_500_000e18;
address constant GTC_TOKEN = 0xDe30da39c46104798bB5aA3fe8B9e0e1F348163F;
IGTC gtcToken = IGTC(GTC_TOKEN);
address constant TIMELOCK = 0x57a8865cfB1eCEf7253c27da6B4BC3dAEE5Be518;
address constant PROPOSER = 0xc2E2B715d9e302947Ec7e312fd2384b5a1296099; // kbw.eth
address constant DEPLOYED_BRAVO_GOVERNOR = 0x1a84384e1f1b12D53E60C8C528178dC87767b488;
struct Delegate {
string handle;
address addr;
uint96 votes;
}
Delegate[] delegates;
GitcoinGovernor governorBravo;
function setUp() public virtual {
// The latest block when this test was written. If you update the fork block
// make sure to also update the top 6 delegates below.
uint256 _forkBlock = 17_032_826;
vm.createSelectFork(vm.rpcUrl("mainnet"), _forkBlock);
// Taken from https://www.tally.xyz/gov/gitcoin/delegates?sort=voting_power_desc.
// If you update these delegates (including updating order in the array),
// make sure to update any tests that reference specific delegates.
Delegate[] memory _delegates = new Delegate[](6);
_delegates[0] = Delegate("kevinolsen.eth", 0x4Be88f63f919324210ea3A2cCAD4ff0734425F91, 1.8e6);
_delegates[1] = Delegate("janineleger.eth", 0x2df9a188fBE231B0DC36D14AcEb65dEFbB049479, 1.7e6);
_delegates[2] = Delegate("kbw.eth", PROPOSER, 1.2e6);
_delegates[3] = Delegate("griff.eth", 0x839395e20bbB182fa440d08F850E6c7A8f6F0780, 0.8e6);
_delegates[4] = Delegate("lefteris.eth", 0x2B888954421b424C5D3D9Ce9bB67c9bD47537d12, 0.6e6);
_delegates[5] = Delegate("anon gnosis safe", 0x93F80a67FdFDF9DaF1aee5276Db95c8761cc8561, 0.5e6);
// Fetch up-to-date voting weight for the top delegates.
for (uint256 i; i < _delegates.length; i++) {
Delegate memory _delegate = _delegates[i];
_delegate.votes = gtcToken.getCurrentVotes(_delegate.addr);
delegates.push(_delegate);
}
if (_useDeployedGovernorBravo()) {
// The GitcoinGovernor contract was deployed to mainnet on April 7th 2023
// using DeployScript in this repo.
governorBravo = GitcoinGovernor(payable(DEPLOYED_BRAVO_GOVERNOR));
} else {
// We still want to exercise the script in these tests to give us
// confidence that we could deploy again if necessary.
DeployScript _deployScript = new DeployScript();
_deployScript.setUp();
governorBravo = _deployScript.run();
}
}
function _useDeployedGovernorBravo() internal virtual returns (bool);
}
abstract contract BravoGovernorDeployTest is GitcoinGovernorTestHelper {
function testFuzz_deployment(uint256 _blockNumber) public {
assertEq(governorBravo.name(), "GTC Governor Bravo");
assertEq(address(governorBravo.token()), GTC_TOKEN);
// forgefmt: disable-start
// These values were all copied directly from the mainnet alpha governor at:
// 0xDbD27635A534A3d3169Ef0498beB56Fb9c937489
assertEq(INITIAL_VOTING_DELAY, 13140);
assertEq(INITIAL_VOTING_PERIOD, 40_320);
assertEq(INITIAL_PROPOSAL_THRESHOLD, 1_000_000e18);
// forgefmt: disable-end
assertEq(governorBravo.votingDelay(), INITIAL_VOTING_DELAY);
assertEq(governorBravo.votingPeriod(), INITIAL_VOTING_PERIOD);
assertEq(governorBravo.proposalThreshold(), INITIAL_PROPOSAL_THRESHOLD);
assertEq(governorBravo.quorum(_blockNumber), QUORUM);
assertEq(governorBravo.timelock(), TIMELOCK);
assertEq(governorBravo.COUNTING_MODE(), "support=bravo&quorum=for,abstain¶ms=fractional");
}
}
abstract contract ProposalTestHelper is GitcoinGovernorTestHelper {
//----------------- State and Setup ----------- //
IGovernorAlpha governorAlpha = IGovernorAlpha(0xDbD27635A534A3d3169Ef0498beB56Fb9c937489);
address constant USDC_ADDRESS = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address constant RAD_ADDRESS = 0x31c8EAcBFFdD875c74b94b077895Bd78CF1E64A3;
IERC20 usdcToken = IERC20(USDC_ADDRESS);
IERC20 radToken = IERC20(RAD_ADDRESS);
ICompoundTimelock timelock = ICompoundTimelock(payable(TIMELOCK));
uint256 initialProposalCount;
uint256 upgradeProposalId;
// As defined in the GovernorAlpha ProposalState Enum
uint8 constant PENDING = 0;
uint8 constant ACTIVE = 1;
uint8 constant DEFEATED = 3;
uint8 constant SUCCEEDED = 4;
uint8 constant QUEUED = 5;
uint8 constant EXECUTED = 7;
function setUp() public virtual override {
GitcoinGovernorTestHelper.setUp();
initialProposalCount = governorAlpha.proposalCount();
ProposeScript _proposeScript = new ProposeScript();
upgradeProposalId = _proposeScript.run(governorBravo);
}
//--------------- HELPERS ---------------//
function _assumeReceiver(address _receiver) internal {
assumePayable(_receiver);
vm.assume(
// We don't want the receiver to be the Timelock, as that would make our
// assertions less meaningful -- most of our tests want to confirm that
// proposals can cause tokens to be sent *from* the timelock to somewhere
// else.
_receiver != TIMELOCK
// We also can't have the receiver be the zero address because GTC
// blocks transfers to the zero address -- see line 546:
// https://etherscan.io/address/0xDe30da39c46104798bB5aA3fe8B9e0e1F348163F#code
&& _receiver > address(0)
);
assumeNoPrecompiles(_receiver);
}
function _randomERC20Token(uint256 _seed) internal view returns (IERC20 _token) {
if (_seed % 3 == 0) _token = IERC20(address(gtcToken));
if (_seed % 3 == 1) _token = usdcToken;
if (_seed % 3 == 2) _token = radToken;
}
function _upgradeProposalStartBlock() internal view returns (uint256) {
(,,, uint256 _startBlock,,,,,) = governorAlpha.proposals(upgradeProposalId);
return _startBlock;
}
function _upgradeProposalEndBlock() internal view returns (uint256) {
(,,,, uint256 _endBlock,,,,) = governorAlpha.proposals(upgradeProposalId);
return _endBlock;
}
function _upgradeProposalEta() internal view returns (uint256) {
(,, uint256 _eta,,,,,,) = governorAlpha.proposals(upgradeProposalId);
return _eta;
}
function _jumpToActiveUpgradeProposal() internal {
vm.roll(_upgradeProposalStartBlock() + 1);
}
function _jumpToUpgradeVoteComplete() internal {
vm.roll(_upgradeProposalEndBlock() + 1);
}
function _jumpPastProposalEta() internal {
vm.roll(block.number + 1); // move up one block so we're not in the same block as when queued
vm.warp(_upgradeProposalEta() + 1); // jump past the eta timestamp
}
function _delegatesVoteOnUpgradeProposal(bool _support) internal {
for (uint256 _index = 0; _index < delegates.length; _index++) {
vm.prank(delegates[_index].addr);
governorAlpha.castVote(upgradeProposalId, _support);
}
}
function _passUpgradeProposal() internal {
_jumpToActiveUpgradeProposal();
_delegatesVoteOnUpgradeProposal(true);
_jumpToUpgradeVoteComplete();
}
function _defeatUpgradeProposal() internal {
_jumpToActiveUpgradeProposal();
_delegatesVoteOnUpgradeProposal(false);
_jumpToUpgradeVoteComplete();
}
function _passAndQueueUpgradeProposal() internal {
_passUpgradeProposal();
governorAlpha.queue(upgradeProposalId);
}
function _upgradeToBravoGovernor() internal {
_passAndQueueUpgradeProposal();
_jumpPastProposalEta();
governorAlpha.execute(upgradeProposalId);
}
}
abstract contract AlphaGovernorPreProposalTest is ProposalTestHelper {
function test_Proposal() public {
// Proposal has been recorded
assertEq(governorAlpha.proposalCount(), initialProposalCount + 1);
// Proposal is in the expected state
uint8 _state = governorAlpha.state(upgradeProposalId);
assertEq(_state, PENDING);
// Proposal actions correspond to Governor upgrade
(
address[] memory _targets,
uint256[] memory _values,
string[] memory _signatures,
bytes[] memory _calldatas
) = governorAlpha.getActions(upgradeProposalId);
assertEq(_targets.length, 2);
assertEq(_targets[0], TIMELOCK);
assertEq(_targets[1], address(governorBravo));
assertEq(_values.length, 2);
assertEq(_values[0], 0);
assertEq(_values[1], 0);
assertEq(_signatures.length, 2);
assertEq(_signatures[0], "setPendingAdmin(address)");
assertEq(_signatures[1], "__acceptAdmin()");
assertEq(_calldatas.length, 2);
assertEq(_calldatas[0], abi.encode(address(governorBravo)));
assertEq(_calldatas[1], "");
}
function test_proposalActiveAfterDelay() public {
_jumpToActiveUpgradeProposal();
// Ensure proposal has become active the block after the voting delay
uint8 _state = governorAlpha.state(upgradeProposalId);
assertEq(_state, ACTIVE);
}
function testFuzz_ProposerCanCastVote(bool _willSupport) public {
_jumpToActiveUpgradeProposal();
uint256 _proposerVotes = gtcToken.getPriorVotes(PROPOSER, _upgradeProposalStartBlock());
vm.prank(PROPOSER);
governorAlpha.castVote(upgradeProposalId, _willSupport);
IGovernorAlpha.Receipt memory _receipt = governorAlpha.getReceipt(upgradeProposalId, PROPOSER);
assertEq(_receipt.hasVoted, true);
assertEq(_receipt.support, _willSupport);
assertEq(_receipt.votes, _proposerVotes);
}
function test_ProposalSucceedsWhenAllDelegatesVoteFor() public {
_passUpgradeProposal();
// Ensure proposal state is now succeeded
uint8 _state = governorAlpha.state(upgradeProposalId);
assertEq(_state, SUCCEEDED);
}
function test_ProposalDefeatedWhenAllDelegatesVoteAgainst() public {
_defeatUpgradeProposal();
// Ensure proposal state is now defeated
uint8 _state = governorAlpha.state(upgradeProposalId);
assertEq(_state, DEFEATED);
}
function test_ProposalCanBeQueuedAfterSucceeding() public {
_passUpgradeProposal();
governorAlpha.queue(upgradeProposalId);
// Ensure proposal can be queued after success
uint8 _state = governorAlpha.state(upgradeProposalId);
assertEq(_state, QUEUED);
(
address[] memory _targets,
uint256[] memory _values,
string[] memory _signatures,
bytes[] memory _calldatas
) = governorAlpha.getActions(upgradeProposalId);
uint256 _eta = block.timestamp + timelock.delay();
for (uint256 _index = 0; _index < _targets.length; _index++) {
// Calculate hash of transaction in Timelock
bytes32 _txHash = keccak256(
abi.encode(_targets[_index], _values[_index], _signatures[_index], _calldatas[_index], _eta)
);
// Ensure transaction is queued in Timelock
bool _isQueued = timelock.queuedTransactions(_txHash);
assertEq(_isQueued, true);
}
}
function test_ProposalCanBeExecutedAfterDelay() public {
_passAndQueueUpgradeProposal();
_jumpPastProposalEta();
// Execute the proposal
governorAlpha.execute(upgradeProposalId);
// Ensure the proposal is now executed
uint8 _state = governorAlpha.state(upgradeProposalId);
assertEq(_state, EXECUTED);
// Ensure the governorBravo is now the admin of the timelock
assertEq(timelock.admin(), address(governorBravo));
}
}
abstract contract AlphaGovernorPostProposalTest is ProposalTestHelper {
function _queueAndVoteAndExecuteProposalWithAlphaGovernor(
address[] memory _targets,
uint256[] memory _values,
string[] memory _signatures,
bytes[] memory _calldatas,
bool isGovernorAlphaAdmin
) internal {
// Submit the new proposal
vm.prank(PROPOSER);
uint256 _newProposalId =
governorAlpha.propose(_targets, _values, _signatures, _calldatas, "Proposal for old Governor");
// Pass and execute the new proposal
(,,, uint256 _startBlock, uint256 _endBlock,,,,) = governorAlpha.proposals(_newProposalId);
vm.roll(_startBlock + 1);
for (uint256 _index = 0; _index < delegates.length; _index++) {
vm.prank(delegates[_index].addr);
governorAlpha.castVote(_newProposalId, true);
}
vm.roll(_endBlock + 1);
if (!isGovernorAlphaAdmin) {
vm.expectRevert("Timelock::queueTransaction: Call must come from admin.");
governorAlpha.queue(_newProposalId);
return;
}
governorAlpha.queue(_newProposalId);
vm.roll(block.number + 1);
(,, uint256 _eta,,,,,,) = governorAlpha.proposals(_newProposalId);
vm.warp(_eta + 1);
governorAlpha.execute(_newProposalId);
// Ensure the new proposal is now executed
assertEq(governorAlpha.state(_newProposalId), EXECUTED);
}
function testFuzz_OldGovernorSendsETHAfterProposalIsDefeated(uint128 _amount, address _receiver)
public
{
_assumeReceiver(_receiver);
// Counter-intuitively, the Governor (not the Timelock) must hold the ETH.
// See the deployed GovernorAlpha, line 227:
// https://etherscan.io/address/0xDbD27635A534A3d3169Ef0498beB56Fb9c937489#code
// The governor transfers ETH to the Timelock in the process of executing
// the proposal. The Timelock then just passes that ETH along.
vm.deal(address(governorAlpha), _amount);
uint256 _receiverETHBalance = _receiver.balance;
uint256 _governorETHBalance = address(governorAlpha).balance;
// Defeat the proposal to upgrade the Governor
_defeatUpgradeProposal();
// Create a new proposal to send the ETH.
address[] memory _targets = new address[](1);
uint256[] memory _values = new uint256[](1);
_targets[0] = _receiver;
_values[0] = _amount;
_queueAndVoteAndExecuteProposalWithAlphaGovernor(
_targets,
_values,
new string[](1), // No signature needed for an ETH send.
new bytes[](1), // No calldata needed for an ETH send.
true // GovernorAlpha is still the Timelock admin.
);
// Ensure the ETH has been transferred to the receiver
assertEq(address(governorAlpha).balance, _governorETHBalance - _amount);
assertEq(_receiver.balance, _receiverETHBalance + _amount);
}
function testFuzz_OldGovernorCannotSendETHAfterProposalSucceeds(
uint256 _amount,
address _receiver
) public {
_assumeReceiver(_receiver);
// Counter-intuitively, the Governor must hold the ETH, not the Timelock.
// See the deployed GovernorAlpha, line 227:
// https://etherscan.io/address/0xDbD27635A534A3d3169Ef0498beB56Fb9c937489#code
// The governor transfers ETH to the Timelock in the process of executing
// the proposal. The Timelock then just passes that ETH along.
vm.deal(address(governorAlpha), _amount);
uint256 _receiverETHBalance = _receiver.balance;
uint256 _governorETHBalance = address(governorAlpha).balance;
// Pass and execute the proposal to upgrade the Governor
_upgradeToBravoGovernor();
// Create a new proposal to send the ETH.
address[] memory _targets = new address[](1);
uint256[] memory _values = new uint256[](1);
_targets[0] = _receiver;
_values[0] = _amount;
_queueAndVoteAndExecuteProposalWithAlphaGovernor(
_targets,
_values,
new string[](1), // No signature needed for an ETH send.
new bytes[](1), // No calldata needed for an ETH send.
false // GovernorAlpha is not the Timelock admin.
);
// Ensure no ETH has been transferred to the receiver
assertEq(address(governorAlpha).balance, _governorETHBalance);
assertEq(_receiver.balance, _receiverETHBalance);
}
function testFuzz_OldGovernorSendsTokenAfterProposalIsDefeated(
uint256 _amount,
address _receiver,
uint256 _seed
) public {
_assumeReceiver(_receiver);
IERC20 _token = _randomERC20Token(_seed);
uint256 _receiverTokenBalance = _token.balanceOf(_receiver);
uint256 _timelockTokenBalance = _token.balanceOf(TIMELOCK);
// bound by the number of tokens the timelock currently controls
_amount = bound(_amount, 0, _timelockTokenBalance);
// Defeat the proposal to upgrade the Governor
_defeatUpgradeProposal();
// Craft a new proposal to send the token.
address[] memory _targets = new address[](1);
uint256[] memory _values = new uint256[](1);
string[] memory _signatures = new string [](1);
bytes[] memory _calldatas = new bytes[](1);
_targets[0] = address(_token);
_values[0] = 0;
_signatures[0] = "transfer(address,uint256)";
_calldatas[0] = abi.encode(_receiver, _amount);
_queueAndVoteAndExecuteProposalWithAlphaGovernor(
_targets,
_values,
_signatures,
_calldatas,
true // GovernorAlpha is still the Timelock admin.
);
// Ensure the tokens have been transferred from the timelock to the receiver.
assertEq(_token.balanceOf(TIMELOCK), _timelockTokenBalance - _amount);
assertEq(_token.balanceOf(_receiver), _receiverTokenBalance + _amount);
}
function testFuzz_OldGovernorCanNotSendTokensAfterUpgradeCompletes(
uint256 _amount,
address _receiver,
uint256 _seed
) public {
_assumeReceiver(_receiver);
IERC20 _token = _randomERC20Token(_seed);
uint256 _receiverTokenBalance = _token.balanceOf(_receiver);
uint256 _timelockTokenBalance = _token.balanceOf(TIMELOCK);
// bound by the number of tokens the timelock currently controls
_amount = bound(_amount, 0, _timelockTokenBalance);
// Pass and execute the proposal to upgrade the Governor
_upgradeToBravoGovernor();
// Craft a new proposal to send the token.
address[] memory _targets = new address[](1);
uint256[] memory _values = new uint256[](1);
string[] memory _signatures = new string [](1);
bytes[] memory _calldatas = new bytes[](1);
_targets[0] = address(_token);
_values[0] = 0;
_signatures[0] = "transfer(address,uint256)";
_calldatas[0] = abi.encode(_receiver, _amount);
_queueAndVoteAndExecuteProposalWithAlphaGovernor(
_targets,
_values,
_signatures,
_calldatas,
false // GovernorAlpha is not the Timelock admin anymore.
);
// Ensure no tokens have been transferred from the timelock to the receiver.
assertEq(_token.balanceOf(TIMELOCK), _timelockTokenBalance);
assertEq(_token.balanceOf(_receiver), _receiverTokenBalance);
}
}
abstract contract GovernorBravoProposalHelper is ProposalTestHelper {
// From GovernorCountingSimple
uint8 constant AGAINST = 0;
uint8 constant FOR = 1;
uint8 constant ABSTAIN = 2;
function _buildProposalData(string memory _signature, bytes memory _calldata)
internal
pure
returns (bytes memory)
{
return abi.encodePacked(bytes4(keccak256(bytes(_signature))), _calldata);
}
function _jumpToActiveProposal(uint256 _proposalId) internal {
uint256 _snapshot = governorBravo.proposalSnapshot(_proposalId);
vm.roll(_snapshot + 1);
// Ensure the proposal is now Active
IGovernor.ProposalState _state = governorBravo.state(_proposalId);
assertEq(_state, IGovernor.ProposalState.Active);
}
function _jumpToVotingComplete(uint256 _proposalId) internal {
// Jump one block past the proposal voting deadline
uint256 _deadline = governorBravo.proposalDeadline(_proposalId);
vm.roll(_deadline + 1);
}
function _jumpPastProposalEta(uint256 _proposalId) internal {
uint256 _eta = governorBravo.proposalEta(_proposalId);
vm.roll(block.number + 1);
vm.warp(_eta + 1);
}
function _delegatesVoteOnBravoGovernor(uint256 _proposalId, uint8 _support) internal {
require(_support < 3, "Invalid value for support");
for (uint256 _index = 0; _index < delegates.length; _index++) {
vm.prank(delegates[_index].addr);
governorBravo.castVote(_proposalId, _support);
}
}
function _buildTokenSendProposal(address _token, uint256 _tokenAmount, address _receiver)
internal
pure
returns (
address[] memory _targets,
uint256[] memory _values,
bytes[] memory _calldata,
string memory _description
)
{
// Craft a new proposal to send _token.
_targets = new address[](1);
_values = new uint256[](1);
_calldata = new bytes[](1);
_targets[0] = _token;
_values[0] = 0;
_calldata[0] =
_buildProposalData("transfer(address,uint256)", abi.encode(_receiver, _tokenAmount));
_description = "Transfer some tokens from the new Governor";
}
function _submitTokenSendProposalToGovernorBravo(
address _token,
uint256 _amount,
address _receiver
)
internal
returns (
uint256 _newProposalId,
address[] memory _targets,
uint256[] memory _values,
bytes[] memory _calldata,
string memory _description
)
{
(_targets, _values, _calldata, _description) =
_buildTokenSendProposal(_token, _amount, _receiver);
// Submit the new proposal
vm.prank(PROPOSER);
_newProposalId = governorBravo.propose(_targets, _values, _calldata, _description);
// Ensure proposal is in the expected state
IGovernor.ProposalState _state = governorBravo.state(_newProposalId);
assertEq(_state, IGovernor.ProposalState.Pending);
}
// Take a proposal through its full lifecycle, from proposing it, to voting on
// it, to queuing it, to executing it (if relevant) via GovernorBravo.
function _queueAndVoteAndExecuteProposalWithBravoGovernor(
address[] memory _targets,
uint256[] memory _values,
bytes[] memory _calldatas,
string memory _description,
uint8 _voteType
) internal {
// Submit the new proposal
vm.prank(PROPOSER);
uint256 _newProposalId = governorBravo.propose(
_targets, // Go away formatter!
_values,
_calldatas,
_description
);
// Ensure proposal is Pending.
IGovernor.ProposalState _state = governorBravo.state(_newProposalId);
assertEq(_state, IGovernor.ProposalState.Pending);
_jumpToActiveProposal(_newProposalId);
// Have all delegates cast their weight with the specified support type.
_delegatesVoteOnBravoGovernor(_newProposalId, _voteType);
_jumpToVotingComplete(_newProposalId);
_state = governorBravo.state(_newProposalId);
if (_voteType == AGAINST || _voteType == ABSTAIN) {
// The proposal should have failed.
assertEq(_state, IGovernor.ProposalState.Defeated);
// Attempt to queue the proposal.
vm.expectRevert("Governor: proposal not successful");
governorBravo.queue(_targets, _values, _calldatas, keccak256(bytes(_description)));
_jumpPastProposalEta(_newProposalId);
// Attempt to execute the proposal.
vm.expectRevert("Governor: proposal not successful");
governorBravo.execute(_targets, _values, _calldatas, keccak256(bytes(_description)));
// Exit this function, there's nothing left to test.
return;
}
// The voteType was FOR. Ensure the proposal has succeeded.
assertEq(_state, IGovernor.ProposalState.Succeeded);
// Queue the proposal
governorBravo.queue(_targets, _values, _calldatas, keccak256(bytes(_description)));
// Ensure the proposal is queued
_state = governorBravo.state(_newProposalId);
assertEq(_state, IGovernor.ProposalState.Queued);
_jumpPastProposalEta(_newProposalId);
// Execute the proposal
governorBravo.execute(_targets, _values, _calldatas, keccak256(bytes(_description)));
// Ensure the proposal is executed
_state = governorBravo.state(_newProposalId);
assertEq(_state, IGovernor.ProposalState.Executed);
}
function assertEq(IGovernor.ProposalState _actual, IGovernor.ProposalState _expected) internal {
assertEq(uint8(_actual), uint8(_expected));
}
}
abstract contract BravoGovernorProposalTest is GovernorBravoProposalHelper {
function setUp() public virtual override(ProposalTestHelper) {
ProposalTestHelper.setUp();
}
function testFuzz_NewGovernorCanReceiveNewProposal(uint256 _gtcAmount, address _gtcReceiver)
public
{
_assumeReceiver(_gtcReceiver);
_upgradeToBravoGovernor();
_submitTokenSendProposalToGovernorBravo(address(gtcToken), _gtcAmount, _gtcReceiver);
}
function testFuzz_NewGovernorCanDefeatProposal(uint256 _amount, address _receiver, uint256 _seed)
public
{
IERC20 _token = _randomERC20Token(_seed);
_assumeReceiver(_receiver);
_upgradeToBravoGovernor();
(
address[] memory _targets,
uint256[] memory _values,
bytes[] memory _calldatas,
string memory _description
) = _buildTokenSendProposal(address(_token), _amount, _receiver);
_queueAndVoteAndExecuteProposalWithBravoGovernor(
_targets,
_values,
_calldatas,
_description,
(_amount % 2 == 1 ? AGAINST : ABSTAIN) // Randomize vote type.
);
// It should not be possible to queue the proposal
vm.expectRevert("Governor: proposal not successful");
governorBravo.queue(_targets, _values, _calldatas, keccak256(bytes(_description)));
}
function testFuzz_NewGovernorCanPassProposalToSendToken(
uint256 _amount,
address _receiver,
uint256 _seed
) public {
IERC20 _token = _randomERC20Token(_seed);
_assumeReceiver(_receiver);
uint256 _timelockTokenBalance = _token.balanceOf(TIMELOCK);
// bound by the number of tokens the timelock currently controls
_amount = bound(_amount, 0, _timelockTokenBalance);
uint256 _initialTokenBalance = _token.balanceOf(_receiver);
_upgradeToBravoGovernor();
(
address[] memory _targets,
uint256[] memory _values,
bytes[] memory _calldatas,
string memory _description
) = _buildTokenSendProposal(address(_token), _amount, _receiver);
_queueAndVoteAndExecuteProposalWithBravoGovernor(
_targets, _values, _calldatas, _description, FOR
);
// Ensure the tokens have been transferred
assertEq(_token.balanceOf(_receiver), _initialTokenBalance + _amount);
assertEq(_token.balanceOf(TIMELOCK), _timelockTokenBalance - _amount);
}
function testFuzz_NewGovernorCanPassProposalToSendETH(uint256 _amount, address _receiver) public {
_assumeReceiver(_receiver);
vm.deal(TIMELOCK, _amount);
uint256 _timelockETHBalance = TIMELOCK.balance;
uint256 _receiverETHBalance = _receiver.balance;
_upgradeToBravoGovernor();
// Craft a new proposal to send ETH.
address[] memory _targets = new address[](1);
uint256[] memory _values = new uint256[](1);
_targets[0] = _receiver;
_values[0] = _amount;
_queueAndVoteAndExecuteProposalWithBravoGovernor(
_targets,
_values,
new bytes[](1), // There is no calldata for a plain ETH call.
"Transfer some ETH via the new Governor",
FOR // Vote/suppport type.
);
// Ensure the ETH was transferred.
assertEq(_receiver.balance, _receiverETHBalance + _amount);
assertEq(TIMELOCK.balance, _timelockETHBalance - _amount);
}
function testFuzz_NewGovernorCanPassProposalToSendETHWithTokens(
uint256 _amountETH,
uint256 _amountToken,
address _receiver,
uint256 _seed
) public {
IERC20 _token = _randomERC20Token(_seed);
_assumeReceiver(_receiver);
vm.deal(TIMELOCK, _amountETH);
uint256 _timelockETHBalance = TIMELOCK.balance;
uint256 _receiverETHBalance = _receiver.balance;
// Bound _amountToken by the number of tokens the timelock currently controls.
uint256 _timelockTokenBalance = _token.balanceOf(TIMELOCK);
uint256 _receiverTokenBalance = _token.balanceOf(_receiver);
_amountToken = bound(_amountToken, 0, _timelockTokenBalance);
_upgradeToBravoGovernor();
// Craft a new proposal to send ETH and tokens.
address[] memory _targets = new address[](2);
uint256[] memory _values = new uint256[](2);
bytes[] memory _calldatas = new bytes[](2);
// First call transfers tokens.
_targets[0] = address(_token);
_calldatas[0] =
_buildProposalData("transfer(address,uint256)", abi.encode(_receiver, _amountToken));
// Second call sends ETH.
_targets[1] = _receiver;
_values[1] = _amountETH;
_queueAndVoteAndExecuteProposalWithBravoGovernor(
_targets,
_values,
_calldatas,
"Transfer tokens and ETH via the new Governor",
FOR // Vote/suppport type.
);
// Ensure the ETH was transferred.
assertEq(_receiver.balance, _receiverETHBalance + _amountETH);
assertEq(TIMELOCK.balance, _timelockETHBalance - _amountETH);
// Ensure the tokens were transferred.
assertEq(_token.balanceOf(_receiver), _receiverTokenBalance + _amountToken);
assertEq(_token.balanceOf(TIMELOCK), _timelockTokenBalance - _amountToken);
}
function testFuzz_NewGovernorFailedProposalsCantSendETH(uint256 _amount, address _receiver)
public
{
_assumeReceiver(_receiver);
vm.deal(TIMELOCK, _amount);
uint256 _timelockETHBalance = TIMELOCK.balance;
uint256 _receiverETHBalance = _receiver.balance;
_upgradeToBravoGovernor();
// Craft a new proposal to send ETH.
address[] memory _targets = new address[](1);
uint256[] memory _values = new uint256[](1);
_targets[0] = _receiver;
_values[0] = _amount;
_queueAndVoteAndExecuteProposalWithBravoGovernor(
_targets,
_values,
new bytes[](1), // There is no calldata for a plain ETH call.
"Transfer some ETH via the new Governor",
(_amount % 2 == 1 ? AGAINST : ABSTAIN) // Randomize vote type.
);
// Ensure ETH was *not* transferred.
assertEq(_receiver.balance, _receiverETHBalance);
assertEq(TIMELOCK.balance, _timelockETHBalance);
}
function testFuzz_NewGovernorCanUpdateSettingsViaSuccessfulProposal(
uint256 _newDelay,
uint256 _newVotingPeriod,
uint256 _newProposalThreshold
) public {
// The upper bounds are arbitrary here.
_newDelay = bound(_newDelay, 0, 50_000); // about a week at 1 block per 12s
_newVotingPeriod = bound(_newVotingPeriod, 1, 200_000); // about a month
_newProposalThreshold = bound(_newProposalThreshold, 0, 42 ether);
_upgradeToBravoGovernor();
address[] memory _targets = new address[](3);
uint256[] memory _values = new uint256[](3);
bytes[] memory _calldatas = new bytes[](3);
string memory _description = "Update governance settings";
_targets[0] = address(governorBravo);
_calldatas[0] = _buildProposalData("setVotingDelay(uint256)", abi.encode(_newDelay));
_targets[1] = address(governorBravo);
_calldatas[1] = _buildProposalData("setVotingPeriod(uint256)", abi.encode(_newVotingPeriod));
_targets[2] = address(governorBravo);
_calldatas[2] =
_buildProposalData("setProposalThreshold(uint256)", abi.encode(_newProposalThreshold));
// Submit the new proposal
vm.prank(PROPOSER);
uint256 _newProposalId = governorBravo.propose(_targets, _values, _calldatas, _description);
// Ensure proposal is in the expected state
IGovernor.ProposalState _state = governorBravo.state(_newProposalId);
assertEq(_state, IGovernor.ProposalState.Pending);
_jumpToActiveProposal(_newProposalId);
_delegatesVoteOnBravoGovernor(_newProposalId, FOR);
_jumpToVotingComplete(_newProposalId);
// Ensure the proposal has succeeded
_state = governorBravo.state(_newProposalId);
assertEq(_state, IGovernor.ProposalState.Succeeded);
// Queue the proposal
governorBravo.queue(_targets, _values, _calldatas, keccak256(bytes(_description)));
// Ensure the proposal is queued
_state = governorBravo.state(_newProposalId);
assertEq(_state, IGovernor.ProposalState.Queued);
_jumpPastProposalEta(_newProposalId);
// Execute the proposal
governorBravo.execute(_targets, _values, _calldatas, keccak256(bytes(_description)));
// Ensure the proposal is executed
_state = governorBravo.state(_newProposalId);
assertEq(_state, IGovernor.ProposalState.Executed);
// Confirm that governance settings have updated.
assertEq(governorBravo.votingDelay(), _newDelay);
assertEq(governorBravo.votingPeriod(), _newVotingPeriod);
assertEq(governorBravo.proposalThreshold(), _newProposalThreshold);
}
function testFuzz_NewGovernorCanPassMixedProposal(
uint256 _amount,
address _receiver,
uint256 _seed
) public {
IERC20 _token = _randomERC20Token(_seed);
_assumeReceiver(_receiver);
uint256 _timelockTokenBalance = _token.balanceOf(TIMELOCK);
// bound by the number of tokens the timelock currently controls
_amount = bound(_amount, 0, _timelockTokenBalance);
uint256 _initialTokenBalance = _token.balanceOf(_receiver);
_upgradeToBravoGovernor();
(
uint256 _newProposalId,
address[] memory _targets,
uint256[] memory _values,
bytes[] memory _calldatas,
string memory _description
) = _submitTokenSendProposalToGovernorBravo(address(_token), _amount, _receiver);
_jumpToActiveProposal(_newProposalId);
// Delegates vote with a mix of For/Against/Abstain with For winning.
vm.prank(delegates[0].addr);
governorBravo.castVote(_newProposalId, FOR);
vm.prank(delegates[1].addr);
governorBravo.castVote(_newProposalId, FOR);
vm.prank(delegates[2].addr);
governorBravo.castVote(_newProposalId, FOR);
vm.prank(delegates[3].addr);
governorBravo.castVote(_newProposalId, AGAINST);
vm.prank(delegates[4].addr);
governorBravo.castVote(_newProposalId, ABSTAIN);
vm.prank(delegates[5].addr);
governorBravo.castVote(_newProposalId, AGAINST);
// The vote should pass. We are asserting against the raw delegate voting
// weight as a sanity check. In the event that the fork block is changed and
// voting weights are materially different than they were when the test was
// written, we want this assertion to fail.
assertGt(
delegates[0].votes + delegates[1].votes + delegates[2].votes, // FOR votes.
delegates[3].votes + delegates[5].votes // AGAINST votes.
);
_jumpToVotingComplete(_newProposalId);
// Ensure the proposal has succeeded
IGovernor.ProposalState _state = governorBravo.state(_newProposalId);
assertEq(_state, IGovernor.ProposalState.Succeeded);
// Queue the proposal
governorBravo.queue(_targets, _values, _calldatas, keccak256(bytes(_description)));
_jumpPastProposalEta(_newProposalId);
// Execute the proposal
governorBravo.execute(_targets, _values, _calldatas, keccak256(bytes(_description)));
// Ensure the proposal is executed
_state = governorBravo.state(_newProposalId);
assertEq(_state, IGovernor.ProposalState.Executed);
// Ensure the tokens have been transferred
assertEq(_token.balanceOf(_receiver), _initialTokenBalance + _amount);
assertEq(_token.balanceOf(TIMELOCK), _timelockTokenBalance - _amount);
}
function testFuzz_NewGovernorCanDefeatMixedProposal(
uint256 _amount,
address _receiver,
uint256 _seed
) public {
IERC20 _token = _randomERC20Token(_seed);
_assumeReceiver(_receiver);
uint256 _timelockTokenBalance = _token.balanceOf(TIMELOCK);