import {z} from 'zod' export type GetApiV1ChainNameAddressWalletAddressHistoricalBalancesArgs = {chainName: string, walletAddress: string, 'quote-currency'?: string | undefined, 'no-spam'?: boolean | undefined, 'block-height'?: number | undefined, date?: string | undefined}; export type GetApiV1ChainNameAddressWalletAddressHistoricalBalancesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ChainNameAddressWalletAddressHistoricalBalancesResponse = 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(), chain_tip_height: z.number().int().optional(), chain_tip_signed_at: 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(), last_transferred_block_height: z.number().int().optional(), contract_display_name: z.string().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(), 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 }; /** * Fetches the historical native and fungible (ERC20) tokens held by an address at a given block height or date. Response includes daily prices and other metadata. * * GET /v1/{chainName}/address/{walletAddress}/historical_balances/ */ export const getApiV1ChainNameAddressWalletAddressHistoricalBalances = async (args: GetApiV1ChainNameAddressWalletAddressHistoricalBalancesArgs, options?: GetApiV1ChainNameAddressWalletAddressHistoricalBalancesOptions) => { 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['no-spam'] != null) query.append("no-spam", String(args['no-spam'])) if (args['block-height'] != null) query.append("block-height", String(args['block-height'])) if (args.date != null) query.append("date", String(args.date)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.chainName))}/address/${encodeURIComponent(String(args.walletAddress))}/historical_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(getApiV1ChainNameAddressWalletAddressHistoricalBalancesResponse, await res.json()) };