Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
NonfungiblePositionManager – Uniswap SDK
Skip to content

NonfungiblePositionManager

The NonfungiblePositionManager module provides utilities for interacting with the Uniswap V3 NonfungiblePositionManager contract, which manages liquidity positions as NFTs.

Import

import {
  createCallParameters,
  addCallParameters,
  collectCallParameters,
  removeCallParameters,
  safeTransferFromParameters
} from '@uniswap/v3-sdk'

Functions

createCallParameters

Produces the calldata for creating and initializing a new pool.

function createCallParameters(pool: Pool): MethodParameters

Example

import { createCallParameters, Pool, FeeAmount } from '@uniswap/v3-sdk'
 
const pool = new Pool(
  token0,
  token1,
  FeeAmount.MEDIUM,
  sqrtRatioX96,
  0n, // No liquidity yet
  tick
)
 
const { calldata, value } = createCallParameters(pool)

addCallParameters

Produces the calldata for adding liquidity to a position.

function addCallParameters(
  position: Position,
  options: AddLiquidityOptions
): MethodParameters

MintOptions (for new positions)

OptionTypeDescription
slippageTolerancePercentHow much the pool price is allowed to move
deadlinebigintWhen the transaction expires
recipientstringAccount that receives the minted NFT
createPoolbooleanWhether to create the pool if not initialized
useNativeCurrencyWhether to use native ETH (must match a pool token)
token0PermitPermitOptionsOptional permit for token0
token1PermitPermitOptionsOptional permit for token1

IncreaseOptions (for existing positions)

OptionTypeDescription
slippageTolerancePercentHow much the pool price is allowed to move
deadlinebigintWhen the transaction expires
tokenIdBigintIshThe ID of the position to increase
useNativeCurrencyWhether to use native ETH
token0PermitPermitOptionsOptional permit for token0
token1PermitPermitOptionsOptional permit for token1

Example: Minting a New Position

import { addCallParameters, Position, Pool, FeeAmount } from '@uniswap/v3-sdk'
import { Percent } from '@uniswap/sdk-core'
 
const position = Position.fromAmounts({
  pool,
  tickLower: -60,
  tickUpper: 60,
  amount0: '1000000',
  amount1: '1000000000000000000',
  useFullPrecision: true
})
 
const { calldata, value } = addCallParameters(position, {
  slippageTolerance: new Percent(50, 10000), // 0.5%
  deadline: BigInt(Math.floor(Date.now() / 1000) + 1800),
  recipient: '0x1234567890123456789012345678901234567890'
})

Example: Increasing Liquidity

const { calldata, value } = addCallParameters(position, {
  slippageTolerance: new Percent(50, 10000),
  deadline: BigInt(Math.floor(Date.now() / 1000) + 1800),
  tokenId: 12345 // Existing position NFT ID
})

Example: Using Native ETH

import { Ether } from '@uniswap/sdk-core'
 
const { calldata, value } = addCallParameters(position, {
  slippageTolerance: new Percent(50, 10000),
  deadline: BigInt(deadline),
  recipient: recipientAddress,
  useNative: Ether.onChain(1) // Use ETH instead of WETH
})
 
// value will contain the ETH amount to send

collectCallParameters

Produces the calldata for collecting fees from a position.

function collectCallParameters(options: CollectOptions): MethodParameters

CollectOptions

OptionTypeDescription
tokenIdBigintIshThe ID of the token to collect fees from
expectedCurrencyOwed0CurrencyAmount<Currency>Expected token0 fees
expectedCurrencyOwed1CurrencyAmount<Currency>Expected token1 fees
recipientstringAccount that receives the fees

Example

import { collectCallParameters } from '@uniswap/v3-sdk'
import { CurrencyAmount } from '@uniswap/sdk-core'
 
const { calldata, value } = collectCallParameters({
  tokenId: 12345,
  expectedCurrencyOwed0: CurrencyAmount.fromRawAmount(token0, '1000000'),
  expectedCurrencyOwed1: CurrencyAmount.fromRawAmount(token1, '1000000000000000000'),
  recipient: '0x1234567890123456789012345678901234567890'
})

