import {z} from 'zod' export type GetApiTransactionsTransactionHashLogsArgs = {transaction_hash: string}; export type GetApiTransactionsTransactionHashLogsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const addressTag = z.object({address_hash: z.string(), display_name: z.string(), label: z.string()}); export const watchlistName = z.object({display_name: z.string(), label: z.string()}); export const addressParam = z.object({hash: z.string(), implementation_name: z.string(), name: z.string(), ens_domain_name: z.string().optional(), metadata: z.object({}).optional(), is_contract: z.boolean(), private_tags: z.array(addressTag), watchlist_names: z.array(watchlistName), public_tags: z.array(addressTag), is_verified: z.boolean()}); export const decodedInputLogParameter = z.object({name: z.string(), type: z.string(), value: z.string(), indexed: z.boolean()}); export const decodedInputLog = z.object({method_call: z.string(), method_id: z.string(), parameters: z.array(decodedInputLogParameter)}); export const log = z.object({address_hash: addressParam.optional(), block_hash: z.string().optional(), block_number: z.number().int().optional(), data: z.string(), decoded: decodedInputLog, index: z.number().int(), smart_contract: addressParam, topics: z.array(z.string()), transaction_hash: z.string()}); export const getApiTransactionsTransactionHashLogsResponse = z.object({items: z.array(log), next_page_params: z.object({})}); 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 }; /** * get transaction logs * * GET /transactions/{transaction_hash}/logs */ export const getApiTransactionsTransactionHashLogs = async (args: GetApiTransactionsTransactionHashLogsArgs, options?: GetApiTransactionsTransactionHashLogsOptions) => { const {baseUrl = "https://eth.blockscout.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/transactions/${encodeURIComponent(String(args.transaction_hash))}/logs${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(getApiTransactionsTransactionHashLogsResponse, await res.json()) };