Skip to content

Commit

Permalink
refactor: restructure baseMigrator a bit so that more can be reused a…
Browse files Browse the repository at this point in the history
…cross pool type
  • Loading branch information
chefburger committed Jul 9, 2024
1 parent a548dfc commit dd3a7ea
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
735666
735570
Original file line number Diff line number Diff line change
@@ -1 +1 @@
692529
692493
Original file line number Diff line number Diff line change
@@ -1 +1 @@
736981
736950
Original file line number Diff line number Diff line change
@@ -1 +1 @@
792404
792734
Original file line number Diff line number Diff line change
@@ -1 +1 @@
751819
752222
Original file line number Diff line number Diff line change
@@ -1 +1 @@
793762
794168
Original file line number Diff line number Diff line change
@@ -1 +1 @@
735678
735582
Original file line number Diff line number Diff line change
@@ -1 +1 @@
692541
692505
Original file line number Diff line number Diff line change
@@ -1 +1 @@
736978
736947
Original file line number Diff line number Diff line change
@@ -1 +1 @@
790386
790716
Original file line number Diff line number Diff line change
@@ -1 +1 @@
749801
750204
Original file line number Diff line number Diff line change
@@ -1 +1 @@
791741
792146
37 changes: 23 additions & 14 deletions src/base/BaseMigrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,41 @@ contract BaseMigrator is IBaseMigrator, PeripheryImmutableState, Multicall, Self
}
}

