import {z} from 'zod' export type GetApiV1CqCovalentAppBitcoinTransactionsArgs = {address?: string | undefined, 'page-size'?: number | undefined, 'page-number'?: number | undefined}; export type GetApiV1CqCovalentAppBitcoinTransactionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1CqCovalentAppBitcoinTransactionsResponse = z.object({updated_at: z.string().optional(), items: z.array(z.object({chain_id: z.number().int().optional(), chain_name: z.string().optional(), contract_decimals: z.number().int().optional(), block_signed_at: z.string().optional(), block_height: z.number().int().optional(), block_hash: z.string().optional(), tx_hash: z.string().optional(), tx_idx: z.number().int().optional(), type: z.string().optional(), address: z.string().optional(), value: z.string().optional(), quote: z.number().optional(), quote_rate: z.number().optional(), fees_paid: z.string().optional(), gas_quote: z.number().optional(), gas_quote_rate: z.number().optional(), coinbase: z.boolean().optional(), locktime: z.number().int().optional(), weight: z.number().int().optional()})).optional(), pagination: z.object({has_more: z.boolean().optional(), page_number: z.number().int().optional(), page_size: z.number().int().optional(), total_count: z.number().int().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 full transaction history of a Bitcoin non-HD wallet address. * * GET /v1/cq/covalent/app/bitcoin/transactions/ */ export const getApiV1CqCovalentAppBitcoinTransactions = async (args: GetApiV1CqCovalentAppBitcoinTransactionsArgs, options?: GetApiV1CqCovalentAppBitcoinTransactionsOptions) => { 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.address != null) query.append("address", String(args.address)) if (args['page-size'] != null) query.append("page-size", String(args['page-size'])) if (args['page-number'] != null) query.append("page-number", String(args['page-number'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/cq/covalent/app/bitcoin/transactions/${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(getApiV1CqCovalentAppBitcoinTransactionsResponse, await res.json()) };