Skip to content

Commit

Permalink
feat: Add pool index in CLPoolManager
Browse files Browse the repository at this point in the history
If you want to query v4 pool information, you need to know the poolId, which is very long and not user-friendly.
So i suggest we can record the pool index which start from 1,  we just need to remember the pool index, then we can use index to query the pool information easily.
  • Loading branch information
ChefSnoopy committed Apr 25, 2024
1 parent 2eb67b3 commit d2ce275
Show file tree
Hide file tree
Showing 25 changed files with 128 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
348848
348804
Original file line number Diff line number Diff line change
@@ -1 +1 @@
59396
59352
Original file line number Diff line number Diff line change
@@ -1 +1 @@
242441
242397
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
82514
82470
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52476
52432
Original file line number Diff line number Diff line change
@@ -1 +1 @@
36547
80852
Original file line number Diff line number Diff line change
@@ -1 +1 @@
42374
42330
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54672
54606
Original file line number Diff line number Diff line change
@@ -1 +1 @@
100748
100682
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25040458
25040392
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
35806
Original file line number Diff line number Diff line change
@@ -1 +1 @@
101510
101444
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
41456
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
35809
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
19174
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37497
81802
Original file line number Diff line number Diff line change
@@ -1 +1 @@
29405
29361
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
21601
2 changes: 1 addition & 1 deletion .forge-snapshots/ExtsloadTest#extsload.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7468
7490
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
60 changes: 60 additions & 0 deletions src/helpers/CLPoolHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: UNLICENSED
// Copyright (C) 2024 PancakeSwap
pragma solidity ^0.8.24;

import {PoolId, PoolIdLibrary, PoolKey} from "../types/PoolId.sol";
import {ICLPoolManagerView} from "./interfaces/ICLPoolManagerView.sol";
import {CLPool} from "../pool-cl/libraries/CLPool.sol";

contract CLPoolHelper {
using PoolIdLibrary for PoolKey;

struct PoolInfo {
PoolId poolId;
// the current price
uint160 sqrtPriceX96;
// the current tick
int24 tick;
// protocol swap fee represented as integer denominator (1/x), taken as a % of the LP swap fee
// upper 8 bits are for 1->0, and the lower 8 are for 0->1
// the minimum permitted denominator is 4 - meaning the maximum protocol fee is 25%
// granularity is increments of 0.38% (100/type(uint8).max)
/// bits 16 14 12 10 8 6 4 2 0
/// | swap |
/// ┌───────────┬───────────┬
/// protocolFee : | 1->0 | 0 -> 1 |
/// └───────────┴───────────┴
uint16 protocolFee;
// used for the swap fee, either static at initialize or dynamic via hook
uint24 swapFee;
uint256 feeGrowthGlobal0X128;
uint256 feeGrowthGlobal1X128;
/// @dev current active liquidity
uint128 liquidity;
}

ICLPoolManagerView public immutable poolManager;

constructor(address _poolManager) {
poolManager = ICLPoolManagerView(_poolManager);
}

function getPoolId(PoolKey memory key) public pure returns (PoolId) {
return key.toId();
}

function getPoolInfoByIndex(uint256 poolIndex) public view returns (PoolInfo memory) {
PoolId poolId = poolManager.poolIds(poolIndex);
ICLPoolManagerView.CLPoolState memory poolState = poolManager.pools(poolId);
return PoolInfo({
poolId: poolId,
sqrtPriceX96: poolState.slot0.sqrtPriceX96,
tick: poolState.slot0.tick,
protocolFee: poolState.slot0.protocolFee,
swapFee: poolState.slot0.swapFee,
feeGrowthGlobal0X128: poolState.feeGrowthGlobal0X128,
feeGrowthGlobal1X128: poolState.feeGrowthGlobal1X128,
liquidity: poolState.liquidity
});
}
}
20 changes: 20 additions & 0 deletions src/helpers/interfaces/ICLPoolManagerView.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {CLPool} from "../../pool-cl/libraries/CLPool.sol";
import {PoolId} from "../../types/PoolId.sol";

