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 GetApiV1YieldPoolsArgs = {blockchains?: Array | undefined, providers?: Array | undefined, trusted?: boolean | undefined, with_active_boosts?: boolean | undefined, recently_created?: boolean | undefined, with_liquidity_from?: string | undefined, my_tokens_filter_address?: 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 GetApiV1YieldPoolsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiSupportedYields = z.enum(["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 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 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 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 apiYieldBriefPoolDex = z.object({amm_type: apiAmmType}); export const apiYieldPool = z.object({address: z.string(), protocol: apiSupportedYields, is_trusted: z.boolean(), tokens: z.array(apiToken), pool_statistics: apiPoolStatistics, pool: apiYieldBriefPoolDex.optional()}); export const apiYieldSearchResponse = z.object({total_count: z.number().int(), pools: z.array(apiYieldPool)}); export const getApiV1YieldPoolsResponse = z.array(apiYieldSearchResponse); 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/yield/pools */ export const getApiV1YieldPools = async (args: GetApiV1YieldPoolsArgs, options?: GetApiV1YieldPoolsOptions) => { 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.providers ?? []) query.append("providers", 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.my_tokens_filter_address != null) query.append("my_tokens_filter_address", String(args.my_tokens_filter_address)) 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/yield/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(getApiV1YieldPoolsResponse, await res.json()) };