Skip to content

Commit

Permalink
Merge pull request #10 from GalloDaSballo/fix-curve-claim
Browse files Browse the repository at this point in the history
Fix: Curve Claim Flow + Tests + Griefing
  • Loading branch information
GalloDaSballo authored Oct 10, 2024
2 parents fc5bff2 + c6e9dfd commit 1dcfdfd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 14 deletions.
28 changes: 22 additions & 6 deletions src/CurveV2GaugeRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,30 @@ contract CurveV2GaugeRewards is BribeInitiative {
duration = _duration;
}

function depositIntoGauge() external returns (uint256) {
uint256 amount = governance.claimForInitiative(address(this));
uint256 public remainder;

bold.approve(address(gauge), amount);
gauge.deposit_reward_token(address(bold), amount, duration);

emit DepositIntoGauge(amount);
/// @notice Governance transfers Bold, and we deposit it into the gauge
/// @dev Doing this allows anyone to trigger the claim
function onClaimForInitiative(uint16, uint256 _bold) external override onlyGovernance {
_depositIntoGauge(_bold);
}

function _depositIntoGauge(uint256 amount) internal {

// For small donations queue them into the contract
if(amount < duration * 1000) {
remainder += amount;
return;
}

uint256 total = amount + remainder;
remainder = 0;

return amount;
bold.approve(address(gauge), total);
gauge.deposit_reward_token(address(bold), total, duration);

emit DepositIntoGauge(total);
}

}
2 changes: 2 additions & 0 deletions src/UniV4Donations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ contract UniV4Donations is BribeInitiative, BaseHook {
}

function _donateToPool() internal returns (uint256) {
/// @audit TODO: Need to use storage value here I think
/// TODO: Test and fix release speed, which looks off
Vesting memory _vesting = _restartVesting(uint240(governance.claimForInitiative(address(this))));
uint256 amount =
(_vesting.amount * (block.timestamp - vestingEpochStart()) / VESTING_EPOCH_DURATION) - _vesting.released;
Expand Down
61 changes: 53 additions & 8 deletions test/CurveV2GaugeRewards.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ contract CurveV2GaugeRewardsTest is Test {
ILiquidityGauge private gauge;
CurveV2GaugeRewards private curveV2GaugeRewards;

address mockGovernance = address(0x123123);

function setUp() public {
vm.createSelectFork(vm.rpcUrl("mainnet"), 20430000);

Expand All @@ -68,7 +70,7 @@ contract CurveV2GaugeRewardsTest is Test {

curveV2GaugeRewards = new CurveV2GaugeRewards(
// address(vm.computeCreateAddress(address(this), vm.getNonce(address(this)) + 1)),
address(new MockGovernance()),
address(mockGovernance),
address(lusd),
address(lqty),
address(gauge),
Expand Down Expand Up @@ -117,14 +119,57 @@ contract CurveV2GaugeRewardsTest is Test {
vm.stopPrank();
}

function test_depositIntoGauge() public {
vm.startPrank(lusdHolder);
lusd.transfer(address(curveV2GaugeRewards), 1000e18);
vm.stopPrank();
function test_claimAndDepositIntoGaugeFuzz(uint128 amt) public {
deal(address(lusd), mockGovernance, amt);
vm.assume(amt > 604800);


// Pretend a Proposal has passed
vm.startPrank(
address(mockGovernance)
);
lusd.transfer(address(curveV2GaugeRewards), amt);

assertEq(lusd.balanceOf(address(curveV2GaugeRewards)), amt);
curveV2GaugeRewards.onClaimForInitiative(0, amt);
assertEq(lusd.balanceOf(address(curveV2GaugeRewards)), curveV2GaugeRewards.remainder());
}

/// @dev If the amount rounds down below 1 per second it reverts
function test_claimAndDepositIntoGaugeGrief() public {
uint256 amt = 604800 - 1;
deal(address(lusd), mockGovernance, amt);


// Pretend a Proposal has passed
vm.startPrank(
address(mockGovernance)
);
lusd.transfer(address(curveV2GaugeRewards), amt);

assertEq(lusd.balanceOf(address(curveV2GaugeRewards)), amt);
curveV2GaugeRewards.onClaimForInitiative(0, amt);
assertEq(lusd.balanceOf(address(curveV2GaugeRewards)), curveV2GaugeRewards.remainder());
}


/// @dev Fuzz test that shows that given a total = amt + dust, the dust is lost permanently
function test_noDustGriefFuzz(uint128 amt, uint128 dust) public {
uint256 total = uint256(amt) + uint256(dust);
deal(address(lusd), mockGovernance, total);


vm.mockCall(
address(governance), abi.encode(IGovernance.claimForInitiative.selector), abi.encode(uint256(1000e18))
// Pretend a Proposal has passed
vm.startPrank(
address(mockGovernance)
);
curveV2GaugeRewards.depositIntoGauge();
// Dust amount
lusd.transfer(address(curveV2GaugeRewards), amt);
// Rest
lusd.transfer(address(curveV2GaugeRewards), dust);

assertEq(lusd.balanceOf(address(curveV2GaugeRewards)), total);
curveV2GaugeRewards.onClaimForInitiative(0, amt);
assertEq(lusd.balanceOf(address(curveV2GaugeRewards)), curveV2GaugeRewards.remainder() + dust);
}
}

0 comments on commit 1dcfdfd

Please sign in to comment.