From 9b057f299a70bbda8923e3abcec7979df89a591c Mon Sep 17 00:00:00 2001 From: Tina Zheng Date: Mon, 27 Nov 2023 19:51:25 -0500 Subject: [PATCH] add mint flow --- contracts/NonfungiblePositionManagerV4.sol | 48 +++++++++++++++++-- .../INonfungiblePositionManagerV4.sol | 1 + 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/contracts/NonfungiblePositionManagerV4.sol b/contracts/NonfungiblePositionManagerV4.sol index 5bb9f239c..587beb43b 100644 --- a/contracts/NonfungiblePositionManagerV4.sol +++ b/contracts/NonfungiblePositionManagerV4.sol @@ -5,15 +5,18 @@ import {IERC721Metadata} from "@openzeppelin/contracts/token/ERC721/extensions/I import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol"; import {PoolKey} from "@uniswap/v4-core/contracts/types/PoolKey.sol"; -import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/contracts/types/PoolId.sol"; -import {IPoolManager} from "@uniswap/v4-core/contracts/interfaces/IPoolManager.sol"; +import {IPoolManager, PoolManager} from "@uniswap/v4-core/contracts/PoolManager.sol"; import {Currency} from "@uniswap/v4-core/contracts/types/Currency.sol"; +import {BalanceDelta} from "@uniswap/v4-core/contracts/types/BalanceDelta.sol"; +import {TickMath} from "@uniswap/v4-core/contracts/libraries/TickMath.sol"; +import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/contracts/types/PoolId.sol"; import {INonfungiblePositionManagerV4} from "./interfaces/INonfungiblePositionManagerV4.sol"; import {PeripheryValidation} from "./base/PeripheryValidation.sol"; import {PeripheryPayments} from "./base/PeripheryPayments.sol"; import {SelfPermit} from "./base/SelfPermit.sol"; import {Multicall} from "./base/Multicall.sol"; +import {LiquidityAmounts} from "./libraries/LiquidityAmounts.sol"; contract NonfungiblePositionManagerV4 is INonfungiblePositionManagerV4, @@ -53,6 +56,14 @@ contract NonfungiblePositionManagerV4 is uint128 tokensOwed1; } + enum CallbackAction {Mint} + + struct CallbackData { + CallbackAction callbackAction; + address sender; + bytes params; + } + /// @dev Pool keys by poolIds mapping(bytes32 => PoolKey) private _poolIdToPoolKey; @@ -133,7 +144,7 @@ contract NonfungiblePositionManagerV4 is checkDeadline(params.deadline) returns (uint256 tokenId, uint128 liquidity, uint256 amount0, uint256 amount1) { - // TODO: implement this + poolManager.lock(abi.encode(CallbackData(CallbackAction.Mint, msg.sender, abi.encode(params)))); } modifier isAuthorizedForToken(uint256 tokenId) { @@ -211,4 +222,35 @@ contract NonfungiblePositionManagerV4 is function totalSupply() external view returns (uint256) { // TODO: implement this } + + struct AddLiquidityParams { + PoolKey poolKey; + int24 tickLower; + int24 tickUpper; + uint256 amount0Desired; + uint256 amount1Desired; + uint256 amount0Min; + uint256 amount1Min; + bytes hookData; + } + + /// @notice Add liquidity to an initialized pool + function _addLiquidity(AddLiquidityParams memory params) internal returns (uint128 liquidity, BalanceDelta delta) { + (uint160 sqrtPriceX96,,,) = poolManager.getSlot0(params.poolKey.toId()); + uint160 sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(params.tickLower); + uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(params.tickUpper); + liquidity = LiquidityAmounts.getLiquidityForAmounts( + sqrtPriceX96, sqrtRatioAX96, sqrtRatioBX96, params.amount0Desired, params.amount1Desired + ); + delta = poolManager.modifyPosition( + params.poolKey, + IPoolManager.ModifyPositionParams(params.tickLower, params.tickUpper, int256(int128(liquidity))), + params.hookData + ); + require( + uint256(int256(delta.amount0())) >= params.amount0Min + && uint256(int256(delta.amount1())) >= params.amount1Min, + "Price slippage check" + ); + } } diff --git a/contracts/interfaces/INonfungiblePositionManagerV4.sol b/contracts/interfaces/INonfungiblePositionManagerV4.sol index a167324c3..c590b62ee 100644 --- a/contracts/interfaces/INonfungiblePositionManagerV4.sol +++ b/contracts/interfaces/INonfungiblePositionManagerV4.sol @@ -85,6 +85,7 @@ interface INonfungiblePositionManagerV4 is ILockCallback, IPeripheryPayments, IE uint256 amount1Min; address recipient; uint256 deadline; + bytes hookData; } /// @notice Creates a new position wrapped in a NFT