阅读 508

uniswap sdk

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.

文章分类
前端
文章标签