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: support getters for clpool to access pool state #26

Merged
merged 1 commit into from
May 7, 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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
348848
348884
Original file line number Diff line number Diff line change
@@ -1 +1 @@
59396
59432
Original file line number Diff line number Diff line change
@@ -1 +1 @@
242441
242477
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
82514
82478
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52476
52440
Original file line number Diff line number Diff line change
@@ -1 +1 @@
36547
36559
Original file line number Diff line number Diff line change
@@ -1 +1 @@
42374
42386
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54672
54614
Original file line number Diff line number Diff line change
@@ -1 +1 @@
100748
100690
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25040458
25040400
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
35872
35814
Original file line number Diff line number Diff line change
@@ -1 +1 @@
101510
101452
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
41522
41473
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
35875
35817
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4835
4857
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19218
19187
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37497
37517
Original file line number Diff line number Diff line change
@@ -1 +1 @@
29405
29418
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#testNoOp_gas_Swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21667
21614
2 changes: 1 addition & 1 deletion .forge-snapshots/ExtsloadTest#extsloadInBatch.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11352
11374
14 changes: 12 additions & 2 deletions src/pool-cl/CLPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {PoolId, PoolIdLibrary} from "../types/PoolId.sol";
import {BalanceDelta, BalanceDeltaLibrary} from "../types/BalanceDelta.sol";
import {Extsload} from "../Extsload.sol";
import {SafeCast} from "../libraries/SafeCast.sol";
import {CLPoolGetters} from "./libraries/CLPoolGetters.sol";

