import { AtomicTypeTag, TypeTag } from '@manahippo/move-to-ts'; import { Types } from 'aptos'; import { CoinInfo } from '../generated/coin_list/coin_list'; import { App } from '../generated'; import { CONFIGS } from '../config'; import { CoinListClient } from '../coinlist'; export type UITokenAmount = number; export type UITokenAmountRatio = number; export type PriceType = { xToY: UITokenAmountRatio; yToX: UITokenAmountRatio; }; export type QuoteType = { inputSymbol: string; outputSymbol: string; inputUiAmt: UITokenAmount; outputUiAmt: UITokenAmount; avgPrice: UITokenAmountRatio; initialPrice?: UITokenAmountRatio; finalPrice?: UITokenAmountRatio; priceImpact?: number; }; export abstract class TradingPool { abstract get isRoutable(): boolean; // X-Y abstract get xCoinInfo(): CoinInfo; abstract get yCoinInfo(): CoinInfo; get xTag() { return this.xCoinInfo.token_type.toTypeTag(); } get yTag() { return this.yCoinInfo.token_type.toTypeTag(); } // functions that depend on pool's onchain state abstract isStateLoaded(): boolean; abstract reloadState(app: App): Promise; abstract getPrice(): PriceType; abstract getQuote(inputUiAmt: UITokenAmount, isXtoY: boolean): QuoteType; // build payload directly if not routable abstract makePayload(inputUiAmt: UITokenAmount, minOutAmt: UITokenAmount): Types.EntryFunctionPayload; getTagE(): TypeTag { return AtomicTypeTag.U8; } } export abstract class TradingPoolProvider { constructor(public app: App, public netConfig = CONFIGS.testnet, public registry: CoinListClient) {} abstract loadPoolList(): Promise; async reloadAllPoolState() { const pools = await this.loadPoolList(); const promises = pools.map((pool) => pool.reloadState(this.app)); await Promise.all(promises); } }