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 NullableValidatorSelector = ValidatorsByIdentifiers | ValidatorsByDashboard | ValidatorsByDeposit | ValidatorsByWithdrawal | ValidatorsByEntity; export type SyncCommitteePeriod = number; export type SyncCommitteePeriodByNumber = {number: SyncCommitteePeriod}; export type SyncCommitteeView = 'latest' | 'next'; export type SyncCommitteePeriodByChainView = {view: SyncCommitteeView}; export type SyncCommitteePeriodSelector = SyncCommitteePeriodByNumber | SyncCommitteePeriodByChainView; export type SyncCommitteeChainOptionalValidator = {chain: Chain, cursor?: Cursor | undefined, page_size?: PageSize | undefined, validator?: NullableValidatorSelector | undefined, sync_committee_period: SyncCommitteePeriodSelector}; export type CreateApiApiV2EthereumSyncCommitteeValidatorsArgs = {body: SyncCommitteeChainOptionalValidator}; export type CreateApiApiV2EthereumSyncCommitteeValidatorsOptions = {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 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 syncCommitteeValidatorsValidator = z.object({validator: validator, duties: validatorSyncCommitteeDutyParticipation}); export const syncCommitteeValidatorsData = z.array(syncCommitteeValidatorsValidator); 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 createApiApiV2EthereumSyncCommitteeValidatorsResponse = z.object({data: syncCommitteeValidatorsData, 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 }; /** * Validators * * POST /api/v2/ethereum/sync-committee/validators */ export const createApiApiV2EthereumSyncCommitteeValidators = async (args: CreateApiApiV2EthereumSyncCommitteeValidatorsArgs, options?: CreateApiApiV2EthereumSyncCommitteeValidatorsOptions) => { 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/sync-committee/validators${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(createApiApiV2EthereumSyncCommitteeValidatorsResponse, await res.json()) };