Fractions
Fraction
constructor(numerator: BigintIsh, denominator: BigintIsh = ONE)The base class which all subsequent fraction classes extend. Not meant to be used directly.
Properties
numerator
numerator: JSBIdenominator
denominator: JSBIquotient
quotient: JSBIPerforms floor division.
Methods
invert
invert(): Fractionadd
add(other: Fraction | BigintIsh): Fractionsubtract
subtract(other: Fraction | BigintIsh): Fractionmultiply
multiply(other: Fraction | BigintIsh): Fractiondivide
divide(other: Fraction | BigintIsh): FractiontoSignificant
toSignificant(
significantDigits: number,
format: object = { groupSeparator: '' },
rounding: Rounding = Rounding.ROUND_HALF_UP
): stringFormats a fraction to the specified number of significant digits.
For format options, see toFormat.
toFixed
toFixed(
decimalPlaces: number,
format: object = { groupSeparator: '' },
rounding: Rounding = Rounding.ROUND_HALF_UP
): stringFormats a fraction to the specified number of decimal places.
For format options, see toFormat.
Percent
Responsible for formatting percentages (10% instead of 0.1).
Example
import { Percent } from '@whiteswap/sdk'
const percent = new Percent('60', '100')
console.log(percent.toSignificant(2)) // 60toSignificant
See toSignificant.
toFixed
See toFixed.
TokenAmount
constructor(token: Token, amount: BigintIsh)Responsible for formatting token amounts with specific decimal places.
Example
import { Token, TokenAmount } from '@whiteswap/sdk'
const FRIED = new Token(ChainId.MAINNET, '0xfa1aFe1000000000000000000000000000000000', 18, 'FRIED', 'Beans')
const tokenAmount = new TokenAmount(FRIED, '3000000000000000000')
console.log(tokenAmount.toExact()) // 3Properties
token
token: Tokenraw
raw: JSBIReturns the full token amount, unadjusted for decimals.
Methods
add
add(other: TokenAmount): TokenAmountsubtract
subtract(other: TokenAmount): TokenAmounttoSignificant
See toSignificant.
toFixed
See toFixed.
toExact
toExact(format: object = { groupSeparator: '' }): stringPrice
constructor(baseToken: Token, quoteToken: Token, denominator: BigintIsh, numerator: BigintIsh)Responsible for denominating the relative price between two tokens. Denominator and numerator must be unadjusted for decimals.
Example
import { ChainId, WETH as WETHs, Token, Price } from '@whiteswap/sdk'
const WETH = WETHs[ChainId.MAINNET]
const ABC = new Token(ChainId.MAINNET, '0xabc0000000000000000000000000000000000000', 18, 'ABC')
const price = new Price(WETH, ABC, '1000000000000000000', '123000000000000000000')
console.log(price.toSignificant(3)) // 123This example shows the ETH/XYZ price, where ETH is the base token, and XYZ is the quote token. The price is constructed from an amount of XYZ (the numerator) / an amount of WETH (the denominator).
Static Methods
fromRoute
fromRoute(route: Route): PriceProperties
baseToken
baseToken: TokenquoteToken
quoteToken: Tokenscalar
scalar: FractionUsed to adjust the price for the decimals of the base and quote tokens.
raw
raw: FractionReturns the raw price, unadjusted for decimals.
adjusted
adjusted: FractionReturns the price, adjusted for decimals.
Methods
invert
invert(): Pricemultiply
multiply(other: Price): Pricequote
quote(tokenAmount: TokenAmount): TokenAmountGiven an asset amount, returns an equivalent value of the other asset, according to the current price.
toSignificant
See toSignificant.
toFixed
See toFixed.
Last updated
Was this helpful?