Skip to content

Commit

Permalink
feat: support salt for cl-pool nfpManager
Browse files Browse the repository at this point in the history
  • Loading branch information
chefburger committed Jun 7, 2024
1 parent 5ed3f39 commit 22c8d6f
Show file tree
Hide file tree
Showing 36 changed files with 216 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185311
182676
Original file line number Diff line number Diff line change
@@ -1 +1 @@
638186
636695
Original file line number Diff line number Diff line change
@@ -1 +1 @@
906704
904184
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1274539
1270878
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148991
147212
Original file line number Diff line number Diff line change
@@ -1 +1 @@
216053
213764
Original file line number Diff line number Diff line change
@@ -1 +1 @@
241767
239002
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54723
53954
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52095
51680
2 changes: 1 addition & 1 deletion .forge-snapshots/BinFungibleTokenTest#testBurn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26896
27059
2 changes: 1 addition & 1 deletion .forge-snapshots/BinFungibleTokenTest#testMint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
67373
67621
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154616
152292
Original file line number Diff line number Diff line change
@@ -1 +1 @@
145793
143954
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151891
149662
Original file line number Diff line number Diff line change
@@ -1 +1 @@
152610
150280
Original file line number Diff line number Diff line change
@@ -1 +1 @@
152651
150337
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179342
176041
Original file line number Diff line number Diff line change
@@ -1 +1 @@
164893
161837
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162899
159837
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162936
159885
Original file line number Diff line number Diff line change
@@ -1 +1 @@
193297
188524
Original file line number Diff line number Diff line change
@@ -1 +1 @@
155207
152243
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactInput.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
255442
251030
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192524
189735
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactOutput.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
260676
257088
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
191719
189249
2 changes: 1 addition & 1 deletion .forge-snapshots/NonfungiblePositionManager#burn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62578
64452
2 changes: 1 addition & 1 deletion .forge-snapshots/NonfungiblePositionManager#collect.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
265663
266684
Original file line number Diff line number Diff line change
@@ -1 +1 @@
161440
160846
Original file line number Diff line number Diff line change
@@ -1 +1 @@
195159
194416
2 changes: 1 addition & 1 deletion .forge-snapshots/NonfungiblePositionManager#mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
607379
607284
35 changes: 20 additions & 15 deletions src/pool-cl/NonfungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ contract NonfungiblePositionManager is
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
uint128 tokensOwed1,
bytes32 salt
)
{
Position memory position = _positions[tokenId];
Expand All @@ -97,7 +98,8 @@ contract NonfungiblePositionManager is
position.feeGrowthInside0LastX128,
position.feeGrowthInside1LastX128,
position.tokensOwed0,
position.tokensOwed1
position.tokensOwed1,
position.salt
);
}

Expand Down Expand Up @@ -224,6 +226,7 @@ contract NonfungiblePositionManager is
poolKey: params.poolKey,
tickLower: params.tickLower,
tickUpper: params.tickUpper,
salt: params.salt,
amount0Desired: params.amount0Desired,
amount1Desired: params.amount1Desired,
amount0Min: params.amount0Min,
Expand All @@ -234,9 +237,8 @@ contract NonfungiblePositionManager is
uint256 tokenId = _nextId++;
_mint(params.recipient, tokenId);

