import {z} from 'zod' export type StakingAddress = string; export type GetApiConsensusValidatorsAddressHistoryArgs = {limit?: number | undefined, offset?: number | undefined, from?: number | undefined, to?: number | undefined, address: StakingAddress}; export type GetApiConsensusValidatorsAddressHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const textBigInt = z.string(); export const validatorHistoryPoint = z.object({epoch: z.number().int(), active_balance: textBigInt.optional(), active_shares: textBigInt.optional(), debonding_balance: textBigInt.optional(), debonding_shares: textBigInt.optional(), num_delegators: z.number().int().optional()}); export const validatorHistory = z.object({total_count: z.number().int(), is_total_count_clipped: z.boolean(), address: z.string().optional(), history: z.array(validatorHistoryPoint)}); 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 }; /** * Returns historical information for a single validator. * * GET /consensus/validators/{address}/history */ export const getApiConsensusValidatorsAddressHistory = async (args: GetApiConsensusValidatorsAddressHistoryArgs, options?: GetApiConsensusValidatorsAddressHistoryOptions) => { const {baseUrl = "https://nexus.oasis.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) const url = `${baseUrl.replace(/\/+$/, '')}/consensus/validators/${encodeURIComponent(String(args.address))}/history${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(validatorHistory, await res.json()) };