import {z} from 'zod' export type GetApiV2BlockchainMasterchainMasterchainSeqnoBlocksArgs = {masterchain_seqno: number}; export type GetApiV2BlockchainMasterchainMasterchainSeqnoBlocksOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const blockCurrencyCollection = z.object({grams: z.number().int(), other: z.array(z.object({id: z.number().int(), value: z.string()}))}); export const blockValueFlow = z.object({burned: blockCurrencyCollection.optional(), created: blockCurrencyCollection, exported: blockCurrencyCollection, fees_collected: blockCurrencyCollection, fees_imported: blockCurrencyCollection, from_prev_blk: blockCurrencyCollection, imported: blockCurrencyCollection, minted: blockCurrencyCollection, recovered: blockCurrencyCollection, to_next_blk: blockCurrencyCollection}); export const blockchainBlock = z.object({after_merge: z.boolean(), after_split: z.boolean(), before_split: z.boolean(), created_by: z.string(), end_lt: z.number().int(), file_hash: z.string(), gen_catchain_seqno: z.number().int(), gen_software_capabilities: z.number().int().optional(), gen_software_version: z.number().int().optional(), gen_utime: z.number().int(), global_id: z.number().int(), in_msg_descr_length: z.number().int(), key_block: z.boolean(), master_ref: z.string().optional(), min_ref_mc_seqno: z.number().int(), out_msg_descr_length: z.number().int(), prev_key_block_seqno: z.number().int(), prev_refs: z.array(z.string()), rand_seed: z.string(), root_hash: z.string(), seqno: z.number().int(), shard: z.string(), start_lt: z.number().int(), tx_quantity: z.number().int(), value_flow: blockValueFlow, version: z.number().int(), vert_seqno: z.number().int(), want_merge: z.boolean(), want_split: z.boolean(), workchain_id: z.number().int()}); export const blockchainBlocks = z.object({blocks: z.array(blockchainBlock)}); 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 all blocks in all shards and workchains between target and previous masterchain block according to shards last blocks snapshot in masterchain. We don't recommend to build your app around this method because it has problem with scalability and will work very slow in the future. * * GET /v2/blockchain/masterchain/{masterchain_seqno}/blocks */ export const getApiV2BlockchainMasterchainMasterchainSeqnoBlocks = async (args: GetApiV2BlockchainMasterchainMasterchainSeqnoBlocksArgs, options?: GetApiV2BlockchainMasterchainMasterchainSeqnoBlocksOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/blockchain/masterchain/${encodeURIComponent(String(args.masterchain_seqno))}/blocks${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(blockchainBlocks, await res.json()) };