import {z} from 'zod' export type Chain = 'mainnet' | 'hoodi'; 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 EvaluationWindow = '24h' | '7d' | '30d' | '90d' | 'all_time'; export type NamedEvaluationWindow = {evaluation_window: EvaluationWindow}; 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 TimeRangeSelector = NamedEvaluationWindow | TimeRangeSelectorTime | TimeRangeSelectorEpoch | TimeRangeSelectorSlot; export type AggregateValidatorChainStartEnd = {chain?: Chain | undefined, validator: ValidatorsSelector, range: TimeRangeSelector}; export type CreateApiApiV2EthereumValidatorsRewardsAggregateArgs = {body: AggregateValidatorChainStartEnd}; export type CreateApiApiV2EthereumValidatorsRewardsAggregateOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const wei = z.string(); export const rewardBreakdownHead = z.object({total: wei, reward: wei, penalty: wei, missed_reward: wei}); export const rewardBreakdown = z.object({total: wei, reward: wei, penalty: wei, missed_reward: wei}); export const attestationInclusionDelay = z.object({total: wei, missed_reward: wei}).nullable(); export const attestationRewards = z.object({total: wei.optional(), head: rewardBreakdownHead, source: rewardBreakdown, target: rewardBreakdown, inactivity_leak_penalty: wei, inclusion_delay: attestationInclusionDelay.optional()}); export const proposalRewards = z.object({total: wei, execution_layer_reward: wei, attestation_inclusion_reward: wei, sync_inclusion_reward: wei, slashing_inclusion_reward: wei, missed_cl_reward: wei, missed_el_reward: wei}); export const finalityParams = z.enum(["not_finalized", "finalized"]); export const validatorRewardsAggregateData = z.object({total: wei, total_reward: wei, total_penalty: wei, total_missed: wei.optional(), attestation: attestationRewards, sync_committee: rewardBreakdown, proposal: proposalRewards, finality: finalityParams.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 validatorRewardsAggregateContainerList = z.object({data: validatorRewardsAggregateData, 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 }; /** * Rewards Aggregated * * POST /api/v2/ethereum/validators/rewards-aggregate */ export const createApiApiV2EthereumValidatorsRewardsAggregate = async (args: CreateApiApiV2EthereumValidatorsRewardsAggregateArgs, options?: CreateApiApiV2EthereumValidatorsRewardsAggregateOptions) => { 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/rewards-aggregate${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(validatorRewardsAggregateContainerList, await res.json()) };