import {z} from 'zod' export type GetApiV2AssetsBatchGetArgs = {names: Array}; export type GetApiV2AssetsBatchGetOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v2AssetTypeEnum = z.enum(["NATIVE_CURRENCY", "TOKEN"]); export const v2ConvertedValue = z.object({amount: z.string().optional(), currencyCode: z.string().optional()}); export const assetTokenInfoStandardEnum = z.enum(["ERC20", "ERC721", "ERC1155", "TRC20", "SPL_TOKEN", "SPL_TOKEN_2022", "ICS20", "JETTON", "TIP20", "ERC7984"]); export const v2CantonTokenDetails = z.object({instrumentId: z.string().optional(), instrumentAdmin: z.string().optional(), supportsTransferPreapproval: z.boolean().optional()}); export const v2SuiTokenDetails = z.object({gasless: z.boolean().optional()}); export const v2EVMTokenDetails = z.object({zamaConfidentialTokenPair: z.string().optional()}); export const v2AssetTokenInfo = z.object({contractAddress: z.string().optional(), standard: assetTokenInfoStandardEnum.optional(), denom: z.string().optional(), cantonTokenDetails: v2CantonTokenDetails.optional(), tip20Currency: z.string().optional(), suiTokenDetails: v2SuiTokenDetails.optional(), evmTokenDetails: v2EVMTokenDetails.optional()}); export const v2Asset = z.object({name: z.string().optional(), displayName: z.string().optional(), network: z.string().optional(), type: v2AssetTypeEnum.optional(), symbol: z.string().optional(), decimals: z.number().int().optional(), convertedValue: v2ConvertedValue.optional(), tokenInfo: v2AssetTokenInfo.optional()}); export const v2BatchGetAssetsResponse = z.object({assets: z.array(v2Asset).optional()}); 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 }; /** * BatchGetAssets * * GET /v2/assets:batchGet */ export const getApiV2AssetsBatchGet = async (args: GetApiV2AssetsBatchGetArgs, options?: GetApiV2AssetsBatchGetOptions) => { const {baseUrl = "https://api.utila.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.names ?? []) query.append("names", String(item)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/assets:batchGet${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(v2BatchGetAssetsResponse, await res.json()) };