contract CLPoolManager is ICLPoolManager, Fees, Extsload {
using SafeCast for int256;
Expand All @@ -28,6 +29,7 @@ contract CLPoolManager is ICLPoolManager, Fees, Extsload {
using CLPoolParametersHelper for bytes32;
using CLPool for *;
using CLPosition for mapping(bytes32 => CLPosition.Info);
using CLPoolGetters for CLPool.State;

/// @inheritdoc ICLPoolManager
int24 public constant override MAX_TICK_SPACING = type(int16).max;
Expand Down Expand Up @@ -287,11 +289,19 @@ contract CLPoolManager is ICLPoolManager, Fees, Extsload {
}

function getPoolTickInfo(PoolId id, int24 tick) external view returns (Tick.Info memory) {
return pools[id].ticks[tick];
return pools[id].getPoolTickInfo(tick);
}

function getPoolBitmapInfo(PoolId id, int16 word) external view returns (uint256 tickBitmap) {
return pools[id].tickBitmap[word];
return pools[id].getPoolBitmapInfo(word);
}

function getFeeGrowthGlobals(PoolId id)
external
view
returns (uint256 feeGrowthGlobal0x128, uint256 feeGrowthGlobal1x128)
{
return pools[id].getFeeGrowthGlobals();
}

/// @inheritdoc IPoolManager
Expand Down
24 changes: 24 additions & 0 deletions src/pool-cl/libraries/CLPoolGetters.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2024 PancakeSwap
pragma solidity ^0.8.24;

import {CLPool} from "./CLPool.sol";
import {Tick} from "./Tick.sol";

library CLPoolGetters {
function getPoolTickInfo(CLPool.State storage pool, int24 tick) internal view returns (Tick.Info memory) {
return pool.ticks[tick];
}

function getPoolBitmapInfo(CLPool.State storage pool, int16 word) internal view returns (uint256 tickBitmap) {
return pool.tickBitmap[word];
}

function getFeeGrowthGlobals(CLPool.State storage pool)
internal
view
returns (uint256 feeGrowthGlobal0x128, uint256 feeGrowthGlobal1x128)
{
return (pool.feeGrowthGlobal0X128, pool.feeGrowthGlobal1X128);
}
}
158 changes: 158 additions & 0 deletions test/pool-cl/libraries/CLPoolGetters.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";
import {Test} from "forge-std/Test.sol";
import {CLPoolGetters} from "../../../src/pool-cl/libraries/CLPoolGetters.sol";
import {CLPool} from "../../../src/pool-cl/libraries/CLPool.sol";
import {Tick} from "../../../src/pool-cl/libraries/Tick.sol";

contract CLPoolGettersTest is Test, GasSnapshot {
CLPool.State pool;

using CLPoolGetters for CLPool.State;

function testGetPoolTickInfo() public {
// info stored for each initialized individual tick
// struct Info {
// uint128 liquidityGross;
// int128 liquidityNet;
// uint256 feeGrowthOutside0X128;
// uint256 feeGrowthOutside1X128;
// }

int24 tick = 5;
int24 randomTick = 15;

{
Tick.Info memory info = pool.getPoolTickInfo(tick);
assertEq(info.liquidityGross, 0);
assertEq(info.liquidityNet, 0);
assertEq(info.feeGrowthOutside0X128, 0);
assertEq(info.feeGrowthOutside1X128, 0);

pool.ticks[tick] = Tick.Info(100, 200, 300, 400);
info = pool.getPoolTickInfo(tick);
assertEq(info.liquidityGross, 100);
assertEq(info.liquidityNet, 200);
assertEq(info.feeGrowthOutside0X128, 300);
assertEq(info.feeGrowthOutside1X128, 400);

// access random tick
info = pool.getPoolTickInfo(randomTick);
assertEq(info.liquidityGross, 0);
assertEq(info.liquidityNet, 0);
assertEq(info.feeGrowthOutside0X128, 0);
assertEq(info.feeGrowthOutside1X128, 0);

// tick clear
delete pool.ticks[tick];
info = pool.getPoolTickInfo(tick);
assertEq(info.liquidityGross, 0);
assertEq(info.liquidityNet, 0);
assertEq(info.feeGrowthOutside0X128, 0);
assertEq(info.feeGrowthOutside1X128, 0);
}

tick = -5;
randomTick = -15;
{
Tick.Info memory info = pool.getPoolTickInfo(tick);
assertEq(info.liquidityGross, 0);
assertEq(info.liquidityNet, 0);
assertEq(info.feeGrowthOutside0X128, 0);
assertEq(info.feeGrowthOutside1X128, 0);

pool.ticks[tick] = Tick.Info(100, 200, 300, 400);
info = pool.getPoolTickInfo(tick);
assertEq(info.liquidityGross, 100);
assertEq(info.liquidityNet, 200);
assertEq(info.feeGrowthOutside0X128, 300);
assertEq(info.feeGrowthOutside1X128, 400);

// access random tick
info = pool.getPoolTickInfo(randomTick);
assertEq(info.liquidityGross, 0);
assertEq(info.liquidityNet, 0);
assertEq(info.feeGrowthOutside0X128, 0);
assertEq(info.feeGrowthOutside1X128, 0);

// tick clear
delete pool.ticks[tick];
info = pool.getPoolTickInfo(tick);
assertEq(info.liquidityGross, 0);
assertEq(info.liquidityNet, 0);
assertEq(info.feeGrowthOutside0X128, 0);
assertEq(info.feeGrowthOutside1X128, 0);
}

tick = 0;
randomTick = type(int24).max;
{
Tick.Info memory info = pool.getPoolTickInfo(tick);
assertEq(info.liquidityGross, 0);
assertEq(info.liquidityNet, 0);
assertEq(info.feeGrowthOutside0X128, 0);
assertEq(info.feeGrowthOutside1X128, 0);

pool.ticks[tick] = Tick.Info(100, 200, 300, 400);
info = pool.getPoolTickInfo(tick);
assertEq(info.liquidityGross, 100);
assertEq(info.liquidityNet, 200);
assertEq(info.feeGrowthOutside0X128, 300);
assertEq(info.feeGrowthOutside1X128, 400);

// access random tick
info = pool.getPoolTickInfo(randomTick);
assertEq(info.liquidityGross, 0);
assertEq(info.liquidityNet, 0);
assertEq(info.feeGrowthOutside0X128, 0);
assertEq(info.feeGrowthOutside1X128, 0);

// tick clear
delete pool.ticks[tick];
info = pool.getPoolTickInfo(tick);
assertEq(info.liquidityGross, 0);
assertEq(info.liquidityNet, 0);
assertEq(info.feeGrowthOutside0X128, 0);
assertEq(info.feeGrowthOutside1X128, 0);
}
}

function testGetPoolBitmapInfo() public {
uint256 bitmap = pool.getPoolBitmapInfo(10);
assertEq(bitmap, 0);

pool.tickBitmap[10] = 100;
bitmap = pool.getPoolBitmapInfo(10);
assertEq(bitmap, 100);

// access random word
bitmap = pool.getPoolBitmapInfo(100);
assertEq(bitmap, 0);

// set it back
pool.tickBitmap[10] = 0;
pool.getPoolBitmapInfo(10);
assertEq(bitmap, 0);
}

function testGetFeeGrowthGlobals() public {
(uint256 feeGrowthGlobal0X128, uint256 feeGrowthGlobal1X128) = pool.getFeeGrowthGlobals();
assertEq(feeGrowthGlobal0X128, 0);
assertEq(feeGrowthGlobal1X128, 0);

pool.feeGrowthGlobal0X128 = 100;
pool.feeGrowthGlobal1X128 = 200;
(feeGrowthGlobal0X128, feeGrowthGlobal1X128) = pool.getFeeGrowthGlobals();
assertEq(feeGrowthGlobal0X128, 100);
assertEq(feeGrowthGlobal1X128, 200);

// set it back
pool.feeGrowthGlobal0X128 = 0;
pool.feeGrowthGlobal1X128 = 0;
(feeGrowthGlobal0X128, feeGrowthGlobal1X128) = pool.getFeeGrowthGlobals();
assertEq(feeGrowthGlobal0X128, 0);
assertEq(feeGrowthGlobal1X128, 0);
}
}
Loading