import {z} from 'zod' export type GetApiV2RewardsValidationRoundsArgs = {election_id?: number | undefined, block?: number | undefined, unixtime?: number | undefined}; export type GetApiV2RewardsValidationRoundsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const validationRound = z.object({bonuses: z.number().int().optional(), election_id: z.number().int(), end_block: z.number().int().optional(), end_utime: z.number().int().optional(), finished: z.boolean(), next_election_id: z.number().int().optional(), prev_election_id: z.number().int().optional(), start_block: z.number().int(), start_utime: z.number().int().optional(), total_stake: z.number().int().optional()}); export const validationRoundsResponse = z.object({response_time_ms: z.number().int(), rounds: z.array(validationRound)}); 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 validation round metadata * * GET /v2/rewards/validation-rounds */ export const getApiV2RewardsValidationRounds = async (args: GetApiV2RewardsValidationRoundsArgs, options?: GetApiV2RewardsValidationRoundsOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.election_id != null) query.append("election_id", String(args.election_id)) if (args.block != null) query.append("block", String(args.block)) if (args.unixtime != null) query.append("unixtime", String(args.unixtime)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/rewards/validation-rounds${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(validationRoundsResponse, await res.json()) };