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 CreateApiApiV2EthereumBlockMevTimingArgs = {body: {chain?: Chain | undefined, block: BlockSelector}}; export type CreateApiApiV2EthereumBlockMevTimingOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const mevTimingStatus = z.enum(["on_time_mev", "timing_games", "aggressive_tg", "no_mev", "unknown", "pending"]); export const wei = z.string(); export const slotOffsetMs = z.number().int(); export const mevWinningBid = z.object({value: wei, slot_offset_ms: slotOffsetMs, relays: z.array(z.string()), builder_pubkey: z.string().nullable()}); export const mevBid = z.object({value: wei, slot_offset_ms: slotOffsetMs, status: mevTimingStatus}); export const blockMevTimingData = z.object({status: mevTimingStatus, winning_bid: mevWinningBid.nullable(), first_better_bid: mevBid.nullable(), best_later_bid: mevBid.nullable(), missed_value: wei.nullable(), relay_count: z.number().int().gte(0).nullable(), bid_count: z.number().int().gte(0).nullable()}); export const blockMevTimingContainer = z.object({data: blockMevTimingData}); 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 }; /** * MEV Timing * * POST /api/v2/ethereum/block/mev-timing */ export const createApiApiV2EthereumBlockMevTiming = async (args: CreateApiApiV2EthereumBlockMevTimingArgs, options?: CreateApiApiV2EthereumBlockMevTimingOptions) => { 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/mev-timing${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(blockMevTimingContainer, await res.json()) };