import {z} from 'zod' export type GetApiV1ChainNameTransactionV2TxHashArgs = {chainName: string, txHash: string, 'quote-currency'?: string | undefined, 'no-logs'?: boolean | undefined, 'with-internal'?: boolean | undefined, 'with-input-data'?: boolean | undefined, 'with-classification'?: boolean | undefined, 'account-address'?: string | undefined}; export type GetApiV1ChainNameTransactionV2TxHashOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ChainNameTransactionV2TxHashResponse = z.object({updated_at: 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({block_signed_at: z.string().optional(), block_height: z.number().int().optional(), block_hash: z.string().optional(), tx_hash: z.string().optional(), tx_offset: z.number().int().optional(), successful: z.boolean().optional(), from_address: z.string().optional(), miner_address: z.string().optional(), to_address: z.string().optional(), value: z.string().optional(), value_quote: z.number().optional(), pretty_value_quote: 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(), gas_offered: z.number().int().optional(), gas_spent: z.number().int().optional(), gas_price: z.number().int().optional(), fees_paid: z.string().optional(), gas_quote: z.number().optional(), pretty_gas_quote: z.string().optional(), gas_quote_rate: z.number().optional(), explorers: z.array(z.object({label: z.string().optional(), url: z.string().optional()})).optional(), log_events: z.array(z.object({block_signed_at: z.string().optional(), block_height: z.number().int().optional(), tx_offset: z.number().int().optional(), log_offset: z.number().int().optional(), tx_hash: z.string().optional(), raw_log_topics: z.array(z.string()).optional(), sender_contract_decimals: z.number().int().optional(), sender_name: z.string().optional(), sender_contract_ticker_symbol: z.string().optional(), sender_address: z.string().optional(), sender_logo_url: z.string().optional(), supports_erc: z.array(z.string()).optional(), sender_factory_address: z.string().optional(), raw_log_data: z.string().optional(), decoded: z.object({name: z.string().optional(), signature: z.string().optional(), params: z.array(z.object({name: z.string().optional(), type: z.string().optional(), indexed: z.boolean().optional(), decoded: z.boolean().optional(), value: z.string().optional()})).optional()}).optional()})).optional(), internal_transfers: z.array(z.object({from_address: z.string().optional(), to_address: z.string().optional(), value: z.string().optional(), gas_limit: z.number().int().optional()})).optional(), input_data: z.object({method_id: z.string().optional()}).optional(), classification: z.object({version: z.number().int().optional(), type: z.string().optional(), category: z.string().optional(), description: z.string().optional(), account_address: z.string().optional(), account_abstraction: z.object({standard: z.string().optional(), entry_point: z.string().optional(), bundler: z.string().optional(), sender: z.string().optional(), paymaster: z.string().optional()}).optional(), sent: z.array(z.object({action: z.string().optional(), from: z.object({address: z.string().optional()}).optional(), to: z.object({}).optional(), amount: z.string().optional(), token: z.object({}).optional()})).optional(), received: z.array(z.object({})).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 a single transaction, including its decoded event logs. For foundational chains, can also retrieve internal transactions, state changes and method ID where available. * * GET /v1/{chainName}/transaction_v2/{txHash}/ */ export const getApiV1ChainNameTransactionV2TxHash = async (args: GetApiV1ChainNameTransactionV2TxHashArgs, options?: GetApiV1ChainNameTransactionV2TxHashOptions) => { 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-logs'] != null) query.append("no-logs", String(args['no-logs'])) if (args['with-internal'] != null) query.append("with-internal", String(args['with-internal'])) if (args['with-input-data'] != null) query.append("with-input-data", String(args['with-input-data'])) if (args['with-classification'] != null) query.append("with-classification", String(args['with-classification'])) if (args['account-address'] != null) query.append("account-address", String(args['account-address'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.chainName))}/transaction_v2/${encodeURIComponent(String(args.txHash))}/${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(getApiV1ChainNameTransactionV2TxHashResponse, await res.json()) };