|
1 | 1 | import { createFileRoute } from '@tanstack/react-router' |
2 | 2 | import { json } from '@tanstack/react-start' |
3 | | -import type { Address, Hex } from 'ox' |
4 | | -import type { Chain, Client, Transport } from 'viem' |
| 3 | +import { |
| 4 | + fetchTraceData, |
| 5 | + type TraceData, |
| 6 | +} from '#lib/queries/trace' |
5 | 7 | import { zHash } from '#lib/zod' |
6 | | -import { getConfig } from '#wagmi.config' |
7 | 8 |
|
8 | | -export interface CallTrace { |
9 | | - type: 'CALL' | 'DELEGATECALL' | 'STATICCALL' | 'CREATE' | 'CREATE2' |
10 | | - from: Address.Address |
11 | | - to?: Address.Address |
12 | | - gas: Hex.Hex |
13 | | - gasUsed: Hex.Hex |
14 | | - input: Hex.Hex |
15 | | - output?: Hex.Hex |
16 | | - value?: Hex.Hex |
17 | | - error?: string |
18 | | - revertReason?: string |
19 | | - calls?: CallTrace[] |
20 | | -} |
21 | | - |
22 | | -export interface AccountState { |
23 | | - balance?: Hex.Hex |
24 | | - nonce?: number |
25 | | - code?: Hex.Hex |
26 | | - storage?: Record<Hex.Hex, Hex.Hex> |
27 | | -} |
28 | | - |
29 | | -export interface PrestateDiff { |
30 | | - pre: Record<Address.Address, AccountState> |
31 | | - post: Record<Address.Address, AccountState> |
32 | | -} |
33 | | - |
34 | | -export interface TraceData { |
35 | | - trace: CallTrace | null |
36 | | - prestate: PrestateDiff | null |
37 | | -} |
38 | | - |
39 | | -async function traceTransaction( |
40 | | - client: Client<Transport, Chain>, |
41 | | - hash: Hex.Hex, |
42 | | -): Promise<CallTrace | null> { |
43 | | - return client.request({ |
44 | | - method: 'debug_traceTransaction', |
45 | | - params: [hash, { tracer: 'callTracer' }], |
46 | | - } as Parameters<typeof client.request>[0]) |
47 | | -} |
48 | | - |
49 | | -async function tracePrestate( |
50 | | - client: Client<Transport, Chain>, |
51 | | - hash: Hex.Hex, |
52 | | -): Promise<PrestateDiff | null> { |
53 | | - return client.request({ |
54 | | - method: 'debug_traceTransaction', |
55 | | - params: [ |
56 | | - hash, |
57 | | - { tracer: 'prestateTracer', tracerConfig: { diffMode: true } }, |
58 | | - ], |
59 | | - } as Parameters<typeof client.request>[0]) |
60 | | -} |
| 9 | +export type { |
| 10 | + AccountState, |
| 11 | + CallTrace, |
| 12 | + PrestateDiff, |
| 13 | + TraceData, |
| 14 | +} from '#lib/queries/trace' |
61 | 15 |
|
62 | 16 | export const Route = createFileRoute('/api/tx/trace/$hash')({ |
63 | 17 | server: { |
64 | 18 | handlers: { |
65 | 19 | GET: async ({ params }) => { |
66 | 20 | try { |
67 | | - const client = getConfig().getClient() |
68 | 21 | const hash = zHash().parse(params.hash) |
69 | | - const [trace, prestate] = await Promise.all([ |
70 | | - traceTransaction(client, hash).catch(() => null), |
71 | | - tracePrestate(client, hash).catch(() => null), |
72 | | - ]) |
73 | | - return json<TraceData>({ trace, prestate }) |
| 22 | + const traceData = await fetchTraceData(hash) |
| 23 | + return json<TraceData>(traceData) |
74 | 24 | } catch (error) { |
75 | 25 | console.error('Trace error:', error) |
76 | 26 | return json({ error: 'Failed to fetch trace' }, { status: 500 }) |
|
0 commit comments