import {z} from 'zod' export type GetApiChartTokenPoolArgs = {token: string, pool: string, type?: ('1s' | '5s' | '15s' | '1m' | '3m' | '5m' | '15m' | '30m' | '1h' | '2h' | '4h' | '6h' | '8h' | '12h' | '1d' | '3d' | '1w' | '1mn') | undefined, time_from?: number | undefined, time_to?: number | undefined, marketCap?: boolean | undefined, currency?: ('usd' | 'sol' | 'eur') | undefined, removeOutliers?: boolean | undefined, timezone?: string | undefined, fastCache?: boolean | undefined}; export type GetApiChartTokenPoolOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const oHLCV = z.object({open: z.number().optional(), close: z.number().optional(), low: z.number().optional(), high: z.number().optional(), volume: z.number().optional(), time: z.number().int().optional()}); export const chartResponse = z.object({oclhv: z.array(oHLCV).optional()}); 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 OHLCV Data for a token/pool pair * * GET /chart/{token}/{pool} */ export const getApiChartTokenPool = async (args: GetApiChartTokenPoolArgs, options?: GetApiChartTokenPoolOptions) => { const {baseUrl = "https://data.solanatracker.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.time_from != null) query.append("time_from", String(args.time_from)) if (args.time_to != null) query.append("time_to", String(args.time_to)) if (args.marketCap != null) query.append("marketCap", String(args.marketCap)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.removeOutliers != null) query.append("removeOutliers", String(args.removeOutliers)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (args.fastCache != null) query.append("fastCache", String(args.fastCache)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/chart/${encodeURIComponent(String(args.token))}/${encodeURIComponent(String(args.pool))}${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(chartResponse, await res.json()) };