From dd3a7ea0293d10f3f45818ab5ccb5d603a423fb2 Mon Sep 17 00:00:00 2001 From: chefburger Date: Tue, 9 Jul 2024 15:58:06 +0800 Subject: [PATCH] refactor: restructure baseMigrator a bit so that more can be reused across pool type --- ...V2Test#testMigrateFromV2IncludingInit.snap | 2 +- ...apV2Test#testMigrateFromV2WithoutInit.snap | 2 +- ...t#testMigrateFromV2WithoutNativeToken.snap | 2 +- ...V3Test#testMigrateFromV3IncludingInit.snap | 2 +- ...apV3Test#testMigrateFromV3WithoutInit.snap | 2 +- ...t#testMigrateFromV3WithoutNativeToken.snap | 2 +- ...V2Test#testMigrateFromV2IncludingInit.snap | 2 +- ...apV2Test#testMigrateFromV2WithoutInit.snap | 2 +- ...t#testMigrateFromV2WithoutNativeToken.snap | 2 +- ...V3Test#testMigrateFromV3IncludingInit.snap | 2 +- ...apV3Test#testMigrateFromV3WithoutInit.snap | 2 +- ...t#testMigrateFromV3WithoutNativeToken.snap | 2 +- src/base/BaseMigrator.sol | 37 ++++++++++++------- src/interfaces/IBaseMigrator.sol | 1 + src/pool-cl/CLMigrator.sol | 11 +----- test/pool-cl/migrator/CLMigratorFromV3.sol | 24 ++++++++---- 16 files changed, 53 insertions(+), 44 deletions(-) diff --git a/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2IncludingInit.snap b/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2IncludingInit.snap index 8d2d6be..e7da379 100644 --- a/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2IncludingInit.snap +++ b/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2IncludingInit.snap @@ -1 +1 @@ -735666 \ No newline at end of file +735570 \ No newline at end of file diff --git a/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2WithoutInit.snap b/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2WithoutInit.snap index 986bf48..7a68bf4 100644 --- a/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2WithoutInit.snap +++ b/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2WithoutInit.snap @@ -1 +1 @@ -692529 \ No newline at end of file +692493 \ No newline at end of file diff --git a/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2WithoutNativeToken.snap b/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2WithoutNativeToken.snap index 872459e..a5a965d 100644 --- a/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2WithoutNativeToken.snap +++ b/.forge-snapshots/CLMigratorFromPancakeswapV2Test#testMigrateFromV2WithoutNativeToken.snap @@ -1 +1 @@ -736981 \ No newline at end of file +736950 \ No newline at end of file diff --git a/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3IncludingInit.snap b/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3IncludingInit.snap index 6beff83..1339a8f 100644 --- a/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3IncludingInit.snap +++ b/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3IncludingInit.snap @@ -1 +1 @@ -792404 \ No newline at end of file +792734 \ No newline at end of file diff --git a/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3WithoutInit.snap b/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3WithoutInit.snap index 5f4795f..c5787ea 100644 --- a/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3WithoutInit.snap +++ b/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3WithoutInit.snap @@ -1 +1 @@ -751819 \ No newline at end of file +752222 \ No newline at end of file diff --git a/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3WithoutNativeToken.snap b/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3WithoutNativeToken.snap index f6ee269..81bfead 100644 --- a/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3WithoutNativeToken.snap +++ b/.forge-snapshots/CLMigratorFromPancakeswapV3Test#testMigrateFromV3WithoutNativeToken.snap @@ -1 +1 @@ -793762 \ No newline at end of file +794168 \ No newline at end of file diff --git a/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2IncludingInit.snap b/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2IncludingInit.snap index e0f3658..13b947f 100644 --- a/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2IncludingInit.snap +++ b/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2IncludingInit.snap @@ -1 +1 @@ -735678 \ No newline at end of file +735582 \ No newline at end of file diff --git a/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2WithoutInit.snap b/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2WithoutInit.snap index 2d64a4b..7d1afb5 100644 --- a/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2WithoutInit.snap +++ b/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2WithoutInit.snap @@ -1 +1 @@ -692541 \ No newline at end of file +692505 \ No newline at end of file diff --git a/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2WithoutNativeToken.snap b/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2WithoutNativeToken.snap index a742474..c4e1288 100644 --- a/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2WithoutNativeToken.snap +++ b/.forge-snapshots/CLMigratorFromUniswapV2Test#testMigrateFromV2WithoutNativeToken.snap @@ -1 +1 @@ -736978 \ No newline at end of file +736947 \ No newline at end of file diff --git a/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3IncludingInit.snap b/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3IncludingInit.snap index 7e6e378..f605b1c 100644 --- a/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3IncludingInit.snap +++ b/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3IncludingInit.snap @@ -1 +1 @@ -790386 \ No newline at end of file +790716 \ No newline at end of file diff --git a/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3WithoutInit.snap b/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3WithoutInit.snap index 508efc4..88b1574 100644 --- a/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3WithoutInit.snap +++ b/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3WithoutInit.snap @@ -1 +1 @@ -749801 \ No newline at end of file +750204 \ No newline at end of file diff --git a/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3WithoutNativeToken.snap b/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3WithoutNativeToken.snap index 61297cd..5212cb2 100644 --- a/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3WithoutNativeToken.snap +++ b/.forge-snapshots/CLMigratorFromUniswapV3Test#testMigrateFromV3WithoutNativeToken.snap @@ -1 +1 @@ -791741 \ No newline at end of file +792146 \ No newline at end of file diff --git a/src/base/BaseMigrator.sol b/src/base/BaseMigrator.sol index 9be2edc..c5b822e 100644 --- a/src/base/BaseMigrator.sol +++ b/src/base/BaseMigrator.sol @@ -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); } diff --git a/src/interfaces/IBaseMigrator.sol b/src/interfaces/IBaseMigrator.sol index e5af3ed..afd3b6a 100644 --- a/src/interfaces/IBaseMigrator.sol +++ b/src/interfaces/IBaseMigrator.sol @@ -33,5 +33,6 @@ interface IBaseMigrator is IPeripheryImmutableState, IMulticall, ISelfPermit { uint256 amount1Min; // decide whether to collect fee bool collectFee; + uint256 deadline; } } diff --git a/src/pool-cl/CLMigrator.sol b/src/pool-cl/CLMigrator.sol index 01d26e9..b0d5692 100644 --- a/src/pool-cl/CLMigrator.sol +++ b/src/pool-cl/CLMigrator.sol @@ -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( diff --git a/test/pool-cl/migrator/CLMigratorFromV3.sol b/test/pool-cl/migrator/CLMigratorFromV3.sol index adb7aa7..c087744 100644 --- a/test/pool-cl/migrator/CLMigratorFromV3.sol +++ b/test/pool-cl/migrator/CLMigratorFromV3.sol @@ -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({ @@ -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({ @@ -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({ @@ -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({ @@ -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({ @@ -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({ @@ -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({ @@ -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({