import {z} from 'zod' export type GetApiV2TradeOpportunitiesArgs = {limit?: number | undefined}; export type GetApiV2TradeOpportunitiesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tradeV2OpportunityPoolResponseDto = 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(), signals: z.array(z.enum(["new-active", "trending", "high-turnover", "high-fee", "top-volume", "bot-viable"])), opportunityScore: z.number()}); export const tradeV2OpportunitiesDataResponseDto = z.object({pools: z.array(tradeV2OpportunityPoolResponseDto), count: z.number()}); export const tradeV2OpportunitiesResponseDto = z.object({status: z.number(), message: z.string(), error: z.boolean(), data: tradeV2OpportunitiesDataResponseDto}); 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 }; /** * Ranked new-contract opportunity pools * * GET /v2/trade/opportunities */ export const getApiV2TradeOpportunities = async (args: GetApiV2TradeOpportunitiesArgs, options?: GetApiV2TradeOpportunitiesOptions) => { 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(/\/+$/, '')}/v2/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(tradeV2OpportunitiesResponseDto, await res.json()) };