import {z} from 'zod' export type GetApiV1ChainNameAddressWalletAddressTransactionsSummaryArgs = {chainName: string, walletAddress: string, 'quote-currency'?: string | undefined, 'with-gas'?: boolean | undefined, 'with-transfer-count'?: boolean | undefined}; export type GetApiV1ChainNameAddressWalletAddressTransactionsSummaryOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ChainNameAddressWalletAddressTransactionsSummaryResponse = z.object({updated_at: z.string().optional(), address: z.string().optional(), chain_id: z.number().int().optional(), chain_name: z.string().optional(), items: z.array(z.object({total_count: z.number().int().optional(), transfer_count: z.number().int().optional(), earliest_transaction: z.object({block_signed_at: z.string().optional(), tx_hash: z.string().optional(), tx_detail_link: z.string().optional()}).optional(), latest_transaction: z.object({}).optional(), gas_summary: z.object({total_sent_count: z.number().int().optional(), total_fees_paid: z.string().optional(), total_gas_quote: z.number().optional(), pretty_total_gas_quote: z.string().optional(), average_gas_quote_per_tx: z.number().optional(), pretty_average_gas_quote_per_tx: z.string().optional(), gas_metadata: 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()}).optional()}).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 earliest and latest transactions, and the transaction count, for a wallet. Also enriched with gas expenditure details and total ERC20 token transfers count. * * GET /v1/{chainName}/address/{walletAddress}/transactions_summary/ */ export const getApiV1ChainNameAddressWalletAddressTransactionsSummary = async (args: GetApiV1ChainNameAddressWalletAddressTransactionsSummaryArgs, options?: GetApiV1ChainNameAddressWalletAddressTransactionsSummaryOptions) => { 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['with-gas'] != null) query.append("with-gas", String(args['with-gas'])) if (args['with-transfer-count'] != null) query.append("with-transfer-count", String(args['with-transfer-count'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.chainName))}/address/${encodeURIComponent(String(args.walletAddress))}/transactions_summary/${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(getApiV1ChainNameAddressWalletAddressTransactionsSummaryResponse, await res.json()) };