Skip to content

Commit

Permalink
optimization: avoid unnecessary refund call after adding liquidity
Browse files Browse the repository at this point in the history
  • Loading branch information
chefburger committed Jul 9, 2024
1 parent a7773d4 commit e28b5b3
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/pool-cl/CLMigrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ contract CLMigrator is ICLMigrator, BaseMigrator {
v4MintParams.poolKey.currency0, v4MintParams.poolKey.currency1, extraAmount0, extraAmount1
);

uint256 amount0Desired = amount0Received + extraAmount0;
uint256 amount1Desired = amount1Received + extraAmount1;
uint256 amount0In = amount0Received + extraAmount0;
uint256 amount1In = amount1Received + extraAmount1;
INonfungiblePositionManager.MintParams memory mintParams = INonfungiblePositionManager.MintParams({
poolKey: v4MintParams.poolKey,
tickLower: v4MintParams.tickLower,
tickUpper: v4MintParams.tickUpper,
salt: v4MintParams.salt,
amount0Desired: amount0Desired,
amount1Desired: amount1Desired,
amount0Desired: amount0In,
amount1Desired: amount1In,
amount0Min: v4MintParams.amount0Min,
amount1Min: v4MintParams.amount1Min,
recipient: v4MintParams.recipient,
Expand All @@ -46,11 +46,11 @@ contract CLMigrator is ICLMigrator, BaseMigrator {

// refund if necessary, ETH is supported by CurrencyLib
unchecked {
if (amount0Desired > amount0Consumed) {
v4MintParams.poolKey.currency0.transfer(v4MintParams.recipient, amount0Desired - amount0Consumed);
if (amount0In > amount0Consumed) {
v4MintParams.poolKey.currency0.transfer(v4MintParams.recipient, amount0In - amount0Consumed);
}
if (amount1Desired > amount1Consumed) {
v4MintParams.poolKey.currency1.transfer(v4MintParams.recipient, amount1Desired - amount1Consumed);
if (amount1In > amount1Consumed) {
v4MintParams.poolKey.currency1.transfer(v4MintParams.recipient, amount1In - amount1Consumed);
}
}
}
Expand All @@ -77,15 +77,15 @@ contract CLMigrator is ICLMigrator, BaseMigrator {
v4MintParams.poolKey.currency0, v4MintParams.poolKey.currency1, extraAmount0, extraAmount1
);

uint256 amount0Desired = amount0Received + extraAmount0;
uint256 amount1Desired = amount1Received + extraAmount1;
uint256 amount0In = amount0Received + extraAmount0;
uint256 amount1In = amount1Received + extraAmount1;
INonfungiblePositionManager.MintParams memory mintParams = INonfungiblePositionManager.MintParams({
poolKey: v4MintParams.poolKey,
tickLower: v4MintParams.tickLower,
tickUpper: v4MintParams.tickUpper,
salt: v4MintParams.salt,
amount0Desired: amount0Desired,
amount1Desired: amount1Desired,
amount0Desired: amount0In,
amount1Desired: amount1In,
amount0Min: v4MintParams.amount0Min,
amount1Min: v4MintParams.amount1Min,
recipient: v4MintParams.recipient,
Expand All @@ -95,11 +95,11 @@ contract CLMigrator is ICLMigrator, BaseMigrator {

// refund if necessary, ETH is supported by CurrencyLib
unchecked {
if (amount0Desired > amount0Consumed) {
v4MintParams.poolKey.currency0.transfer(v4MintParams.recipient, amount0Desired - amount0Consumed);
if (amount0In > amount0Consumed) {
v4MintParams.poolKey.currency0.transfer(v4MintParams.recipient, amount0In - amount0Consumed);
}
if (amount1Desired > amount1Consumed) {
v4MintParams.poolKey.currency1.transfer(v4MintParams.recipient, amount1Desired - amount1Consumed);
if (amount1In > amount1Consumed) {
v4MintParams.poolKey.currency1.transfer(v4MintParams.recipient, amount1In - amount1Consumed);
}
}
}
Expand All @@ -117,7 +117,9 @@ contract CLMigrator is ICLMigrator, BaseMigrator {

(tokenId, liquidity, amount0Consumed, amount1Consumed) =
nonfungiblePositionManager.mint{value: nativePair ? params.amount0Desired : 0}(params);
if (nativePair) {

// receive surplus ETH from positionManager
if (nativePair && params.amount0Desired > amount0Consumed) {
nonfungiblePositionManager.refundETH();
}
}
Expand Down

0 comments on commit e28b5b3

Please sign in to comment.