diff --git a/contracts/NonfungiblePositionManagerV4.sol b/contracts/NonfungiblePositionManagerV4.sol index c23464bf..29c0153c 100644 --- a/contracts/NonfungiblePositionManagerV4.sol +++ b/contracts/NonfungiblePositionManagerV4.sol @@ -10,6 +10,7 @@ import {Currency, CurrencyLibrary} from "@uniswap/v4-core/contracts/types/Curren 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 {Position} from "@uniswap/v4-core/contracts/libraries/Position.sol"; import {INonfungiblePositionManagerV4} from "./interfaces/INonfungiblePositionManagerV4.sol"; import {PeripheryValidation} from "./base/PeripheryValidation.sol"; @@ -35,6 +36,7 @@ contract NonfungiblePositionManagerV4 is error NotApproved(); error NotCleared(); error NonexistentToken(); + error Slippage(); // TODO: rename this to be more descriptive? // details about the Uniswap position struct TokenPosition { @@ -151,6 +153,9 @@ contract NonfungiblePositionManagerV4 is settleDeltas(data.sender, params.poolKey, delta); return abi.encode(tokenId, liquidity, delta.amount0(), delta.amount1()); } + + // TODO: implement add liquidity + return abi.encode(0); } /// @inheritdoc INonfungiblePositionManagerV4 @@ -169,11 +174,13 @@ contract NonfungiblePositionManagerV4 is checkDeadline(params.deadline) returns (uint256 tokenId, uint128 liquidity, uint256 amount0, uint256 amount1) { - (tokenId, liquidity, amount0, amount1) = - poolManager.lock(abi.encode(CallbackData(CallbackAction.Mint, msg.sender, abi.encode(params)))); + (tokenId, liquidity, amount0, amount1) = abi.decode( + poolManager.lock(abi.encode(CallbackData(CallbackAction.Mint, msg.sender, abi.encode(params)))), + (uint256, uint128, uint256, uint256) + ); } - function mintCallback(MintParams calldata params) + function mintCallback(MintParams memory params) internal returns (uint256 tokenId, uint128 liquidity, BalanceDelta delta) { @@ -186,12 +193,28 @@ contract NonfungiblePositionManagerV4 is amount0Desired: params.amount0Desired, amount1Desired: params.amount1Desired, amount0Min: params.amount0Min, - amount1Min: params.amount1Min + amount1Min: params.amount1Min, + hookData: params.hookData }) ); tokenId = _nextId++; - _mint(tokenId); + _mint(params.recipient, tokenId); + + Position.Info memory positionInfo = + poolManager.getPosition(poolId, address(this), params.tickLower, params.tickUpper); + _positions[tokenId] = TokenPosition({ + nonce: 0, + operator: address(0), + poolId: poolId, + tickLower: params.tickLower, + tickUpper: params.tickUpper, + liquidity: liquidity, + feeGrowthInside0LastX128: positionInfo.feeGrowthInside0LastX128, + feeGrowthInside1LastX128: positionInfo.feeGrowthInside1LastX128, + tokensOwed0: 0, + tokensOwed1: 0 + }); } modifier isAuthorizedForToken(uint256 tokenId) { @@ -294,10 +317,8 @@ contract NonfungiblePositionManagerV4 is 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" - ); + if ( + uint256(int256(delta.amount0())) < params.amount0Min || uint256(int256(delta.amount1())) < params.amount1Min + ) revert Slippage(); } }