Skip to content

Commit

Permalink
Refactor modifyLiquidity function in Pool (#604)
Browse files Browse the repository at this point in the history
This change optimizes the `modifyLiquidity` function in the Pool.sol file. Local variables were moved, improving code readability and making the code more cohesive. Variables in memory were moved on the stack, improving performance. Corresponding snapshot values were also updated to reflect these changes.
  • Loading branch information
shuhuiluo authored May 11, 2024
1 parent 8b4473e commit 8dfc536
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151456
151376
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
329809
329729
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 @@
284524
284443
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 @@
141632
141552
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151432
151352
Original file line number Diff line number Diff line change
@@ -1 +1 @@
299984
299904
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23627
23601
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185379
185299
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 @@
121413
121333
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 @@
118188
118108
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121401
121321
21 changes: 10 additions & 11 deletions src/libraries/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ library Pool {
uint128 liquidityGrossAfterLower;
bool flippedUpper;
uint128 liquidityGrossAfterUpper;
uint256 feeGrowthInside0X128;
uint256 feeGrowthInside1X128;
}

/// @notice Effect changes to a position in a pool
Expand All @@ -167,8 +165,6 @@ library Pool {
int24 tickUpper = params.tickUpper;
checkTicks(tickLower, tickUpper);

uint256 feesOwed0;
uint256 feesOwed1;
{
ModifyLiquidityState memory state;

Expand Down Expand Up @@ -197,11 +193,17 @@ library Pool {
}
}

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

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

// Fees earned from LPing are added to the user's currency delta.
feeDelta = toBalanceDelta(feesOwed0.toInt128(), feesOwed1.toInt128());
}

// clear any tick data that is no longer needed
if (liquidityDelta < 0) {
Expand Down Expand Up @@ -246,9 +248,6 @@ library Pool {
);
}
}

// Fees earned from LPing are added to the user's currency delta.
feeDelta = toBalanceDelta(feesOwed0.toInt128(), feesOwed1.toInt128());
}

struct SwapCache {
Expand Down

0 comments on commit 8dfc536

Please sign in to comment.