import {z} from 'zod' export type GetApiGetBlockArgs = {include_data?: boolean | undefined}; export type GetApiGetBlockOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const mD5 = z.string(); export const preciseNumber = z.string(); export const blockDetails = z.object({prev_block_id: mD5.optional(), height: z.number().int().optional(), round: z.number().int().optional(), num_confirmed: z.object({algorithm: z.number().int().optional(), benchmark: z.number().int().optional(), binary: z.number().int().optional(), advance: z.number().int().optional(), challenge: z.number().int().optional(), deposit: z.number().int().optional(), fraud: z.number().int().optional(), precommit: z.number().int().optional(), proof: z.number().int().optional(), topup: z.number().int().optional()}).optional(), num_active: z.object({algorithm: z.number().int().optional(), benchmark: z.number().int().optional(), advance: z.number().int().optional(), challenge: z.number().int().optional(), deposit: z.number().int().optional(), opow: z.number().int().optional(), player: z.number().int().optional()}).optional(), timestamp: z.number().int().optional(), emissions: z.object({algorithm: preciseNumber.optional(), advance: preciseNumber.optional(), benchmarker: preciseNumber.optional(), delegator: preciseNumber.optional(), bootstrap: preciseNumber.optional(), vault: preciseNumber.optional(), challenge_owner: preciseNumber.optional()}).optional(), gamma_value: z.number().optional()}); export const algorithmId = z.string(); export const challengeId = z.string(); export const address = z.string(); export const blockData = z.object({confirmed_ids: z.object({algorithm: z.array(algorithmId).optional(), benchmark: z.array(mD5).optional(), binary: z.array(algorithmId).optional(), advance: z.array(algorithmId).optional(), challenge: z.array(challengeId).optional(), deposit: z.array(address).optional(), fraud: z.array(address).optional(), precommit: z.array(address).optional(), proof: z.array(address).optional(), topup: z.array(address).optional()}).optional(), active_ids: z.object({code: z.array(algorithmId).optional(), benchmark: z.array(mD5).optional(), advance: z.array(algorithmId).optional(), challenge: z.array(challengeId).optional(), deposit: z.array(mD5).optional(), opow: z.array(address).optional(), player: z.array(address).optional()}).optional()}); export const block = z.object({id: mD5.optional(), details: blockDetails.optional(), data: blockData.optional(), config: z.object({}).optional()}); export const getBlockResponse = z.object({block: block.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 latest block * * GET /get-block */ export const getApiGetBlock = async (args: GetApiGetBlockArgs, options?: GetApiGetBlockOptions) => { const {baseUrl = "https://mainnet-api.tig.foundation", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include_data != null) query.append("include_data", String(args.include_data)) const url = `${baseUrl.replace(/\/+$/, '')}/get-block${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(getBlockResponse, await res.json()) };