Skip to content

Commit

Permalink
fix: issue-1: precision issue in getPendingDistributionAmount (#78)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin <benjaminxh+github@gmail.com>
  • Loading branch information
ksatyarth2 and bxmmm1 authored Nov 26, 2024
1 parent d6a54d4 commit e6352a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mainnet-contracts/src/PufferRevenueDepositor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ contract PufferRevenueDepositor is
UUPSUpgradeable
{
using EnumerableSet for EnumerableSet.AddressSet;
using Math for uint256;

/**
* @notice The maximum rewards distribution window.
Expand Down Expand Up @@ -94,7 +95,7 @@ contract PufferRevenueDepositor is
uint256 timePassed = block.timestamp - $.lastDepositTimestamp;
uint256 remainingTime = rewardsDistributionWindow - Math.min(timePassed, rewardsDistributionWindow);

return $.lastDepositAmount * remainingTime / rewardsDistributionWindow;
return Math.mulDiv(uint256($.lastDepositAmount), remainingTime, rewardsDistributionWindow, Math.Rounding.Ceil);
}

/**
Expand Down
18 changes: 17 additions & 1 deletion mainnet-contracts/test/unit/PufferRevenueDepositorTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,16 @@ contract PufferRevenueDepositorTest is UnitTestHelper {

assertEq(revenueDepositor.getPendingDistributionAmount(), 1, "Pending distribution amount should be 1");

// After half of the distribution window, the pending distribution amount is half of the total amount
// After half of the distribution window, the pending distribution amount should still be 1 due to rounding up
vm.warp(block.timestamp + 12 hours);
assertEq(
revenueDepositor.getPendingDistributionAmount(),
1,
"Pending distribution amount should be 1 due to rounding up"
);

// Only after the full distribution window should it become 0
vm.warp(block.timestamp + 12 hours);
assertEq(revenueDepositor.getPendingDistributionAmount(), 0, "Pending distribution amount should be 0");
}

Expand Down Expand Up @@ -204,6 +211,15 @@ contract PufferRevenueDepositorTest is UnitTestHelper {
revenueDepositor.callTargets(targets, data);
}

function test_smallRewardsAmount_precisionLoss() public withRewardsDistributionWindow(1 days) {
vm.deal(address(revenueDepositor), 2); // 2 wei
vm.startPrank(OPERATIONS_MULTISIG);
revenueDepositor.depositRevenue();
assertEq(revenueDepositor.getPendingDistributionAmount(), 2, "Pending distribution amount should be 2");
vm.warp(block.timestamp + 1 seconds);
assertEq(revenueDepositor.getPendingDistributionAmount(), 2, "Pending distribution amount should be 2");
}

function testRevert_constructor_zeroAddressVault() public {
vm.expectRevert(IPufferRevenueDepositor.InvalidAddress.selector);
new PufferRevenueDepositor(
Expand Down

0 comments on commit e6352a0

Please sign in to comment.