// todo: about the salt
CLPosition.Info memory positionInfo = poolManager.getPosition(
params.poolKey.toId(), address(this), params.tickLower, params.tickUpper, bytes32(0)
params.poolKey.toId(), address(this), params.tickLower, params.tickUpper, params.salt
);
_positions[tokenId] = Position({
nonce: 0,
Expand All @@ -248,7 +250,8 @@ contract NonfungiblePositionManager is
feeGrowthInside0LastX128: positionInfo.feeGrowthInside0LastX128,
feeGrowthInside1LastX128: positionInfo.feeGrowthInside1LastX128,
tokensOwed0: 0,
tokensOwed1: 0
tokensOwed1: 0,
salt: params.salt
});
if (address(_poolIdToPoolKey[params.poolKey.toId()].poolManager) == address(0)) {
_poolIdToPoolKey[params.poolKey.toId()] = params.poolKey;
Expand All @@ -263,26 +266,27 @@ contract NonfungiblePositionManager is
IncreaseLiquidityParams memory params = abi.decode(data.params, (IncreaseLiquidityParams));
Position storage nftPosition = _positions[params.tokenId];
PoolId poolId = nftPosition.poolId;
PoolKey memory poolKey = _poolIdToPoolKey[poolId];
uint128 existingLiquility = nftPosition.liquidity;
int24 tickLower = nftPosition.tickLower;
int24 tickUpper = nftPosition.tickUpper;
bytes32 salt = nftPosition.salt;
PoolKey memory poolKey = _poolIdToPoolKey[poolId];

(uint128 liquidity, BalanceDelta delta) = addLiquidity(
AddLiquidityParams({
poolKey: poolKey,
tickLower: tickLower,
tickUpper: tickUpper,
salt: salt,
amount0Desired: params.amount0Desired,
amount1Desired: params.amount1Desired,
amount0Min: params.amount0Min,
amount1Min: params.amount1Min
})
);

// todo: about the salt
CLPosition.Info memory poolManagerPositionInfo =
poolManager.getPosition(poolId, address(this), tickLower, tickUpper, bytes32(0));
poolManager.getPosition(poolId, address(this), tickLower, tickUpper, salt);

/// @dev This can be overflow in following cases:
/// 1. feeGrowthInside0LastX128 is overflow
Expand Down Expand Up @@ -323,10 +327,11 @@ contract NonfungiblePositionManager is
DecreaseLiquidityParams memory params = abi.decode(data.params, (DecreaseLiquidityParams));
Position storage nftPosition = _positions[params.tokenId];
PoolId poolId = nftPosition.poolId;
PoolKey memory poolKey = _poolIdToPoolKey[poolId];
uint128 liquidity = nftPosition.liquidity;
int24 tickLower = nftPosition.tickLower;
int24 tickUpper = nftPosition.tickUpper;
bytes32 salt = nftPosition.salt;
PoolKey memory poolKey = _poolIdToPoolKey[poolId];

if (liquidity < params.liquidity) {
revert InvalidLiquidityDecreaseAmount();
Expand All @@ -339,13 +344,13 @@ contract NonfungiblePositionManager is
tickUpper: tickUpper,
liquidity: params.liquidity,
amount0Min: params.amount0Min,
amount1Min: params.amount1Min
amount1Min: params.amount1Min,
salt: salt
})
);

// todo: about the salt
CLPosition.Info memory poolManagerPositionInfo =
poolManager.getPosition(poolId, address(this), tickLower, tickUpper, bytes32(0));
poolManager.getPosition(poolId, address(this), tickLower, tickUpper, salt);

/// @dev This can be overflow in following cases:
/// 1. feeGrowthInside0LastX128 is overflow
Expand Down Expand Up @@ -390,17 +395,17 @@ contract NonfungiblePositionManager is
Position storage nftPosition = _positions[params.tokenId];
Position memory nftPositionCache = _positions[params.tokenId];
PoolId poolId = nftPositionCache.poolId;
bytes32 salt = nftPositionCache.salt;
PoolKey memory poolKey = _poolIdToPoolKey[poolId];

uint128 tokensOwed0 = nftPositionCache.tokensOwed0;
uint128 tokensOwed1 = nftPositionCache.tokensOwed1;

