import {z} from 'zod' export type CreateApiAnkrGetBlockchainStatsArgs = {body: {id: number, jsonrpc: '2.0', method: 'ankr_getBlockchainStats', params: {blockchain?: (Array<'arbitrum' | 'avalanche' | 'base' | 'bsc' | 'eth' | 'fantom' | 'flare' | 'gnosis' | 'linea' | 'optimism' | 'polygon' | 'scroll' | 'somnia_mainnet' | 'taiko' | 'telos' | 'xlayer' | 'avalanche_fuji' | 'base_sepolia' | 'eth_sepolia' | 'optimism_sepolia' | 'polygon_amoy'>) | undefined}}}; export type CreateApiAnkrGetBlockchainStatsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAnkrGetBlockchainStatsResponse = z.object({id: z.number().int(), jsonrpc: z.literal("2.0"), result: z.object({stats: z.array(z.object({blockTimeMs: z.number().int().gte(0), blockchain: z.string(), latestBlockNumber: z.number().int().gte(0), nativeCoinUsdPrice: z.string(), totalEventsCount: z.number().int().gte(0), totalTransactionsCount: z.number().int().gte(0)})).nullable()})}); 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 per-chain stats: latest block, totals, native coin USD price * * POST /?ankr_getBlockchainStats */ export const createApiAnkrGetBlockchainStats = async (args: CreateApiAnkrGetBlockchainStatsArgs, options?: CreateApiAnkrGetBlockchainStatsOptions) => { const {baseUrl = "https://rpc.ankr.com/multichain/569ca1f392f7d3d0bb1073a1173ecb649af1d9249231fe59fa95db19d5ae41fe", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/?ankr_getBlockchainStats${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiAnkrGetBlockchainStatsResponse, await res.json()) };