import {z} from 'zod' export type Chain = 'mainnet' | 'hoodi'; export type NamedChain = {chain?: Chain | undefined}; export type CreateApiApiV2EthereumQueuesArgs = {body: NamedChain}; export type CreateApiApiV2EthereumQueuesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const count = z.number().int().gte(0); export const wei = z.string(); export const timestamp = z.number().int().gte(0); export const queueStatsDepositQueue = z.object({deposit_count: count.optional(), balance: wei.optional(), estimated_processed_at: timestamp.optional(), churn: wei}); export const queueStatsExitQueue = z.object({count: count.optional(), balance: wei.optional(), estimated_processed_at: timestamp.optional(), churn: wei}); export const timeSecondsDuration = z.number().int().gte(0); export const queueStatsWithdrawalSweep = z.object({estimated_sweep_delay: timeSecondsDuration.optional(), last_swept_validator_index: z.number().int()}); export const finalityParamsOnlyNoFinalization = z.literal("not_finalized"); export const queueStatsData = z.object({deposit_queue: queueStatsDepositQueue, exit_queue: queueStatsExitQueue, withdrawal_sweep: queueStatsWithdrawalSweep, finality: finalityParamsOnlyNoFinalization}); export const queueStatsContainer = z.object({data: queueStatsData}); 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 }; /** * Network Queue Overview * * POST /api/v2/ethereum/queues */ export const createApiApiV2EthereumQueues = async (args: CreateApiApiV2EthereumQueuesArgs, options?: CreateApiApiV2EthereumQueuesOptions) => { const {baseUrl = "https://beaconcha.in", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/ethereum/queues${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(queueStatsContainer, await res.json()) };