if (nftPositionCache.liquidity > 0) {
mintAccumulatedPositionFee(poolKey, nftPositionCache.tickLower, nftPositionCache.tickUpper);
mintAccumulatedPositionFee(poolKey, nftPositionCache.tickLower, nftPositionCache.tickUpper, salt);

// todo: about the salt
CLPosition.Info memory poolManagerPositionInfo = poolManager.getPosition(
poolId, address(this), nftPositionCache.tickLower, nftPositionCache.tickUpper, bytes32(0)
poolId, address(this), nftPositionCache.tickLower, nftPositionCache.tickUpper, salt
);

/// @dev This can be overflow in following cases:
Expand Down
19 changes: 11 additions & 8 deletions src/pool-cl/base/LiquidityManagement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ import {LiquidityAmounts} from "../libraries/LiquidityAmounts.sol";
abstract contract LiquidityManagement is CLPeripheryImmutableState, PeripheryPayments {
using PoolIdLibrary for PoolKey;

// todo: think if salt require non zero byte
bytes32 constant SALT_0 = bytes32(0);

error PriceSlippageCheckFailed();

struct AddLiquidityParams {
PoolKey poolKey;
int24 tickLower;
int24 tickUpper;
bytes32 salt;
uint256 amount0Desired;
uint256 amount1Desired;
uint256 amount0Min;
Expand All @@ -42,16 +40,19 @@ abstract contract LiquidityManagement is CLPeripheryImmutableState, PeripheryPay
uint128 liquidity;
uint256 amount0Min;
uint256 amount1Min;
bytes32 salt;
}

/// @notice Claim accumulated fees from the position and mint them to the NFP contract
function mintAccumulatedPositionFee(PoolKey memory poolKey, int24 tickLower, int24 tickUpper) internal {
function mintAccumulatedPositionFee(PoolKey memory poolKey, int24 tickLower, int24 tickUpper, bytes32 salt)
internal
{
CLPosition.Info memory poolManagerPositionInfo =
poolManager.getPosition(poolKey.toId(), address(this), tickLower, tickUpper, SALT_0);
poolManager.getPosition(poolKey.toId(), address(this), tickLower, tickUpper, salt);

if (poolManagerPositionInfo.liquidity > 0) {
(, BalanceDelta feeDelta) = poolManager.modifyLiquidity(
poolKey, ICLPoolManager.ModifyLiquidityParams(tickLower, tickUpper, 0, SALT_0), ""
poolKey, ICLPoolManager.ModifyLiquidityParams(tickLower, tickUpper, 0, salt), ""
);

mintFeeDelta(poolKey, feeDelta);
Expand Down Expand Up @@ -82,7 +83,9 @@ abstract contract LiquidityManagement is CLPeripheryImmutableState, PeripheryPay
BalanceDelta feeDelta;
(delta, feeDelta) = poolManager.modifyLiquidity(
params.poolKey,
ICLPoolManager.ModifyLiquidityParams(params.tickLower, params.tickUpper, int256(uint256(liquidity)), SALT_0),
ICLPoolManager.ModifyLiquidityParams(
params.tickLower, params.tickUpper, int256(uint256(liquidity)), params.salt
),
""
);

Expand All @@ -105,7 +108,7 @@ abstract contract LiquidityManagement is CLPeripheryImmutableState, PeripheryPay
(delta, feeDelta) = poolManager.modifyLiquidity(
params.poolKey,
ICLPoolManager.ModifyLiquidityParams(
params.tickLower, params.tickUpper, -int256(uint256(params.liquidity)), SALT_0
params.tickLower, params.tickUpper, -int256(uint256(params.liquidity)), params.salt
),
""
);
Expand Down
5 changes: 4 additions & 1 deletion src/pool-cl/interfaces/INonfungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ interface INonfungiblePositionManager is
// how many uncollected tokens are owed to the position, as of the last computation
uint128 tokensOwed0;
uint128 tokensOwed1;
bytes32 salt;
}

/// @notice Returns the position information associated with a given token ID.
Expand All @@ -105,7 +106,8 @@ interface INonfungiblePositionManager is
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
uint128 tokensOwed1,
bytes32 salt
);

/// @notice Initialize the pool state for a given pool ID.
Expand All @@ -123,6 +125,7 @@ interface INonfungiblePositionManager is
PoolKey poolKey;
int24 tickLower;
int24 tickUpper;
bytes32 salt;
uint256 amount0Desired;
uint256 amount1Desired;
uint256 amount0Min;
Expand Down
1 change: 1 addition & 0 deletions test/pool-cl/CLSwapRouterInvariant.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ contract CLSwapRouterHandler is Test {
poolKey: pk,
tickLower: -10,
tickUpper: 10,
salt: bytes32(0),
amount0Desired: amt,
amount1Desired: amt,
amount0Min: 0,
Expand Down
Loading

0 comments on commit 22c8d6f

Please sign in to comment.