Skip to content

Commit

Permalink
feat: burn 1e3 shares for first mint into BinPool - part 2 (#215)
Browse files Browse the repository at this point in the history
* feat: example if we tweak add/remove bin to tree

* feat: updated gas cost

* feat: add test around getNextNonEmptyBin
  • Loading branch information
ChefMist authored Nov 14, 2024
1 parent 19ecc15 commit b2770c7
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137654
142478
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
196277
197607
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
311574
311580
2 changes: 1 addition & 1 deletion .forge-snapshots/BinMintBurnFeeHookTest#test_Burn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
184559
185889
2 changes: 1 addition & 1 deletion .forge-snapshots/BinMintBurnFeeHookTest#test_Mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
410526
410532
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23389
23404
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146654
147984
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142933
142994
Original file line number Diff line number Diff line change
@@ -1 +1 @@
287239
295835
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138120
139450
Original file line number Diff line number Diff line change
@@ -1 +1 @@
971323
971377
Original file line number Diff line number Diff line change
@@ -1 +1 @@
330068
330122
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337831
337837
Original file line number Diff line number Diff line change
@@ -1 +1 @@
140319
140325
Original file line number Diff line number Diff line change
@@ -1 +1 @@
304870
304876
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137102
109014
6 changes: 4 additions & 2 deletions src/pool-bin/libraries/BinPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ library BinPool {

binReserves = binReserves.sub(amountsOutFromBin);

if (supply == amountToBurn) _removeBinIdToTree(self, id);
/// @dev _removeBinIdToTree if supply is MINIMUM_SHARE after burning as min share is too low liquidity for trade anyway
if (supply - amountToBurn == MINIMUM_SHARE) _removeBinIdToTree(self, id);

self.reserveOfBin[id] = binReserves;
amounts[i] = amountsOutFromBin;
Expand Down Expand Up @@ -459,7 +460,8 @@ library BinPool {
}

if (shares == 0 || amountsInToBin == 0) revert BinPool__ZeroShares(id);
if (supply == 0) _addBinIdToTree(self, id);
/// @dev if supply was originally MINIMUM_SHARE (people added and remove liquidity before) or 0 (new bin), add binId to tree
if (supply <= MINIMUM_SHARE) _addBinIdToTree(self, id);

self.reserveOfBin[id] = binReserves.add(amountsInToBin);
}
Expand Down
63 changes: 63 additions & 0 deletions test/pool-bin/BinPoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,69 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper {
assertEq(shares, liquidity);
}

function test_getNextNonEmptyBin() public {
poolManager.initialize(key, activeId);

// add 1 eth of tokenX and 1 eth to activeId - 2 to active + 2 bins
token0.mint(address(this), 10 ether);
token1.mint(address(this), 10 ether);
(IBinPoolManager.MintParams memory mintParams,) = _getMultipleBinMintParams(activeId, 2 ether, 2 ether, 3, 3);
binLiquidityHelper.mint(key, mintParams, "");

// swapForY is true, means search for bin to the left as tokenY reside on the left side of the bin
bool swapForY = true;
for (uint24 i = 0; i < 5; i++) {
// [-2, -1, activeId, 1, 2] are bins initialized due to liqudiity adding above
uint24 nextEmptyBin = poolManager.getNextNonEmptyBin(key.toId(), swapForY, activeId + 3 - i);
assertEq(nextEmptyBin, activeId + 2 - i);
}

IBinPoolManager.BurnParams memory burnParams;

// burn activeId-1
burnParams = _getSingleBinBurnLiquidityParams(key, poolManager, activeId - 1, address(binLiquidityHelper), 100);
binLiquidityHelper.burn(key, burnParams, "");

// as activeId-1 bin is empty now, verify the next non empty bin to the left of activeId is activeId-2
assertEq(poolManager.getNextNonEmptyBin(key.toId(), true, activeId), activeId - 2);

// burn activeId+1
burnParams = _getSingleBinBurnLiquidityParams(key, poolManager, activeId + 1, address(binLiquidityHelper), 100);
binLiquidityHelper.burn(key, burnParams, "");

// as activeId+1 bin is empty now, verify the next non empty bin to the left of activeId+2 is activeId
assertEq(poolManager.getNextNonEmptyBin(key.toId(), true, activeId + 2), activeId);
}

function test_getNextNonEmptyBin_AddRemoveAddLiquidity() public {
// initialize
poolManager.initialize(key, activeId);

// mint, verify activeId is in treeMath
token0.mint(address(this), 2 ether);
token1.mint(address(this), 2 ether);
IBinPoolManager.MintParams memory mintParams = _getSingleBinMintParams(activeId, 1 ether, 1 ether);
binLiquidityHelper.mint(key, mintParams, "");
assertEq(poolManager.getNextNonEmptyBin(key.toId(), true, activeId + 1), activeId);

// remove, verify activeId not in treeMath
IBinPoolManager.BurnParams memory burnParams;
burnParams = _getSingleBinBurnLiquidityParams(key, poolManager, activeId, address(binLiquidityHelper), 100);
binLiquidityHelper.burn(key, burnParams, "");
assertEq(poolManager.getNextNonEmptyBin(key.toId(), true, activeId + 1), type(uint24).max);

// mint, verify activeId in treeMath again
binLiquidityHelper.mint(key, mintParams, "");
assertEq(poolManager.getNextNonEmptyBin(key.toId(), true, activeId + 1), activeId);
}

function test_getNextNonEmptyBin_NoBinWithLiqudiity() public {
poolManager.initialize(key, activeId);

assertEq(poolManager.getNextNonEmptyBin(key.toId(), true, activeId), type(uint24).max);
assertEq(poolManager.getNextNonEmptyBin(key.toId(), false, activeId), 0);
}

receive() external payable {}

function supportsInterface(bytes4) external pure returns (bool) {
Expand Down

0 comments on commit b2770c7

Please sign in to comment.