import {z} from 'zod' export type GetApiApiMarketDataArgs = {startDate: string, endDate: string, tokens?: string | undefined}; export type GetApiApiMarketDataOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const futarchyAmmDailyActivity = z.object({token: z.string(), date: z.string(), has_conditional_volume: z.boolean(), spot_buy_volume: z.string(), spot_sell_volume: z.string(), spot_base_volume: z.string(), spot_target_volume: z.string(), spot_usdc_fees: z.string(), spot_token_fees: z.string(), spot_token_fees_usdc: z.string(), spot_protocol_fee_usd: z.string(), spot_lp_fee_usd: z.string(), spot_trade_count: z.number().int(), conditional_buy_volume: z.string().nullable(), conditional_sell_volume: z.string().nullable(), conditional_base_volume: z.string().nullable(), conditional_target_volume: z.string().nullable(), conditional_usdc_fees: z.string().nullable(), conditional_token_fees: z.string().nullable(), conditional_token_fees_usdc: z.string().nullable(), conditional_protocol_fee_usd: z.string().nullable(), conditional_lp_fee_usd: z.string().nullable(), conditional_trade_count: z.number().int().nullable(), total_buy_volume: z.string(), total_sell_volume: z.string(), total_base_volume: z.string(), total_target_volume: z.string(), total_usdc_fees: z.string(), total_token_fees: z.string(), total_token_fees_usdc: z.string(), total_protocol_fee_usd: z.string(), total_lp_fee_usd: z.string(), total_trade_count: z.number().int(), conditional_reconciled: z.boolean(), pending_open_proposals: z.number().int()}); export const meteoraDailyVolume = z.object({token: z.string(), date: z.string(), base_volume: z.string(), target_volume: z.string(), buy_volume: z.string(), sell_volume: z.string(), trade_count: z.number().int(), average_price: z.string(), usdc_fees: z.string(), token_fees: z.string(), token_fees_usdc: z.string(), token_per_usdc: z.string().nullable()}); export const marketDataResponse = z.object({filters: z.object({tokens: z.union([z.literal("all"), z.array(z.string())]), startDate: z.string(), endDate: z.string()}), source: z.literal("user-pool-etl"), futarchyAMM: z.object({source: z.literal("etl-user-pool-daily"), count: z.number().int(), data: z.array(futarchyAmmDailyActivity)}), meteora: z.object({source: z.literal("etl-meteora-daily"), count: z.number().int(), data: z.array(meteoraDailyVolume)})}); 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 }; /** * Daily market data * * GET /api/market-data */ export const getApiApiMarketData = async (args: GetApiApiMarketDataArgs, options: GetApiApiMarketDataOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.tokens != null) query.append("tokens", String(args.tokens)) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/market-data${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(marketDataResponse, await res.json()) };