Skip to content

Commit

Permalink
fix: [hexen-r9] add overflow check for bin liquidity
Browse files Browse the repository at this point in the history
  • Loading branch information
chefburger committed Nov 11, 2024
1 parent 15b5220 commit 8c00656
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142478
142475
Original file line number Diff line number Diff line change
@@ -1 +1 @@
188410
188265
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
311254
311495
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
189451
189959
2 changes: 1 addition & 1 deletion .forge-snapshots/BinMintBurnFeeHookTest#test_Mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
410336
410577
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23287
23548
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118691
118546
Original file line number Diff line number Diff line change
@@ -1 +1 @@
968475
970284
Original file line number Diff line number Diff line change
@@ -1 +1 @@
327787
329605
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337511
337752
Original file line number Diff line number Diff line change
@@ -1 +1 @@
140062
140304
Original file line number Diff line number Diff line change
@@ -1 +1 @@
173098
177927
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179126
184209
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133129
133637
Original file line number Diff line number Diff line change
@@ -1 +1 @@
304550
304791
26 changes: 22 additions & 4 deletions src/pool-bin/libraries/BinPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ library BinPool {
error BinPool__NoLiquidityToReceiveFees();
/// @dev if swap exactIn, x for y, unspecifiedToken = token y. if swap x for exact out y, unspecified token is x
error BinPool__InsufficientAmountUnSpecified();
error BinPool__MaxLiquidityPerBinExceeded();

/// @dev The state of a pool
struct State {
Expand Down Expand Up @@ -168,7 +169,15 @@ library BinPool {
amountsInWithFees = amountsInWithFees.sub(pFee);
}

self.reserveOfBin[swapState.activeId] = binReserves.add(amountsInWithFees).sub(amountsOutOfBin);
bytes32 newReserve = binReserves.add(amountsInWithFees).sub(amountsOutOfBin);
if (
newReserve.getLiquidity(swapState.activeId.getPriceFromId(params.binStep))
> Constants.MAX_LIQUIDITY_PER_BIN
) {
revert BinPool__MaxLiquidityPerBinExceeded();
}

self.reserveOfBin[swapState.activeId] = newReserve;
}
}

Expand Down Expand Up @@ -347,9 +356,13 @@ library BinPool {

/// @dev overflow check on total reserves and the resulting liquidity
uint256 price = activeId.getPriceFromId(binStep);
binReserves.add(amountIn).getLiquidity(price);
bytes32 newReserves = binReserves.add(amountIn);
uint256 liquidity = newReserves.getLiquidity(price);
if (liquidity > Constants.MAX_LIQUIDITY_PER_BIN) {
revert BinPool__MaxLiquidityPerBinExceeded();
}

self.reserveOfBin[activeId] = binReserves.add(amountIn);
self.reserveOfBin[activeId] = newReserves;
result = toBalanceDelta(-(amount0.safeInt128()), -(amount1.safeInt128()));
}

Expand Down Expand Up @@ -457,7 +470,12 @@ library BinPool {
if (shares == 0 || amountsInToBin == 0) revert BinPool__ZeroShares(id);
if (supply == 0) _addBinIdToTree(self, id);

self.reserveOfBin[id] = binReserves.add(amountsInToBin);
bytes32 newReserves = binReserves.add(amountsInToBin);
if (newReserves.getLiquidity(price) > Constants.MAX_LIQUIDITY_PER_BIN) {
revert BinPool__MaxLiquidityPerBinExceeded();
}

self.reserveOfBin[id] = newReserves;
}

/// @notice Subtract share from user's position and update total share supply of bin
Expand Down
4 changes: 4 additions & 0 deletions src/pool-bin/libraries/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ library Constants {
uint256 internal constant SQUARED_PRECISION = PRECISION * PRECISION;

uint256 internal constant BASIS_POINT_MAX = 10_000;

// (2^256 - 1) / (2 * log(2**128) / log(1.0001))
uint256 internal constant MAX_LIQUIDITY_PER_BIN =
65251743116719673010965625540244653191619923014385985379600384103134737;
}
11 changes: 11 additions & 0 deletions test/pool-bin/libraries/BinPoolDonate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import {PackedUint128Math} from "../../../src/pool-bin/libraries/math/PackedUint
import {SafeCast} from "../../../src/pool-bin/libraries/math/SafeCast.sol";
import {BinPoolParametersHelper} from "../../../src/pool-bin/libraries/BinPoolParametersHelper.sol";
import {BinTestHelper} from "../helpers/BinTestHelper.sol";
import {Constants} from "../../../src/pool-bin/libraries/Constants.sol";
import {PriceHelper} from "../../../src/pool-bin/libraries/PriceHelper.sol";

contract BinPoolDonateTest is BinTestHelper {
using PackedUint128Math for bytes32;
using BinPoolParametersHelper for bytes32;
using SafeCast for uint256;
using BinHelper for bytes32;

MockVault public vault;
BinPoolManager public poolManager;
Expand Down Expand Up @@ -119,6 +122,14 @@ contract BinPoolDonateTest is BinTestHelper {
addLiquidityToBin(key, poolManager, bob, activeId, 1e18, 1e18, 1e18, 1e18, "");
poolManager.getPosition(poolId, bob, activeId, 0).share;

bytes32 newReserves = PackedUint128Math.encode(1e18 + amt0, 1e18 + amt1);
uint256 price = PriceHelper.getPriceFromId(activeId, poolParam.getBinStep());
if (newReserves.getLiquidity(price) > Constants.MAX_LIQUIDITY_PER_BIN) {
vm.expectRevert(BinPool.BinPool__MaxLiquidityPerBinExceeded.selector);
poolManager.donate(key, amt0, amt1, "");
return;
}

poolManager.donate(key, amt0, amt1, "");

// Verify reserve after donate
Expand Down

0 comments on commit 8c00656

Please sign in to comment.