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 SlotOffsetMs = number; export type SlotOffsetMsRange = {min?: (SlotOffsetMs | null) | undefined, max?: (SlotOffsetMs | null) | undefined}; export type Cursor = string; export type MevBidsPageSize = number; export type BlockMevBidsRequest = {chain?: Chain | undefined, block: BlockSelector, slot_offset_ms_range?: SlotOffsetMsRange | undefined, cursor?: Cursor | undefined, page_size?: MevBidsPageSize | undefined}; export type CreateApiApiV2EthereumBlockMevBidsArgs = {body: BlockMevBidsRequest}; export type CreateApiApiV2EthereumBlockMevBidsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const wei = z.string(); export const slotOffsetMs = z.number().int(); export const block = z.number().int().gte(0); export const mevSource = z.enum(["get_header", "data_api", "ultrasound"]); export const blockMevBidData = z.object({relay: z.string(), builder_pubkey: z.string().nullable(), value: wei, slot_offset_ms: slotOffsetMs.nullable(), relay_received_at: z.number().int(), won: z.boolean(), block_number: block, block_hash: z.string(), parent_hash: z.string(), source: mevSource, optimistic_submission: z.boolean()}); export const paging = z.object({next_cursor: z.string().optional()}); export const createApiApiV2EthereumBlockMevBidsResponse = z.object({data: z.array(blockMevBidData), paging: paging.optional(), ready: z.boolean()}); 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 Bids * * POST /api/v2/ethereum/block/mev-bids */ export const createApiApiV2EthereumBlockMevBids = async (args: CreateApiApiV2EthereumBlockMevBidsArgs, options?: CreateApiApiV2EthereumBlockMevBidsOptions) => { 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-bids${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(createApiApiV2EthereumBlockMevBidsResponse, await res.json()) };