From 8298067be736cfc8a5d58cddf7b61682f56bf3e5 Mon Sep 17 00:00:00 2001 From: Sara Reynolds <30504811+snreynolds@users.noreply.github.com> Date: Thu, 7 Dec 2023 17:32:12 -0500 Subject: [PATCH] add pool getters (#438) * add getters * update snaps * clean test --- ...swap hook, already cached dynamic fee.snap | 2 +- .../cached dynamic fee, no hooks.snap | 2 +- .forge-snapshots/donate gas with 1 token.snap | 2 +- .../donate gas with 2 tokens.snap | 2 +- .../erc20 collect protocol fees.snap | 2 +- .../gas overhead of no-op lock.snap | 2 +- .forge-snapshots/initialize.snap | 2 +- .forge-snapshots/mint with empty hook.snap | 2 +- .forge-snapshots/mint with native token.snap | 2 +- .forge-snapshots/mint.snap | 2 +- .../mintWithEmptyHookEOAInitiated.snap | 2 +- .../modify position with noop.snap | 2 +- .../native collect protocol fees.snap | 2 +- .../poolManager bytecode size.snap | 2 +- .forge-snapshots/simple swap with native.snap | 2 +- .forge-snapshots/simple swap.snap | 2 +- .forge-snapshots/simpleSwapEOAInitiated.snap | 2 +- .../simpleSwapNativeEOAInitiated.snap | 2 +- ...p against liquidity with native token.snap | 2 +- .forge-snapshots/swap against liquidity.snap | 2 +- .../swap burn claim for input.snap | 2 +- .../swap mint output as claim.snap | 2 +- .forge-snapshots/swap with dynamic fee.snap | 2 +- .forge-snapshots/swap with hooks.snap | 2 +- .forge-snapshots/swap with noop.snap | 2 +- .../update dynamic fee in before swap.snap | 2 +- src/PoolManager.sol | 10 ++++++++++ src/libraries/PoolGetters.sol | 14 +++++++++++++ test/Tick.t.sol | 20 +++++++++++++++++++ 29 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 src/libraries/PoolGetters.sol diff --git a/.forge-snapshots/before swap hook, already cached dynamic fee.snap b/.forge-snapshots/before swap hook, already cached dynamic fee.snap index f7c4a7117..59ca73e70 100644 --- a/.forge-snapshots/before swap hook, already cached dynamic fee.snap +++ b/.forge-snapshots/before swap hook, already cached dynamic fee.snap @@ -1 +1 @@ -193153 \ No newline at end of file +193083 \ No newline at end of file diff --git a/.forge-snapshots/cached dynamic fee, no hooks.snap b/.forge-snapshots/cached dynamic fee, no hooks.snap index 9ea1db117..b592a9470 100644 --- a/.forge-snapshots/cached dynamic fee, no hooks.snap +++ b/.forge-snapshots/cached dynamic fee, no hooks.snap @@ -1 +1 @@ -148516 \ No newline at end of file +148446 \ No newline at end of file diff --git a/.forge-snapshots/donate gas with 1 token.snap b/.forge-snapshots/donate gas with 1 token.snap index 1bc661eb3..3ad88ae0b 100644 --- a/.forge-snapshots/donate gas with 1 token.snap +++ b/.forge-snapshots/donate gas with 1 token.snap @@ -1 +1 @@ -139130 \ No newline at end of file +139060 \ No newline at end of file diff --git a/.forge-snapshots/donate gas with 2 tokens.snap b/.forge-snapshots/donate gas with 2 tokens.snap index 36e65184f..205667613 100644 --- a/.forge-snapshots/donate gas with 2 tokens.snap +++ b/.forge-snapshots/donate gas with 2 tokens.snap @@ -1 +1 @@ -186552 \ No newline at end of file +186482 \ No newline at end of file diff --git a/.forge-snapshots/erc20 collect protocol fees.snap b/.forge-snapshots/erc20 collect protocol fees.snap index f6af67bf2..6489f5191 100644 --- a/.forge-snapshots/erc20 collect protocol fees.snap +++ b/.forge-snapshots/erc20 collect protocol fees.snap @@ -1 +1 @@ -27033 \ No newline at end of file +26991 \ No newline at end of file diff --git a/.forge-snapshots/gas overhead of no-op lock.snap b/.forge-snapshots/gas overhead of no-op lock.snap index d8e63744a..fd0c301b5 100644 --- a/.forge-snapshots/gas overhead of no-op lock.snap +++ b/.forge-snapshots/gas overhead of no-op lock.snap @@ -1 +1 @@ -15268 \ No newline at end of file +15224 \ No newline at end of file diff --git a/.forge-snapshots/initialize.snap b/.forge-snapshots/initialize.snap index 95fd29353..7351c1a9a 100644 --- a/.forge-snapshots/initialize.snap +++ b/.forge-snapshots/initialize.snap @@ -1 +1 @@ -78675 \ No newline at end of file +78563 \ No newline at end of file diff --git a/.forge-snapshots/mint with empty hook.snap b/.forge-snapshots/mint with empty hook.snap index 8537190e0..978bf762d 100644 --- a/.forge-snapshots/mint with empty hook.snap +++ b/.forge-snapshots/mint with empty hook.snap @@ -1 +1 @@ -319051 \ No newline at end of file +319005 \ No newline at end of file diff --git a/.forge-snapshots/mint with native token.snap b/.forge-snapshots/mint with native token.snap index eff86ee1d..4173ed21a 100644 --- a/.forge-snapshots/mint with native token.snap +++ b/.forge-snapshots/mint with native token.snap @@ -1 +1 @@ -201832 \ No newline at end of file +201786 \ No newline at end of file diff --git a/.forge-snapshots/mint.snap b/.forge-snapshots/mint.snap index 78eb8f6bf..50dbde71a 100644 --- a/.forge-snapshots/mint.snap +++ b/.forge-snapshots/mint.snap @@ -1 +1 @@ -201752 \ No newline at end of file +201706 \ No newline at end of file diff --git a/.forge-snapshots/mintWithEmptyHookEOAInitiated.snap b/.forge-snapshots/mintWithEmptyHookEOAInitiated.snap index eda502201..199a20cdd 100644 --- a/.forge-snapshots/mintWithEmptyHookEOAInitiated.snap +++ b/.forge-snapshots/mintWithEmptyHookEOAInitiated.snap @@ -1 +1 @@ -250947 \ No newline at end of file +250901 \ No newline at end of file diff --git a/.forge-snapshots/modify position with noop.snap b/.forge-snapshots/modify position with noop.snap index 8d9f09965..c59242528 100644 --- a/.forge-snapshots/modify position with noop.snap +++ b/.forge-snapshots/modify position with noop.snap @@ -1 +1 @@ -56515 \ No newline at end of file +56472 \ No newline at end of file diff --git a/.forge-snapshots/native collect protocol fees.snap b/.forge-snapshots/native collect protocol fees.snap index 5a35b1041..d71adf7a4 100644 --- a/.forge-snapshots/native collect protocol fees.snap +++ b/.forge-snapshots/native collect protocol fees.snap @@ -1 +1 @@ -38699 \ No newline at end of file +38657 \ No newline at end of file diff --git a/.forge-snapshots/poolManager bytecode size.snap b/.forge-snapshots/poolManager bytecode size.snap index 74e647d5b..4edaeab31 100644 --- a/.forge-snapshots/poolManager bytecode size.snap +++ b/.forge-snapshots/poolManager bytecode size.snap @@ -1 +1 @@ -25626 \ No newline at end of file +26329 \ No newline at end of file diff --git a/.forge-snapshots/simple swap with native.snap b/.forge-snapshots/simple swap with native.snap index 0eb569864..db7a72e7d 100644 --- a/.forge-snapshots/simple swap with native.snap +++ b/.forge-snapshots/simple swap with native.snap @@ -1 +1 @@ -197401 \ No newline at end of file +197331 \ No newline at end of file diff --git a/.forge-snapshots/simple swap.snap b/.forge-snapshots/simple swap.snap index 6b8f63604..563c04884 100644 --- a/.forge-snapshots/simple swap.snap +++ b/.forge-snapshots/simple swap.snap @@ -1 +1 @@ -205969 \ No newline at end of file +205899 \ No newline at end of file diff --git a/.forge-snapshots/simpleSwapEOAInitiated.snap b/.forge-snapshots/simpleSwapEOAInitiated.snap index 33e2de06b..ce9cd7fe1 100644 --- a/.forge-snapshots/simpleSwapEOAInitiated.snap +++ b/.forge-snapshots/simpleSwapEOAInitiated.snap @@ -1 +1 @@ -177213 \ No newline at end of file +177187 \ No newline at end of file diff --git a/.forge-snapshots/simpleSwapNativeEOAInitiated.snap b/.forge-snapshots/simpleSwapNativeEOAInitiated.snap index 4766ba9f9..eda6f70c5 100644 --- a/.forge-snapshots/simpleSwapNativeEOAInitiated.snap +++ b/.forge-snapshots/simpleSwapNativeEOAInitiated.snap @@ -1 +1 @@ -173875 \ No newline at end of file +173849 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity with native token.snap b/.forge-snapshots/swap against liquidity with native token.snap index 8ddd40ca9..8e00a61bc 100644 --- a/.forge-snapshots/swap against liquidity with native token.snap +++ b/.forge-snapshots/swap against liquidity with native token.snap @@ -1 +1 @@ -127994 \ No newline at end of file +127924 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity.snap b/.forge-snapshots/swap against liquidity.snap index 26359d0c4..4e05ab0b5 100644 --- a/.forge-snapshots/swap against liquidity.snap +++ b/.forge-snapshots/swap against liquidity.snap @@ -1 +1 @@ -115485 \ No newline at end of file +115415 \ No newline at end of file diff --git a/.forge-snapshots/swap burn claim for input.snap b/.forge-snapshots/swap burn claim for input.snap index f6035b282..8be49d505 100644 --- a/.forge-snapshots/swap burn claim for input.snap +++ b/.forge-snapshots/swap burn claim for input.snap @@ -1 +1 @@ -134557 \ No newline at end of file +134509 \ No newline at end of file diff --git a/.forge-snapshots/swap mint output as claim.snap b/.forge-snapshots/swap mint output as claim.snap index 312b47616..139f5b044 100644 --- a/.forge-snapshots/swap mint output as claim.snap +++ b/.forge-snapshots/swap mint output as claim.snap @@ -1 +1 @@ -218140 \ No newline at end of file +218114 \ No newline at end of file diff --git a/.forge-snapshots/swap with dynamic fee.snap b/.forge-snapshots/swap with dynamic fee.snap index 599f4da06..654ec0f6e 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -192395 \ No newline at end of file +192325 \ No newline at end of file diff --git a/.forge-snapshots/swap with hooks.snap b/.forge-snapshots/swap with hooks.snap index 0e7a8ddb0..b57fc3012 100644 --- a/.forge-snapshots/swap with hooks.snap +++ b/.forge-snapshots/swap with hooks.snap @@ -1 +1 @@ -115463 \ No newline at end of file +115393 \ No newline at end of file diff --git a/.forge-snapshots/swap with noop.snap b/.forge-snapshots/swap with noop.snap index 1ffec7516..b244ea4d5 100644 --- a/.forge-snapshots/swap with noop.snap +++ b/.forge-snapshots/swap with noop.snap @@ -1 +1 @@ -49734 \ No newline at end of file +49689 \ No newline at end of file diff --git a/.forge-snapshots/update dynamic fee in before swap.snap b/.forge-snapshots/update dynamic fee in before swap.snap index 38bea6bf8..c2a101a73 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -198966 \ No newline at end of file +198919 \ No newline at end of file diff --git a/src/PoolManager.sol b/src/PoolManager.sol index ec33a6322..39b0e8801 100644 --- a/src/PoolManager.sol +++ b/src/PoolManager.sol @@ -21,6 +21,7 @@ import {Claims} from "./Claims.sol"; import {PoolId, PoolIdLibrary} from "./types/PoolId.sol"; import {BalanceDelta, BalanceDeltaLibrary} from "./types/BalanceDelta.sol"; import {Lockers} from "./libraries/Lockers.sol"; +import {PoolGetters} from "./libraries/PoolGetters.sol"; /// @notice Holds the state for all pools contract PoolManager is IPoolManager, Fees, NoDelegateCall, Claims { @@ -31,6 +32,7 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, Claims { using Position for mapping(bytes32 => Position.Info); using CurrencyLibrary for Currency; using FeeLibrary for uint24; + using PoolGetters for Pool.State; /// @inheritdoc IPoolManager int24 public constant MAX_TICK_SPACING = TickMath.MAX_TICK_SPACING; @@ -446,6 +448,14 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, Claims { return Lockers.getCurrentHook(); } + function getPoolTickInfo(PoolId id, int24 tick) external view returns (Pool.TickInfo memory) { + return pools[id].getPoolTickInfo(tick); + } + + function getPoolBitmapInfo(PoolId id, int16 word) external view returns (uint256 tickBitmap) { + return pools[id].getPoolBitmapInfo(word); + } + /// @notice receive native tokens for native pools receive() external payable {} } diff --git a/src/libraries/PoolGetters.sol b/src/libraries/PoolGetters.sol new file mode 100644 index 000000000..df7eb1172 --- /dev/null +++ b/src/libraries/PoolGetters.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity ^0.8.20; + +import {Pool} from "./Pool.sol"; + +library PoolGetters { + function getPoolTickInfo(Pool.State storage pool, int24 tick) external view returns (Pool.TickInfo memory) { + return pool.ticks[tick]; + } + + function getPoolBitmapInfo(Pool.State storage pool, int16 word) external view returns (uint256 tickBitmap) { + return pool.tickBitmap[word]; + } +} diff --git a/test/Tick.t.sol b/test/Tick.t.sol index 9849fe96d..08d3b9278 100644 --- a/test/Tick.t.sol +++ b/test/Tick.t.sol @@ -6,8 +6,10 @@ import {GasSnapshot} from "../lib/forge-gas-snapshot/src/GasSnapshot.sol"; import {Constants} from "./utils/Constants.sol"; import {Pool} from "../src/libraries/Pool.sol"; import {TickMath} from "../src/libraries/TickMath.sol"; +import {PoolGetters} from "../src/libraries/PoolGetters.sol"; contract TickTest is Test, GasSnapshot { + using PoolGetters for Pool.State; using Pool for Pool.State; int24 constant LOW_TICK_SPACING = 10; @@ -28,6 +30,10 @@ contract TickTest is Test, GasSnapshot { pool.ticks[tick] = info; } + function setTickBitmap(int16 word, uint256 bitmap) internal { + pool.tickBitmap[word] = bitmap; + } + function getFeeGrowthInside( int24 tickLower, int24 tickUpper, @@ -459,6 +465,20 @@ contract TickTest is Test, GasSnapshot { assertEq(info.feeGrowthOutside1X128, 2); } + function test_getPoolTickInfo(int24 tick, Pool.TickInfo memory info) public { + setTick(tick, info); + Pool.TickInfo memory actualInfo = pool.getPoolTickInfo(tick); + assertEq(actualInfo.liquidityGross, info.liquidityGross); + assertEq(actualInfo.liquidityNet, info.liquidityNet); + assertEq(actualInfo.feeGrowthOutside0X128, info.feeGrowthOutside0X128); + assertEq(actualInfo.feeGrowthOutside1X128, info.feeGrowthOutside1X128); + } + + function test_getPoolBitmapInfo(int16 word, uint256 bitmap) public { + setTickBitmap(word, bitmap); + assertEq(pool.getPoolBitmapInfo(word), bitmap); + } + function testTick_tickSpacingToParametersInvariants_fuzz(int24 tickSpacing) public { vm.assume(tickSpacing <= TickMath.MAX_TICK_SPACING); vm.assume(tickSpacing >= TickMath.MIN_TICK_SPACING);