uniswap sdk

1,307 阅读4分钟

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')

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'))

Static Methods

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

Computes the pair address for the passed Tokens.

Properties

liquidityToken: Token

A Token representing the liquidity token for the pair.

token0 token1 reserve0(The reserve of token0) reserve1(The reserve of token1)

Methods

reserveOf(token: Token): TokenAmount

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

getOutputAmount(inputAmount: TokenAmount): [TokenAmount, Pair]

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]

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

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
): TokenAmount

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

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

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)

Properties

pairs: Pair[]

The ordered pairs that the route is comprised of.

path: Token[]

The full path from input token to output token.

input: string

The input token.

output: string

The output token.

midPrice: Price

Returns the current mid price along the route.

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

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)

Properties

route: Route

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

tradeType: TradeType

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

inputAmount: TokenAmount

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

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

The average price that the trade would execute at.

nextMidPrice: Price

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

slippage: Percent

The slippage incurred by the trade.

maximumAmountIn(slippageTolerance: Percent): TokenAmount

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[]

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[]

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)

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

Properties

numerator: JSBI
denominator: JSBI
quotient: JSBI

Methods

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

Formats a fraction to the specified number of significant digits.

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

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>

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>

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