Dependence

prevent installation of unused dependencies (e.g. @ethersproject/providers and @ethersproject/contracts, only used in Fetcher)

Class

token
import { ChainId, Token } from '@uniswap/sdk'

const token = new Token(ChainId.MAINNET, '0xc0FFee0000000000000000000000000000000000'.18.'HOT'.'Caffeine')
Copy the code

Properties

chainId address decimals symbol name

Methods

equals(other: Token): boolean

Checks if the current instance is equal to another (has an identical chainId and address).

sortsBefore(other: Token): boolean

Checks if the current instance sorts before another, by address.

Pair

constructor(tokenAmountA: TokenAmount, tokenAmountB: TokenAmount)

The Pair entity represents a Uniswap pair with a balance of each of its pair tokens.

import { ChainId, Token, TokenAmount, Pair } from '@uniswap/sdk'

const HOT = new Token(ChainId.MAINNET, '0xc0FFee0000000000000000000000000000000000'.18.'HOT'.'Caffeine')
const NOT = new Token(ChainId.MAINNET, '0xDeCAf00000000000000000000000000000000000'.18.'NOT'.'Caffeine')

const pair = new Pair(new TokenAmount(HOT, '2000000000000000000'), new TokenAmount(NOT, '1000000000000000000'))
Copy the code

Static Methods

getAddress(tokenA: Token, tokenB: Token): string

Computes the pair address for the passed Tokens.

Properties

liquidityToken: Token
Copy the code

A Token representing the liquidity token for the pair.

Token0 Token1 Reserve0 (The reserveofToken0) reserve1 (The reserveofToken1)Copy the code

Methods

reserveOf(token: Token): TokenAmount
Copy the code

Returns reserve0 or reserve1, depending on whether token0 or token1 is passed in.

getOutputAmount(inputAmount: TokenAmount): [TokenAmount, Pair]
Copy the code

Pricing function for exact input amounts. Returns maximum output amount based on current reserves and the new Pair that would exist if the trade were executed.

getInputAmount(outputAmount: TokenAmount): [TokenAmount, Pair]
Copy the code

Pricing function for exact output amounts. Returns minimum input amount based on current reserves and the new Pair that would exist if the trade were executed.

getLiquidityMinted(totalSupply: TokenAmount, tokenAmountA: TokenAmount, tokenAmountB: TokenAmount): TokenAmount
Copy the code

Calculates the exact amount of liquidity tokens minted from a given amount of token0 and token1.

getLiquidityValue(
  token: Token,
  totalSupply: TokenAmount,
  liquidity: TokenAmount,
  feeOn: boolean = false, kLast? : BigintIsh ): TokenAmountCopy the code

Calculates the exact amount of token0 or token1 that the given amount of liquidity tokens represent.

Route
constructor(pairs: Pair[], input: Token)
Copy the code

The Route entity represents one or more ordered Uniswap pairs with a fully specified path from input token to output token.

import { ChainId, Token, TokenAmount, Pair, Route } from '@uniswap/sdk'

const HOT = new Token(ChainId.MAINNET, '0xc0FFee0000000000000000000000000000000000'.18.'HOT'.'Caffeine')
const NOT = new Token(ChainId.MAINNET, '0xDeCAf00000000000000000000000000000000000'.18.'NOT'.'Caffeine')
const HOT_NOT = new Pair(new TokenAmount(HOT, '2000000000000000000'), new TokenAmount(NOT, '1000000000000000000'))

const route = new Route([HOT_NOT], NOT)
Copy the code

Properties

pairs: Pair[]
Copy the code

The ordered pairs that the route is comprised of.

path: Token[]
Copy the code

The full path from input token to output token.

input: string
Copy the code

The input token.

output: string
Copy the code

The output token.

midPrice: Price
Copy the code

Returns the current mid price along the route.

Trade
constructor(route: Route, amount: TokenAmount, tradeType: TradeType)
Copy the code

The Trade entity represents a fully specified trade along a route. This entity supplies all the information necessary to craft a router transaction.

import { ChainId, Token, TokenAmount, Pair, TradeType, Route, Trade } from '@uniswap/sdk'

