import {z} from 'zod' export type Chain = 'mainnet' | 'hoodi'; export type EntityName = string; export type EvaluationWindow = '24h' | '7d' | '30d' | '90d' | 'all_time'; export type NamedEvaluationWindow = {evaluation_window: EvaluationWindow}; export type Cursor = string; export type PageSize = number; export type SubEntitiesSortField = 'beaconscore' | 'net_share' | 'validator_count'; export type SortOrder = 'asc' | 'desc'; export type SubEntitiesOverviewRequest = {chain?: Chain | undefined, entity: EntityName, range: NamedEvaluationWindow, cursor?: Cursor | undefined, page_size?: PageSize | undefined, sort_by?: SubEntitiesSortField | undefined, sort_order?: SortOrder | undefined}; export type CreateApiApiV2EthereumEntitySubEntitiesArgs = {body: SubEntitiesOverviewRequest}; export type CreateApiApiV2EthereumEntitySubEntitiesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const subEntityName = z.string(); export const percent = z.number().gte(0).lte(1); export const subEntitiesOverviewData = z.object({sub_entity: subEntityName, validator_count: z.number().int().gte(0), beaconscore: percent.nullable(), net_share: percent}); export const timestamp = z.number().int().gte(0); 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 paging = z.object({next_cursor: z.string().optional()}); export const createApiApiV2EthereumEntitySubEntitiesResponse = z.object({data: z.array(subEntitiesOverviewData), last_updated: timestamp, range: resultRange, paging: paging.optional()}); 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 }; /** * 💎 Sub Entities * * POST /api/v2/ethereum/entity/sub-entities */ export const createApiApiV2EthereumEntitySubEntities = async (args: CreateApiApiV2EthereumEntitySubEntitiesArgs, options?: CreateApiApiV2EthereumEntitySubEntitiesOptions) => { 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/entity/sub-entities${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(createApiApiV2EthereumEntitySubEntitiesResponse, await res.json()) };