Skip to content

Commit

Permalink
add mint flow
Browse files Browse the repository at this point in the history
  • Loading branch information
tinaszheng committed Nov 28, 2023
1 parent 6e82a2e commit 9b057f2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
48 changes: 45 additions & 3 deletions contracts/NonfungiblePositionManagerV4.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"
);
}
}
1 change: 1 addition & 0 deletions contracts/interfaces/INonfungiblePositionManagerV4.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9b057f2

Please sign in to comment.