UNPKG

@razorlabs/amm-sdk

Version:

🛠 An SDK for building applications on top of RazorDEX.

294 lines (283 loc) • 13.8 kB
import { Percent, Token, CurrencyAmount, Price, BigintIsh, Currency, TradeType, NativeCurrency, ChainId } from '@razorlabs/swap-sdk-core'; export * from '@razorlabs/swap-sdk-core'; import { Aptos, InputViewFunctionData, InputEntryFunctionData } from '@aptos-labs/ts-sdk'; declare const ZERO_PERCENT: Percent; declare const ONE_HUNDRED_PERCENT: Percent; declare const AMM_MODULE_ADDRESS = "0xc4e68f29fa608d2630d11513c8de731b09a975f2f75ea945160491b9bfd36992"; declare const AMM_SIGNER = "0xf317511756cb5bd755a7456ad900974645788926742b39d81771e17cd54b1c80"; declare const FACTORY_ADDRESS = "0xc4e68f29fa608d2630d11513c8de731b09a975f2f75ea945160491b9bfd36992::amm_factory"; declare const FACTORY_ADDRESS_MAP: { 250: string; 126: string; }; declare const WMOVE: { 250: Token; 126: Token; }; declare const WNATIVE: Record<number, Token>; declare const NATIVE: Record<number, { name: string; symbol: string; decimals: number; }>; declare const computePairAddress: ({ tokenA, tokenB }: { tokenA: Token; tokenB: Token; }) => string; declare class Pair { readonly liquidityToken: Token; private readonly tokenAmounts; static getAddress(tokenA: Token, tokenB: Token): string; constructor(currencyAmountA: CurrencyAmount<Token>, tokenAmountB: CurrencyAmount<Token>); /** * Returns true if the token is either token0 or token1 * @param token to check */ involvesToken(token: Token): boolean; /** * Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0 */ get token0Price(): Price<Token, Token>; /** * Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1 */ get token1Price(): Price<Token, Token>; /** * Return the price of the given token in terms of the other token in the pair. * @param token token to return price of */ priceOf(token: Token): Price<Token, Token>; /** * Returns the chain ID of the tokens in the pair. */ get chainId(): number; get token0(): Token; get token1(): Token; get reserve0(): CurrencyAmount<Token>; get reserve1(): CurrencyAmount<Token>; reserveOf(token: Token): CurrencyAmount<Token>; getOutputAmount(inputAmount: CurrencyAmount<Token>): [CurrencyAmount<Token>, Pair]; getInputAmount(outputAmount: CurrencyAmount<Token>): [CurrencyAmount<Token>, Pair]; getLiquidityMinted(totalSupply: CurrencyAmount<Token>, tokenAmountA: CurrencyAmount<Token>, tokenAmountB: CurrencyAmount<Token>): CurrencyAmount<Token>; getLiquidityValue(token: Token, totalSupply: CurrencyAmount<Token>, liquidity: CurrencyAmount<Token>, feeOn?: boolean, kLast?: BigintIsh): CurrencyAmount<Token>; } declare class Route<TInput extends Currency, TOutput extends Currency> { readonly pairs: Pair[]; readonly path: Token[]; readonly input: TInput; readonly output: TOutput; constructor(pairs: Pair[], input: TInput, output: TOutput); private _midPrice; get midPrice(): Price<TInput, TOutput>; get chainId(): number; } interface InputOutput<TInput extends Currency, TOutput extends Currency> { readonly inputAmount: CurrencyAmount<TInput>; readonly outputAmount: CurrencyAmount<TOutput>; } declare function inputOutputComparator<TInput extends Currency, TOutput extends Currency>(a: InputOutput<TInput, TOutput>, b: InputOutput<TInput, TOutput>): number; declare function tradeComparator<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(a: Trade<TInput, TOutput, TTradeType>, b: Trade<TInput, TOutput, TTradeType>): number; interface BestTradeOptions { maxNumResults?: number; maxHops?: number; } /** * Represents a trade executed against a list of pairs. * Does not account for slippage, i.e. trades that front run this trade and move the price. */ declare class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> { /** * The route of the trade, i.e. which pairs the trade goes through and the input/output currencies. */ readonly route: Route<TInput, TOutput>; /** * The type of the trade, either exact in or exact out. */ readonly tradeType: TTradeType; /** * The input amount for the trade assuming no slippage. */ readonly inputAmount: CurrencyAmount<TInput>; /** * The output amount for the trade assuming no slippage. */ readonly outputAmount: CurrencyAmount<TOutput>; /** * The price expressed in terms of output amount/input amount. */ readonly executionPrice: Price<TInput, TOutput>; /** * The percent difference between the mid price before the trade and the trade execution price. */ readonly priceImpact: Percent; /** * Constructs an exact in trade with the given amount in and route * @param route route of the exact in trade * @param amountIn the amount being passed in */ static exactIn<TInput extends Currency, TOutput extends Currency>(route: Route<TInput, TOutput>, amountIn: CurrencyAmount<TInput>): Trade<TInput, TOutput, TradeType.EXACT_INPUT>; /** * Constructs an exact out trade with the given amount out and route * @param route route of the exact out trade * @param amountOut the amount returned by the trade */ static exactOut<TInput extends Currency, TOutput extends Currency>(route: Route<TInput, TOutput>, amountOut: CurrencyAmount<TOutput>): Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>; constructor(route: Route<TInput, TOutput>, amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>, tradeType: TTradeType); /** * Get the minimum amount that must be received from this trade for the given slippage tolerance * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade */ minimumAmountOut(slippageTolerance: Percent): CurrencyAmount<TOutput>; /** * Get the maximum amount in that can be spent via this trade for the given slippage tolerance * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade */ maximumAmountIn(slippageTolerance: Percent): CurrencyAmount<TInput>; /** * Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token * amount to an output token, making at most `maxHops` hops. * Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting * the amount in among multiple routes. * @param pairs the pairs to consider in finding the best trade * @param nextAmountIn exact amount of input currency to spend * @param currencyOut the desired currency out * @param maxNumResults maximum number of results to return * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair * @param currentPairs used in recursion; the current list of pairs * @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter * @param bestTrades used in recursion; the current list of best trades */ static bestTradeExactIn<TInput extends Currency, TOutput extends Currency>(pairs: Pair[], currencyAmountIn: CurrencyAmount<TInput>, currencyOut: TOutput, { maxNumResults, maxHops }?: BestTradeOptions, currentPairs?: Pair[], nextAmountIn?: CurrencyAmount<Currency>, bestTrades?: Trade<TInput, TOutput, TradeType.EXACT_INPUT>[]): Trade<TInput, TOutput, TradeType.EXACT_INPUT>[]; /** * Return the execution price after accounting for slippage tolerance * @param slippageTolerance the allowed tolerated slippage */ worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput>; /** * similar to the above method but instead targets a fixed output amount * given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token * to an output token amount, making at most `maxHops` hops * note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting * the amount in among multiple routes. * @param pairs the pairs to consider in finding the best trade * @param currencyIn the currency to spend * @param nextAmountOut the exact amount of currency out * @param maxNumResults maximum number of results to return * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair * @param currentPairs used in recursion; the current list of pairs * @param currencyAmountOut used in recursion; the original value of the currencyAmountOut parameter * @param bestTrades used in recursion; the current list of best trades */ static bestTradeExactOut<TInput extends Currency, TOutput extends Currency>(pairs: Pair[], currencyIn: TInput, currencyAmountOut: CurrencyAmount<TOutput>, { maxNumResults, maxHops }?: BestTradeOptions, currentPairs?: Pair[], nextAmountOut?: CurrencyAmount<Currency>, bestTrades?: Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[]): Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[]; } declare function isTradeBetter(tradeA: Trade<Currency, Currency, TradeType> | undefined | null, tradeB: Trade<Currency, Currency, TradeType> | undefined | null, minimumDelta?: Percent): boolean | undefined; /** * Move is the main usage of a 'native' currency, i.e. for Movement mainnet and all testnets */ declare class Move extends NativeCurrency { protected constructor(chainId: number); get wrapped(): Token; private static _moveCache; static onChain(chainId: number): Move; equals(other: Currency): boolean; } declare function getNetworkRPCUrl(chainId: ChainId): string; declare function getNetworkIndexerUrl(chainId: ChainId): string; declare function getDefaultProvider(chainId: ChainId): Aptos; declare const getReserves: (pairAddress: string) => InputViewFunctionData; /** * Contains methods for constructing instances of pairs and tokens from on-chain data. */ declare abstract class Fetcher { /** * Cannot be constructed. */ private constructor(); /** * Fetch information for a given token on the given chain, using the given provider. * @param chainId chain of the token * @param address address of the token on the chain * @param provider provider used to fetch the token * @param symbol optional symbol of the token * @param name optional name of the token */ static fetchTokenData(chainId: ChainId, address: string, provider: Aptos | undefined, symbol: string, name?: string): Promise<Token>; /** * Fetches information about a pair and constructs a pair from the given two tokens. * @param tokenA first token * @param tokenB second token * @param provider the provider to use to fetch the data */ static fetchPairData(tokenA: Token, tokenB: Token, provider?: Aptos): Promise<Pair>; } /** * * Native is the main usage of a 'native' currency, i.e. for BSC mainnet and all testnets */ declare class Native extends NativeCurrency { protected constructor({ chainId, decimals, name, symbol, }: { chainId: number; decimals: number; symbol: string; name: string; }); get wrapped(): Token; private static cache; static onChain(chainId: number): Native; equals(other: Currency): boolean; } /** * Options for producing the arguments to send call to the router. */ interface TradeOptions { /** * How much the execution price is allowed to move unfavorably from the trade execution price. */ allowedSlippage: Percent; /** * How long the swap is valid until it expires, in seconds. * This will be used to produce a `deadline` parameter which is computed from when the swap call parameters * are generated. */ ttl: number; /** * The account that should receive the output of the swap. */ recipient: string; } interface TradeOptionsDeadline extends Omit<TradeOptions, 'ttl'> { /** * When the transaction expires. * This is an alternate to specifying the ttl, for when you do not want to use local time. */ deadline: number; } /** * The parameters to use in the call to the Router to execute a trade. */ interface SwapParameters { /** * The method to call on the Router. */ methodName: string; /** * The arguments to pass to the method, all hex encoded. */ args: InputEntryFunctionData['functionArguments']; } /** * Represents the Razor AMM Router, and has static methods for helping execute trades. */ declare abstract class Router { /** * Cannot be constructed. */ private constructor(); /** * Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade. * @param trade to produce call parameters for * @param options options for the call parameters */ static swapCallParameters(trade: Trade<Currency, Currency, TradeType>, options: TradeOptions | TradeOptionsDeadline): SwapParameters; } export { AMM_MODULE_ADDRESS, AMM_SIGNER, type BestTradeOptions, FACTORY_ADDRESS, FACTORY_ADDRESS_MAP, Fetcher, Move, NATIVE, Native, ONE_HUNDRED_PERCENT, Pair, Route, Router, type SwapParameters, Trade, type TradeOptions, type TradeOptionsDeadline, WMOVE, WNATIVE, ZERO_PERCENT, computePairAddress, getDefaultProvider, getNetworkIndexerUrl, getNetworkRPCUrl, getReserves, inputOutputComparator, isTradeBetter, tradeComparator };