import {z} from 'zod' export type CreateApiAnkrGetBlocksArgs = {body: {id: number, jsonrpc: '2.0', method: 'ankr_getBlocks', params: {blockchain: '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', fromBlock?: (number | string) | undefined, toBlock?: (number | string) | undefined, decodeLogs?: boolean | undefined, decodeTxData?: boolean | undefined, descOrder?: boolean | undefined, includeLogs?: boolean | undefined, includeTxs?: boolean | undefined}}}; export type CreateApiAnkrGetBlocksOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAnkrGetBlocksResponse = z.object({id: z.number().int(), jsonrpc: z.literal("2.0"), result: z.object({blocks: z.array(z.object({blockchain: z.string(), number: z.string(), hash: z.string(), parentHash: z.string(), nonce: z.string(), mixHash: z.string(), sha3Uncles: z.string(), logsBloom: z.string(), stateRoot: z.string(), transactionsRoot: z.string(), receiptsRoot: z.string(), miner: z.string(), difficulty: z.string(), totalDifficulty: z.string(), extraData: z.string(), size: z.string(), gasLimit: z.string(), gasUsed: z.string(), timestamp: z.string(), transactions: z.array(z.object({})).nullable(), uncles: z.array(z.string()).nullable()})).nullable().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 a range of blocks with optional decoded transactions and logs * * POST /?ankr_getBlocks */ export const createApiAnkrGetBlocks = async (args: CreateApiAnkrGetBlocksArgs, options?: CreateApiAnkrGetBlocksOptions) => { 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_getBlocks${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(createApiAnkrGetBlocksResponse, await res.json()) };