import {z} from 'zod' export type GetApiPoolsPoolIdArgs = {pool_id: string}; export type GetApiPoolsPoolIdOptions = {baseUrl?: string | undefined, projectId?: string | undefined, fetch?: typeof fetch | undefined}; export const pool = z.object({pool_id: z.string(), hex: z.string(), vrf_key: z.string(), blocks_minted: z.number().int(), blocks_epoch: z.number().int(), live_stake: z.string(), live_size: z.number(), live_saturation: z.number(), live_delegators: z.number(), active_stake: z.string(), active_size: z.number(), declared_pledge: z.string(), live_pledge: z.string(), margin_cost: z.number(), fixed_cost: z.string(), reward_account: z.string(), owners: z.array(z.string()), registration: z.array(z.string()), retirement: z.array(z.string()), calidus_key: z.object({id: z.string(), pub_key: z.string(), nonce: z.number().int(), tx_hash: z.string(), block_height: z.number().int(), block_time: z.number().int(), epoch: z.number().int()}).nullable()}); 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 }; /** * Specific stake pool * * GET /pools/{pool_id} */ export const getApiPoolsPoolId = async (args: GetApiPoolsPoolIdArgs, options?: GetApiPoolsPoolIdOptions) => { const {baseUrl = "https://cardano-mainnet.blockfrost.io/api/v0", fetch: fetchFn = fetch, projectId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (projectId !== undefined) headers.set("project_id", projectId) const url = `${baseUrl.replace(/\/+$/, '')}/pools/${encodeURIComponent(String(args.pool_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(pool, await res.json()) };