import {z} from 'zod' export type GetApiV1Brc20SwapPoolListArgs = {search?: string | undefined, start: number, limit: number}; export type GetApiV1Brc20SwapPoolListOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1Brc20SwapPoolListResponse = z.object({code: z.number(), msg: z.string(), data: z.object({total: z.number().optional(), list: z.array(z.object({tick0: z.string().optional(), tick1: z.string().optional(), lp: z.string().optional(), tvl: z.string().optional(), volume24h: z.string().optional(), volume7d: z.string().optional()})).optional()})}); 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 }; /** * Gets the pool list information. * * GET /v1/brc20-swap/pool_list */ export const getApiV1Brc20SwapPoolList = async (args: GetApiV1Brc20SwapPoolListArgs, options?: GetApiV1Brc20SwapPoolListOptions) => { const {baseUrl = "https://open-api.unisat.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.start != null) query.append("start", String(args.start)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/brc20-swap/pool_list${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(getApiV1Brc20SwapPoolListResponse, await res.json()) };