const HOT = new Token(ChainId.MAINNET, '0xc0FFee0000000000000000000000000000000000'.18.'HOT'.'Caffeine')
const NOT = new Token(ChainId.MAINNET, '0xDeCAf00000000000000000000000000000000000'.18.'NOT'.'Caffeine')
const HOT_NOT = new Pair(new TokenAmount(HOT, '2000000000000000000'), new TokenAmount(NOT, '1000000000000000000'))
const NOT_TO_HOT = new Route([HOT_NOT], NOT)

const trade = new Trade(NOT_TO_HOT, new TokenAmount(NOT, '1000000000000000'), TradeType.EXACT_INPUT)
Copy the code

Properties

route: Route
Copy the code

The path property of the route should be passed as the path parameter to router functions.

tradeType: TradeType
Copy the code

TradeType.EXACT_INPUT corresponds to swapExactFor router functions. TradeType.EXACT_OUTPUT corresponds to swapForExact router functions.

inputAmount: TokenAmount
Copy the code

For exact input trades, this value should be passed as amountIn to router functions. For exact output trades, this value should be multiplied by a factor >1, representing slippage tolerance, and passed as amountInMax to router functions.

outputAmount: TokenAmount
Copy the code

For exact output trades, this value should be passed as amountOut to router functions. For exact input trades, this value should be multiplied by a factor <1, representing slippage tolerance, and passed as amountOutMin to router functions.

executionPrice: Price
Copy the code

The average price that the trade would execute at.

nextMidPrice: Price
Copy the code

What the new mid price would be if the trade were to execute.

slippage: Percent
Copy the code

The slippage incurred by the trade.

maximumAmountIn(slippageTolerance: Percent): TokenAmount
Copy the code

Returns the maximum amount of the input token that should be spent on the trade, given the slippage tolerance.

Useful when constructing a transaction for a trade of type EXACT_OUT.

Methods

Trade.bestTradeExactIn(
    pairs: Pair[],
    amountIn: TokenAmount,
    tokenOut: Token,
    { maxNumResults = 3, maxHops = 3 }: BestTradeOptions = {}): Trade[]
Copy the code

Given a list of pairs, a fixed amount in, and token amount out, this method returns the best maxNumResults trades that swap an input token amount to an output token, making at most maxHops hops. The returned trades are sorted by output amount, in decreasing order, and all share the given input amount.

Trade.bestTradeExactOut(
    pairs: Pair[],
    tokenIn: Token,
    amountOut: TokenAmount,
    { maxNumResults = 3, maxHops = 3 }: BestTradeOptions = {}): Trade[]
Copy the code

Similar to the above method, but targets a fixed output token amount. The returned trades are sorted by input amount, in increasing order, and all share the given output amount.

Fractions
constructor(numerator: BigintIsh, denominator: BigintIsh = ONE)
Copy the code

The base class which all subsequent fraction classes extend. Not meant to be used directly.

Properties

numerator: JSBI
Copy the code
denominator: JSBI
Copy the code
quotient: JSBI
Copy the code

Methods

invert(): Fraction
Copy the code
add(other: Fraction | BigintIsh): Fraction
Copy the code
multiply(other: Fraction | BigintIsh): Fraction
Copy the code
divide(other: Fraction | BigintIsh): Fraction
Copy the code
toSignificant(
  significantDigits: number,
  format: object = { groupSeparator: ' ' },
  rounding: Rounding = Rounding.ROUND_HALF_UP
): string
Copy the code

Formats a fraction to the specified number of significant digits.

toFixed(
  decimalPlaces: number,
  format: object = { groupSeparator: ' ' },
  rounding: Rounding = Rounding.ROUND_HALF_UP
): string
Copy the code

Formats a fraction to the specified number of decimal places.

Uniswap.org/docs/v2/SDK…

Fetcher
async fetchTokenData(
  chainId: ChainId,
  address: string, provider = getDefaultProvider(getNetwork(chainId)), symbol? : string, name? : string ):Promise<Token>
Copy the code

Initializes a class instance from a chainId and token address, if the decimals of the token are unknown and cannot be fetched externally. Decimals are fetched via an ethers.js v5 provider. If not passed in, a default provider is used.

async fetchPairData(
  tokenA: Token,
  tokenB: Token,
  provider = getDefaultProvider(getNetwork(tokenA.chainId))
): Promise<Pair>
Copy the code

Initializes a class instance from two Tokens, If the pair’s laws of these tokens are unknown and cannot be touchable. Pair reserves are touchvia an ethers.js v5 provider. If not passed in, a default provider is used.