Skip to content

Commit

Permalink
add pool getters (Uniswap#438)
Browse files Browse the repository at this point in the history
* add getters

* update snaps

* clean test
  • Loading branch information
snreynolds authored and hyunchel committed Feb 21, 2024
1 parent 8c9c83f commit 8298067
Show file tree
Hide file tree
Showing 29 changed files with 70 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
193153
193083
2 changes: 1 addition & 1 deletion .forge-snapshots/cached dynamic fee, no hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148516
148446
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 1 token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139130
139060
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 2 tokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186552
186482
2 changes: 1 addition & 1 deletion .forge-snapshots/erc20 collect protocol fees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
27033
26991
2 changes: 1 addition & 1 deletion .forge-snapshots/gas overhead of no-op lock.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15268
15224
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
78675
78563
2 changes: 1 addition & 1 deletion .forge-snapshots/mint with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
319051
319005
2 changes: 1 addition & 1 deletion .forge-snapshots/mint with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
201832
201786
2 changes: 1 addition & 1 deletion .forge-snapshots/mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
201752
201706
2 changes: 1 addition & 1 deletion .forge-snapshots/mintWithEmptyHookEOAInitiated.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
250947
250901
2 changes: 1 addition & 1 deletion .forge-snapshots/modify position with noop.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
56515
56472
2 changes: 1 addition & 1 deletion .forge-snapshots/native collect protocol fees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
38699
38657
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25626
26329
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap with native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
197401
197331
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
205969
205899
2 changes: 1 addition & 1 deletion .forge-snapshots/simpleSwapEOAInitiated.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177213
177187
2 changes: 1 addition & 1 deletion .forge-snapshots/simpleSwapNativeEOAInitiated.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
173875
173849
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127994
127924
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115485
115415
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn claim for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134557
134509
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint output as claim.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
218140
218114
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192395
192325
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115463
115393
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with noop.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
49734
49689
2 changes: 1 addition & 1 deletion .forge-snapshots/update dynamic fee in before swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
198966
198919
10 changes: 10 additions & 0 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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 {}
}
14 changes: 14 additions & 0 deletions src/libraries/PoolGetters.sol
Original file line number Diff line number Diff line change
@@ -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];
}
}
20 changes: 20 additions & 0 deletions test/Tick.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8298067

Please sign in to comment.