From 10f748e56dc866b713ee6ea1ab59a25eaa52a51b Mon Sep 17 00:00:00 2001 From: saucepoint <98790946+saucepoint@users.noreply.github.com> Date: Sat, 27 Jul 2024 12:08:53 -0400 Subject: [PATCH] use position key helper instead of manually hashing (#213) --- test/StateViewTest.t.sol | 19 ++++++++++--------- test/position-managers/Execute.t.sol | 7 ++++--- test/position-managers/NativeToken.t.sol | 15 ++++++++------- test/position-managers/PositionManager.t.sol | 11 ++++++----- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/test/StateViewTest.t.sol b/test/StateViewTest.t.sol index 746f14fb..8bf00a40 100644 --- a/test/StateViewTest.t.sol +++ b/test/StateViewTest.t.sol @@ -18,6 +18,7 @@ import {TickBitmap} from "@uniswap/v4-core/src/libraries/TickBitmap.sol"; import {FixedPoint128} from "@uniswap/v4-core/src/libraries/FixedPoint128.sol"; import {Deployers} from "@uniswap/v4-core/test/utils/Deployers.sol"; import {Fuzzers} from "@uniswap/v4-core/src/test/Fuzzers.sol"; +import {Position} from "@uniswap/v4-core/src/libraries/Position.sol"; import {StateView} from "../src/lens/StateView.sol"; @@ -289,7 +290,7 @@ contract StateViewTest is Test, Deployers, Fuzzers, GasSnapshot { modifyLiquidityRouter.modifyLiquidity(key, IPoolManager.ModifyLiquidityParams(-60, 60, 0, 0), ZERO_BYTES); bytes32 positionId = - keccak256(abi.encodePacked(address(modifyLiquidityRouter), int24(-60), int24(60), bytes32(0))); + Position.calculatePositionKey(address(modifyLiquidityRouter), int24(-60), int24(60), bytes32(0)); (uint128 liquidity, uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) = state.getPositionInfo(poolId, positionId); @@ -325,8 +326,8 @@ contract StateViewTest is Test, Deployers, Fuzzers, GasSnapshot { key, IPoolManager.ModifyLiquidityParams(_params.tickLower, _params.tickUpper, 0, 0), ZERO_BYTES ); - bytes32 positionId = keccak256( - abi.encodePacked(address(modifyLiquidityRouter), _params.tickLower, _params.tickUpper, bytes32(0)) + bytes32 positionId = Position.calculatePositionKey( + address(modifyLiquidityRouter), _params.tickLower, _params.tickUpper, bytes32(0) ); (uint128 liquidity, uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) = @@ -462,8 +463,8 @@ contract StateViewTest is Test, Deployers, Fuzzers, GasSnapshot { modifyLiquidityRouter.modifyLiquidity( key, IPoolManager.ModifyLiquidityParams(_params.tickLower, _params.tickUpper, 0, 0), ZERO_BYTES ); - bytes32 positionId = keccak256( - abi.encodePacked(address(modifyLiquidityRouter), _params.tickLower, _params.tickUpper, bytes32(0)) + bytes32 positionId = Position.calculatePositionKey( + address(modifyLiquidityRouter), _params.tickLower, _params.tickUpper, bytes32(0) ); (, uint256 feeGrowthInside0X128_, uint256 feeGrowthInside1X128_) = state.getPositionInfo(poolId, positionId); @@ -506,14 +507,14 @@ contract StateViewTest is Test, Deployers, Fuzzers, GasSnapshot { modifyLiquidityRouter.modifyLiquidity(key, _paramsA, ZERO_BYTES); modifyLiquidityRouter.modifyLiquidity(key, _paramsB, ZERO_BYTES); - bytes32 positionIdA = keccak256( - abi.encodePacked(address(modifyLiquidityRouter), _paramsA.tickLower, _paramsA.tickUpper, bytes32(0)) + bytes32 positionIdA = Position.calculatePositionKey( + address(modifyLiquidityRouter), _paramsA.tickLower, _paramsA.tickUpper, bytes32(0) ); uint128 liquidityA = state.getPositionLiquidity(poolId, positionIdA); assertEq(liquidityA, uint128(uint256(_paramsA.liquidityDelta))); - bytes32 positionIdB = keccak256( - abi.encodePacked(address(modifyLiquidityRouter), _paramsB.tickLower, _paramsB.tickUpper, bytes32(0)) + bytes32 positionIdB = Position.calculatePositionKey( + address(modifyLiquidityRouter), _paramsB.tickLower, _paramsB.tickUpper, bytes32(0) ); uint128 liquidityB = state.getPositionLiquidity(poolId, positionIdB); assertEq(liquidityB, uint128(uint256(_paramsB.liquidityDelta))); diff --git a/test/position-managers/Execute.t.sol b/test/position-managers/Execute.t.sol index 6671f139..718a04b3 100644 --- a/test/position-managers/Execute.t.sol +++ b/test/position-managers/Execute.t.sol @@ -11,6 +11,7 @@ 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 {Position} from "@uniswap/v4-core/src/libraries/Position.sol"; import {IERC20} from "forge-std/interfaces/IERC20.sol"; @@ -65,7 +66,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers { increaseLiquidity(tokenId, config, liquidityToAdd, ZERO_BYTES); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, initialLiquidity + liquidityToAdd); @@ -91,7 +92,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers { lpm.modifyLiquidities(calls, _deadline); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, initialLiquidity + liquidityToAdd + liquidityToAdd2); @@ -113,7 +114,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers { lpm.modifyLiquidities(calls, _deadline); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, initialLiquidity + liquidityToAdd); diff --git a/test/position-managers/NativeToken.t.sol b/test/position-managers/NativeToken.t.sol index b9ed854a..926f124b 100644 --- a/test/position-managers/NativeToken.t.sol +++ b/test/position-managers/NativeToken.t.sol @@ -18,6 +18,7 @@ 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"; @@ -81,7 +82,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { BalanceDelta delta = abi.decode(result[0], (BalanceDelta)); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, uint256(params.liquidityDelta)); @@ -117,7 +118,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { BalanceDelta delta = abi.decode(result[0], (BalanceDelta)); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, uint256(params.liquidityDelta)); @@ -143,7 +144,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { mintWithNative(SQRT_PRICE_1_1, config, liquidityToAdd, address(this), ZERO_BYTES); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, uint256(params.liquidityDelta)); @@ -193,7 +194,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { mintWithNative(SQRT_PRICE_1_1, config, liquidityToAdd, address(this), ZERO_BYTES); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, uint256(params.liquidityDelta)); @@ -255,7 +256,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { // verify position liquidity increased bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, liquidityToAdd + liquidityToAdd); // liquidity was doubled @@ -297,7 +298,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { // verify position liquidity increased bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, liquidityToAdd + liquidityToAdd); // liquidity was doubled @@ -335,7 +336,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { BalanceDelta delta = decreaseLiquidity(tokenId, config, decreaseLiquidityDelta, ZERO_BYTES); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, uint256(params.liquidityDelta) - decreaseLiquidityDelta); diff --git a/test/position-managers/PositionManager.t.sol b/test/position-managers/PositionManager.t.sol index c3fd3a49..cb72ee91 100644 --- a/test/position-managers/PositionManager.t.sol +++ b/test/position-managers/PositionManager.t.sol @@ -13,6 +13,7 @@ import {LiquidityAmounts} from "@uniswap/v4-core/test/utils/LiquidityAmounts.sol import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; import {FixedPointMathLib} from "solmate/src/utils/FixedPointMathLib.sol"; import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; +import {Position} from "@uniswap/v4-core/src/libraries/Position.sol"; import {IERC20} from "forge-std/interfaces/IERC20.sol"; @@ -97,7 +98,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { assertEq(lpm.ownerOf(tokenId), address(this)); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, uint256(params.liquidityDelta)); @@ -176,7 +177,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { assertEq(lpm.ownerOf(1), address(this)); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, uint256(params.liquidityDelta)); @@ -221,7 +222,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { assertEq(lpm.ownerOf(1), address(this)); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, uint256(params.liquidityDelta)); @@ -273,7 +274,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { decreaseLiquidity(tokenId, config, decreaseLiquidityDelta, ZERO_BYTES); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, uint256(params.liquidityDelta) - decreaseLiquidityDelta); @@ -302,7 +303,7 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers { BalanceDelta delta = decreaseLiquidity(tokenId, config, decreaseLiquidityDelta, ZERO_BYTES); bytes32 positionId = - keccak256(abi.encodePacked(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId))); + Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId)); (uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId); assertEq(liquidity, uint256(params.liquidityDelta) - decreaseLiquidityDelta);