Skip to content

Swap SDK Core

Core primitives used across all PancakeSwap swap SDKs. Provides currency types, fractional arithmetic, and amount/price representations for both EVM and Solana chains.

Installation

npm install @pancakeswap/swap-sdk-core

Quick start

import { Token, CurrencyAmount, Price, Percent, Fraction } from '@pancakeswap/swap-sdk-core'
import { ChainId } from '@pancakeswap/chains'
 
const USDC = new Token(ChainId.ETHEREUM, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 6, 'USDC', 'USD Coin')
const DAI  = new Token(ChainId.ETHEREUM, '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18, 'DAI', 'Dai')
 
const oneUSDC = CurrencyAmount.fromRawAmount(USDC, 1_000_000n)       // 1 USDC (6 decimals)
const slippage = new Percent(50, 10_000) // 0.5%
const minOut   = oneUSDC.multiply(new Fraction(1).subtract(slippage))
 
const price = new Price(USDC, DAI, 1_000_000n, 10n ** 18n)
console.log(price.toSignificant(6)) // "1.00000"

Token

Represents an ERC-20 token.

new Token(chainId, address, decimals, symbol?, name?, projectLink?, logoURI?)

Properties

PropertyTypeDescription
chainIdnumberChain ID where the token is deployed
addressstringChecksummed contract address
decimalsnumberToken decimal places
symbolstring | undefinedTicker symbol
namestring | undefinedFull token name
isNativefalseAlways false for Token
isTokentrueAlways true for Token
wrappedTokenReturns itself (tokens are already wrapped)

Methods

MethodSignatureDescription
equals(other: Currency) → booleanTrue if same chain and address
sortsBefore(other: Token) → booleanTrue if this token address sorts before the other

NativeCurrency

Abstract base class for native currencies (ETH, BNB, etc.).

PropertyTypeDescription
chainIdnumberChain ID
decimalsnumberDecimal places (usually 18)
symbolstringTicker (e.g. "ETH", "BNB")
namestringFull name
isNativetrueAlways true
isTokenfalseAlways false
wrappedTokenThe wrapped ERC-20 equivalent (e.g. WETH)

CurrencyAmount

A fractional amount of a currency with decimal-aware formatting.

Static constructors

MethodSignatureDescription
CurrencyAmount.fromRawAmount(currency, rawAmount) → CurrencyAmountFrom an on-chain integer (e.g. 1000000n for 1 USDC)
CurrencyAmount.fromFractionalAmount(currency, numerator, denominator) → CurrencyAmountFrom a numerator/denominator pair

Properties

PropertyTypeDescription
currencyCurrencyThe currency this amount represents
decimalScalebigint10^decimals
numeratorbigintRaw numerator
denominatorbigintRaw denominator
quotientbigintInteger part of the amount

Methods

MethodSignatureDescription
add(other: CurrencyAmount) → CurrencyAmountAdd two amounts of the same currency
subtract(other: CurrencyAmount) → CurrencyAmountSubtract
multiply(other: Fraction | bigint) → CurrencyAmountScale by a fraction
divide(other: Fraction | bigint) → CurrencyAmountDivide by a fraction
toSignificant(significantDigits, format?, rounding?) → stringFormat to N significant digits
toFixed(decimalPlaces?, format?, rounding?) → stringFormat to fixed decimal places
toExact(format?) → stringExact decimal string representation
wrappedgetter → CurrencyAmount<Token>Wrapped version (for native currencies)

Fraction

Arbitrary-precision rational number.

new Fraction(numerator, denominator?)  // denominator defaults to 1

Properties

PropertyTypeDescription
numeratorbigintNumerator
denominatorbigintDenominator
quotientbigintInteger division result
remainderFractionFractional remainder
asFractionFractionReturns itself

Methods

MethodSignatureDescription
invert() → FractionSwap numerator and denominator
add(other: Fraction | bigint) → FractionAdd
subtract(other: Fraction | bigint) → FractionSubtract
multiply(other: Fraction | bigint) → FractionMultiply
divide(other: Fraction | bigint) → FractionDivide
lessThan(other: Fraction | bigint) → booleanComparison
equalTo(other: Fraction | bigint) → booleanEquality
greaterThan(other: Fraction | bigint) → booleanComparison
toSignificant(significantDigits, format?, rounding?) → stringFormat
toFixed(decimalPlaces, format?, rounding?) → stringFormat

Percent

A Fraction subclass for percentages. All arithmetic returns Percent.

new Percent(numerator, denominator?)  // e.g. new Percent(1, 100) = 1%

Inherits all Fraction methods. toSignificant and toFixed output the human-readable percentage (e.g. new Percent(1, 100).toSignificant(2)"1").

Price

Exchange rate between two currencies.

new Price(baseCurrency, quoteCurrency, denominator, numerator)
ParameterDescription
baseCurrencyThe input currency
quoteCurrencyThe output currency
denominatorBase currency amount (integer)
numeratorQuote currency amount (integer)

Properties

PropertyTypeDescription
baseCurrencyCurrencyInput currency
quoteCurrencyCurrencyOutput currency
scalarFractionDecimal adjustment ratio
adjustedForDecimalsFractionPrice fraction adjusted for both token decimals

Methods

MethodSignatureDescription
invert() → PriceReturn the inverse price
multiply(other: Price) → PriceCompose two prices (base → quote → other)
quote(currencyAmount: CurrencyAmount) → CurrencyAmountApply the price to an amount
toSignificant(significantDigits, format?, rounding?) → stringHuman-readable price
toFixed(decimalPlaces, format?, rounding?) → stringFixed-decimal price