import {z} from 'zod' export type GetApiV1ChainNameAddressWalletAddressBalancesNativeArgs = {chainName: string, walletAddress: string, 'quote-currency'?: string | undefined, 'block-height'?: number | undefined}; export type GetApiV1ChainNameAddressWalletAddressBalancesNativeOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ChainNameAddressWalletAddressBalancesNativeResponse = z.object({address: z.string().optional(), updated_at: z.string().optional(), quote_currency: z.string().optional(), chain_id: z.number().int().optional(), chain_name: 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(), supports_erc: z.array(z.string()).optional(), logo_url: z.string().optional(), block_height: z.number().int().optional(), balance: z.string().optional(), quote_rate: z.number().optional(), quote: z.number().optional(), pretty_quote: 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 }; /** * Lightweight endpoint to just get the native token balance for an EVM address. * * GET /v1/{chainName}/address/{walletAddress}/balances_native/ */ export const getApiV1ChainNameAddressWalletAddressBalancesNative = async (args: GetApiV1ChainNameAddressWalletAddressBalancesNativeArgs, options?: GetApiV1ChainNameAddressWalletAddressBalancesNativeOptions) => { 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['quote-currency'] != null) query.append("quote-currency", String(args['quote-currency'])) if (args['block-height'] != null) query.append("block-height", String(args['block-height'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.chainName))}/address/${encodeURIComponent(String(args.walletAddress))}/balances_native/${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(getApiV1ChainNameAddressWalletAddressBalancesNativeResponse, await res.json()) };