Skip to content

Commit

Permalink
Add salt to modifyLiquidity params (#608)
Browse files Browse the repository at this point in the history
* add salt poc and tests

* update gas test

* fmt

* gas snaps

* use lastCallGas

* update

* add .snap

* use external

* use external

* use overloaded func

* use isolate

* assertEq, update gas snaps

* use scratch space, add multi router test

* undo lib/forge-std

* comments:

* update gas
  • Loading branch information
snreynolds authored May 9, 2024
1 parent 50e7be5 commit 241e025
Show file tree
Hide file tree
Showing 39 changed files with 252 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
146357
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
278788
279342
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136156
136584
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
145947
146333
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
294885
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 @@
108839
108818
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 @@
149324
149260
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 @@
57332
57354
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 @@
59565
59587
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22548
22710
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115868
116427
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
113155
113605
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115856
116415
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 @@
130538
130629
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148889
148937
Original file line number Diff line number Diff line change
@@ -1 +1 @@
113433
113524
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124803
124851
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136807
136898
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn native 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125922
126013
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint native output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148001
147983
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
164904
164886
Original file line number Diff line number Diff line change
@@ -1 +1 @@
224124
224212
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 @@
149105
149153
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124815
124863
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with lp fee and protocol fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181394
181496
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 @@
159641
159743
11 changes: 6 additions & 5 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,22 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
}

/// @inheritdoc IPoolManager
function getLiquidity(PoolId id, address _owner, int24 tickLower, int24 tickUpper)
function getLiquidity(PoolId id, address _owner, int24 tickLower, int24 tickUpper, bytes32 salt)
external
view
override
returns (uint128 liquidity)
{
return pools[id].positions.get(_owner, tickLower, tickUpper).liquidity;
return pools[id].positions.get(_owner, tickLower, tickUpper, salt).liquidity;
}

function getPosition(PoolId id, address _owner, int24 tickLower, int24 tickUpper)
function getPosition(PoolId id, address _owner, int24 tickLower, int24 tickUpper, bytes32 salt)
external
view
override
returns (Position.Info memory position)
{
return pools[id].positions.get(_owner, tickLower, tickUpper);
return pools[id].positions.get(_owner, tickLower, tickUpper, salt);
}

/// @inheritdoc IPoolManager
Expand Down Expand Up @@ -239,7 +239,8 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
tickLower: params.tickLower,
tickUpper: params.tickUpper,
liquidityDelta: params.liquidityDelta.toInt128(),
tickSpacing: key.tickSpacing
tickSpacing: key.tickSpacing,
salt: params.salt
})
);

Expand Down
6 changes: 4 additions & 2 deletions src/interfaces/IPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload {
function getLiquidity(PoolId id) external view returns (uint128 liquidity);

/// @notice Get the current value of liquidity for the specified pool and position
function getLiquidity(PoolId id, address owner, int24 tickLower, int24 tickUpper)
function getLiquidity(PoolId id, address owner, int24 tickLower, int24 tickUpper, bytes32 salt)
external
view
returns (uint128 liquidity);
Expand All @@ -115,7 +115,7 @@ interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload {
returns (uint256 feeGrowthGlobal0, uint256 feeGrowthGlobal1);

/// @notice Get the position struct for a specified pool and position
function getPosition(PoolId id, address owner, int24 tickLower, int24 tickUpper)
function getPosition(PoolId id, address owner, int24 tickLower, int24 tickUpper, bytes32 salt)
external
view
returns (Position.Info memory position);
Expand Down Expand Up @@ -152,6 +152,8 @@ interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload {
int24 tickUpper;
// how to modify the liquidity
int256 liquidityDelta;
// a value to set if you want unique liquidity positions at the same range
bytes32 salt;
}

/// @notice Modify the liquidity for the given pool
Expand Down
4 changes: 3 additions & 1 deletion src/libraries/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ library Pool {
int128 liquidityDelta;
// the spacing between ticks
int24 tickSpacing;
// used to distinguish positions of the same owner, at the same tick range
bytes32 salt;
}

struct ModifyLiquidityState {
Expand Down Expand Up @@ -200,7 +202,7 @@ library Pool {

(state.feeGrowthInside0X128, state.feeGrowthInside1X128) = getFeeGrowthInside(self, tickLower, tickUpper);

Position.Info storage position = self.positions.get(params.owner, tickLower, tickUpper);
Position.Info storage position = self.positions.get(params.owner, tickLower, tickUpper, params.salt);
(feesOwed0, feesOwed1) =
position.update(liquidityDelta, state.feeGrowthInside0X128, state.feeGrowthInside1X128);

Expand Down
10 changes: 7 additions & 3 deletions src/libraries/Position.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ library Position {
/// @param owner The address of the position owner
/// @param tickLower The lower tick boundary of the position
/// @param tickUpper The upper tick boundary of the position
/// @param salt A unique value to differentiate between multiple positions in the same range
/// @return position The position info struct of the given owners' position
function get(mapping(bytes32 => Info) storage self, address owner, int24 tickLower, int24 tickUpper)
function get(mapping(bytes32 => Info) storage self, address owner, int24 tickLower, int24 tickUpper, bytes32 salt)
internal
view
returns (Info storage position)
{
// positionKey = keccak256(abi.encodePacked(owner, tickLower, tickUpper))
// positionKey = keccak256(abi.encodePacked(owner, tickLower, tickUpper, salt))
bytes32 positionKey;

/// @solidity memory-safe-assembly
assembly {
mstore(0x26, salt) // [0x26, 0x46)
mstore(0x06, tickUpper) // [0x23, 0x26)
mstore(0x03, tickLower) // [0x20, 0x23)
mstore(0, owner) // [0x0c, 0x20)
positionKey := keccak256(0x0c, 0x1a)
positionKey := keccak256(0x0c, 0x3a) // len is 58 bytes
mstore(0x26, 0) // rewrite 0x26 to 0
}
position = self[positionKey];
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/PoolNestedActionsTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ contract NestedActionExecutor is Test, PoolTestBase {
error KeyNotSet();

IPoolManager.ModifyLiquidityParams internal ADD_LIQ_PARAMS =
IPoolManager.ModifyLiquidityParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1e18});
IPoolManager.ModifyLiquidityParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1e18, salt: 0});

IPoolManager.ModifyLiquidityParams internal REMOVE_LIQ_PARAMS =
IPoolManager.ModifyLiquidityParams({tickLower: -120, tickUpper: 120, liquidityDelta: -1e18});
IPoolManager.ModifyLiquidityParams({tickLower: -120, tickUpper: 120, liquidityDelta: -1e18, salt: 0});

IPoolManager.SwapParams internal SWAP_PARAMS =
IPoolManager.SwapParams({zeroForOne: true, amountSpecified: -100, sqrtPriceLimitX96: Constants.SQRT_RATIO_1_2});
Expand Down
2 changes: 1 addition & 1 deletion src/test/SkipCallsTestHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ contract SkipCallsTestHook is BaseTestHooks, Test {
) public {
// first hook needs to add liquidity for itself
IPoolManager.ModifyLiquidityParams memory newParams =
IPoolManager.ModifyLiquidityParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1e18});
IPoolManager.ModifyLiquidityParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1e18, salt: 0});
IPoolManager(manager).modifyLiquidity(key, newParams, hookData);
// hook removes liquidity
IPoolManager(manager).modifyLiquidity(key, params, hookData);
Expand Down
Loading

0 comments on commit 241e025

Please sign in to comment.