import {z} from 'zod' export type GetApiBlockByHashArgs = {hash: string}; export type GetApiBlockByHashOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const blockID = z.object({hash: z.string(), parts: z.object({total: z.number().int(), hash: z.string()})}); export const blockHeader = z.object({version: z.object({block: z.string(), app: z.string()}), chain_id: z.string(), height: z.string(), time: z.string(), last_block_id: blockID, last_commit_hash: z.string(), data_hash: z.string(), validators_hash: z.string(), next_validators_hash: z.string(), consensus_hash: z.string(), app_hash: z.string(), last_results_hash: z.string(), evidence_hash: z.string(), proposer_address: z.string()}); export const pubKey = z.object({type: z.string().optional(), value: z.string().optional()}); export const validator = z.object({pub_key: pubKey.optional(), voting_power: z.number().int().optional(), address: z.string().optional()}); export const evidence = z.object({type: z.string().optional(), height: z.number().int().optional(), time: z.number().int().optional(), total_voting_power: z.number().int().optional(), validator: validator.optional()}); export const commit = z.object({type: z.number().int(), height: z.string(), round: z.number().int(), block_id: blockID, timestamp: z.string(), validator_address: z.string(), validator_index: z.number().int(), signature: z.string()}); export const block = z.object({header: blockHeader.optional(), data: z.array(z.string()).optional(), evidence: z.array(evidence).optional(), last_commit: z.object({height: z.number().int().optional(), round: z.number().int().optional(), block_id: blockID.optional(), signatures: z.array(commit).optional()}).optional()}); export const blockComplete = z.object({block_id: blockID.optional(), block: block.optional()}); export const blockResponse = z.object({id: z.number().int().optional(), jsonrpc: z.string().optional(), result: blockComplete.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 block by hash * * GET /block_by_hash */ export const getApiBlockByHash = async (args: GetApiBlockByHashArgs, options?: GetApiBlockByHashOptions) => { const {baseUrl = "http://localhost:26657", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.hash != null) query.append("hash", String(args.hash)) const url = `${baseUrl.replace(/\/+$/, '')}/block_by_hash${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(blockResponse, await res.json()) };