import {z} from 'zod' export type GetApiV1AllchainsAddressWalletAddressBalancesArgs = {walletAddress: string, chains?: string | undefined, limit?: number | undefined, before?: string | undefined, 'cutoff-timestamp'?: number | undefined, 'quote-currency'?: string | undefined}; export type GetApiV1AllchainsAddressWalletAddressBalancesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1AllchainsAddressWalletAddressBalancesResponse = z.object({updated_at: z.string().optional(), cursor_before: z.string().optional(), quote_currency: z.string().optional(), items: z.array(z.object({contract_decimals: z.number().int().optional(), contract_name: z.string().optional(), contract_ticker_symbol: z.string().optional(), contract_address: z.string().optional(), contract_display_name: z.string().optional(), supports_erc: z.array(z.string()).optional(), logo_urls: z.object({token_logo_url: z.string().optional(), protocol_logo_url: z.string().optional(), chain_logo_url: z.string().optional()}).optional(), last_transferred_at: z.string().optional(), is_native_token: z.boolean().optional(), type: z.string().optional(), is_spam: z.boolean().optional(), balance: z.string().optional(), balance_24h: z.string().optional(), quote_rate: z.number().optional(), quote_rate_24h: z.number().optional(), quote: z.number().optional(), quote_24h: z.number().optional(), pretty_quote: z.string().optional(), pretty_quote_24h: z.string().optional(), chain_id: z.number().int().optional(), chain_name: z.string().optional(), chain_display_name: z.string().optional()})).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 }; /** * Fetch paginated spot & historical native and token balances for a single address on up to 10 EVM chains with one API call. * * GET /v1/allchains/address/{walletAddress}/balances/ */ export const getApiV1AllchainsAddressWalletAddressBalances = async (args: GetApiV1AllchainsAddressWalletAddressBalancesArgs, options?: GetApiV1AllchainsAddressWalletAddressBalancesOptions) => { const {baseUrl = "https://api.covalenthq.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.chains != null) query.append("chains", String(args.chains)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.before != null) query.append("before", String(args.before)) if (args['cutoff-timestamp'] != null) query.append("cutoff-timestamp", String(args['cutoff-timestamp'])) if (args['quote-currency'] != null) query.append("quote-currency", String(args['quote-currency'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/allchains/address/${encodeURIComponent(String(args.walletAddress))}/balances/${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(getApiV1AllchainsAddressWalletAddressBalancesResponse, await res.json()) };