Skip to content

V2 SDK

TypeScript SDK for PancakeSwap V2 AMM. Provides pair math, trade computation, and helpers for building swap and liquidity transactions against V2 pools.

Installation

npm install @pancakeswap/v2-sdk

Quick start

import { Pair, Trade, Route, Router } from '@pancakeswap/v2-sdk'
import { Token, CurrencyAmount, TradeType, Percent } from '@pancakeswap/swap-sdk-core'
import { ChainId } from '@pancakeswap/chains'
 
const CAKE = new Token(ChainId.BSC, '0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82', 18, 'CAKE')
const BUSD = new Token(ChainId.BSC, '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56', 18, 'BUSD')
 
// Construct pair from on-chain reserves
const pair = new Pair(
  CurrencyAmount.fromRawAmount(CAKE, reserveCAKE),
  CurrencyAmount.fromRawAmount(BUSD, reserveBUSD),
)
 
// Find best trade
const [trade] = Trade.bestTradeExactIn(
  [pair],
  CurrencyAmount.fromRawAmount(CAKE, 10n ** 18n),
  BUSD,
)
 
// Build calldata
const { methodName, args, value } = Router.swapCallParameters(trade, {
  ttl: 50,
  recipient: '0xYOUR_ADDRESS',
  allowedSlippage: new Percent(1, 100), // 1%
})

Pair

Represents a V2 AMM pair and its reserves.

new Pair(currencyAmountA, currencyAmountB)

Properties

PropertyTypeDescription
token0TokenThe lower-sorted token
token1TokenThe higher-sorted token
reserve0CurrencyAmountReserve of token0
reserve1CurrencyAmountReserve of token1
token0PricePricePrice of token0 in terms of token1
token1PricePricePrice of token1 in terms of token0
liquidityTokenTokenThe LP token for this pair
chainIdnumberChain ID (from tokens)

Methods

MethodSignatureDescription
reserveOf(token) → CurrencyAmountReserve of a given token
getOutputAmount(inputAmount) → [CurrencyAmount, Pair]Simulate a swap; returns output and resulting pair
getInputAmount(outputAmount) → [CurrencyAmount, Pair]Compute required input for a desired output
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) → CurrencyAmountLP tokens minted for a deposit
getLiquidityValue(token, totalSupply, liquidity, feeOn?, kLast?) → CurrencyAmountToken amount redeemable for LP tokens
involvesToken(token) → booleanWhether the pair contains a given token
Pair.getAddress(tokenA, tokenB) → stringCompute the deterministic pair address

Route

A single-path route through one or more pairs.

new Route(pairs, input, output?)
PropertyTypeDescription
pairsPair[]Ordered list of pairs in the route
pathToken[]Ordered token path
inputCurrencyInput currency
outputCurrencyOutput currency
midPricePriceGeometric mid-price across the route
chainIdnumberChain ID

Trade

A computed swap along a route.

Static constructors

MethodSignatureDescription
Trade.exactIn(route, amountIn) → TradeExact-input trade
Trade.exactOut(route, amountOut) → TradeExact-output trade
Trade.bestTradeExactIn(pairs, currencyAmountIn, currencyOut, options?) → Trade[]Find the best exact-input trades; sorted best-first
Trade.bestTradeExactOut(pairs, currencyIn, currencyAmountOut, options?) → Trade[]Find the best exact-output trades; sorted best-first

bestTradeExactIn / bestTradeExactOut options

OptionTypeDefaultDescription
maxNumResultsnumber3Maximum number of results to return
maxHopsnumber3Maximum route hops

Properties and methods

MemberType / SignatureDescription
routeRouteThe route this trade follows
tradeTypeTradeTypeEXACT_INPUT or EXACT_OUTPUT
inputAmountCurrencyAmountInput amount
outputAmountCurrencyAmountOutput amount
executionPricePriceEffective execution price
priceImpactPercentPrice impact vs mid-price
minimumAmountOut(slippageTolerance) → CurrencyAmountMinimum output after slippage
maximumAmountIn(slippageTolerance) → CurrencyAmountMaximum input after slippage

Router

Static helpers for building V2 Router calldata.

MethodSignatureDescription
Router.swapCallParameters(trade, tradeOptions) → { methodName, args, value }Build swap calldata for the V2 Router

tradeOptions

OptionTypeDescription
allowedSlippagePercentMaximum slippage tolerance
recipientstringAddress to receive output tokens
ttlnumberDeadline in seconds from now
deadlinenumberAbsolute Unix timestamp deadline (alternative to ttl)
feeOnTransferbooleanUse supportingFeeOnTransfer router methods

Fetcher

Fetch pair data from on-chain state.

MethodSignatureDescription
Fetcher.fetchPairData(tokenA, tokenB, provider?) → Promise<Pair>Fetch reserves and construct a Pair