// SPDX-License-Identifier: GPL-2.0-or-later pragma solidity =0.7.6; pragma abicoder v2; // TODO: change to import from @airdao/astra-cl-core import '@airdao/astra-cl-core/contracts/libraries/LowGasSafeMath.sol'; // TODO: change to import from @airdao/astradex import './interfaces/classic/IAstraPair.sol'; import './interfaces/INonfungiblePositionManager.sol'; import './libraries/TransferHelper.sol'; import './interfaces/ICLMigrator.sol'; import './base/PeripheryImmutableState.sol'; import './base/Multicall.sol'; import './base/SelfPermit.sol'; import './interfaces/external/ISAMB.sol'; import './base/PoolInitializer.sol'; /// @title AstraDEX CL Migrator contract CLMigrator is ICLMigrator, PeripheryImmutableState, PoolInitializer, Multicall, SelfPermit { using LowGasSafeMath for uint256; address public immutable nonfungiblePositionManager; constructor( address _factory, address _SAMB, address _nonfungiblePositionManager ) PeripheryImmutableState(_factory, _SAMB) { nonfungiblePositionManager = _nonfungiblePositionManager; } receive() external payable { require(msg.sender == SAMB, 'Not SAMB'); } function migrate(MigrateParams calldata params) external override { require(params.percentageToMigrate > 0, 'Percentage too small'); require(params.percentageToMigrate <= 100, 'Percentage too large'); // burn classic liquidity to this address IAstraPair(params.pair).transferFrom(msg.sender, params.pair, params.liquidityToMigrate); (uint256 amount0Classic, uint256 amount1Classic) = IAstraPair(params.pair).burn(address(this)); // calculate the amounts to migrate to CL uint256 amount0ClassicToMigrate = amount0Classic.mul(params.percentageToMigrate) / 100; uint256 amount1ClassicToMigrate = amount1Classic.mul(params.percentageToMigrate) / 100; // approve the position manager up to the maximum token amounts TransferHelper.safeApprove(params.token0, nonfungiblePositionManager, amount0ClassicToMigrate); TransferHelper.safeApprove(params.token1, nonfungiblePositionManager, amount1ClassicToMigrate); // mint CL position (, , uint256 amount0CL, uint256 amount1CL) = INonfungiblePositionManager(nonfungiblePositionManager).mint( INonfungiblePositionManager.MintParams({ token0: params.token0, token1: params.token1, fee: params.fee, tickLower: params.tickLower, tickUpper: params.tickUpper, amount0Desired: amount0ClassicToMigrate, amount1Desired: amount1ClassicToMigrate, amount0Min: params.amount0Min, amount1Min: params.amount1Min, recipient: params.recipient, deadline: params.deadline }) ); // if necessary, clear allowance and refund dust if (amount0CL < amount0Classic) { if (amount0CL < amount0ClassicToMigrate) { TransferHelper.safeApprove(params.token0, nonfungiblePositionManager, 0); } uint256 refund0 = amount0Classic - amount0CL; if (params.refundAsAMB && params.token0 == SAMB) { ISAMB(SAMB).withdraw(refund0); TransferHelper.safeTransferAMB(msg.sender, refund0); } else { TransferHelper.safeTransfer(params.token0, msg.sender, refund0); } } if (amount1CL < amount1Classic) { if (amount1CL < amount1ClassicToMigrate) { TransferHelper.safeApprove(params.token1, nonfungiblePositionManager, 0); } uint256 refund1 = amount1Classic - amount1CL; if (params.refundAsAMB && params.token1 == SAMB) { ISAMB(SAMB).withdraw(refund1); TransferHelper.safeTransferAMB(msg.sender, refund1); } else { TransferHelper.safeTransfer(params.token1, msg.sender, refund1); } } } }