Created
February 9, 2022 15:04
-
-
Save godtaehee/d6edf5acf384e5e145b15ca5fa13960a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.6; | |
import '@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol'; | |
import '@openzeppelin/contracts/interfaces/IERC20.sol'; | |
import '@uniswap/v3-periphery/contracts/interfaces/INonfungiblePositionManager.sol'; | |
import '../libraries/TransferHelper.sol'; | |
contract MintPosition { | |
struct MintResult { | |
uint256 tokenId; | |
uint128 liquidity; | |
uint256 amount0; | |
uint256 amount1; | |
} | |
/// @notice Represents the deposit of an NFT | |
struct Deposit { | |
address owner; | |
uint128 liquidity; | |
address token0; | |
address token1; | |
} | |
/// @dev deposits[tokenId] => Deposit | |
mapping(uint256 => Deposit) public deposits; | |
INonfungiblePositionManager public immutable nonfungiblePositionManager; | |
constructor(INonfungiblePositionManager _nonfungiblePositionManager) { | |
nonfungiblePositionManager = _nonfungiblePositionManager; | |
} | |
function _createDeposit(address owner, uint256 tokenId) internal { | |
(, , address token0, address token1, , , , uint128 liquidity, , , , ) = | |
nonfungiblePositionManager.positions(tokenId); | |
// set the owner and data for position | |
// operator is msg.sender | |
deposits[tokenId] = Deposit({owner: owner, liquidity: liquidity, token0: token0, token1: token1}); | |
} | |
function mint(INonfungiblePositionManager.MintParams memory params) external returns ( | |
uint256 tokenId, | |
uint128 liquidity, | |
uint256 amount0, | |
uint256 amount1 | |
) { | |
IERC20(params.token0).transferFrom(msg.sender, address(this), params.amount0Desired); | |
IERC20(params.token1).transferFrom(msg.sender, address(this), params.amount1Desired); | |
IERC20(params.token0).approve(address(nonfungiblePositionManager), params.amount0Desired); | |
IERC20(params.token1).approve(address(nonfungiblePositionManager), params.amount1Desired); | |
(tokenId, liquidity, amount0, amount1) = nonfungiblePositionManager.mint(params); | |
_createDeposit(msg.sender, tokenId); | |
// Remove allowance and refund in both assets. | |
if (amount0 < params.amount0Desired) { | |
IERC20(params.token0).approve(address(nonfungiblePositionManager), 0); | |
uint256 refund0 = params.amount0Desired - amount0; | |
IERC20(params.token0).transfer(msg.sender, refund0); | |
} | |
if (amount1 < params.amount1Desired) { | |
IERC20(params.token1).approve(address(nonfungiblePositionManager), 0); | |
uint256 refund1 = params.amount1Desired - amount1; | |
IERC20(params.token1).transfer(msg.sender, refund1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment