import {z} from 'zod' export type GetApiV2TradePoolsArgs = {sortBy?: ('tvl' | 'volume30d' | 'volume1d') | undefined, sortOrder?: ('asc' | 'desc') | undefined, search?: string | undefined, page?: number | undefined, limit?: number | undefined, poolVersion?: ('v1' | 'v2' | 'all') | undefined}; export type GetApiV2TradePoolsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const poolListRowResponseDto = z.object({fee: z.number(), tickSpacing: z.number(), protocolFees: z.number(), tradingFees: z.number(), creator: z.string().optional(), token0CompositeKey: z.string().optional(), token1CompositeKey: z.string().optional(), token0Image: z.string(), token1Image: z.string(), contractVersion: z.literal("v2"), token0: z.string(), token1: z.string(), poolName: z.string(), poolRef: z.string(), price: z.string().optional(), sqrtPrice: z.string().optional(), tick: z.number().optional(), token0Tvl: z.string().optional(), token1Tvl: z.string().optional(), tvlUsd: z.number().optional(), volume1d: z.number().optional(), volume7d: z.number().optional(), volume30d: z.number().optional(), trades1d: z.number().optional(), fee24h: z.number().optional(), apr1d: z.number().optional(), token0Price: z.number().optional(), token1Price: z.number().optional()}); export const poolsResponseDto = z.object({status: z.number(), message: z.string(), error: z.boolean(), data: z.array(poolListRowResponseDto)}); 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 }; /** * All new-contract pools with price and TVL (chain-direct) * * GET /v2/trade/pools */ export const getApiV2TradePools = async (args: GetApiV2TradePoolsArgs, options?: GetApiV2TradePoolsOptions) => { const {baseUrl = "https://swap-backend.stage.defi.ovh.gala.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.search != null) query.append("search", String(args.search)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.poolVersion != null) query.append("poolVersion", String(args.poolVersion)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/trade/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(poolsResponseDto, await res.json()) };