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 Timestamp = number; export type TimeRangeStartEnd = {start: Timestamp, end: Timestamp}; export type TimeRangeSelectorTime = {timestamp: TimeRangeStartEnd}; export type Epoch = number; export type EpochByNumber = {number: Epoch}; export type ChainView = 'latest' | 'finalized'; export type EpochByChainView = {view: ChainView}; export type EpochSelector = EpochByNumber | EpochByChainView; export type EpochRangeStartEnd = {start: EpochSelector, end: EpochSelector}; export type TimeRangeSelectorEpoch = {epoch: EpochRangeStartEnd}; export type Slot = number; export type SlotByNumber = {number: Slot}; export type ConsensusLayerBlockRoot = string; export type SlotByConsensusLayerBlockRoot = {root: ConsensusLayerBlockRoot}; export type SlotByChainView = {view: ChainView}; export type SlotSelector = SlotByNumber | SlotByConsensusLayerBlockRoot | SlotByChainView; export type SlotRangeStartEnd = {start?: SlotSelector | undefined, end?: SlotSelector | undefined}; export type TimeRangeSelectorSlot = {slot: SlotRangeStartEnd}; export type TimeRangeSelectorNullable = TimeRangeSelectorTime | TimeRangeSelectorEpoch | TimeRangeSelectorSlot | null; export type ValidatorChainOptionalStartEndCursorPageSize = {chain?: Chain | undefined, cursor?: Cursor | undefined, page_size?: PageSize | undefined, validator: ValidatorsSelector, range?: TimeRangeSelectorNullable | undefined}; export type CreateApiApiV2EthereumValidatorsAttestationSlotsArgs = {body: ValidatorChainOptionalStartEndCursorPageSize}; export type CreateApiApiV2EthereumValidatorsAttestationSlotsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const slot = z.number().int().gte(0); export const finalityParams = z.enum(["not_finalized", "finalized"]); export const validatorAttestationsData = z.object({slot: slot, assigned_validator_count: z.number().int(), finality: finalityParams}); export const paging = z.object({next_cursor: z.string().optional()}); 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 createApiApiV2EthereumValidatorsAttestationSlotsResponse = z.object({data: z.array(validatorAttestationsData), 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 }; /** * Attestation Slots * * POST /api/v2/ethereum/validators/attestation-slots */ export const createApiApiV2EthereumValidatorsAttestationSlots = async (args: CreateApiApiV2EthereumValidatorsAttestationSlotsArgs, options?: CreateApiApiV2EthereumValidatorsAttestationSlotsOptions) => { 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/attestation-slots${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(createApiApiV2EthereumValidatorsAttestationSlotsResponse, await res.json()) };