removeCallParameters

Produces the calldata for removing liquidity from a position.

function removeCallParameters(
  position: Position,
  options: RemoveLiquidityOptions
): MethodParameters

RemoveLiquidityOptions

OptionTypeDescription
tokenIdBigintIshThe ID of the token to exit
liquidityPercentagePercentPercentage of position liquidity to exit
slippageTolerancePercentHow much the pool price can move
deadlinebigintWhen the transaction expires
burnTokenbooleanWhether to burn the NFT if fully exiting
permitobjectOptional permit for the NFT
collectOptionsCollectOptionsParameters for collecting tokens

Example

import { removeCallParameters, Position } from '@uniswap/v3-sdk'
import { Percent, CurrencyAmount } from '@uniswap/sdk-core'
 
const { calldata, value } = removeCallParameters(position, {
  tokenId: 12345,
  liquidityPercentage: new Percent(100, 100), // 100% = full exit
  slippageTolerance: new Percent(50, 10000),
  deadline: BigInt(Math.floor(Date.now() / 1000) + 1800),
  burnToken: true, // Burn NFT after full exit
  collectOptions: {
    tokenId: 12345,
    expectedCurrencyOwed0: CurrencyAmount.fromRawAmount(token0, '1000000'),
    expectedCurrencyOwed1: CurrencyAmount.fromRawAmount(token1, '1000000000000000000'),
    recipient: recipientAddress
  }
})

safeTransferFromParameters

Produces the calldata for transferring a position NFT.

function safeTransferFromParameters(options: SafeTransferOptions): MethodParameters

SafeTransferOptions

OptionTypeDescription
senderstringThe account sending the NFT
recipientstringThe account receiving the NFT
tokenIdBigintIshThe ID of the token being sent
dataHex.HexOptional data for onERC721Received

Example

import { safeTransferFromParameters } from '@uniswap/v3-sdk'
 
const { calldata, value } = safeTransferFromParameters({
  sender: '0xSenderAddress...',
  recipient: '0xRecipientAddress...',
  tokenId: 12345
})

Complete Workflow Example

import {
  Pool,
  Position,
  FeeAmount,
  addCallParameters,
  collectCallParameters,
  removeCallParameters
} from '@uniswap/v3-sdk'
import { Percent, CurrencyAmount } from '@uniswap/sdk-core'
 
// 1. Create a position
const pool = new Pool(token0, token1, FeeAmount.MEDIUM, sqrtRatioX96, liquidity, tick)
 
const position = Position.fromAmounts({
  pool,
  tickLower: -60,
  tickUpper: 60,
  amount0: '1000000',
  amount1: '1000000000000000000',
  useFullPrecision: true
})
 
// 2. Mint the position
const mintParams = addCallParameters(position, {
  slippageTolerance: new Percent(50, 10000),
  deadline: BigInt(Math.floor(Date.now() / 1000) + 1800),
  recipient: userAddress
})
 
// 3. Later, collect fees
const collectParams = collectCallParameters({
  tokenId: positionNftId,
  expectedCurrencyOwed0: CurrencyAmount.fromRawAmount(token0, fees0),
  expectedCurrencyOwed1: CurrencyAmount.fromRawAmount(token1, fees1),
  recipient: userAddress
})
 
// 4. Finally, remove liquidity and burn
const removeParams = removeCallParameters(position, {
  tokenId: positionNftId,
  liquidityPercentage: new Percent(100, 100),
  slippageTolerance: new Percent(50, 10000),
  deadline: BigInt(Math.floor(Date.now() / 1000) + 1800),
  burnToken: true,
  collectOptions: {
    tokenId: positionNftId,
    expectedCurrencyOwed0: CurrencyAmount.fromRawAmount(token0, 0),
    expectedCurrencyOwed1: CurrencyAmount.fromRawAmount(token1, 0),
    recipient: userAddress
  }
})