UNPKG

@astonic-io/astonic-sdk

Version:

Official SDK for interacting with the Astonic Protocol

243 lines (242 loc) 11.4 kB
import { IBroker0828 } from '@astonic-io/astonic-bindings-ts'; import { BigNumber, BigNumberish, Signer, providers } from 'ethers'; import { Address, TradingLimit, TradingLimitsConfig, TradingLimitsState } from './interfaces'; export interface Exchange { providerAddr: Address; id: string; assets: Address[]; } export interface Asset { address: Address; symbol: string; } export interface TradablePair { id: string; assets: [Asset, Asset]; path: Array<{ providerAddr: Address; id: string; assets: [Address, Address]; }>; } export declare class Astonic { private readonly signerOrProvider; private readonly broker; private readonly router; private exchanges; /** * This constructor is private, use the static create or createWithParams methods * to create a new Astonic instance * @param signerOrProvider an ethers provider or connected signer * @param brokerAddress the address of the broker contract * @param exchanges exchange data for the broker */ private constructor(); /** * Creates a new Astonic object instance. * When constructed with only a Provider only read-only operations are supported * @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps * @returns a new Astonic object instance */ static create(signerOrProvider: Signer | providers.Provider): Promise<Astonic>; /** * Create a new Astonic object instance given a broker address and optional exchanges data * When constructed with a Provider, only read-only operations are supported * @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps * @param brokerAddr the address of the broker contract * @param exchanges the exchanges data for the broker * @returns a new Astonic object instance */ static createWithParams(signerOrProvider: Signer | providers.Provider, brokerAddr: Address, routerAddr: Address, exchanges?: Exchange[]): Astonic; /** * Returns a new Astonic instance connected to the given signer * @param signer an ethers signer * @returns new Astonic object instance */ connectSigner(signer: Signer): Astonic; /** * Get tradable pairs for backwards compatibility * @returns an array of Asset pairs */ getTradablePairs(options?: { cached?: boolean; }): Promise<[Asset, Asset][]>; /** * Returns a list of all tradable pairs on Astonic via direct exchanges. * Each pair is represented using the TradablePair interface, with its id * (a concatenation of the two asset symbols in alphabetical order), * the two Asset objects, and a path (an array with a single direct exchange hop). * @returns An array of direct TradablePair objects. */ getDirectPairs(): Promise<TradablePair[]>; /** * Returns a list of all tradable pairs on Astonic, including those achievable * via two-hop routes. For two-hop pairs, the path will contain two exchange hops. * Each TradablePair contains an id (the concatenation of the two asset symbols in alphabetical order), * the two Asset objects, and an array of exchange details for each hop. * @returns An array of TradablePair objects representing available trade routes. */ getTradablePairsWithPath(options?: { cached?: boolean; }): Promise<readonly TradablePair[]>; /** * Returns the amount of tokenIn to be sold to buy amountOut of tokenOut. * If the provided tradablePair has a single (direct) pricing path, then direct pricing is used. * Otherwise, routed pricing via the AstonicRouter is applied. * @param tokenIn the token to be sold * @param tokenOut the token to be bought * @param amountOut the desired amount of tokenOut to be obtained * @param tradablePair the TradablePair object containing the pricing path information * @returns the amount of tokenIn to be sold */ getAmountIn(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish, tradablePair?: TradablePair): Promise<BigNumber>; /** * Returns the amount of tokenOut to be bought by selling amountIn of tokenIn. * If the provided tradablePair has a single (direct) pricing path, then direct pricing is used. * Otherwise, routed pricing via the AstonicRouter is applied. * @param tokenIn the token to be sold * @param tokenOut the token to be bought * @param amountIn the amount of tokenIn to be sold * @param tradablePair the TradablePair object containing the pricing path information * @returns the amount of tokenOut to be bought */ getAmountOut(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish, tradablePair?: TradablePair): Promise<BigNumber>; /** * Internal method for direct pricing: retrieves the exchange for the given tokens * and returns the amountIn using the broker. */ private getAmountInDirect; /** * Internal method for direct pricing: retrieves the exchange for the given tokens * and returns the amountOut using the broker. */ private getAmountOutDirect; /** * Internal method for routed pricing: uses the AstonicRouter to determine the required tokenIn * for obtaining amountOut through a multi-hop route specified in tradablePair.path. */ private getAmountInRouted; /** * Internal method for routed pricing: uses the AstonicRouter to determine the amountOut * obtainable by selling amountIn through a multi-hop route specified in tradablePair.path. */ private getAmountOutRouted; /** * Increases the broker's trading allowance for the given token * @param token the token to increase the allowance for * @param amount the amount to increase the allowance by * @returns the populated TransactionRequest object */ increaseTradingAllowance(tokenIn: Address, amount: BigNumberish, tradablePair?: TradablePair): Promise<providers.TransactionRequest>; /** * Increases the broker's trading allowance for the given token * @param token the token to increase the allowance for * @param amount the amount to increase the allowance by * @returns the populated TransactionRequest object */ wrapToken(tokenIn: Address, amount: BigNumberish): Promise<providers.TransactionRequest>; /** * Increases the broker's trading allowance for the given token * @param token the token to increase the allowance for * @param amount the amount to increase the allowance by * @returns the populated TransactionRequest object */ unwrapToken(tokenIn: Address, amount: BigNumberish): Promise<providers.TransactionRequest>; /** * Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut. * If the tradablePair contains a single-hop route, a direct swap is executed using swapExactTokensForTokens on the broker. * Otherwise, a routed swap is executed via the router. * @param tokenIn the token to be sold * @param tokenOut the token to be bought * @param amountIn the amount of tokenIn to be sold * @param amountOutMin the minimum amount of tokenOut to be bought * @param tradablePair the tradable pair details to determine routing * @returns the populated TransactionRequest object */ swapIn(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish, amountOutMin: BigNumberish, tradablePair?: TradablePair): Promise<providers.TransactionRequest>; private swapInDirect; private swapInRouted; /** * Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut. * If the tradablePair contains a single-hop route, a direct swap is executed using swapTokensForExactTokens on the broker. * Otherwise, a routed swap is executed via the router. * @param tokenIn the token to be sold * @param tokenOut the token to be bought * @param amountOut the amount of tokenOut to be bought * @param amountInMax the maximum amount of tokenIn to be sold * @returns the populated TransactionRequest object */ swapOut(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish, amountInMax: BigNumberish, tradablePair?: TradablePair): Promise<providers.TransactionRequest>; private swapOutDirect; private swapOutRouted; /** * Helper method to build the steps for a routed swap, ensuring proper token ordering * through the path segments */ private buildSteps; /** * Returns the astonic instance's broker contract * @returns broker contract */ getBroker(): IBroker0828; /** * Finds a tradable pair for the given input and output tokens * @param tokenIn the input token address * @param tokenOut the output token address * @returns the tradable pair containing the path between the tokens * @throws if no path is found between the tokens */ findPairForTokens(tokenIn: Address, tokenOut: Address): Promise<TradablePair>; /** * Returns the list of exchanges available in Astonic (cached) * @returns the list of exchanges */ getExchanges(): Promise<Exchange[]>; /** * Returns the list of exchanges for a given exchange provider address * @returns list of exchanges */ getExchangesForProvider(exchangeProviderAddr: Address): Promise<Exchange[]>; /** * Returns the Astonic exchange (if any) for a given pair of tokens * @param token0 the address of the first token * @param token1 the address of the second token * @returns exchange */ getExchangeForTokens(token0: Address, token1: Address): Promise<Exchange>; /** * Returns the Astonic exchange for a given exchange id * @param exchangeId the id of the exchange * @returns the exchange with the given id */ getExchangeById(exchangeId: string): Promise<Exchange>; /** * Returns whether trading is enabled in the given mode for a given exchange id * @param exchangeId the id of the exchange * @param mode the trading mode * @returns true if trading is enabled in the given mode, false otherwise */ isTradingEnabled(exchangeId: string): Promise<boolean>; /** * Return the trading limits for a given exchange id. Each limit is an object with the following fields: * asset: the address of the asset with the limit * maxIn: the maximum amount of the asset that can be sold * maxOut: the maximum amount of the asset that can be bought * until: the timestamp until which the limit is valid * @param exchangeId the id of the exchange * @returns the list of trading limits */ getTradingLimits(exchangeId: string): Promise<TradingLimit[]>; /** * Returns the trading limits configurations for a given exchange id * @param exchangeId the id of the exchange * @returns the trading limits configuration */ getTradingLimitConfig(exchangeId: string): Promise<TradingLimitsConfig[]>; /** * Returns the trading limits state for a given exchange id * @param exchangeId the id of the exchange * @returns the trading limits state */ getTradingLimitState(exchangeId: string): Promise<TradingLimitsState[]>; }