From f9d5e3b0fd0c97d785215fd6f264f33072595c0d Mon Sep 17 00:00:00 2001 From: ChefMist <133624774+ChefMist@users.noreply.github.com> Date: Fri, 28 Jun 2024 16:20:45 +0800 Subject: [PATCH] feat: incldue bin liquidity in getBin --- .../BinHookTest#testBurnSucceedsWithHook.snap | 2 +- ...inHookTest#testDonateSucceedsWithHook.snap | 2 +- ...okTest#testInitializeSucceedsWithHook.snap | 2 +- .../BinHookTest#testMintSucceedsWithHook.snap | 2 +- .../BinHookTest#testSwapSucceedsWithHook.snap | 2 +- .../BinPoolManagerBytecodeSize.snap | 2 +- ...oolManagerTest#testBurnNativeCurrency.snap | 2 +- ...anagerTest#testFuzzUpdateDynamicLPFee.snap | 2 +- ...BinPoolManagerTest#testGasBurnHalfBin.snap | 2 +- ...inPoolManagerTest#testGasBurnNineBins.snap | 2 +- .../BinPoolManagerTest#testGasBurnOneBin.snap | 2 +- .../BinPoolManagerTest#testGasDonate.snap | 2 +- .../BinPoolManagerTest#testGasGetBin.snap | 1 + ...nPoolManagerTest#testGasMintNneBins-1.snap | 2 +- ...nPoolManagerTest#testGasMintNneBins-2.snap | 2 +- ...inPoolManagerTest#testGasMintOneBin-1.snap | 2 +- ...inPoolManagerTest#testGasMintOneBin-2.snap | 2 +- ...olManagerTest#testGasSwapMultipleBins.snap | 2 +- ...nagerTest#testGasSwapOverBigBinIdGate.snap | 2 +- ...nPoolManagerTest#testGasSwapSingleBin.snap | 2 +- ...#testInitialize_gasCheck_withoutHooks.snap | 2 +- ...oolManagerTest#testMintNativeCurrency.snap | 2 +- ...BinPoolManagerTest#testSetProtocolFee.snap | 2 +- .../CLPoolManagerBytecodeSize.snap | 2 +- ...oolManagerTest#initializeWithoutHooks.snap | 2 +- src/pool-bin/BinPoolManager.sol | 5 ++- src/pool-bin/interfaces/IBinPoolManager.sol | 6 ++- src/pool-bin/libraries/BinPool.sol | 13 ++++++- test/pool-bin/BinHookReturnsDelta.t.sol | 8 ++-- test/pool-bin/BinPoolManager.t.sol | 39 ++++++++++++++++--- test/pool-bin/libraries/BinPoolDonate.t.sol | 8 ++-- .../pool-bin/libraries/BinPoolLiquidity.t.sol | 4 +- 32 files changed, 87 insertions(+), 45 deletions(-) create mode 100644 .forge-snapshots/BinPoolManagerTest#testGasGetBin.snap diff --git a/.forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap b/.forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap index e5ae9359..91400cc7 100644 --- a/.forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap +++ b/.forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap @@ -1 +1 @@ -178510 \ No newline at end of file +178537 \ No newline at end of file diff --git a/.forge-snapshots/BinHookTest#testDonateSucceedsWithHook.snap b/.forge-snapshots/BinHookTest#testDonateSucceedsWithHook.snap index a9c414a9..d9bce25b 100644 --- a/.forge-snapshots/BinHookTest#testDonateSucceedsWithHook.snap +++ b/.forge-snapshots/BinHookTest#testDonateSucceedsWithHook.snap @@ -1 +1 @@ -181771 \ No newline at end of file +181798 \ No newline at end of file diff --git a/.forge-snapshots/BinHookTest#testInitializeSucceedsWithHook.snap b/.forge-snapshots/BinHookTest#testInitializeSucceedsWithHook.snap index b48bc474..9df4bbaf 100644 --- a/.forge-snapshots/BinHookTest#testInitializeSucceedsWithHook.snap +++ b/.forge-snapshots/BinHookTest#testInitializeSucceedsWithHook.snap @@ -1 +1 @@ -247511 +247538 \ No newline at end of file diff --git a/.forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap b/.forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap index 62809a53..eb6a7fa5 100644 --- a/.forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap +++ b/.forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap @@ -1 +1 @@ -328286 \ No newline at end of file +328313 \ No newline at end of file diff --git a/.forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap b/.forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap index 8b63c1bc..f358d90a 100644 --- a/.forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap +++ b/.forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap @@ -1 +1 @@ -189314 \ No newline at end of file +189341 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerBytecodeSize.snap b/.forge-snapshots/BinPoolManagerBytecodeSize.snap index 1572975a..6862e3e7 100644 --- a/.forge-snapshots/BinPoolManagerBytecodeSize.snap +++ b/.forge-snapshots/BinPoolManagerBytecodeSize.snap @@ -1 +1 @@ -24762 +24980 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap b/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap index d2693889..22b37f39 100644 --- a/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap +++ b/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap @@ -1 +1 @@ -133576 \ No newline at end of file +133598 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testFuzzUpdateDynamicLPFee.snap b/.forge-snapshots/BinPoolManagerTest#testFuzzUpdateDynamicLPFee.snap index 01b4887a..b390edb0 100644 --- a/.forge-snapshots/BinPoolManagerTest#testFuzzUpdateDynamicLPFee.snap +++ b/.forge-snapshots/BinPoolManagerTest#testFuzzUpdateDynamicLPFee.snap @@ -1 +1 @@ -32258 \ No newline at end of file +32285 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap index 70c95ed9..c695522c 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap @@ -1 +1 @@ -142340 \ No newline at end of file +142367 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap index 1768679d..c65be605 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap @@ -1 +1 @@ -288101 \ No newline at end of file +288123 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap index b2cc8d2f..eeef9d39 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap @@ -1 +1 @@ -126681 \ No newline at end of file +126702 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasDonate.snap b/.forge-snapshots/BinPoolManagerTest#testGasDonate.snap index ecbbbad2..5ce609f8 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasDonate.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasDonate.snap @@ -1 +1 @@ -114379 \ No newline at end of file +114406 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasGetBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasGetBin.snap new file mode 100644 index 00000000..01488fde --- /dev/null +++ b/.forge-snapshots/BinPoolManagerTest#testGasGetBin.snap @@ -0,0 +1 @@ +3584 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-1.snap b/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-1.snap index 6d6bbe41..ca43c4be 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-1.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-1.snap @@ -1 +1 @@ -967549 \ No newline at end of file +967576 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-2.snap b/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-2.snap index 5710d86c..47747aee 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-2.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-2.snap @@ -1 +1 @@ -325766 \ No newline at end of file +325793 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-1.snap b/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-1.snap index 607525b7..5d4b1d8a 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-1.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-1.snap @@ -1 +1 @@ -337248 \ No newline at end of file +337275 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-2.snap b/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-2.snap index 9e1d1c59..5371dc37 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-2.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-2.snap @@ -1 +1 @@ -139650 \ No newline at end of file +139677 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap index 0f44a535..99964826 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap @@ -1 +1 @@ -171614 \ No newline at end of file +171641 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap index 8d2927e1..39cf23d6 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap @@ -1 +1 @@ -177495 \ No newline at end of file +177522 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap index c8ab0679..030e68c7 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap @@ -1 +1 @@ -132481 \ No newline at end of file +132508 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testInitialize_gasCheck_withoutHooks.snap b/.forge-snapshots/BinPoolManagerTest#testInitialize_gasCheck_withoutHooks.snap index feb78d4b..93865901 100644 --- a/.forge-snapshots/BinPoolManagerTest#testInitialize_gasCheck_withoutHooks.snap +++ b/.forge-snapshots/BinPoolManagerTest#testInitialize_gasCheck_withoutHooks.snap @@ -1 +1 @@ -162167 \ No newline at end of file +162194 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testMintNativeCurrency.snap b/.forge-snapshots/BinPoolManagerTest#testMintNativeCurrency.snap index 8d976032..a16d2168 100644 --- a/.forge-snapshots/BinPoolManagerTest#testMintNativeCurrency.snap +++ b/.forge-snapshots/BinPoolManagerTest#testMintNativeCurrency.snap @@ -1 +1 @@ -304205 \ No newline at end of file +304232 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testSetProtocolFee.snap b/.forge-snapshots/BinPoolManagerTest#testSetProtocolFee.snap index 8830ba11..7ccf6571 100644 --- a/.forge-snapshots/BinPoolManagerTest#testSetProtocolFee.snap +++ b/.forge-snapshots/BinPoolManagerTest#testSetProtocolFee.snap @@ -1 +1 @@ -34341 \ No newline at end of file +34368 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerBytecodeSize.snap b/.forge-snapshots/CLPoolManagerBytecodeSize.snap index 6981f87e..4cbe3896 100644 --- a/.forge-snapshots/CLPoolManagerBytecodeSize.snap +++ b/.forge-snapshots/CLPoolManagerBytecodeSize.snap @@ -1 +1 @@ -24839 \ No newline at end of file +24829 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#initializeWithoutHooks.snap b/.forge-snapshots/CLPoolManagerTest#initializeWithoutHooks.snap index d43a864a..e65bdc36 100644 --- a/.forge-snapshots/CLPoolManagerTest#initializeWithoutHooks.snap +++ b/.forge-snapshots/CLPoolManagerTest#initializeWithoutHooks.snap @@ -1 +1 @@ -149801 \ No newline at end of file +149782 \ No newline at end of file diff --git a/src/pool-bin/BinPoolManager.sol b/src/pool-bin/BinPoolManager.sol index 4d83e74e..01e29f29 100644 --- a/src/pool-bin/BinPoolManager.sol +++ b/src/pool-bin/BinPoolManager.sol @@ -66,9 +66,10 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload { external view override - returns (uint128 binReserveX, uint128 binReserveY) + returns (uint128 binReserveX, uint128 binReserveY, uint256 binLiquidity) { - (binReserveX, binReserveY) = pools[id].getBin(binId); + PoolKey memory key = poolIdToPoolKey[id]; + (binReserveX, binReserveY, binLiquidity) = pools[id].getBin(key.parameters.getBinStep(), binId); } /// @inheritdoc IBinPoolManager diff --git a/src/pool-bin/interfaces/IBinPoolManager.sol b/src/pool-bin/interfaces/IBinPoolManager.sol index 92e8f76f..ebd9d73c 100644 --- a/src/pool-bin/interfaces/IBinPoolManager.sol +++ b/src/pool-bin/interfaces/IBinPoolManager.sol @@ -130,7 +130,11 @@ interface IBinPoolManager is IProtocolFees, IPoolManager, IExtsload { /// @param id The id of the bin /// @return binReserveX The reserve of token X in the bin /// @return binReserveY The reserve of token Y in the bin - function getBin(PoolId id, uint24 binId) external view returns (uint128 binReserveX, uint128 binReserveY); + /// @return binLiquidity The liquidity in the bin + function getBin(PoolId id, uint24 binId) + external + view + returns (uint128 binReserveX, uint128 binReserveY, uint256 binLiquidity); /// @notice Returns the positon of owner at a binId /// @param id The id of PoolKey diff --git a/src/pool-bin/libraries/BinPool.sol b/src/pool-bin/libraries/BinPool.sol index a89d4863..f4334afa 100644 --- a/src/pool-bin/libraries/BinPool.sol +++ b/src/pool-bin/libraries/BinPool.sol @@ -257,11 +257,20 @@ library BinPool { } /// @notice Returns the reserves of a bin + /// @param binStep The binStep of the bin /// @param id The id of the bin /// @return binReserveX The reserve of token X in the bin /// @return binReserveY The reserve of token Y in the bin - function getBin(State storage self, uint24 id) internal view returns (uint128 binReserveX, uint128 binReserveY) { - (binReserveX, binReserveY) = self.reserveOfBin[id].decode(); + /// @return binLiquidity The liquidity in the bin + function getBin(State storage self, uint16 binStep, uint24 id) + internal + view + returns (uint128 binReserveX, uint128 binReserveY, uint256 binLiquidity) + { + bytes32 binReserves = self.reserveOfBin[id]; + + (binReserveX, binReserveY) = binReserves.decode(); + binLiquidity = binReserves.getLiquidity(id.getPriceFromId(binStep)); } /// @dev Returns next non-empty bin diff --git a/test/pool-bin/BinHookReturnsDelta.t.sol b/test/pool-bin/BinHookReturnsDelta.t.sol index 827c4bca..8e3607a3 100644 --- a/test/pool-bin/BinHookReturnsDelta.t.sol +++ b/test/pool-bin/BinHookReturnsDelta.t.sol @@ -100,10 +100,10 @@ contract BinHookReturnsDelta is Test, GasSnapshot, BinTestHelper { function testMint_MintMore() external { IBinPoolManager.MintParams memory mintParams = _getSingleBinMintParams(activeId, 1 ether, 1 ether); BalanceDelta delta = binLiquidityHelper.mint(key, mintParams, abi.encode(0)); - (uint128 reserveXBefore, uint128 reserveYBefore) = poolManager.getBin(key.toId(), activeId); + (uint128 reserveXBefore, uint128 reserveYBefore,) = poolManager.getBin(key.toId(), activeId); BalanceDelta delta2 = binLiquidityHelper.mint(key, mintParams, abi.encode(mintParams.amountIn)); - (uint128 reserveXAfter, uint128 reserveYAfter) = poolManager.getBin(key.toId(), activeId); + (uint128 reserveXAfter, uint128 reserveYAfter,) = poolManager.getBin(key.toId(), activeId); assertEq(reserveXAfter - reserveXBefore, 2 * reserveXBefore); assertEq(reserveYAfter - reserveYBefore, 2 * reserveYBefore); @@ -116,7 +116,7 @@ contract BinHookReturnsDelta is Test, GasSnapshot, BinTestHelper { IBinPoolManager.MintParams memory mintParams = _getSingleBinMintParams(activeId, 1 ether, 1 ether); binLiquidityHelper.mint(key, mintParams, abi.encode(0)); - (uint128 reserveXBefore, uint128 reserveYBefore) = poolManager.getBin(key.toId(), activeId); + (uint128 reserveXBefore, uint128 reserveYBefore,) = poolManager.getBin(key.toId(), activeId); assertEq(reserveXBefore, 1 ether); assertEq(reserveYBefore, 1 ether); @@ -128,7 +128,7 @@ contract BinHookReturnsDelta is Test, GasSnapshot, BinTestHelper { binLiquidityHelper.burn(key, burnParams, ""); - (uint128 reserveXAfter, uint128 reserveYAfter) = poolManager.getBin(key.toId(), activeId); + (uint128 reserveXAfter, uint128 reserveYAfter,) = poolManager.getBin(key.toId(), activeId); assertEq(reserveXAfter, 0); assertEq(reserveYAfter, 0); diff --git a/test/pool-bin/BinPoolManager.t.sol b/test/pool-bin/BinPoolManager.t.sol index 7426676a..fee0983e 100644 --- a/test/pool-bin/BinPoolManager.t.sol +++ b/test/pool-bin/BinPoolManager.t.sol @@ -36,6 +36,8 @@ import {BinTestHelper} from "./helpers/BinTestHelper.sol"; import {Hooks} from "../../src/libraries/Hooks.sol"; import {ParametersHelper} from "../../src/libraries/math/ParametersHelper.sol"; import {BinPosition} from "../../src/pool-bin/libraries/BinPosition.sol"; +import {PriceHelper} from "../../src/pool-bin/libraries/PriceHelper.sol"; +import {BinHelper} from "../../src/pool-bin/libraries/BinHelper.sol"; contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper { using PoolIdLibrary for PoolKey; @@ -43,6 +45,8 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper { using PackedUint128Math for bytes32; using PackedUint128Math for uint128; using BinPoolParametersHelper for bytes32; + using PriceHelper for uint24; + using BinHelper for bytes32; error PoolAlreadyInitialized(); error PoolNotInitialized(); @@ -408,7 +412,7 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper { // liquidity added with salt 0x1234 not salt 0 for (uint256 i = 0; i < binIds.length; i++) { - (uint128 binReserveX, uint128 binReserveY) = poolManager.getBin(key.toId(), binIds[i]); + (uint128 binReserveX, uint128 binReserveY,) = poolManager.getBin(key.toId(), binIds[i]); // make sure the liquidity is added to the correct bin if (binIds[i] < activeId) { @@ -437,7 +441,7 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper { binLiquidityHelper.burn(key, burnParams, ""); for (uint256 i = 0; i < binIds.length; i++) { - (uint128 binReserveX, uint128 binReserveY) = poolManager.getBin(key.toId(), binIds[i]); + (uint128 binReserveX, uint128 binReserveY,) = poolManager.getBin(key.toId(), binIds[i]); // make sure the liquidity is added to the correct bin assertEq(binReserveX, 0 ether); @@ -467,7 +471,7 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper { // liquidity added with salt 0x1234 not salt 0 for (uint256 i = 0; i < binIds.length; i++) { - (uint128 binReserveX, uint128 binReserveY) = poolManager.getBin(key.toId(), binIds[i]); + (uint128 binReserveX, uint128 binReserveY,) = poolManager.getBin(key.toId(), binIds[i]); // make sure the liquidity is added to the correct bin if (binIds[i] < activeId) { @@ -499,7 +503,7 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper { binLiquidityHelper.mint(key, mintParams, ""); for (uint256 i = 0; i < binIds.length; i++) { - (uint128 binReserveX, uint128 binReserveY) = poolManager.getBin(key.toId(), binIds[i]); + (uint128 binReserveX, uint128 binReserveY,) = poolManager.getBin(key.toId(), binIds[i]); // make sure the liquidity is added to the correct bin if (binIds[i] < activeId) { @@ -532,7 +536,7 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper { binLiquidityHelper.mint(key, mintParams, ""); for (uint256 i = 0; i < binIds.length; i++) { - (uint128 binReserveX, uint128 binReserveY) = poolManager.getBin(key.toId(), binIds[i]); + (uint128 binReserveX, uint128 binReserveY,) = poolManager.getBin(key.toId(), binIds[i]); // make sure the liquidity is added to the correct bin if (binIds[i] < activeId) { @@ -565,7 +569,7 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper { binLiquidityHelper.burn(key, burnParams, ""); for (uint256 i = 0; i < binIds.length; i++) { - (uint128 binReserveX, uint128 binReserveY) = poolManager.getBin(key.toId(), binIds[i]); + (uint128 binReserveX, uint128 binReserveY,) = poolManager.getBin(key.toId(), binIds[i]); // make sure the liquidity is added to the correct bin if (binIds[i] < activeId) { @@ -1119,6 +1123,29 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper { binDonateHelper.donate(key, 10 ether, 10 ether, ""); } + function testGasGetBin() public { + // Initialize and add 1e18 token0, token1 to the active bin. price of bin: 2**128, 3.4e38 + poolManager.initialize(key, activeId, new bytes(0)); + + // add 1 eth of tokenX and 1 eth of tokenY liquidity at activeId + token0.mint(address(this), 1 ether); + token1.mint(address(this), 1 ether); + IBinPoolManager.MintParams memory mintParams = _getSingleBinMintParams(activeId, 1 ether, 1 ether); + binLiquidityHelper.mint(key, mintParams, ""); + + snapStart("BinPoolManagerTest#testGasGetBin"); + (uint128 reserveX, uint128 reserveY, uint256 liquidity) = poolManager.getBin(key.toId(), activeId); + snapEnd(); + + assertEq(reserveX, 1e18); + assertEq(reserveY, 1e18); + + bytes32 binReserves = reserveX.encode(reserveY); + uint16 binStep = key.parameters.getBinStep(); + uint256 binLiquidity = binReserves.getLiquidity(activeId.getPriceFromId(binStep)); + assertEq(liquidity, binLiquidity); + } + receive() external payable {} function supportsInterface(bytes4) external pure returns (bool) { diff --git a/test/pool-bin/libraries/BinPoolDonate.t.sol b/test/pool-bin/libraries/BinPoolDonate.t.sol index 09a2231a..278fae85 100644 --- a/test/pool-bin/libraries/BinPoolDonate.t.sol +++ b/test/pool-bin/libraries/BinPoolDonate.t.sol @@ -74,7 +74,7 @@ contract BinPoolDonateTest is BinTestHelper { // Verify reserve before donate uint128 reserveX; uint128 reserveY; - (reserveX, reserveY) = poolManager.getBin(poolId, activeId); + (reserveX, reserveY,) = poolManager.getBin(poolId, activeId); assertEq(reserveX, 2e18); assertEq(reserveY, 2e18); @@ -82,7 +82,7 @@ contract BinPoolDonateTest is BinTestHelper { poolManager.donate(key, 2e18, 2e18, ""); // Verify reserve after donate - (reserveX, reserveY) = poolManager.getBin(poolId, activeId); + (reserveX, reserveY,) = poolManager.getBin(poolId, activeId); assertEq(reserveX, 4e18); assertEq(reserveY, 4e18); @@ -96,7 +96,7 @@ contract BinPoolDonateTest is BinTestHelper { assertEq(removeDelta2.amount1(), 2e18); // Verify no reserve remaining - (reserveX, reserveY) = poolManager.getBin(poolId, activeId); + (reserveX, reserveY,) = poolManager.getBin(poolId, activeId); assertEq(reserveX, 0); assertEq(reserveY, 0); @@ -115,7 +115,7 @@ contract BinPoolDonateTest is BinTestHelper { poolManager.donate(key, amt0, amt1, ""); // Verify reserve after donate - (uint128 reserveX, uint128 reserveY) = poolManager.getBin(poolId, activeId); + (uint128 reserveX, uint128 reserveY,) = poolManager.getBin(poolId, activeId); assertEq(reserveX, 1e18 + amt0); assertEq(reserveY, 1e18 + amt1); } diff --git a/test/pool-bin/libraries/BinPoolLiquidity.t.sol b/test/pool-bin/libraries/BinPoolLiquidity.t.sol index f1a60a2a..31512990 100644 --- a/test/pool-bin/libraries/BinPoolLiquidity.t.sol +++ b/test/pool-bin/libraries/BinPoolLiquidity.t.sol @@ -81,7 +81,7 @@ contract BinPoolLiquidityTest is BinTestHelper { for (uint256 i; i < total; ++i) { uint24 id = getId(activeId, i, nbBinY); - (uint128 binReserveX, uint128 binReserveY) = poolManager.getBin(poolId, id); + (uint128 binReserveX, uint128 binReserveY,) = poolManager.getBin(poolId, id); if (id < activeId) { assertEq(binReserveX, 0, "test_SimpleMint::3"); @@ -152,7 +152,7 @@ contract BinPoolLiquidityTest is BinTestHelper { uint24 id = getId(activeId, i, nbBinY); // (uint128 binReserveX, uint128 binReserveY) = pairWnative.getBin(id); - (uint128 binReserveX, uint128 binReserveY) = poolManager.getBin(poolId, id); + (uint128 binReserveX, uint128 binReserveY,) = poolManager.getBin(poolId, id); if (id < activeId) { assertEq(binReserveX, 0, "test_SimpleMint::1");