UNPKG

@razorlabs/swap-sdk-core

Version:

🛠 An SDK for building applications on top of RazorDEX.

272 lines (260 loc) • 10.5 kB
type BigintIsh = bigint | number | string; declare enum TradeType { EXACT_INPUT = 0, EXACT_OUTPUT = 1 } declare enum Rounding { ROUND_DOWN = 0, ROUND_HALF_UP = 1, ROUND_UP = 2 } declare enum ChainId { MAINNET = 126, BARDOCK_TESTNET = 250 } declare const MINIMUM_LIQUIDITY = 1000n; declare const ZERO = 0n; declare const ONE = 1n; declare const TWO = 2n; declare const THREE = 3n; declare const FIVE = 5n; declare const TEN = 10n; declare const _100 = 100n; declare const _9975 = 9975n; declare const _10000 = 10000n; declare const MaxU256: bigint; declare enum MoveType { u8 = "u8", u16 = "u16", u32 = "u32", u64 = "u64", u128 = "u128", u256 = "u256" } declare const MOVE_TYPE_MAXIMA: { u8: bigint; u16: bigint; u32: bigint; u64: bigint; u128: bigint; u256: bigint; }; /** * Represents the native currency of the chain on which it resides, e.g. */ declare abstract class NativeCurrency extends BaseCurrency { readonly isNative: true; readonly isToken: false; } interface SerializedToken { chainId: number; address: string; decimals: number; symbol: string; name?: string; projectLink?: string; } /** * Represents a token with a unique address and some metadata. */ declare class Token extends BaseCurrency { readonly isNative: false; readonly isToken: true; /** * The contract address on the chain on which this token lives */ readonly address: string; readonly projectLink?: string; constructor(chainId: number, address: string, decimals: number, symbol: string, name?: string, projectLink?: string); /** * Returns true if the two tokens are equivalent, i.e. have the same chainId and address. * @param other other token to compare */ equals(other: Currency): boolean; /** * Returns true if the address of this token sorts before the address of the other token * @param other other token to compare * @throws if the tokens have the same address * @throws if the tokens are on different chains */ sortsBefore(other: Token): boolean; /** * Return this token, which does not need to be wrapped */ get wrapped(): Token; get serialize(): SerializedToken; } type Currency = NativeCurrency | Token; /** * A currency is any fungible financial instrument, including Move, all fungible tokens, and other chain-native currencies */ declare abstract class BaseCurrency { /** * Returns whether the currency is native to the chain and must be wrapped (e.g. Move) */ abstract readonly isNative: boolean; /** * Returns whether the currency is a token that is usable in PancakeSwap without wrapping */ abstract readonly isToken: boolean; /** * The chain ID on which this currency resides */ readonly chainId: number; /** * The decimals used in representing currency amounts */ readonly decimals: number; /** * The symbol of the currency, i.e. a short textual non-unique identifier */ readonly symbol: string; /** * The name of the currency, i.e. a descriptive textual non-unique identifier */ readonly name?: string; /** * Constructs an instance of the base class `BaseCurrency`. * @param chainId the chain ID on which this currency resides * @param decimals decimals of the currency * @param symbol symbol of the currency * @param name of the currency */ protected constructor(chainId: number, decimals: number, symbol: string, name?: string); /** * Returns whether this currency is functionally equivalent to the other currency * @param other the other currency */ abstract equals(other: Currency): boolean; /** * Return the wrapped version of this currency that can be used with the PancakeSwap contracts. Currencies must * implement this to be used in PancakeSwap */ abstract get wrapped(): Token; } declare class Fraction { readonly numerator: bigint; readonly denominator: bigint; constructor(numerator: BigintIsh, denominator?: BigintIsh); private static tryParseFraction; get quotient(): bigint; get remainder(): Fraction; invert(): Fraction; add(other: Fraction | BigintIsh): Fraction; subtract(other: Fraction | BigintIsh): Fraction; lessThan(other: Fraction | BigintIsh): boolean; equalTo(other: Fraction | BigintIsh): boolean; greaterThan(other: Fraction | BigintIsh): boolean; multiply(other: Fraction | BigintIsh): Fraction; divide(other: Fraction | BigintIsh): Fraction; toSignificant(significantDigits: number, format?: object, rounding?: Rounding): string; toFixed(decimalPlaces: number, format?: object, rounding?: Rounding): string; /** * Helper method for converting any super class back to a fraction */ get asFraction(): Fraction; } declare class Percent extends Fraction { /** * This boolean prevents a fraction from being interpreted as a Percent */ readonly isPercent: true; add(other: Fraction | BigintIsh): Percent; subtract(other: Fraction | BigintIsh): Percent; multiply(other: Fraction | BigintIsh): Percent; divide(other: Fraction | BigintIsh): Percent; toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string; toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string; } declare class CurrencyAmount<T extends Currency> extends Fraction { readonly currency: T; readonly decimalScale: bigint; /** * Returns a new currency amount instance from the unit-less amount of token, i.e. the raw amount * @param currency the currency in the amount * @param rawAmount the raw token or ether amount */ static fromRawAmount<T extends Currency>(currency: T, rawAmount: BigintIsh): CurrencyAmount<T>; /** * Construct a currency amount with a denominator that is not equal to 1 * @param currency the currency * @param numerator the numerator of the fractional token amount * @param denominator the denominator of the fractional token amount */ static fromFractionalAmount<T extends Currency>(currency: T, numerator: BigintIsh, denominator: BigintIsh): CurrencyAmount<T>; protected constructor(currency: T, numerator: BigintIsh, denominator?: BigintIsh); add(other: CurrencyAmount<T>): CurrencyAmount<T>; subtract(other: CurrencyAmount<T>): CurrencyAmount<T>; multiply(other: Fraction | BigintIsh): CurrencyAmount<T>; divide(other: Fraction | BigintIsh): CurrencyAmount<T>; toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string; toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string; toExact(format?: object): string; get wrapped(): CurrencyAmount<Token>; } declare class Price<TBase extends Currency, TQuote extends Currency> extends Fraction { readonly baseCurrency: TBase; readonly quoteCurrency: TQuote; readonly scalar: Fraction; /** * Construct a price, either with the base and quote currency amount, or the * @param args */ constructor(...args: [TBase, TQuote, BigintIsh, BigintIsh] | [{ baseAmount: CurrencyAmount<TBase>; quoteAmount: CurrencyAmount<TQuote>; }]); /** * Flip the price, switching the base and quote currency */ invert(): Price<TQuote, TBase>; /** * Multiply the price by another price, returning a new price. The other price must have the same base currency as this price's quote currency * @param other the other price */ multiply<TOtherQuote extends Currency>(other: Price<TQuote, TOtherQuote>): Price<TBase, TOtherQuote>; /** * Return the amount of quote currency corresponding to a given amount of the base currency * @param currencyAmount the amount of base currency to quote against the price */ quote(currencyAmount: CurrencyAmount<TBase>): CurrencyAmount<TQuote>; /** * Get the value scaled by decimals for formatting * @private */ private get adjustedForDecimals(); toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string; toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string; } /** * Indicates that the pair has insufficient reserves for a desired output amount. I.e. the amount of output cannot be * obtained by sending any amount of input. */ declare class InsufficientReservesError extends Error { readonly isInsufficientReservesError: true; constructor(); } /** * Indicates that the input amount is too small to produce any amount of output. I.e. the amount of input sent is less * than the price of a single unit of output after fees. */ declare class InsufficientInputAmountError extends Error { readonly isInsufficientInputAmountError: true; constructor(); } declare function validateMoveTypeInstance(value: bigint, moveType: MoveType): void; declare function sqrt(y: bigint): bigint; declare function sortedInsert<T>(items: T[], add: T, maxSize: number, comparator: (a: T, b: T) => number): T | null; /** * Returns the percent difference between the mid price and the execution price, i.e. price impact. * @param midPrice mid price before the trade * @param inputAmount the input amount of the trade * @param outputAmount the output amount of the trade */ declare function computePriceImpact<TBase extends Currency, TQuote extends Currency>(midPrice: Price<TBase, TQuote>, inputAmount: CurrencyAmount<TBase>, outputAmount: CurrencyAmount<TQuote>): Percent; declare function getTokenComparator(balances: { [tokenAddress: string]: CurrencyAmount<Token> | undefined; }): (tokenA: Token, tokenB: Token) => number; declare function sortCurrencies<T extends Currency>(currencies: T[]): T[]; declare function getCurrencyAddress(currency: Currency): string; export { BaseCurrency, type BigintIsh, ChainId, type Currency, CurrencyAmount, FIVE, Fraction, InsufficientInputAmountError, InsufficientReservesError, MINIMUM_LIQUIDITY, MOVE_TYPE_MAXIMA, MaxU256, MoveType, NativeCurrency, ONE, Percent, Price, Rounding, type SerializedToken, TEN, THREE, TWO, Token, TradeType, ZERO, _100, _10000, _9975, computePriceImpact, getCurrencyAddress, getTokenComparator, sortCurrencies, sortedInsert, sqrt, validateMoveTypeInstance };