Skip to content

Commit

Permalink
[Feat] feat: add poolId -> poolKey mapping (#91)
Browse files Browse the repository at this point in the history
* feat: add poolId -> poolKey mapping

* feat: include poolIdToPoolKey for BinPoolManager
  • Loading branch information
ChefMist authored Jun 28, 2024
1 parent 8087dc8 commit 62d1e90
Show file tree
Hide file tree
Showing 51 changed files with 95 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178488
178510
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181749
181771
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136508
247511
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
328264
328286
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
189292
189314
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24280
24762
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133558
133576
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1873
1895
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32236
32258
Original file line number Diff line number Diff line change
@@ -1 +1 @@
34993
35015
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142318
142340
Original file line number Diff line number Diff line change
@@ -1 +1 @@
288084
288101
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126663
126681
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114357
114379
Original file line number Diff line number Diff line change
@@ -1 +1 @@
967527
967549
Original file line number Diff line number Diff line change
@@ -1 +1 @@
325744
325766
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337226
337248
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139628
139650
Original file line number Diff line number Diff line change
@@ -1 +1 @@
171592
171614
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177473
177495
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132459
132481
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
162167
Original file line number Diff line number Diff line change
@@ -1 +1 @@
304183
304205
Original file line number Diff line number Diff line change
@@ -1 +1 @@
34319
34341
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24256
24839
Original file line number Diff line number Diff line change
@@ -1 +1 @@
351833
351855
Original file line number Diff line number Diff line change
@@ -1 +1 @@
167274
167296
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238701
238723
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
164176
164198
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
109028
109050
Original file line number Diff line number Diff line change
@@ -1 +1 @@
58707
149801
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115249
115271
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131684
131706
Original file line number Diff line number Diff line change
@@ -1 +1 @@
164270
164292
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151715
151737
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
72014
72036
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143900
143922
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
87989
88011
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
72017
72039
Original file line number Diff line number Diff line change
@@ -1 +1 @@
31987
32009
2 changes: 1 addition & 1 deletion .forge-snapshots/ExtsloadTest#extsload.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7315
7337
2 changes: 1 addition & 1 deletion .forge-snapshots/ExtsloadTest#extsloadInBatch.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11028
11050
Original file line number Diff line number Diff line change
@@ -1 +1 @@
82036
82069
Original file line number Diff line number Diff line change
@@ -1 +1 @@
42809
42842
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
42805
42838
15 changes: 15 additions & 0 deletions src/interfaces/IPoolManager.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {IHooks} from "./IHooks.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {PoolId} from "../types/PoolId.sol";
import {Currency} from "../types/Currency.sol";

interface IPoolManager {
/// @notice Thrown when trying to interact with a non-initialized pool
Expand All @@ -25,4 +27,17 @@ interface IPoolManager {
/// 2) For BinPool only, when hook#beforeMint() is called and hook call this function to update the lp fee
/// 3) other use case where the hook might want to on an ad-hoc basis increase/reduce lp fee
function updateDynamicLPFee(PoolKey memory key, uint24 newDynamicLPFee) external;

/// @notice Return PoolKey for a given PoolId
function poolIdToPoolKey(PoolId id)
external
view
returns (
Currency currency0,
Currency currency1,
IHooks hooks,
IPoolManager poolManager,
uint24 fee,
bytes32 parameters
);
}
4 changes: 4 additions & 0 deletions src/pool-bin/BinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {

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

mapping(PoolId id => PoolKey) public poolIdToPoolKey;

constructor(IVault vault, uint256 controllerGasLimit) ProtocolFees(vault, controllerGasLimit) {}

/// @notice pool manager specified in the pool key must match current contract
Expand Down Expand Up @@ -117,6 +119,8 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
(, uint24 protocolFee) = _fetchProtocolFee(key);
pools[id].initialize(activeId, protocolFee, lpFee);

poolIdToPoolKey[id] = key;

/// @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.hooks, key.fee, key.parameters);

Expand Down
4 changes: 4 additions & 0 deletions src/pool-cl/CLPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ contract CLPoolManager is ICLPoolManager, ProtocolFees, Extsload {

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

mapping(PoolId id => PoolKey) public poolIdToPoolKey;

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

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

poolIdToPoolKey[id] = key;

/// @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.hooks, key.fee, key.parameters);

Expand Down
15 changes: 15 additions & 0 deletions test/pool-bin/BinPoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper {
snapSize("BinPoolManagerBytecodeSize", address(poolManager));
}

function testInitialize_gasCheck_withoutHooks() public {
snapStart("BinPoolManagerTest#testInitialize_gasCheck_withoutHooks");
poolManager.initialize(key, activeId, new bytes(0));
snapEnd();
}

function test_FuzzInitializePool(uint16 binStep) public {
binStep = uint16(bound(binStep, poolManager.MIN_BIN_STEP(), poolManager.MAX_BIN_STEP()));

Expand All @@ -159,6 +165,15 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper {
emit Initialize(key.toId(), key.currency0, key.currency1, IHooks(address(mockHooks)), key.fee, key.parameters);

poolManager.initialize(key, activeId, new bytes(0));

(Currency curr0, Currency curr1, IHooks hooks, IPoolManager pm, uint24 fee, bytes32 parameters) =
poolManager.poolIdToPoolKey(key.toId());
assertEq(Currency.unwrap(curr0), Currency.unwrap(key.currency0));
assertEq(Currency.unwrap(curr1), Currency.unwrap(key.currency1));
assertEq(address(hooks), address(key.hooks));
assertEq(address(pm), address(key.poolManager));
assertEq(fee, key.fee);
assertEq(parameters, key.parameters);
}

function test_FuzzInitializePoolUnusedBits(uint256 randomOneBitOffset) external {
Expand Down
9 changes: 9 additions & 0 deletions test/pool-cl/CLPoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,15 @@ contract CLPoolManagerTest is Test, NoIsolate, Deployers, TokenFixture, GasSnaps
poolManager.initialize(key, sqrtPriceX96, ZERO_BYTES);
(CLPool.Slot0 memory slot0,,,) = poolManager.pools(key.toId());
assertEq(slot0.sqrtPriceX96, sqrtPriceX96);

(Currency curr0, Currency curr1, IHooks hooks, IPoolManager pm, uint24 fee, bytes32 parameters) =
poolManager.poolIdToPoolKey(key.toId());
assertEq(Currency.unwrap(curr0), Currency.unwrap(key.currency0));
assertEq(Currency.unwrap(curr1), Currency.unwrap(key.currency1));
assertEq(address(hooks), address(key.hooks));
assertEq(address(pm), address(key.poolManager));
assertEq(fee, key.fee);
assertEq(parameters, key.parameters);
}

function test_initialize_succeedsWithMaxTickSpacing(uint160 sqrtPriceX96) public {
Expand Down
3 changes: 3 additions & 0 deletions test/vault/FakePoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
pragma solidity ^0.8.24;

import {PoolKey} from "../../src/types/PoolKey.sol";
import {PoolId} from "../../src/types/PoolId.sol";
import {IVault} from "../../src/interfaces/IVault.sol";
import {BalanceDelta, toBalanceDelta} from "../../src/types/BalanceDelta.sol";
import {IPoolManager} from "../../src/interfaces/IPoolManager.sol";

contract FakePoolManager is IPoolManager {
IVault public vault;

mapping(PoolId id => PoolKey) public poolIdToPoolKey;

constructor(IVault _vault) {
vault = _vault;
}
Expand Down

0 comments on commit 62d1e90

Please sign in to comment.