import {z} from 'zod' export type GetApiV1BtcMainnetAddressWalletAddressHdWalletsArgs = {walletAddress: string, 'quote-currency'?: string | undefined}; export type GetApiV1BtcMainnetAddressWalletAddressHdWalletsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1BtcMainnetAddressWalletAddressHdWalletsResponse = z.object({address: z.string().optional(), chain_id: z.number().int().optional(), chain_name: z.string().optional(), chain_tip_height: z.number().int().optional(), chain_tip_signed_at: z.string().optional(), quote_currency: z.string().optional(), updated_at: z.string().optional(), items: z.array(z.object({child_address: z.string().optional(), address_path: z.string().optional(), 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()})).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 balances for each active child address derived from a Bitcoin HD wallet. * * GET /v1/btc-mainnet/address/{walletAddress}/hd_wallets/ */ export const getApiV1BtcMainnetAddressWalletAddressHdWallets = async (args: GetApiV1BtcMainnetAddressWalletAddressHdWalletsArgs, options?: GetApiV1BtcMainnetAddressWalletAddressHdWalletsOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/btc-mainnet/address/${encodeURIComponent(String(args.walletAddress))}/hd_wallets/${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(getApiV1BtcMainnetAddressWalletAddressHdWalletsResponse, await res.json()) };