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

[Discussion]: store poolid in array #93

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 @@
178488
178554
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181749
181793
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136521
291836
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
328264
328330
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
189292
189358
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24285
24891
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133558
133611
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1873
1917
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32236
32302
Original file line number Diff line number Diff line change
@@ -1 +1 @@
34993
35037
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142318
142384
Original file line number Diff line number Diff line change
@@ -1 +1 @@
288084
288136
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126663
126716
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114357
114401
Original file line number Diff line number Diff line change
@@ -1 +1 @@
967527
967593
Original file line number Diff line number Diff line change
@@ -1 +1 @@
325744
325810
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337226
337292
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139628
139694
Original file line number Diff line number Diff line change
@@ -1 +1 @@
171592
171658
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177473
177539
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132459
132525
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
206492
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

44k more gas

Original file line number Diff line number Diff line change
@@ -1 +1 @@
304183
304249
Original file line number Diff line number Diff line change
@@ -1 +1 @@
34319
34385
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
);
}
8 changes: 8 additions & 0 deletions src/pool-bin/BinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {

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

mapping(PoolId id => PoolKey) public poolIdToPoolKey;

uint256 public poolLength;
mapping(uint256 => PoolId id) public poolIds;

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 +122,9 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
(, uint24 protocolFee) = _fetchProtocolFee(key);
pools[id].initialize(activeId, protocolFee, lpFee);

poolIds[++poolLength] = id;
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.fee, binStep, key.hooks);

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.fee, tickSpacing, key.hooks);

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, key.fee, binStep, IHooks(address(mockHooks)));

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 @@ -495,6 +495,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
Loading