function withdrawLiquidityFromV3(
address nfp,
IV3NonfungiblePositionManager.DecreaseLiquidityParams memory decreaseLiquidityParams,
bool collectFee
) internal returns (uint256 amount0Received, uint256 amount1Received) {
function withdrawLiquidityFromV3(V3PoolParams calldata v3PoolParams)
internal
returns (uint256 amount0Received, uint256 amount1Received)
{
IV3NonfungiblePositionManager nfp = IV3NonfungiblePositionManager(v3PoolParams.nfp);
uint256 tokenId = v3PoolParams.tokenId;
///@dev make sure the caller is the owner of the token
/// otherwise once the token is approved to migrator, anyone can steal money through this function
if (msg.sender != IV3NonfungiblePositionManager(nfp).ownerOf(decreaseLiquidityParams.tokenId)) {
if (msg.sender != nfp.ownerOf(tokenId)) {
revert NOT_TOKEN_OWNER();
}

/// @notice decrease liquidity from v3#nfp, make sure migrator has been approved
(amount0Received, amount1Received) =
IV3NonfungiblePositionManager(nfp).decreaseLiquidity(decreaseLiquidityParams);
IV3NonfungiblePositionManager.DecreaseLiquidityParams memory decreaseLiquidityParams =
IV3NonfungiblePositionManager.DecreaseLiquidityParams({
tokenId: tokenId,
liquidity: v3PoolParams.liquidity,
amount0Min: v3PoolParams.amount0Min,
amount1Min: v3PoolParams.amount1Min,
deadline: v3PoolParams.deadline
});
(amount0Received, amount1Received) = nfp.decreaseLiquidity(decreaseLiquidityParams);

/// @notice collect tokens from v3#nfp (including fee if necessary)
IV3NonfungiblePositionManager.CollectParams memory collectParams = IV3NonfungiblePositionManager.CollectParams({
tokenId: decreaseLiquidityParams.tokenId,
tokenId: tokenId,
recipient: address(this),
amount0Max: collectFee ? type(uint128).max : SafeCast.toUint128(amount0Received),
amount1Max: collectFee ? type(uint128).max : SafeCast.toUint128(amount1Received)
amount0Max: v3PoolParams.collectFee ? type(uint128).max : SafeCast.toUint128(amount0Received),
amount1Max: v3PoolParams.collectFee ? type(uint128).max : SafeCast.toUint128(amount1Received)
});

(amount0Received, amount1Received) = IV3NonfungiblePositionManager(nfp).collect(collectParams);
(amount0Received, amount1Received) = nfp.collect(collectParams);

/// @notice the order may mismatch with v4 pool when WETH is invovled
/// the following check makes sure that the output always match the order of v4 pool
(,,, address token1,,,,,,,,) = IV3NonfungiblePositionManager(nfp).positions(decreaseLiquidityParams.tokenId);
(,,, address token1,,,,,,,,) = nfp.positions(tokenId);
if (token1 == WETH9) {
(amount0Received, amount1Received) = (amount1Received, amount0Received);
}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/IBaseMigrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ interface IBaseMigrator is IPeripheryImmutableState, IMulticall, ISelfPermit {
uint256 amount1Min;
// decide whether to collect fee
bool collectFee;
uint256 deadline;
}
}
11 changes: 1 addition & 10 deletions src/pool-cl/CLMigrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,7 @@ contract CLMigrator is ICLMigrator, BaseMigrator {
uint256 extraAmount0,
uint256 extraAmount1
) external payable override {
IV3NonfungiblePositionManager.DecreaseLiquidityParams memory decreaseLiquidityParams =
IV3NonfungiblePositionManager.DecreaseLiquidityParams({
tokenId: v3PoolParams.tokenId,
liquidity: v3PoolParams.liquidity,
amount0Min: v3PoolParams.amount0Min,
amount1Min: v3PoolParams.amount1Min,
deadline: v4MintParams.deadline
});
(uint256 amount0Received, uint256 amount1Received) =
withdrawLiquidityFromV3(v3PoolParams.nfp, decreaseLiquidityParams, v3PoolParams.collectFee);
(uint256 amount0Received, uint256 amount1Received) = withdrawLiquidityFromV3(v3PoolParams);

/// @notice if user mannually specify the price range, they need to send extra token
batchAndNormalizeTokens(
Expand Down
24 changes: 16 additions & 8 deletions test/pool-cl/migrator/CLMigratorFromV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ abstract contract CLMigratorFromV3 is OldVersionHelper, GasSnapshot {
liquidity: liquidityFromV3Before,
amount0Min: 9.9 ether,
amount1Min: 9.9 ether,
collectFee: false
collectFee: false,
deadline: block.timestamp + 100
});

ICLMigrator.V4CLPoolParams memory v4MintParams = ICLMigrator.V4CLPoolParams({
Expand Down Expand Up @@ -218,7 +219,8 @@ abstract contract CLMigratorFromV3 is OldVersionHelper, GasSnapshot {
liquidity: liquidityFromV3Before,
amount0Min: 9.9 ether,
amount1Min: 9.9 ether,
collectFee: false
collectFee: false,
deadline: block.timestamp + 100
});

ICLMigrator.V4CLPoolParams memory v4MintParams = ICLMigrator.V4CLPoolParams({
Expand Down Expand Up @@ -297,7 +299,8 @@ abstract contract CLMigratorFromV3 is OldVersionHelper, GasSnapshot {
liquidity: liquidityFromV3Before,
amount0Min: 9.9 ether,
amount1Min: 9.9 ether,
collectFee: false
collectFee: false,
deadline: block.timestamp + 100
});

ICLMigrator.V4CLPoolParams memory v4MintParams = ICLMigrator.V4CLPoolParams({
Expand Down Expand Up @@ -375,7 +378,8 @@ abstract contract CLMigratorFromV3 is OldVersionHelper, GasSnapshot {
liquidity: liquidityFromV3Before,
amount0Min: 9.9 ether,
amount1Min: 9.9 ether,
collectFee: false
collectFee: false,
deadline: block.timestamp + 100
});

ICLMigrator.V4CLPoolParams memory v4MintParams = ICLMigrator.V4CLPoolParams({
Expand Down Expand Up @@ -463,7 +467,8 @@ abstract contract CLMigratorFromV3 is OldVersionHelper, GasSnapshot {
liquidity: liquidityFromV3Before,
amount0Min: 9.9 ether,
amount1Min: 9.9 ether,
collectFee: false
collectFee: false,
deadline: block.timestamp + 100
});

ICLMigrator.V4CLPoolParams memory v4MintParams = ICLMigrator.V4CLPoolParams({
Expand Down Expand Up @@ -553,7 +558,8 @@ abstract contract CLMigratorFromV3 is OldVersionHelper, GasSnapshot {
liquidity: liquidityFromV3Before,
amount0Min: 0,
amount1Min: 0,
collectFee: false
collectFee: false,
deadline: block.timestamp + 100
});

ICLMigrator.V4CLPoolParams memory v4MintParams = ICLMigrator.V4CLPoolParams({
Expand Down Expand Up @@ -640,7 +646,8 @@ abstract contract CLMigratorFromV3 is OldVersionHelper, GasSnapshot {
liquidity: liquidityFromV3Before,
amount0Min: 0,
amount1Min: 0,
collectFee: false
collectFee: false,
deadline: block.timestamp + 100
});

ICLMigrator.V4CLPoolParams memory v4MintParams = ICLMigrator.V4CLPoolParams({
Expand Down Expand Up @@ -729,7 +736,8 @@ abstract contract CLMigratorFromV3 is OldVersionHelper, GasSnapshot {
liquidity: liquidityFromV3Before / 2,
amount0Min: 9.9 ether / 2,
amount1Min: 9.9 ether / 2,
collectFee: false
collectFee: false,
deadline: block.timestamp + 100
});

ICLMigrator.V4CLPoolParams memory v4MintParams = ICLMigrator.V4CLPoolParams({
Expand Down

0 comments on commit dd3a7ea

Please sign in to comment.