|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {IGovernance} from "../src/interfaces/IGovernance.sol"; |
| 5 | +import {IInitiative} from "../src/interfaces/IInitiative.sol"; |
| 6 | +import {Governance} from "../src/Governance.sol"; |
| 7 | +import {MockERC20Tester} from "./mocks/MockERC20Tester.sol"; |
| 8 | +import {MockStakingV1} from "./mocks/MockStakingV1.sol"; |
| 9 | +import {MockStakingV1Deployer} from "./mocks/MockStakingV1Deployer.sol"; |
| 10 | + |
| 11 | +contract MockInitiative is IInitiative { |
| 12 | + struct OnAfterAllocateLQTYParams { |
| 13 | + uint256 currentEpoch; |
| 14 | + address user; |
| 15 | + IGovernance.UserState userState; |
| 16 | + IGovernance.Allocation allocation; |
| 17 | + IGovernance.InitiativeState initiativeStat; |
| 18 | + } |
| 19 | + |
| 20 | + OnAfterAllocateLQTYParams[] public onAfterAllocateLQTYCalls; |
| 21 | + |
| 22 | + function numOnAfterAllocateLQTYCalls() external view returns (uint256) { |
| 23 | + return onAfterAllocateLQTYCalls.length; |
| 24 | + } |
| 25 | + |
| 26 | + function onAfterAllocateLQTY( |
| 27 | + uint256 _currentEpoch, |
| 28 | + address _user, |
| 29 | + IGovernance.UserState calldata _userState, |
| 30 | + IGovernance.Allocation calldata _allocation, |
| 31 | + IGovernance.InitiativeState calldata _initiativeState |
| 32 | + ) external override { |
| 33 | + onAfterAllocateLQTYCalls.push( |
| 34 | + OnAfterAllocateLQTYParams(_currentEpoch, _user, _userState, _allocation, _initiativeState) |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + function onRegisterInitiative(uint256) external override {} |
| 39 | + function onUnregisterInitiative(uint256) external override {} |
| 40 | + function onClaimForInitiative(uint256, uint256) external override {} |
| 41 | +} |
| 42 | + |
| 43 | +contract InitiativeHooksTest is MockStakingV1Deployer { |
| 44 | + uint32 constant START_TIME = 1732873631; |
| 45 | + uint32 constant EPOCH_DURATION = 7 days; |
| 46 | + uint32 constant EPOCH_VOTING_CUTOFF = 6 days; |
| 47 | + |
| 48 | + IGovernance.Configuration config = IGovernance.Configuration({ |
| 49 | + registrationFee: 0, |
| 50 | + registrationThresholdFactor: 0, |
| 51 | + unregistrationThresholdFactor: 4 ether, |
| 52 | + unregistrationAfterEpochs: 4, |
| 53 | + votingThresholdFactor: 0, |
| 54 | + minClaim: 0, |
| 55 | + minAccrual: 0, |
| 56 | + epochStart: START_TIME - EPOCH_DURATION, |
| 57 | + epochDuration: EPOCH_DURATION, |
| 58 | + epochVotingCutoff: EPOCH_VOTING_CUTOFF |
| 59 | + }); |
| 60 | + |
| 61 | + MockStakingV1 stakingV1; |
| 62 | + MockERC20Tester lqty; |
| 63 | + MockERC20Tester lusd; |
| 64 | + MockERC20Tester bold; |
| 65 | + Governance governance; |
| 66 | + MockInitiative initiative; |
| 67 | + address[] noInitiatives; // left empty |
| 68 | + address[] initiatives; |
| 69 | + int256[] votes; |
| 70 | + int256[] vetos; |
| 71 | + address voter; |
| 72 | + |
| 73 | + function setUp() external { |
| 74 | + vm.warp(START_TIME); |
| 75 | + |
| 76 | + (stakingV1, lqty, lusd) = deployMockStakingV1(); |
| 77 | + |
| 78 | + bold = new MockERC20Tester("BOLD Stablecoin", "BOLD"); |
| 79 | + vm.label(address(bold), "BOLD"); |
| 80 | + |
| 81 | + governance = new Governance({ |
| 82 | + _lqty: address(lqty), |
| 83 | + _lusd: address(lusd), |
| 84 | + _stakingV1: address(stakingV1), |
| 85 | + _bold: address(bold), |
| 86 | + _config: config, |
| 87 | + _owner: address(this), |
| 88 | + _initiatives: new address[](0) |
| 89 | + }); |
| 90 | + |
| 91 | + initiative = new MockInitiative(); |
| 92 | + initiatives.push(address(initiative)); |
| 93 | + governance.registerInitialInitiatives(initiatives); |
| 94 | + |
| 95 | + voter = makeAddr("voter"); |
| 96 | + lqty.mint(voter, 1 ether); |
| 97 | + |
| 98 | + vm.startPrank(voter); |
| 99 | + lqty.approve(governance.deriveUserProxyAddress(voter), type(uint256).max); |
| 100 | + governance.depositLQTY(1 ether); |
| 101 | + vm.stopPrank(); |
| 102 | + |
| 103 | + votes.push(); |
| 104 | + vetos.push(); |
| 105 | + } |
| 106 | + |
| 107 | + function test_OnAfterAllocateLQTY_IsCalled_WhenCastingVotes() external { |
| 108 | + vm.startPrank(voter); |
| 109 | + votes[0] = 123; |
| 110 | + governance.allocateLQTY(noInitiatives, initiatives, votes, vetos); |
| 111 | + vm.stopPrank(); |
| 112 | + |
| 113 | + assertEq(initiative.numOnAfterAllocateLQTYCalls(), 1, "onAfterAllocateLQTY should have been called once"); |
| 114 | + (,,, IGovernance.Allocation memory allocation,) = initiative.onAfterAllocateLQTYCalls(0); |
| 115 | + assertEq(allocation.voteLQTY, 123, "wrong voteLQTY 1"); |
| 116 | + |
| 117 | + vm.startPrank(voter); |
| 118 | + votes[0] = 456; |
| 119 | + governance.allocateLQTY(initiatives, initiatives, votes, vetos); |
| 120 | + vm.stopPrank(); |
| 121 | + |
| 122 | + assertEq(initiative.numOnAfterAllocateLQTYCalls(), 3, "onAfterAllocateLQTY should have been called twice more"); |
| 123 | + (,,, allocation,) = initiative.onAfterAllocateLQTYCalls(1); |
| 124 | + assertEq(allocation.voteLQTY, 0, "wrong voteLQTY 2"); |
| 125 | + (,,, allocation,) = initiative.onAfterAllocateLQTYCalls(2); |
| 126 | + assertEq(allocation.voteLQTY, 456, "wrong voteLQTY 3"); |
| 127 | + } |
| 128 | + |
| 129 | + function test_OnAfterAllocateLQTY_IsNotCalled_WhenCastingVetos() external { |
| 130 | + vm.startPrank(voter); |
| 131 | + vetos[0] = 123; |
| 132 | + governance.allocateLQTY(noInitiatives, initiatives, votes, vetos); |
| 133 | + vm.stopPrank(); |
| 134 | + |
| 135 | + assertEq(initiative.numOnAfterAllocateLQTYCalls(), 0, "onAfterAllocateLQTY should not have been called once"); |
| 136 | + } |
| 137 | + |
| 138 | + function test_OnAfterAllocateLQTY_IsCalledOnceWithZeroVotes_WhenCastingVetosAfterHavingCastVotes() external { |
| 139 | + vm.startPrank(voter); |
| 140 | + votes[0] = 123; |
| 141 | + governance.allocateLQTY(noInitiatives, initiatives, votes, vetos); |
| 142 | + vm.stopPrank(); |
| 143 | + |
| 144 | + assertEq(initiative.numOnAfterAllocateLQTYCalls(), 1, "onAfterAllocateLQTY should have been called once"); |
| 145 | + |
| 146 | + vm.startPrank(voter); |
| 147 | + votes[0] = 0; |
| 148 | + vetos[0] = 456; |
| 149 | + governance.allocateLQTY(initiatives, initiatives, votes, vetos); |
| 150 | + vm.stopPrank(); |
| 151 | + |
| 152 | + assertEq(initiative.numOnAfterAllocateLQTYCalls(), 2, "onAfterAllocateLQTY should have been called once more"); |
| 153 | + (,,, IGovernance.Allocation memory allocation,) = initiative.onAfterAllocateLQTYCalls(1); |
| 154 | + assertEq(allocation.voteLQTY, 0, "wrong voteLQTY"); |
| 155 | + } |
| 156 | +} |
0 commit comments