import {z} from 'zod' export type GetApiV1PoolBlockchainByTokenAddressArgs = {blockchain: string, address: string, dexes?: Array | undefined, size?: number | undefined, page?: number | undefined}; export type GetApiV1PoolBlockchainByTokenAddressOptions = {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 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 apiTrimmedPool = z.object({dex: z.string(), address: z.string(), type: apiPoolType, amm_type: apiAmmType, amm_settings: apiJson.optional(), tokens: z.array(z.string()), reserves: apiPoolReserves, restrictions: z.array(apiTokenRestrictions).optional(), fees: apiPoolFees, unavailable_until: z.number().int().optional()}); export const getApiV1PoolBlockchainByTokenAddressResponse = z.array(apiTrimmedPool); 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 associated with given token * * GET /v1/pool/{blockchain}/by-token/{address} */ export const getApiV1PoolBlockchainByTokenAddress = async (args: GetApiV1PoolBlockchainByTokenAddressArgs, options?: GetApiV1PoolBlockchainByTokenAddressOptions) => { 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.dexes ?? []) query.append("dexes", String(item)) 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/pool/${encodeURIComponent(String(args.blockchain))}/by-token/${encodeURIComponent(String(args.address))}${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(getApiV1PoolBlockchainByTokenAddressResponse, await res.json()) };