Skip to content

Commit

Permalink
use position key helper instead of manually hashing (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint authored Jul 27, 2024
1 parent b973438 commit 10f748e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
19 changes: 10 additions & 9 deletions test/StateViewTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)));
Expand Down
7 changes: 4 additions & 3 deletions test/position-managers/Execute.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions test/position-managers/NativeToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));

Expand All @@ -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));

Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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);

Expand Down
11 changes: 6 additions & 5 deletions test/position-managers/PositionManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 10f748e

Please sign in to comment.