import {z} from 'zod' export type Chain = 'mainnet' | 'hoodi'; export type Block = number; export type BlockByNumber = {number: Block}; export type ChainView = 'latest' | 'finalized'; export type BlockByChainView = {view: ChainView}; export type BlockSelector = BlockByNumber | BlockByChainView; export type CreateApiApiV2EthereumBlockRewardsArgs = {body: {chain?: Chain | undefined, block: BlockSelector}}; export type CreateApiApiV2EthereumBlockRewardsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const wei = z.string(); export const executionLayerAddress = z.string(); export const executionLayerAddressDetails = z.object({address: executionLayerAddress, is_contract: z.boolean()}); export const rewardDetails = z.object({amount: wei, recipient: executionLayerAddressDetails}); export const blockRewardMining = z.object({recipient: executionLayerAddressDetails, static_block_reward: wei, uncle_inclusion_reward: wei}).nullable(); export const block = z.number().int().gte(0); export const executionLayerBlockHash = z.string(); export const executionLayerReference = z.object({block: block, hash: executionLayerBlockHash}); export const slot = z.number().int().gte(0); export const consensusLayerBlockRoot = z.string(); export const epoch = z.number().int().gte(0); export const consensusLayerReference = z.object({slot: slot, block_root: consensusLayerBlockRoot.nullable().optional(), epoch: epoch}).nullable(); export const finalityParams = z.enum(["not_finalized", "finalized"]); export const blockRewardData = z.object({total_reward: rewardDetails, priority_fees: rewardDetails, mev: rewardDetails.nullable().optional(), mining: blockRewardMining.optional(), execution_reference: executionLayerReference, consensus_reference: consensusLayerReference.optional(), finality: finalityParams.optional()}); export const blockRewardContainer = z.object({data: blockRewardData}); 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 }; /** * Details * * POST /api/v2/ethereum/block/rewards */ export const createApiApiV2EthereumBlockRewards = async (args: CreateApiApiV2EthereumBlockRewardsArgs, options?: CreateApiApiV2EthereumBlockRewardsOptions) => { 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/block/rewards${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(blockRewardContainer, await res.json()) };