import {z} from 'zod' export type GetApiMessagesAggregatedHistoryArgs = {metric: 'message_count' | 'message_amount_sum', src_address?: Array | undefined, dst_address?: Array | undefined, src_workchain?: number | undefined, dst_workchain?: number | undefined, src_contract?: Array | undefined, dst_contract?: Array | undefined, operation_name?: Array | undefined, minter_address?: string | undefined, from?: string | undefined, to?: string | undefined, interval: '24h' | '8h' | '4h' | '1h' | '15m'}; export type GetApiMessagesAggregatedHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bunbigInt = z.object({}); export const historyMessagesRes = z.object({count_results: z.array(z.object({timestamp: z.string().optional(), value: z.number().int().optional()})).optional(), sum_results: z.array(z.object({timestamp: z.string().optional(), value: bunbigInt.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 messages grouped by timestamp * * GET /messages/aggregated/history */ export const getApiMessagesAggregatedHistory = async (args: GetApiMessagesAggregatedHistoryArgs, options?: GetApiMessagesAggregatedHistoryOptions) => { 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.src_address ?? []) query.append("src_address", String(item)) for (const item of args.dst_address ?? []) query.append("dst_address", String(item)) if (args.src_workchain != null) query.append("src_workchain", String(args.src_workchain)) if (args.dst_workchain != null) query.append("dst_workchain", String(args.dst_workchain)) for (const item of args.src_contract ?? []) query.append("src_contract", String(item)) for (const item of args.dst_contract ?? []) query.append("dst_contract", String(item)) for (const item of args.operation_name ?? []) query.append("operation_name", String(item)) if (args.minter_address != null) query.append("minter_address", String(args.minter_address)) 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(/\/+$/, '')}/messages/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(historyMessagesRes, await res.json()) };