import {z} from 'zod' export type GetApiTransactionsTransactionHashRawTraceArgs = {transaction_hash: string}; export type GetApiTransactionsTransactionHashRawTraceOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const rawTraceCallAction = z.object({callType: z.string(), to: z.string(), from: z.string(), input: z.string(), gas: z.string(), value: z.string()}); export const rawTraceCreateAction = z.object({from: z.string(), gas: z.string(), init: z.string(), value: z.string()}); export const rawTraceSelfDestructAction = z.object({address: z.string(), balance: z.string(), refundAddress: z.string()}); export const rawTraceCallResult = z.object({gasUsed: z.string(), output: z.string()}); export const rawTraceCreateResult = z.object({address: z.string(), code: z.string(), gasUsed: z.string()}); export const rawTrace = z.object({action: z.union([rawTraceCallAction, rawTraceCreateAction, rawTraceSelfDestructAction]), subtraces: z.number().int(), traceAddress: z.array(z.number().int()), type: z.string(), error: z.string().optional(), result: z.union([rawTraceCallResult, rawTraceCreateResult]).optional()}); export const getApiTransactionsTransactionHashRawTraceResponse = z.array(rawTrace); 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 raw trace * * GET /transactions/{transaction_hash}/raw-trace */ export const getApiTransactionsTransactionHashRawTrace = async (args: GetApiTransactionsTransactionHashRawTraceArgs, options?: GetApiTransactionsTransactionHashRawTraceOptions) => { 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))}/raw-trace${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(getApiTransactionsTransactionHashRawTraceResponse, await res.json()) };