import {z} from 'zod' export type GetApiAccountsStakeAddressUtxosArgs = {stake_address: string, count?: number | undefined, page?: number | undefined, order?: ('asc' | 'desc') | undefined}; export type GetApiAccountsStakeAddressUtxosOptions = {baseUrl?: string | undefined, projectId?: string | undefined, fetch?: typeof fetch | undefined}; export const accountUtxoContent = z.array(z.object({address: z.string(), tx_hash: z.string(), tx_index: z.number().int(), output_index: z.number().int(), amount: z.array(z.object({unit: z.string(), quantity: z.string()})), block: z.string(), data_hash: z.string().nullable(), inline_datum: z.string().nullable(), reference_script_hash: z.string().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 }; /** * Account UTXOs * * GET /accounts/{stake_address}/utxos */ export const getApiAccountsStakeAddressUtxos = async (args: GetApiAccountsStakeAddressUtxosArgs, options?: GetApiAccountsStakeAddressUtxosOptions) => { 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 (args.count != null) query.append("count", String(args.count)) if (args.page != null) query.append("page", String(args.page)) if (args.order != null) query.append("order", String(args.order)) if (projectId !== undefined) headers.set("project_id", projectId) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.stake_address))}/utxos${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(accountUtxoContent, await res.json()) };