import {z} from 'zod' export type GetApiV1AnalyticsPoolsPoolIdOverviewArgs = {timeFrame?: ('24H' | '7D' | '30D') | undefined, poolId: string}; export type GetApiV1AnalyticsPoolsPoolIdOverviewOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiV1AnalyticsPoolsPoolIdOverviewResponse = z.object({networkId: z.string(), contractAddress: z.string(), overview: z.object({pool: z.object({poolId: z.string(), poolType: z.enum(["shielded", "unshielded", "mixed", "stableUnshielded"]), tokenA: z.string(), tokenB: z.string(), tvl: z.object({tokenA: z.string(), tokenB: z.string()}), volume24h: z.object({tokenA: z.string(), tokenB: z.string()}), cumulativeVolume: z.object({tokenA: z.string(), tokenB: z.string()}), lastTradeAt: z.string().nullable()}), feeBps: z.number().int().gte(0).lte(10000), asOf: z.string(), timeFrame: z.enum(["24H", "7D", "30D"]), buckets: z.array(z.object({startTime: z.string(), endTime: z.string(), tvl: z.object({tokenA: z.string(), tokenB: z.string()}), volume: z.object({tokenA: z.string(), tokenB: z.string()}), feesEarned: z.object({tokenA: z.string(), tokenB: z.string()}), tradeCount: z.number().int().gte(0)})), limitations: z.array(z.object({title: z.string(), reason: z.string()}))})}); 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 }; /** * Read bucketed TVL, volume, and fee analytics for a single pool. * * GET /v1/analytics/pools/{poolId}/overview */ export const getApiV1AnalyticsPoolsPoolIdOverview = async (args: GetApiV1AnalyticsPoolsPoolIdOverviewArgs, options: GetApiV1AnalyticsPoolsPoolIdOverviewOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.timeFrame != null) query.append("timeFrame", String(args.timeFrame)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/pools/${encodeURIComponent(String(args.poolId))}/overview${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(getApiV1AnalyticsPoolsPoolIdOverviewResponse, await res.json()) };