Skip to content

Commit

Permalink
add initial version of mint
Browse files Browse the repository at this point in the history
  • Loading branch information
tinaszheng committed Nov 28, 2023
1 parent f1ff1bd commit f8b7875
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions contracts/NonfungiblePositionManagerV4.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)
{
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
}
}

0 comments on commit f8b7875

Please sign in to comment.