import {z} from 'zod' export type ApiSupportedBlockchain = 'ton'; export type ApiSupportedYields = 'stonfi' | 'dedust' | 'dedust_v2' | 'stonfi_v2' | 'coffee' | 'tonraffles' | 'tonco' | 'tonco_v2' | 'colossus' | 'bidask' | 'tonstakers' | 'bemo' | 'bemo_v2' | 'hipo' | 'torch_finance' | 'stakee' | 'kton' | 'evaa' | 'coffin' | 'moon' | 'storm_trade' | 'farmix_lending' | 'dao_lama_vault'; export type ApiPoolSortOrder = 'tvl' | 'volume' | 'apr'; export type GetApiV1PoolsArgs = {blockchains?: Array | undefined, dexes?: Array | undefined, trusted?: boolean | undefined, with_active_boosts?: boolean | undefined, recently_created?: boolean | undefined, with_liquidity_from?: string | undefined, search_text?: string | undefined, order?: ApiPoolSortOrder | undefined, descending_order?: boolean | undefined, in_groups?: boolean | undefined, size?: number | undefined, page?: number | undefined}; export type GetApiV1PoolsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiPoolType = z.enum(["public", "private", "slumbering", "invalid"]); export const apiAmmType = z.enum(["constant_product", "cubic_stable", "curve_fi_stable", "concentrated_v3", "tonstakers", "weighted_constant_product", "weighted_stable", "bidask", "moon_dlmm", "bidask_damm", "dedust_v2"]); export const apiJson = z.record(z.string(), z.unknown()); export const apiTokenAddress = z.object({blockchain: z.string(), address: z.string()}); export const apiTokenVerification = z.enum(["whitelisted", "blacklisted", "community", "unknown"]); export const apiTokenMetadata = z.object({name: z.string(), symbol: z.string(), decimals: z.number().int(), image_url: z.string().optional(), listed: z.boolean(), verification: apiTokenVerification}); export const apiToken = z.object({address: apiTokenAddress, metadata: apiTokenMetadata}); export const apiPoolReserves = z.array(z.number()); export const apiTokenRestrictions = z.object({min_swap_amount: z.number().optional(), max_swap_amount: z.number().optional()}); export const apiPoolFees = z.object({average_gas: z.number(), divider: z.number().int().optional(), input: z.number().int().optional(), output: z.number().int().optional(), first_token: z.number().int().optional(), second_token: z.number().int().optional()}); export const apiPool = z.object({dex: z.string(), address: z.string(), type: apiPoolType, amm_type: apiAmmType, amm_settings: apiJson.optional(), tokens: z.array(apiToken), reserves: apiPoolReserves, restrictions: z.array(apiTokenRestrictions).optional(), fees: apiPoolFees, unavailable_until: z.number().int().optional()}); export const apiPoolStatistics = z.object({address: z.string(), tvl_usd: z.number(), volume_usd: z.number(), fee_usd: z.number(), apr: z.number(), lp_apr: z.number(), boost_apr: z.number(), locked_asset_amount: z.string().optional()}); export const apiPoolInfo = z.object({pool: apiPool, info: apiPoolStatistics}); export const apiPoolsSearchResponse = z.object({total_count: z.number().int(), pools: z.array(apiPoolInfo)}); export const getApiV1PoolsResponse = z.array(apiPoolsSearchResponse); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Returns list of pools which correspond to the given params * * GET /v1/pools */ export const getApiV1Pools = async (args: GetApiV1PoolsArgs, options?: GetApiV1PoolsOptions) => { const {baseUrl = "https://backend.swap.coffee", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.blockchains ?? []) query.append("blockchains", String(item)) for (const item of args.dexes ?? []) query.append("dexes", String(item)) if (args.trusted != null) query.append("trusted", String(args.trusted)) if (args.with_active_boosts != null) query.append("with_active_boosts", String(args.with_active_boosts)) if (args.recently_created != null) query.append("recently_created", String(args.recently_created)) if (args.with_liquidity_from != null) query.append("with_liquidity_from", String(args.with_liquidity_from)) if (args.search_text != null) query.append("search_text", String(args.search_text)) if (args.order != null) query.append("order", String(args.order)) if (args.descending_order != null) query.append("descending_order", String(args.descending_order)) if (args.in_groups != null) query.append("in_groups", String(args.in_groups)) if (args.size != null) query.append("size", String(args.size)) if (args.page != null) query.append("page", String(args.page)) if (ApiKey !== undefined) headers.set("X-Api-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/pools${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1PoolsResponse, await res.json()) };