import {z} from 'zod' export type GetApiV1TradeOpportunitiesArgs = {limit?: number | undefined}; export type GetApiV1TradeOpportunitiesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tradePoolAttributesResponseDto = z.object({poolPair: z.string(), poolHash: z.string(), token0: z.string(), token0Image: z.string(), token1: z.string(), token1Image: z.string(), token0CompositeKey: z.string().nullable(), token1CompositeKey: z.string().nullable(), token0OriginChain: z.string().nullable(), token1OriginChain: z.string().nullable(), token0Price: z.string(), token1Price: z.string(), poolName: z.string(), fee: z.string(), fee24h: z.number(), token0Tvl: z.number(), token0TvlUsd: z.number(), token1Tvl: z.number(), token1TvlUsd: z.number(), tvl: z.number(), volume1d: z.number(), volume30d: z.number(), volume7d: z.number(), trades1d: z.number(), poolCreatedAt: z.string().nullable(), dayPerTvl: z.number(), apr1d: z.number(), signals: z.array(z.enum(["new-active", "trending", "high-turnover", "high-fee", "top-volume", "bot-viable"])), opportunityScore: z.number(), contractVersion: z.enum(["v1", "v2"])}); export const tradePoolListDataResponseDto = z.object({pools: z.array(tradePoolAttributesResponseDto), count: z.number()}); export const tradePoolListResponseDto = z.object({status: z.number(), error: z.literal(false), message: z.string().optional(), data: tradePoolListDataResponseDto}); 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 }; /** * Get ranked legacy opportunity pools * * GET /v1/trade/opportunities */ export const getApiV1TradeOpportunities = async (args: GetApiV1TradeOpportunitiesArgs, options?: GetApiV1TradeOpportunitiesOptions) => { 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.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/trade/opportunities${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(tradePoolListResponseDto, await res.json()) };