|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 2 | +// slither-disable-start reentrancy-benign |
| 3 | + |
| 4 | +pragma solidity ^0.8.23; |
| 5 | + |
| 6 | +import {BinaryEligibilityOracleEarningPowerCalculator} from |
| 7 | + "../calculators/BinaryEligibilityOracleEarningPowerCalculator.sol"; |
| 8 | +import {StakerTestBase} from "./StakerTestBase.sol"; |
| 9 | +import {Staker} from "../Staker.sol"; |
| 10 | +import {BinaryEligibilityOracleEarningPowerCalculatorTestBase} from |
| 11 | + "./BinaryEligibilityOracleEarningPowerCalculatorTestBase.sol"; |
| 12 | + |
| 13 | +/// @title StakedBinaryEligibilityOracleEarningPowerCalculatorTestSuite |
| 14 | +/// @author [ScopeLift](https://scopelift.co) |
| 15 | +/// @notice The base contract for testing BinaryEligibilityOracleEarningPowerCalculator. Contains |
| 16 | +/// test setup and helper functions for testing the calculator's behavior when delegatees are below |
| 17 | +/// the eligibility threshold. This contract is designed to be used in conjunction with the |
| 18 | +/// deployment scripts in |
| 19 | +/// `src/script/calculators/DeployBinaryEligibilityOracleEarningPowerCalculator.sol`. |
| 20 | +abstract contract StakedBinaryEligibilityOracleEarningPowerCalculatorTestBase is |
| 21 | + BinaryEligibilityOracleEarningPowerCalculatorTestBase |
| 22 | +{ |
| 23 | + /// @notice Helper to set a delegatee's score below threshold. |
| 24 | + /// @param delegatee The address of the delegatee whose score will be set below threshold. |
| 25 | + function _setDelegateeScoreBelowThreshold(address delegatee) internal { |
| 26 | + vm.startPrank(calculator.scoreOracle()); |
| 27 | + calculator.updateDelegateeScore(delegatee, calculator.delegateeEligibilityThresholdScore() - 1); |
| 28 | + vm.stopPrank(); |
| 29 | + } |
| 30 | + |
| 31 | + /// @notice A test helper that wraps calling the `stake` function and ensures proper earning power |
| 32 | + /// adjustment when delegatee scores are below threshold. |
| 33 | + /// @param _depositor The address of the depositor. |
| 34 | + /// @param _amount The amount to stake. |
| 35 | + /// @param _delegatee The address to which the delegation surrogate is delegating voting power. |
| 36 | + /// @return _depositId The id of the created deposit. |
| 37 | + function _stakeBelowThreshold(address _depositor, uint256 _amount, address _delegatee) |
| 38 | + internal |
| 39 | + virtual |
| 40 | + returns (Staker.DepositIdentifier _depositId) |
| 41 | + { |
| 42 | + _depositId = StakerTestBase._stake(_depositor, _amount, _delegatee); |
| 43 | + _setDelegateeScoreBelowThreshold(_delegatee); |
| 44 | + } |
| 45 | + |
| 46 | + /// @notice Helper to set a delegatee's score above the eligibility threshold. |
| 47 | + /// @dev This should be called after a delegatee is known but before checking their earning power. |
| 48 | + /// @param delegatee The address of the delegatee whose score will be set above threshold. |
| 49 | + function _setDelegateeScoreAboveThreshold(address delegatee) internal { |
| 50 | + vm.startPrank(calculator.scoreOracle()); |
| 51 | + calculator.updateDelegateeScore(delegatee, calculator.delegateeEligibilityThresholdScore() + 1); |
| 52 | + vm.stopPrank(); |
| 53 | + } |
| 54 | + |
| 55 | + /// @notice A test helper that wraps calling the `stake` function and ensures proper earning power |
| 56 | + /// adjustment when delegatee scores are above threshold. |
| 57 | + /// @param _depositor The address of the depositor. |
| 58 | + /// @param _amount The amount to stake. |
| 59 | + /// @param _delegatee The address to which the delegation surrogate is delegating voting power. |
| 60 | + /// @return _depositId The id of the created deposit. |
| 61 | + function _stakeAboveThreshold(address _depositor, uint256 _amount, address _delegatee) |
| 62 | + internal |
| 63 | + virtual |
| 64 | + returns (Staker.DepositIdentifier _depositId) |
| 65 | + { |
| 66 | + _depositId = StakerTestBase._stake(_depositor, _amount, _delegatee); |
| 67 | + |
| 68 | + _setDelegateeScoreAboveThreshold(_delegatee); |
| 69 | + vm.startPrank(_depositor); |
| 70 | + staker.bumpEarningPower(_depositId, _depositor, 0); |
| 71 | + vm.stopPrank(); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +abstract contract StakeBase is StakedBinaryEligibilityOracleEarningPowerCalculatorTestBase { |
| 76 | + function testFuzz_StakerEarnsZeroRewardsWhenDelegateeScoreIsBelowThreshold( |
| 77 | + address _depositor, |
| 78 | + uint96 _amount, |
| 79 | + address _delegatee, |
| 80 | + uint256 _rewardAmount, |
| 81 | + uint256 _percentDuration |
| 82 | + ) public { |
| 83 | + _assumeNotZeroAddressOrStaker(_depositor); |
| 84 | + vm.assume(_delegatee != address(0) && _amount != 0); |
| 85 | + |
| 86 | + _mintStakeToken(_depositor, _amount); |
| 87 | + _rewardAmount = _boundToRealisticReward(_rewardAmount); |
| 88 | + _percentDuration = bound(_percentDuration, 1, 100); |
| 89 | + |
| 90 | + Staker.DepositIdentifier _depositId = _stakeBelowThreshold(_depositor, _amount, _delegatee); |
| 91 | + |
| 92 | + _notifyRewardAmount(_rewardAmount); |
| 93 | + _jumpAheadByPercentOfRewardDuration(_percentDuration); |
| 94 | + |
| 95 | + uint256 unclaimedRewards = staker.unclaimedReward(_depositId); |
| 96 | + |
| 97 | + assertEq(unclaimedRewards, 0); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +abstract contract WithdrawBase is StakedBinaryEligibilityOracleEarningPowerCalculatorTestBase { |
| 102 | + function testFuzz_OnlyStakeWithEligibleEarningPowerClaimsRewardAfterDuration( |
| 103 | + address _depositor1, |
| 104 | + address _depositor2, |
| 105 | + uint96 _amount1, |
| 106 | + uint96 _amount2, |
| 107 | + address _delegatee1, |
| 108 | + address _delegatee2, |
| 109 | + uint256 _rewardAmount, |
| 110 | + uint256 _percentDuration |
| 111 | + ) public { |
| 112 | + _assumeNotZeroAddressOrStaker(_depositor1); |
| 113 | + _assumeNotZeroAddressOrStaker(_depositor2); |
| 114 | + vm.assume(_depositor1 != _depositor2); |
| 115 | + vm.assume(_delegatee1 != address(0) && _delegatee2 != address(0) && _delegatee1 != _delegatee2); |
| 116 | + |
| 117 | + _amount1 = uint96(_boundMintAmount(_amount1)); |
| 118 | + _amount2 = uint96(_boundMintAmount(_amount2)); |
| 119 | + vm.assume(_amount1 != 0 && _amount2 != 0); |
| 120 | + |
| 121 | + _mintStakeToken(_depositor1, _amount1); |
| 122 | + _mintStakeToken(_depositor2, _amount2); |
| 123 | + |
| 124 | + Staker.DepositIdentifier _depositId1 = _stakeBelowThreshold(_depositor1, _amount1, _delegatee1); |
| 125 | + Staker.DepositIdentifier _depositId2 = _stakeAboveThreshold(_depositor2, _amount2, _delegatee2); |
| 126 | + |
| 127 | + _rewardAmount = _boundToRealisticReward(_rewardAmount); |
| 128 | + _notifyRewardAmount(_rewardAmount); |
| 129 | + _percentDuration = bound(_percentDuration, 1, 100); |
| 130 | + _jumpAheadByPercentOfRewardDuration(_percentDuration); |
| 131 | + |
| 132 | + Staker.Deposit memory _deposit2 = _fetchDeposit(_depositId2); |
| 133 | + uint256 _calculatedRewards2 = |
| 134 | + _calculateEarnedRewards(_deposit2.earningPower, _rewardAmount, _percentDuration); |
| 135 | + |
| 136 | + _withdraw(_depositor1, _depositId1, _amount1); |
| 137 | + _withdraw(_depositor2, _depositId2, _amount2); |
| 138 | + |
| 139 | + vm.prank(_depositor1); |
| 140 | + uint256 _actualReward1 = staker.claimReward(_depositId1); |
| 141 | + |
| 142 | + vm.prank(_depositor2); |
| 143 | + uint256 _actualReward2 = staker.claimReward(_depositId2); |
| 144 | + |
| 145 | + assertEq(0, _actualReward1); |
| 146 | + assertApproxEqAbs(_calculatedRewards2, _actualReward2, 1); |
| 147 | + } |
| 148 | +} |
0 commit comments