import {z} from 'zod' export type GetApiTransactionsAggregatedHistoryArgs = {metric: 'transaction_count', address?: Array | undefined, workchain?: number | undefined, from?: string | undefined, to?: string | undefined, interval: '24h' | '8h' | '4h' | '1h' | '15m'}; export type GetApiTransactionsAggregatedHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const historyTransactionsRes = z.object({count_results: z.array(z.object({timestamp: z.string().optional(), value: z.number().int().optional()})).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 }; /** * aggregated transactions grouped by timestamp * * GET /transactions/aggregated/history */ export const getApiTransactionsAggregatedHistory = async (args: GetApiTransactionsAggregatedHistoryArgs, options?: GetApiTransactionsAggregatedHistoryOptions) => { const {baseUrl = "https://anton.tools/api/v0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.metric != null) query.append("metric", String(args.metric)) for (const item of args.address ?? []) query.append("address", String(item)) if (args.workchain != null) query.append("workchain", String(args.workchain)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.interval != null) query.append("interval", String(args.interval)) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/aggregated/history${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(historyTransactionsRes, await res.json()) };