import {z} from 'zod' export type GetApiEvmAlienV3PoolArgs = {chain_id?: 8453 | undefined, pool_address: string}; export type GetApiEvmAlienV3PoolOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const table = z.object({columns: z.array(z.object({name: z.string(), type: z.string()})).optional(), data: z.array(z.array(z.union([z.string(), z.number(), z.boolean(), z.number().int()]))).optional(), rows: z.number().int().optional()}); export const tableResponse = z.array(table); 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 }; /** * V3 - Pool Info * * GET /evm/alien/v3/pool */ export const getApiEvmAlienV3Pool = async (args: GetApiEvmAlienV3PoolArgs, options: GetApiEvmAlienV3PoolOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.cambrian.table.v1.0.0+json") if (args.chain_id != null) query.append("chain_id", String(args.chain_id)) if (args.pool_address != null) query.append("pool_address", String(args.pool_address)) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/evm/alien/v3/pool${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(tableResponse, await res.json()) };