Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PositionManager mint #80

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -112,6 +113,15 @@ contract NonfungiblePositionManagerV4 is
);
}

/// @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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is checking for tickSpacing the right thing to do here? just want to check if this key exists in the mapping yet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also big difference between this mapping vs the one that exists in v3:

  • in v3, there's a poolId indicator PER nft so during burn, we deleted the poolId from the mapping
  • but in v4, this is a poolId PER poolkey and there could be multiple NFTs per poolKey. we don't want to burn during mint, so this is essentially an ever-growing list. but it also means not every mint needs to store an extra poolId mapping

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea i think its a fair chec as tickSpacing can't be 0 in an initialized pool

_poolIdToPoolKey[PoolId.unwrap(poolId)] = poolKey;
}
}

/// @inheritdoc INonfungiblePositionManagerV4
function mint(MintParams calldata params)
external
Expand All @@ -120,7 +130,37 @@ contract NonfungiblePositionManagerV4 is
checkDeadline(params.deadline)
returns (uint256 tokenId, uint128 liquidity, uint256 amount0, uint256 amount1)
{
// TODO: implement this
(liquidity, amount0, amount1) = addLiquidity(
AddLiquidityParams({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this re-defining of AddLiquidityParams is kinda ugly... i think i'd prefer either:

  1. We could have AddLiqParams as a sub-struct of MintParams like
struct MintParams {
   AddLiquidityParams addLiqParams
   address recipient
   uint256 deadline
}

orrrr
2. We could not even bother with the MintParams entirely and just have

function mint(AddLiquidityParams calldata params, address recipient, uint256 deadline)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then these 12 lines could just be like (liquidity, amount0, amount1) = addLiquidity(params); 😏

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ option 2

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++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unchecked!

_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,
tinaszheng marked this conversation as resolved.
Show resolved Hide resolved
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