From 5d815cd37ff93437da89e9b6958fd3a873ffe82e Mon Sep 17 00:00:00 2001 From: Sara Reynolds Date: Wed, 31 Jul 2024 22:21:20 -0400 Subject: [PATCH] add posm view lib --- .../PositionManager_burn_empty.snap | 2 +- .../PositionManager_burn_empty_native.snap | 2 +- .../PositionManager_burn_nonEmpty.snap | 2 +- .../PositionManager_burn_nonEmpty_native.snap | 2 +- .../PositionManager_decrease_burnEmpty.snap | 2 +- ...tionManager_decrease_burnEmpty_native.snap | 2 +- src/PositionManager.sol | 16 ++------- src/libraries/PosmStateLibrary.sol | 26 ++++++++++++++ test/position-managers/Execute.t.sol | 23 +++--------- .../position-managers/IncreaseLiquidity.t.sol | 12 +++---- test/position-managers/NativeToken.t.sol | 35 +++++------------- test/position-managers/PositionManager.t.sol | 36 ++++++------------- test/shared/PosmTestSetup.sol | 16 +++++++++ 13 files changed, 79 insertions(+), 97 deletions(-) create mode 100644 src/libraries/PosmStateLibrary.sol diff --git a/.forge-snapshots/PositionManager_burn_empty.snap b/.forge-snapshots/PositionManager_burn_empty.snap index eae5c224..d49f0e85 100644 --- a/.forge-snapshots/PositionManager_burn_empty.snap +++ b/.forge-snapshots/PositionManager_burn_empty.snap @@ -1 +1 @@ -46761 \ No newline at end of file +46857 \ No newline at end of file diff --git a/.forge-snapshots/PositionManager_burn_empty_native.snap b/.forge-snapshots/PositionManager_burn_empty_native.snap index f4f42539..36767866 100644 --- a/.forge-snapshots/PositionManager_burn_empty_native.snap +++ b/.forge-snapshots/PositionManager_burn_empty_native.snap @@ -1 +1 @@ -46579 \ No newline at end of file +46675 \ No newline at end of file diff --git a/.forge-snapshots/PositionManager_burn_nonEmpty.snap b/.forge-snapshots/PositionManager_burn_nonEmpty.snap index 774f434a..ef57d6cd 100644 --- a/.forge-snapshots/PositionManager_burn_nonEmpty.snap +++ b/.forge-snapshots/PositionManager_burn_nonEmpty.snap @@ -1 +1 @@ -129621 \ No newline at end of file +129716 \ No newline at end of file diff --git a/.forge-snapshots/PositionManager_burn_nonEmpty_native.snap b/.forge-snapshots/PositionManager_burn_nonEmpty_native.snap index ae82b06d..fc07957c 100644 --- a/.forge-snapshots/PositionManager_burn_nonEmpty_native.snap +++ b/.forge-snapshots/PositionManager_burn_nonEmpty_native.snap @@ -1 +1 @@ -122543 \ No newline at end of file +122638 \ No newline at end of file diff --git a/.forge-snapshots/PositionManager_decrease_burnEmpty.snap b/.forge-snapshots/PositionManager_decrease_burnEmpty.snap index 0dd409e3..727637cb 100644 --- a/.forge-snapshots/PositionManager_decrease_burnEmpty.snap +++ b/.forge-snapshots/PositionManager_decrease_burnEmpty.snap @@ -1 +1 @@ -133487 \ No newline at end of file +133582 \ No newline at end of file diff --git a/.forge-snapshots/PositionManager_decrease_burnEmpty_native.snap b/.forge-snapshots/PositionManager_decrease_burnEmpty_native.snap index 432fc190..a2317821 100644 --- a/.forge-snapshots/PositionManager_decrease_burnEmpty_native.snap +++ b/.forge-snapshots/PositionManager_decrease_burnEmpty_native.snap @@ -1 +1 @@ -126226 \ No newline at end of file +126321 \ No newline at end of file diff --git a/src/PositionManager.sol b/src/PositionManager.sol index 5a3be334..a283c123 100644 --- a/src/PositionManager.sol +++ b/src/PositionManager.sol @@ -8,7 +8,6 @@ import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol"; import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol"; import {Position} from "@uniswap/v4-core/src/libraries/Position.sol"; -import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; import {TransientStateLibrary} from "@uniswap/v4-core/src/libraries/TransientStateLibrary.sol"; import {Position} from "@uniswap/v4-core/src/libraries/Position.sol"; import {SafeTransferLib} from "solmate/src/utils/SafeTransferLib.sol"; @@ -25,6 +24,7 @@ import {PositionConfig, PositionConfigLibrary} from "./libraries/PositionConfig. import {BaseActionsRouter} from "./base/BaseActionsRouter.sol"; import {Actions} from "./libraries/Actions.sol"; import {CalldataDecoder} from "./libraries/CalldataDecoder.sol"; +import {PosmStateLibrary} from "./libraries/PosmStateLibrary.sol"; contract PositionManager is IPositionManager, @@ -39,7 +39,7 @@ contract PositionManager is using CurrencyLibrary for Currency; using PoolIdLibrary for PoolKey; using PositionConfigLibrary for PositionConfig; - using StateLibrary for IPoolManager; + using PosmStateLibrary for IPoolManager; using TransientStateLibrary for IPoolManager; using SafeCast for uint256; using CalldataDecoder for bytes; @@ -171,7 +171,7 @@ contract PositionManager is function _burn(uint256 tokenId, PositionConfig calldata config, bytes calldata hookData) internal { if (!_isApprovedOrOwner(_msgSender(), tokenId)) revert NotApproved(_msgSender()); if (positionConfigs[tokenId] != config.toId()) revert IncorrectPositionConfigForTokenId(tokenId); - uint256 liquidity = uint256(_getPositionLiquidity(config, tokenId)); + uint256 liquidity = poolManager.getPositionLiquidity(tokenId, config); BalanceDelta liquidityDelta; // Can only call modify if there is non zero liquidity. @@ -202,16 +202,6 @@ contract PositionManager is ); } - function _getPositionLiquidity(PositionConfig calldata config, uint256 tokenId) - internal - view - returns (uint128 liquidity) - { - bytes32 positionId = - Position.calculatePositionKey(address(this), config.tickLower, config.tickUpper, bytes32(tokenId)); - liquidity = poolManager.getPositionLiquidity(config.poolKey.toId(), positionId); - } - /// @notice Sweeps the entire contract balance of specified currency to the recipient function _sweep(Currency currency, address to) internal { uint256 balance = currency.balanceOfSelf(); diff --git a/src/libraries/PosmStateLibrary.sol b/src/libraries/PosmStateLibrary.sol new file mode 100644 index 00000000..2109991a --- /dev/null +++ b/src/libraries/PosmStateLibrary.sol @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +pragma solidity ^0.8.24; + +import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; +import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; +import {Position} from "@uniswap/v4-core/src/libraries/Position.sol"; +import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; +import {PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol"; + +import {PositionConfig} from "./PositionConfig.sol"; + +/// @notice A library for reading POSM specific information from core, wraps core's StateLibrary +library PosmStateLibrary { + using StateLibrary for IPoolManager; + using PoolIdLibrary for PoolKey; + + function getPositionLiquidity(IPoolManager poolManager, uint256 tokenId, PositionConfig memory config) + internal + view + returns (uint128 liquidity) + { + bytes32 positionId = + Position.calculatePositionKey(address(this), config.tickLower, config.tickUpper, bytes32(tokenId)); + liquidity = poolManager.getPositionLiquidity(config.poolKey.toId(), positionId); + } +} diff --git a/test/position-managers/Execute.t.sol b/test/position-managers/Execute.t.sol index 4d7c5e15..a56c0288 100644 --- a/test/position-managers/Execute.t.sol +++ b/test/position-managers/Execute.t.sol @@ -10,7 +10,6 @@ import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol"; import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol"; import {FixedPointMathLib} from "solmate/src/utils/FixedPointMathLib.sol"; -import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; import {LiquidityAmounts} from "@uniswap/v4-core/test/utils/LiquidityAmounts.sol"; import {Position} from "@uniswap/v4-core/src/libraries/Position.sol"; @@ -31,7 +30,6 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers { using CurrencyLibrary for Currency; using PoolIdLibrary for PoolKey; using Planner for Plan; - using StateLibrary for IPoolManager; PoolId poolId; address alice = makeAddr("ALICE"); @@ -71,9 +69,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers { increaseLiquidity(tokenId, config, liquidityToAdd, ZERO_BYTES); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, initialLiquidity + liquidityToAdd); } @@ -97,9 +93,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers { bytes memory calls = planner.finalizeModifyLiquidity(config.poolKey); lpm.modifyLiquidities(calls, _deadline); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, initialLiquidity + liquidityToAdd + liquidityToAdd2); } @@ -119,9 +113,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers { bytes memory calls = planner.finalizeModifyLiquidity(config.poolKey); lpm.modifyLiquidities(calls, _deadline); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, initialLiquidity + liquidityToAdd); } @@ -175,9 +167,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers { { // old position has no liquidity - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - uint128 liquidity = manager.getPositionLiquidity(config.poolKey.toId(), positionId); + uint128 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, 0); // new token was minted @@ -185,10 +175,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers { assertEq(lpm.ownerOf(newTokenId), address(this)); // new token has expected liquidity - positionId = Position.calculatePositionKey( - address(lpm), newConfig.tickLower, newConfig.tickUpper, bytes32(newTokenId) - ); - liquidity = manager.getPositionLiquidity(config.poolKey.toId(), positionId); + liquidity = _getPositionLiquidity(newTokenId, newConfig); assertEq(liquidity, newLiquidity); } } diff --git a/test/position-managers/IncreaseLiquidity.t.sol b/test/position-managers/IncreaseLiquidity.t.sol index ef76e3cc..3a6317ef 100644 --- a/test/position-managers/IncreaseLiquidity.t.sol +++ b/test/position-managers/IncreaseLiquidity.t.sol @@ -185,16 +185,14 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers { mint(config, liquidityAlice, alice, ZERO_BYTES); vm.stopPrank(); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenIdAlice)); - uint128 oldLiquidity = StateLibrary.getPositionLiquidity(manager, config.poolKey.toId(), positionId); + uint256 oldLiquidity = _getPositionLiquidity(tokenIdAlice, config); // bob can increase liquidity for alice even though he is not the owner / not approved vm.startPrank(bob); increaseLiquidity(tokenIdAlice, config, 100e18, ZERO_BYTES); vm.stopPrank(); - uint128 newLiquidity = StateLibrary.getPositionLiquidity(manager, config.poolKey.toId(), positionId); + uint128 newLiquidity = _getPositionLiquidity(tokenIdAlice, config); // assert liqudity increased by the correct amount assertEq(newLiquidity, oldLiquidity + uint128(100e18)); @@ -415,9 +413,7 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers { mint(config, liquidityAlice, alice, ZERO_BYTES); uint256 tokenIdAlice = lpm.nextTokenId() - 1; - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenIdAlice)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenIdAlice, config); assertEq(liquidity, liquidityAlice); // alice increases with the balance in the position manager @@ -448,7 +444,7 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers { uint256 amount0 = uint128(-delta.amount0()); uint256 amount1 = uint128(-delta.amount1()); - (liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + liquidity = _getPositionLiquidity(tokenIdAlice, config); assertEq(liquidity, 2 * liquidityAlice); // The balances were swept back to this address. diff --git a/test/position-managers/NativeToken.t.sol b/test/position-managers/NativeToken.t.sol index f8d23c54..82602fc6 100644 --- a/test/position-managers/NativeToken.t.sol +++ b/test/position-managers/NativeToken.t.sol @@ -16,9 +16,7 @@ import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; import {FixedPointMathLib} from "solmate/src/utils/FixedPointMathLib.sol"; import {Constants} from "@uniswap/v4-core/test/utils/Constants.sol"; -import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol"; -import {Position} from "@uniswap/v4-core/src/libraries/Position.sol"; import {IERC20} from "forge-std/interfaces/IERC20.sol"; import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol"; @@ -39,7 +37,6 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { using PositionConfigLibrary for PositionConfig; using Planner for Plan; using PoolIdLibrary for PoolKey; - using StateLibrary for IPoolManager; using SafeCast for *; PoolId poolId; @@ -86,9 +83,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { lpm.modifyLiquidities{value: amount0 + 1}(calls, _deadline); BalanceDelta delta = getLastDelta(); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, uint256(params.liquidityDelta)); assertEq(balance0Before - currency0.balanceOfSelf(), uint256(int256(-delta.amount0())), "incorrect amount0"); @@ -130,9 +125,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { lpm.modifyLiquidities{value: amount0 * 2 + 1}(calls, _deadline); BalanceDelta delta = getLastDelta(); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, uint256(params.liquidityDelta)); // only paid the delta amount, with excess tokens returned to caller @@ -156,9 +149,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { uint256 tokenId = lpm.nextTokenId(); mintWithNative(SQRT_PRICE_1_1, config, liquidityToAdd, address(this), ZERO_BYTES); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, uint256(params.liquidityDelta)); // burn liquidity @@ -173,7 +164,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { // No decrease/modifyLiq call will actually happen on the call to burn so the deltas array will be the same length. assertEq(numDeltas, hook.numberDeltasReturned()); - (liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, 0); // TODO: slightly off by 1 bip (0.0001%) @@ -209,9 +200,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { uint256 tokenId = lpm.nextTokenId(); mintWithNative(SQRT_PRICE_1_1, config, liquidityToAdd, address(this), ZERO_BYTES); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, uint256(params.liquidityDelta)); // burn liquidity @@ -221,7 +210,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { burn(tokenId, config, ZERO_BYTES); BalanceDelta deltaBurn = getLastDelta(); - (liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, 0); // TODO: slightly off by 1 bip (0.0001%) @@ -272,9 +261,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { BalanceDelta delta = getLastDelta(); // verify position liquidity increased - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, liquidityToAdd + liquidityToAdd); // liquidity was doubled // verify native token balances changed as expected @@ -321,9 +308,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { BalanceDelta delta = getLastDelta(); // verify position liquidity increased - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, liquidityToAdd + liquidityToAdd); // liquidity was doubled // verify native token balances changed as expected, with overpaid tokens returned @@ -360,9 +345,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { decreaseLiquidity(tokenId, config, decreaseLiquidityDelta, ZERO_BYTES); BalanceDelta delta = getLastDelta(); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, uint256(params.liquidityDelta) - decreaseLiquidityDelta); // verify native token balances changed as expected diff --git a/test/position-managers/PositionManager.t.sol b/test/position-managers/PositionManager.t.sol index 513e9710..2d6a86e4 100644 --- a/test/position-managers/PositionManager.t.sol +++ b/test/position-managers/PositionManager.t.sol @@ -111,9 +111,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { assertEq(lpm.nextTokenId(), 2); assertEq(lpm.ownerOf(tokenId), address(this)); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, uint256(params.liquidityDelta)); assertEq(balance0Before - currency0.balanceOfSelf(), uint256(int256(-delta.amount0())), "incorrect amount0"); @@ -192,9 +190,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { assertEq(tokenId, 1); assertEq(lpm.ownerOf(1), address(this)); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, uint256(params.liquidityDelta)); @@ -209,7 +205,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { burn(tokenId, config, ZERO_BYTES); assertEq(numDeltas, hook.numberDeltasReturned()); - (liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, 0); @@ -238,9 +234,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { assertEq(tokenId, 1); assertEq(lpm.ownerOf(1), address(this)); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, uint256(params.liquidityDelta)); @@ -262,7 +256,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { assertEq(uint256(int256(deltaBurn.amount0())), amount0); assertEq(uint256(int256(deltaBurn.amount1())), amount1); - (liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, 0); @@ -295,9 +289,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { decreaseLiquidity(tokenId, config, decreaseLiquidityDelta, ZERO_BYTES); BalanceDelta delta = getLastDelta(); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, uint256(params.liquidityDelta) - decreaseLiquidityDelta); assertEq(currency0.balanceOfSelf(), balance0Before + uint256(uint128(delta.amount0()))); @@ -325,9 +317,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { uint256 balance1Before = currency1.balanceOfSelf(); decreaseLiquidity(tokenId, config, decreaseLiquidityDelta, ZERO_BYTES); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, uint256(params.liquidityDelta) - decreaseLiquidityDelta); @@ -366,9 +356,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { decreaseLiquidity(tokenId, config, decreaseLiquidityDelta, ZERO_BYTES); BalanceDelta delta = getLastDelta(); - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 liquidity = _getPositionLiquidity(tokenId, config); assertEq(liquidity, uint256(params.liquidityDelta) - decreaseLiquidityDelta); @@ -453,9 +441,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { vm.stopPrank(); // position liquidity increased - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 newLiq,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 newLiq = _getPositionLiquidity(tokenId, config); assertEq(newLiq, liquidity + liquidityToAdd); // alice paid the tokens @@ -497,9 +483,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { { // position liquidity decreased - bytes32 positionId = - Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); - (uint256 newLiq,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); + uint256 newLiq = _getPositionLiquidity(tokenId, config); assertEq(newLiq, liquidity - liquidityToRemove); } diff --git a/test/shared/PosmTestSetup.sol b/test/shared/PosmTestSetup.sol index 4cfe740f..368cc1d8 100644 --- a/test/shared/PosmTestSetup.sol +++ b/test/shared/PosmTestSetup.sol @@ -5,18 +5,25 @@ import "forge-std/Test.sol"; import {Currency} from "@uniswap/v4-core/src/types/Currency.sol"; import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol"; import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; +import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol"; +import {PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol"; import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; import {Deployers} from "@uniswap/v4-core/test/utils/Deployers.sol"; +import {Position} from "@uniswap/v4-core/src/libraries/Position.sol"; import {PositionManager} from "../../src/PositionManager.sol"; import {IERC20} from "forge-std/interfaces/IERC20.sol"; import {LiquidityOperations} from "./LiquidityOperations.sol"; import {IAllowanceTransfer} from "permit2/src/interfaces/IAllowanceTransfer.sol"; import {DeployPermit2} from "permit2/test/utils/DeployPermit2.sol"; import {HookSavesDelta} from "./HookSavesDelta.sol"; +import {PositionConfig} from "../../src/libraries/PositionConfig.sol"; /// @notice A shared test contract that wraps the v4-core deployers contract and exposes basic liquidity operations on posm. contract PosmTestSetup is Test, Deployers, DeployPermit2, LiquidityOperations { + using StateLibrary for IPoolManager; + using PoolIdLibrary for PoolKey; + uint256 constant STARTING_USER_BALANCE = 10_000_000 ether; IAllowanceTransfer permit2; @@ -75,4 +82,13 @@ contract PosmTestSetup is Test, Deployers, DeployPermit2, LiquidityOperations { delta = delta + hook.deltas(i); } } + + function _getPositionLiquidity(uint256 tokenId, PositionConfig memory _config) + internal + returns (uint128 liquidity) + { + bytes32 positionId = + Position.calculatePositionKey(address(lpm), _config.tickLower, _config.tickUpper, bytes32(tokenId)); + liquidity = manager.getPositionLiquidity(_config.poolKey.toId(), positionId); + } }