import {z} from 'zod' export type GetApiDumpConsensusStateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const validatorPriority = z.object({address: z.string().optional(), pub_key: z.object({type: z.string(), value: z.string()}).optional(), voting_power: z.string().optional(), proposer_priority: z.string().optional()}); export const dumpConsensusResponse = z.object({jsonrpc: z.string(), id: z.number().int(), result: z.object({round_state: z.object({height: z.string(), round: z.number().int(), step: z.number().int(), start_time: z.string(), commit_time: z.string(), validators: z.object({validators: z.array(validatorPriority)}), locked_round: z.number().int(), valid_round: z.string(), votes: z.array(z.object({round: z.string().optional(), prevotes: z.array(z.string()).nullable().optional(), prevotes_bit_array: z.string().optional(), precommits: z.array(z.string()).nullable().optional(), precommits_bit_array: z.string().optional()})), commit_round: z.number().int(), last_commit: z.object({votes: z.array(z.string()), votes_bit_array: z.string(), peer_maj_23s: z.object({})}).nullable(), last_validators: z.object({validators: z.array(validatorPriority)}), triggered_timeout_precommit: z.boolean()}), proposer: z.object({address: z.string(), pub_key: z.object({type: z.string(), value: z.string()}), voting_power: z.string(), proposer_priority: z.string()}).optional(), peers: z.array(z.object({node_address: z.string().optional(), peer_state: z.object({round_state: z.object({height: z.string(), round: z.string(), step: z.number().int(), start_time: z.string(), proposal: z.boolean(), proposal_block_parts_header: z.object({total: z.number().int(), hash: z.string()}), proposal_pol_round: z.number().int().nullable(), proposal_pol: z.string().nullable(), prevotes: z.string().nullable(), precommits: z.string().nullable(), last_commit_round: z.number().int().nullable(), last_commit: z.string().nullable(), catchup_commit_round: z.number().int().nullable(), catchup_commit: z.string().nullable()}), stats: z.object({votes: z.string(), block_parts: z.string()})}).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 consensus state * * GET /dump_consensus_state */ export const getApiDumpConsensusState = async (options?: GetApiDumpConsensusStateOptions) => { const {baseUrl = "http://localhost:26657", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/dump_consensus_state${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(dumpConsensusResponse, await res.json()) };