import {z} from 'zod' export type Chain = 'mainnet' | 'hoodi'; 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 Cursor = string; export type PageSize = number; export type NetworkMevTimingRequest = {chain?: Chain | undefined, range?: TimeRangeSelectorNullable | undefined, min_slots_per_day?: number | undefined, cursor?: Cursor | undefined, page_size?: PageSize | undefined}; export type CreateApiApiV2EthereumMevTimingArgs = {body: NetworkMevTimingRequest}; export type CreateApiApiV2EthereumMevTimingOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const timestamp = z.number().int().gte(0); export const mevTimingBreakdown = z.object({on_time_mev: z.number().int().gte(0), timing_games: z.number().int().gte(0), aggressive_tg: z.number().int().gte(0)}); export const wei = z.string(); export const mevTimingBucket = z.object({slot_offset_ms_min: z.number().int(), slot_offset_ms_max: z.number().int(), count: z.number().int().gte(0), max_value: wei.nullable()}); export const mevTimingHistogram = z.array(mevTimingBucket); export const mevTimingDailyData = z.object({timestamp: timestamp, date: z.string(), slots_with_relay: z.number().int().gte(0), slots_with_relay_winners: z.number().int().gte(0), slots_without_relay: z.number().int().gte(0), timing_breakdown: mevTimingBreakdown, median_slot_offset_ms: z.number().nullable(), histogram: mevTimingHistogram}); 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 timeRange = z.object({start: timestamp, end: timestamp}); export const resultRange = z.object({slot: slotRange, epoch: epochRange, timestamp: timeRange}); export const createApiApiV2EthereumMevTimingResponse = z.object({data: z.array(mevTimingDailyData), 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 }; /** * Network MEV Timing * * POST /api/v2/ethereum/mev-timing */ export const createApiApiV2EthereumMevTiming = async (args: CreateApiApiV2EthereumMevTimingArgs, options?: CreateApiApiV2EthereumMevTimingOptions) => { 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/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(createApiApiV2EthereumMevTimingResponse, await res.json()) };