Skip to content

V3 SDK

TypeScript SDK for PancakeSwap V3 concentrated liquidity. Provides pool math, position management, quote computation, and helpers for building swap and liquidity transactions.

Installation

npm install @pancakeswap/v3-sdk

Quick start

import { Pool, Position, NonfungiblePositionManager, FeeAmount } from '@pancakeswap/v3-sdk'
import { Token, CurrencyAmount, Percent } from '@pancakeswap/swap-sdk-core'
import { ChainId } from '@pancakeswap/chains'
 
const USDC = new Token(ChainId.BSC, '0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d', 18, 'USDC')
const CAKE = new Token(ChainId.BSC, '0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82', 18, 'CAKE')
 
// Construct pool from on-chain state
const pool = new Pool(
  USDC,
  CAKE,
  FeeAmount.MEDIUM,    // 2500 = 0.25%
  sqrtPriceX96,        // current sqrt price from contract
  liquidity,           // current liquidity from contract
  tickCurrent,         // current tick from contract
)
 
// Build a position
const position = new Position({
  pool,
  liquidity: 10n ** 18n,
  tickLower: -60,
  tickUpper:  60,
})
 
// Get add-liquidity calldata
const { calldata, value } = NonfungiblePositionManager.addCallParameters(position, {
  slippageTolerance: new Percent(5, 1000),
  recipient: '0xYOUR_ADDRESS',
  deadline: Math.floor(Date.now() / 1000) + 60 * 20,
})

Pool

Represents a V3 liquidity pool and exposes swap simulation.

new Pool(tokenA, tokenB, fee, sqrtRatioX96, liquidity, tickCurrent, ticks?)

Properties

PropertyTypeDescription
token0TokenThe lower-sorted token
token1TokenThe higher-sorted token
feeFeeAmountFee tier
sqrtRatioX96bigintCurrent sqrt price in Q64.96 format
liquiditybigintActive liquidity at the current tick
tickCurrentnumberCurrent tick index
tickSpacingnumberMinimum tick movement for this fee tier
token0PricePricePrice of token0 denominated in token1
token1PricePricePrice of token1 denominated in token0
chainIdnumberChain ID (from tokens)

Methods

MethodSignatureDescription
getOutputAmount(inputAmount, sqrtPriceLimitX96?) → [CurrencyAmount, Pool]Simulate a swap and return output amount + resulting pool state
getInputAmount(outputAmount, sqrtPriceLimitX96?) → [CurrencyAmount, Pool]Compute the required input for a desired output
priceOf(token) → PriceReturn the price of a token in terms of the other
involvesToken(token) → booleanWhether the pool contains a given token

Position

A liquidity position within a V3 pool.

new Position({ pool, liquidity, tickLower, tickUpper })

Properties

PropertyTypeDescription
poolPoolThe pool this position belongs to
liquiditybigintPosition liquidity
tickLowernumberLower tick bound
tickUppernumberUpper tick bound
token0PriceLowerPricetoken0 price at the lower tick
token0PriceUpperPricetoken0 price at the upper tick
amount0CurrencyAmounttoken0 amount at the current price
amount1CurrencyAmounttoken1 amount at the current price
mintAmounts{ amount0: bigint, amount1: bigint }Required token amounts to mint this position

Methods

MethodSignatureDescription
mintAmountsWithSlippage(slippageTolerance) → { amount0: bigint, amount1: bigint }Mint amounts adjusted for slippage
burnAmountsWithSlippage(slippageTolerance) → { amount0: bigint, amount1: bigint }Minimum burn amounts after slippage
Position.fromAmount0({ pool, tickLower, tickUpper, amount0, useFullPrecision }) → PositionConstruct a position from a fixed token0 amount
Position.fromAmount1({ pool, tickLower, tickUpper, amount1 }) → PositionConstruct a position from a fixed token1 amount
Position.fromAmounts({ pool, tickLower, tickUpper, amount0, amount1, useFullPrecision }) → PositionConstruct a position from both amounts (uses the tighter constraint)

NonfungiblePositionManager

Static helpers for building calldata targeting the V3 NFT position manager contract.

MethodSignatureDescription
addCallParameters(position, options) → { calldata: string, value: string }Mint a new position or increase liquidity
removeCallParameters(position, options) → { calldata: string, value: string }Decrease liquidity and collect tokens
collectCallParameters(options) → { calldata: string, value: string }Collect fees for an existing position
safeTransferFromParameters(options) → { calldata: string, value: string }Transfer an NFT position to another address

addCallParameters options

OptionTypeDescription
recipientstringAddress to receive the NFT (new mint only)
deadlinenumberUnix timestamp deadline
slippageTolerancePercentMaximum slippage on amounts
tokenIdnumberIf provided, increases an existing position instead of minting
useNativeNativeCurrencyIf provided, wraps native currency automatically

Trade

A computed trade against one or more routes.

Static constructors

MethodSignatureDescription
Trade.fromRoute(route, amount, tradeType) → Promise<Trade>Build a trade from a single route
Trade.fromRoutes(routes, tradeType) → Promise<Trade>Build a split trade from multiple routes
Trade.bestTradeExactIn(pools, amountIn, currencyOut, options?) → Promise<Trade[]>Find best exact-input trades
Trade.bestTradeExactOut(pools, currencyIn, amountOut, options?) → Promise<Trade[]>Find best exact-output trades

Properties and methods

MemberType / SignatureDescription
swaps{ route, inputAmount, outputAmount }[]Individual route swaps
tradeTypeTradeTypeEXACT_INPUT or EXACT_OUTPUT
inputAmountCurrencyAmountTotal input across all routes
outputAmountCurrencyAmountTotal output across all routes
executionPricePriceEffective execution price
priceImpactPercentPrice impact vs mid-price
minimumAmountOut(slippageTolerance, amountOut?) → CurrencyAmountWorst-case output after slippage
maximumAmountIn(slippageTolerance, amountIn?) → CurrencyAmountWorst-case input after slippage
worstExecutionPrice(slippageTolerance) → PriceWorst acceptable execution price

SwapRouter

MethodSignatureDescription
SwapRouter.swapCallParameters(trades, options) → { calldata: string, value: string }Build calldata for the V3 SwapRouter contract

Route

PropertyTypeDescription
poolsPool[]Ordered list of pools in the path
tokenPathToken[]Ordered list of tokens along the path
inputCurrencyInput currency
outputCurrencyOutput currency
midPricePriceGeometric mid-price across the route
chainIdnumberChain ID

Constants

ExportDescription
FeeAmount.LOWEST100 (0.01%)
FeeAmount.LOW500 (0.05%)
FeeAmount.MEDIUM2500 (0.25%)
FeeAmount.HIGH10000 (1%)
TICK_SPACINGSRecord<FeeAmount, number> — tick spacing per fee tier