import {z} from 'zod' export type AssetClass = 'NATIVE' | 'FT' | 'FIAT' | 'NFT' | 'SFT' | 'VIRTUAL'; export type AssetScope = 'GLOBAL' | 'LOCAL'; export type GetApiAssetsArgs = {blockchainId?: string | undefined, assetClass?: AssetClass | undefined, symbol?: string | undefined, scope?: AssetScope | undefined, deprecated?: boolean | undefined, ids?: Array | undefined, pageCursor?: string | undefined, pageSize?: number | undefined, 'Idempotency-Key'?: string | undefined}; export type GetApiAssetsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const assetClass = z.enum(["NATIVE", "FT", "FIAT", "NFT", "SFT", "VIRTUAL"]); export const assetDetailsOnchain = z.object({symbol: z.string(), name: z.string(), address: z.string().optional(), decimals: z.number(), standards: z.array(z.string()).optional()}); export const assetScope = z.enum(["GLOBAL", "LOCAL"]); export const assetMedia = z.object({url: z.string(), type: z.enum(["image/svg+xml", "image/png"]), attributes: z.object({monochrome: z.boolean().optional()}).optional()}); export const assetNote = z.object({text: z.string(), userId: z.string(), userName: z.string(), updatedAt: z.string()}); export const assetFeature = z.literal("STABLECOIN"); export const assetDetailsMetadata = z.object({scope: assetScope, verified: z.boolean(), deprecated: z.boolean(), deprecationReferralId: z.string().optional(), website: z.string().optional(), media: z.array(assetMedia).optional(), note: assetNote.optional(), features: z.array(assetFeature).optional()}); export const asset = z.object({id: z.string(), legacyId: z.string(), blockchainId: z.string().optional(), displayName: z.string(), displaySymbol: z.string(), providerId: z.string().optional(), peggedAssetId: z.string().optional(), assets: z.array(z.string()).optional(), assetClass: assetClass, decimals: z.number().optional(), onchain: assetDetailsOnchain.optional(), metadata: assetDetailsMetadata}); export const listAssetsResponse = z.object({data: z.array(asset), 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 assets * * GET /assets */ export const getApiAssets = async (args: GetApiAssetsArgs, options?: GetApiAssetsOptions) => { 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.blockchainId != null) query.append("blockchainId", String(args.blockchainId)) if (args.assetClass != null) query.append("assetClass", String(args.assetClass)) if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.deprecated != null) query.append("deprecated", String(args.deprecated)) 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)) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/assets${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(listAssetsResponse, await res.json()) };