import {z} from 'zod' export type GetApiV1AnalyticsPoolsPoolIdPriceChartArgs = {timeFrame?: ('24H' | '7D' | '30D') | undefined, poolId: string}; export type GetApiV1AnalyticsPoolsPoolIdPriceChartOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiV1AnalyticsPoolsPoolIdPriceChartResponse = z.object({networkId: z.string(), contractAddress: z.string(), chart: z.object({poolId: z.string(), poolType: z.enum(["shielded", "unshielded", "mixed", "stableUnshielded"]), tokenA: z.string(), tokenB: z.string(), asOf: z.string(), timeFrame: z.enum(["24H", "7D", "30D"]), points: z.array(z.object({startTime: z.string(), endTime: z.string(), closePriceTokenA: z.string().nullable(), closePriceTokenB: z.string().nullable(), volumeTokenA: z.string(), volumeTokenB: z.string(), tradeCount: z.number().int().gte(0)}))})}); 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 real trade-price history and volume for a single pool. * * GET /v1/analytics/pools/{poolId}/price-chart */ export const getApiV1AnalyticsPoolsPoolIdPriceChart = async (args: GetApiV1AnalyticsPoolsPoolIdPriceChartArgs, options: GetApiV1AnalyticsPoolsPoolIdPriceChartOptions) => { 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))}/price-chart${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(getApiV1AnalyticsPoolsPoolIdPriceChartResponse, await res.json()) };