interface ICLPoolManagerView {
struct CLPoolState {
CLPool.Slot0 slot0;
uint256 feeGrowthGlobal0X128;
uint256 feeGrowthGlobal1X128;
uint128 liquidity;
}

// mapping(PoolId id => CLPool.State) public pools;
function pools(PoolId id) external view returns (CLPoolState memory);

// mapping(uint256 => PoolId id) public poolIds;
function poolIds(uint256 index) external view returns (PoolId);
}
6 changes: 6 additions & 0 deletions src/pool-cl/CLPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ contract CLPoolManager is ICLPoolManager, Fees, Extsload {

mapping(PoolId id => CLPool.State) public pools;

/// @notice Record the total number of pools
uint256 public poolLength;
/// @notice Record the poolId of each pool, used to get the poolId by index
mapping(uint256 => PoolId id) public poolIds;

constructor(IVault _vault, uint256 controllerGasLimit) Fees(_vault, controllerGasLimit) {}

/// @notice pool manager specified in the pool key must match current contract
Expand Down Expand Up @@ -111,6 +116,7 @@ contract CLPoolManager is ICLPoolManager, Fees, Extsload {
(, uint16 protocolFee) = _fetchProtocolFee(key);
tick = pools[id].initialize(sqrtPriceX96, protocolFee, swapFee);

poolIds[++poolLength] = id;
/// @notice Make sure the first event is noted, so that later events from afterHook won't get mixed up with this one
emit Initialize(id, key.currency0, key.currency1, key.fee, tickSpacing, hooks);

Expand Down
21 changes: 21 additions & 0 deletions test/pool-cl/CLPoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {ProtocolFeeControllerTest} from "./helpers/ProtocolFeeControllerTest.sol
import {IProtocolFeeController} from "../../src/interfaces/IProtocolFeeController.sol";
import {CLFeeManagerHook} from "./helpers/CLFeeManagerHook.sol";
import {CLNoOpTestHook} from "./helpers/CLNoOpTestHook.sol";
import {CLPoolHelper} from "../../src/helpers/CLPoolHelper.sol";

contract CLPoolManagerTest is Test, Deployers, TokenFixture, GasSnapshot {
using PoolIdLibrary for PoolKey;
Expand Down Expand Up @@ -72,6 +73,7 @@ contract CLPoolManagerTest is Test, Deployers, TokenFixture, GasSnapshot {

IVault public vault;
CLPoolManager public poolManager;
CLPoolHelper public poolHelper;
CLPoolManagerRouter public router;
ProtocolFeeControllerTest public protocolFeeController;
ProtocolFeeControllerTest public feeController;
Expand All @@ -80,6 +82,7 @@ contract CLPoolManagerTest is Test, Deployers, TokenFixture, GasSnapshot {
function setUp() public {
initializeTokens();
(vault, poolManager) = createFreshManager();
poolHelper = new CLPoolHelper(address(poolManager));
router = new CLPoolManagerRouter(vault, poolManager);
protocolFeeController = new ProtocolFeeControllerTest();
feeController = new ProtocolFeeControllerTest();
Expand All @@ -105,6 +108,21 @@ contract CLPoolManagerTest is Test, Deployers, TokenFixture, GasSnapshot {
});

poolManager.initialize(key, TickMath.MIN_SQRT_RATIO, new bytes(0));

PoolId id = key.toId();
assertEq(PoolId.unwrap(poolManager.poolIds(1)), PoolId.unwrap(id));
assertEq(poolManager.poolLength(), 1);
CLPoolHelper.PoolInfo memory poolInfo = poolHelper.getPoolInfoByIndex(1);
(uint160 sqrtPriceX96, int24 tick, uint16 protocolFee, uint24 swapFee) = poolManager.getSlot0(id);
assertEq(poolInfo.sqrtPriceX96, sqrtPriceX96);
assertEq(poolInfo.tick, tick);
assertEq(poolInfo.sqrtPriceX96, sqrtPriceX96);
assertEq(poolInfo.swapFee, swapFee);

(, uint256 feeGrowthGlobal0X128, uint256 feeGrowthGlobal1X128, uint128 liquidity) = poolManager.pools(id);
assertEq(poolInfo.feeGrowthGlobal0X128, feeGrowthGlobal0X128);
assertEq(poolInfo.feeGrowthGlobal1X128, feeGrowthGlobal1X128);
assertEq(poolInfo.liquidity, liquidity);
}

// 0
Expand All @@ -119,6 +137,9 @@ contract CLPoolManagerTest is Test, Deployers, TokenFixture, GasSnapshot {
});

poolManager.initialize(key, TickMath.MIN_SQRT_RATIO, new bytes(0));

assertEq(PoolId.unwrap(poolManager.poolIds(2)), PoolId.unwrap(key.toId()));
assertEq(poolManager.poolLength(), 2);
}

// 300000 i.e. 30%
Expand Down

0 comments on commit d2ce275

Please sign in to comment.