import {z} from 'zod' export type ChainType = 'EVM' | 'SOL' | 'SUI' | 'UTXO'; export type ChainID = 1 | 10 | 56 | 137 | 8453 | 42161 | 20000000000001 | 30000000000001 | 30000000000002; export type WalletPortfolioRequest = {wallet_address: string, chain_type: ChainType, chain_ids?: Array | undefined, min_balance_usd?: (number | null) | undefined, force_refresh?: boolean | undefined}; export type CreateApiWalletPortfolioArgs = {'X-API-KEY': string, body: WalletPortfolioRequest}; export type CreateApiWalletPortfolioOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const chainType = z.enum(["EVM", "SOL", "SUI", "UTXO"]); export const chainID = z.union([z.literal(1), z.literal(10), z.literal(56), z.literal(137), z.literal(8453), z.literal(42161), z.literal(20000000000001), z.literal(30000000000001), z.literal(30000000000002)]); export const bigInt = z.string(); export const currencyAmount = z.object({ui_amount: z.number(), ui_amount_display: z.string(), raw_amount: bigInt, value_usd: z.number()}); export const walletPortfolioCurrency = z.object({chain_id: chainID, address: z.string().nullable().optional(), symbol: z.string(), decimals: z.number().int(), balance: currencyAmount, price_usd: z.number().nullable().optional(), logo_uri: z.string().nullable().optional(), chain_logo_uri: z.string().nullable().optional()}); export const walletPortfolioResponse = z.object({wallet_address: z.string(), chain_type: chainType, scanned_at: z.string(), expires_at: z.string(), currencies: z.array(walletPortfolioCurrency)}); 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 }; /** * Wallet Portfolio * * POST /wallet/portfolio */ export const createApiWalletPortfolio = async (args: CreateApiWalletPortfolioArgs, options?: CreateApiWalletPortfolioOptions) => { const {baseUrl = "https://api.coinvoyage.io/v2", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-KEY'] != null) headers.set("X-API-KEY", String(args['X-API-KEY'])) if (APIKeyHeader !== undefined) headers.set("X-API-KEY", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/wallet/portfolio${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(walletPortfolioResponse, await res.json()) };