Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: incldue bin liquidity in getBin for BinPool #101

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178510
178537
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181771
181798
Original file line number Diff line number Diff line change
@@ -1 +1 @@
247511
247538
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
328286
328313
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
189314
189341
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24762
24980
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133576
133598
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32258
32285
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142340
142367
Original file line number Diff line number Diff line change
@@ -1 +1 @@
288101
288123
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126681
126702
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114379
114406
1 change: 1 addition & 0 deletions .forge-snapshots/BinPoolManagerTest#testGasGetBin.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3584
Original file line number Diff line number Diff line change
@@ -1 +1 @@
967549
967576
Original file line number Diff line number Diff line change
@@ -1 +1 @@
325766
325793
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337248
337275
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139650
139677
Original file line number Diff line number Diff line change
@@ -1 +1 @@
171614
171641
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177495
177522
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132481
132508
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162167
162194
Original file line number Diff line number Diff line change
@@ -1 +1 @@
304205
304232
Original file line number Diff line number Diff line change
@@ -1 +1 @@
34341
34368
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24839
24829
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149801
149782
5 changes: 3 additions & 2 deletions src/pool-bin/BinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Copy link
Collaborator Author

@ChefMist ChefMist Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also imply we might need poolIdToPoolKey to stay since the param is poolId which does not include binStep

(binReserveX, binReserveY, binLiquidity) = pools[id].getBin(key.parameters.getBinStep(), binId);
}

/// @inheritdoc IBinPoolManager
Expand Down
6 changes: 5 additions & 1 deletion src/pool-bin/interfaces/IBinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions src/pool-bin/libraries/BinPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/pool-bin/BinHookReturnsDelta.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
39 changes: 33 additions & 6 deletions test/pool-bin/BinPoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ 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;
using SafeCast for uint256;
using PackedUint128Math for bytes32;
using PackedUint128Math for uint128;
using BinPoolParametersHelper for bytes32;
using PriceHelper for uint24;
using BinHelper for bytes32;

error PoolAlreadyInitialized();
error PoolNotInitialized();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions test/pool-bin/libraries/BinPoolDonate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ 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);

// Donate
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);

Expand All @@ -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);

Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions test/pool-bin/libraries/BinPoolLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
Loading