Skip to content

Commit

Permalink
add mint function
Browse files Browse the repository at this point in the history
  • Loading branch information
tinaszheng committed Nov 29, 2023
1 parent c64e82e commit 0156721
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion contracts/NonfungiblePositionManagerV4.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 {Currency} from "@uniswap/v4-core/contracts/types/Currency.sol";
import {Position} from "@uniswap/v4-core/contracts/libraries/Position.sol";

import {INonfungiblePositionManagerV4} from "./interfaces/INonfungiblePositionManagerV4.sol";
import {PeripheryValidation} from "./base/PeripheryValidation.sol";
Expand Down Expand Up @@ -120,6 +121,15 @@ contract NonfungiblePositionManagerV4 is
// TODO: implement this
}

/// @dev Caches a pool key
function cachePoolKey(PoolKey memory poolKey) private returns (PoolId poolId) {
poolId = poolKey.toId();
// uninitialized check
if (_poolIdToPoolKey[PoolId.unwrap(poolId)].tickSpacing == 0) {
_poolIdToPoolKey[PoolId.unwrap(poolId)] = poolKey;
}
}

/// @inheritdoc INonfungiblePositionManagerV4
function mint(MintParams calldata params)
external
Expand All @@ -128,7 +138,37 @@ contract NonfungiblePositionManagerV4 is
checkDeadline(params.deadline)
returns (uint256 tokenId, uint128 liquidity, uint256 amount0, uint256 amount1)
{
// TODO: implement this
(liquidity, amount0, amount1) = addLiquidity(
AddLiquidityParams({
poolKey: params.poolKey,
tickLower: params.tickLower,
tickUpper: params.tickUpper,
amount0Desired: params.amount0Desired,
amount1Desired: params.amount1Desired,
amount0Min: params.amount0Min,
amount1Min: params.amount1Min,
hookData: params.hookData
})
);

tokenId = _nextId++;
_mint(params.recipient, tokenId);

PoolId poolId = cachePoolKey(params.poolKey);
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
});
}

/// @inheritdoc INonfungiblePositionManagerV4
Expand Down

0 comments on commit 0156721

Please sign in to comment.