import {z} from 'zod' export type Chain = 'mainnet' | 'hoodi'; export type Cursor = string; export type PageSize = number; export type ValidatorIndex = number; export type ValidatorPublicKey = string; export type ValidatorIndexPublicKey = ValidatorIndex | ValidatorPublicKey; export type ValidatorIndexPublicKeys = Array; export type ValidatorsByIdentifiers = {validator_identifiers: ValidatorIndexPublicKeys}; export type DashboardID = number; export type DashboardGroupID = number | null; export type ValidatorsByDashboard = {dashboard_id: DashboardID, group_id?: DashboardGroupID | undefined}; export type ExecutionLayerAddress = string; export type ValidatorsByDeposit = {deposit_address: ExecutionLayerAddress}; export type AddressOrCredential = string; export type ValidatorsByWithdrawal = {withdrawal: AddressOrCredential}; export type ValidatorsByEntity = {entity: string, sub_entity?: string | undefined}; export type ValidatorsSelector = ValidatorsByIdentifiers | ValidatorsByDashboard | ValidatorsByDeposit | ValidatorsByWithdrawal | ValidatorsByEntity; export type Epoch = number; export type CreateApiApiV2EthereumValidatorsPerformanceListArgs = {body: {chain?: Chain | undefined, cursor?: Cursor | undefined, page_size?: PageSize | undefined, validator: ValidatorsSelector, epoch: Epoch}}; export type CreateApiApiV2EthereumValidatorsPerformanceListOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const validatorIndex = z.number().int().gte(0); export const validatorPublicKey = z.string(); export const validator = z.object({index: validatorIndex.nullable().optional(), public_key: validatorPublicKey.optional()}); export const percent = z.number().gte(0).lte(1); export const performanceBeaconscore = z.object({total: percent.nullable().optional(), attestation: percent.nullable().optional(), proposal: percent.nullable().optional(), sync_committee: percent.nullable().optional()}); export const performanceDutiesAttestation = z.object({included: z.number().int(), assigned: z.number().int(), correct_head: z.number().int(), correct_source: z.number().int(), correct_target: z.number().int(), valuable_correct_head: z.number().int(), valuable_correct_source: z.number().int(), valuable_correct_target: z.number().int(), avg_inclusion_delay: z.number().gte(0).lte(63), avg_inclusion_delay_excluding_missed_slots: z.number().gte(0).lte(63), missed: z.number().int()}); export const validatorSyncCommitteeDutyParticipation = z.object({successful: z.number().int().gte(0), assigned: z.number().int().gte(0), missed: z.number().int().gte(0), missed_including_missed_slots: z.number().int().gte(0), scheduled: z.number().int().gte(0)}); export const performanceDutiesProposal = z.object({successful: z.number().int(), assigned: z.number().int(), missed: z.number().int(), included_slashings: z.number().int()}); export const performanceDutiesDuties = z.object({attestation: performanceDutiesAttestation, sync_committee: validatorSyncCommitteeDutyParticipation, proposal: performanceDutiesProposal}); export const finalityParams = z.enum(["not_finalized", "finalized"]); export const validatorPerformanceDetailedData = z.object({validator: validator, beaconscore: performanceBeaconscore, duties: performanceDutiesDuties, finality: finalityParams}); export const paging = z.object({next_cursor: z.string().optional()}); export const slot = z.number().int().gte(0); export const slotRange = z.object({start: slot, end: slot}); export const epoch = z.number().int().gte(0); export const epochRange = z.object({start: epoch, end: epoch}); export const timestamp = z.number().int().gte(0); export const timeRange = z.object({start: timestamp, end: timestamp}); export const resultRange = z.object({slot: slotRange, epoch: epochRange, timestamp: timeRange}); export const createApiApiV2EthereumValidatorsPerformanceListResponse = z.object({data: z.array(validatorPerformanceDetailedData), paging: paging.optional(), range: resultRange}); 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 }; /** * Performance * * POST /api/v2/ethereum/validators/performance-list */ export const createApiApiV2EthereumValidatorsPerformanceList = async (args: CreateApiApiV2EthereumValidatorsPerformanceListArgs, options?: CreateApiApiV2EthereumValidatorsPerformanceListOptions) => { 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/validators/performance-list${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(createApiApiV2EthereumValidatorsPerformanceListResponse, await res.json()) };