import {z} from 'zod' export type GetApiBlockchainsArgs = {protocol?: string | undefined, deprecated?: boolean | undefined, test?: boolean | undefined, ids?: Array | undefined, pageCursor?: string | undefined, pageSize?: number | undefined}; export type GetApiBlockchainsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const blockchainOnchain = z.object({protocol: z.string(), chainId: z.string().optional(), test: z.boolean(), signingAlgo: z.enum(["ECDSA_SECP256K1", "EDDSA_ED25519"])}); export const assetScope = z.enum(["GLOBAL", "LOCAL"]); export const blockchainMedia = z.object({url: z.string(), type: z.enum(["image/svg+xml", "image/png"])}); export const blockchainExplorer = z.object({base: z.string(), address: z.string().optional(), tx: z.string().optional(), token: z.string().optional()}); export const blockchainMetadata = z.object({scope: assetScope, deprecated: z.boolean(), media: z.array(blockchainMedia).optional(), explorer: blockchainExplorer.optional()}); export const blockchainResponse = z.object({id: z.string(), legacyId: z.string(), displayName: z.string(), nativeAssetId: z.string(), onchain: blockchainOnchain, metadata: blockchainMetadata}); export const listBlockchainsResponse = z.object({data: z.array(blockchainResponse), next: 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 }; /** * List blockchains * * GET /blockchains */ export const getApiBlockchains = async (args: GetApiBlockchainsArgs, options?: GetApiBlockchainsOptions) => { const {baseUrl = "https://api.fireblocks.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.protocol != null) query.append("protocol", String(args.protocol)) if (args.deprecated != null) query.append("deprecated", String(args.deprecated)) if (args.test != null) query.append("test", String(args.test)) for (const item of args.ids ?? []) query.append("ids", String(item)) if (args.pageCursor != null) query.append("pageCursor", String(args.pageCursor)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) const url = `${baseUrl.replace(/\/+$/, '')}/blockchains${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(listBlockchainsResponse, await res.json()) };