import {z} from 'zod' export type GetApiTxsHashArgs = {hash: unknown}; export type GetApiTxsHashOptions = {baseUrl?: string | undefined, projectId?: string | undefined, fetch?: typeof fetch | undefined}; export const txContent = z.object({hash: z.string(), block: z.string(), block_height: z.number().int(), block_time: z.number().int(), slot: z.number().int(), index: z.number().int(), output_amount: z.array(z.object({unit: z.string(), quantity: z.string()})), fees: z.string(), deposit: z.string(), size: z.number().int(), invalid_before: z.string().nullable(), invalid_hereafter: z.string().nullable(), utxo_count: z.number().int(), withdrawal_count: z.number().int(), mir_cert_count: z.number().int(), delegation_count: z.number().int(), stake_cert_count: z.number().int(), pool_update_count: z.number().int(), pool_retire_count: z.number().int(), asset_mint_or_burn_count: z.number().int(), redeemer_count: z.number().int(), valid_contract: z.boolean(), treasury_donation: z.string()}); 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 transaction * * GET /txs/{hash} */ export const getApiTxsHash = async (args: GetApiTxsHashArgs, options?: GetApiTxsHashOptions) => { 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(/\/+$/, '')}/txs/${encodeURIComponent(String(args.hash))}${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(txContent, await res.json()) };