Skip to content

Browser trace viewer

Pair @tevm/server with debug_traceCall to step through EVM opcodes, gas costs, stack items, and memory words in a browser UI — or run the same tracer directly in-browser with ZEVM WebAssembly.

1. Start a local trace server with CORS enabled

Section titled “1. Start a local trace server with CORS enabled”

Expose a local MemoryClient over HTTP with cors: true so your browser app can submit debug_traceCall requests directly:

trace-server.ts
import { createMemoryClient } from 'tevm'
import { createServer } from 'tevm/server'
const client = createMemoryClient()
await client.tevmReady()
// Seed a sample contract returning uint256(42)
const DEMO_CONTRACT = '0x0000000000000000000000000000000000000123'
await client.tevmSetAccount({
address: DEMO_CONTRACT,
deployedBytecode: '0x602a60005260206000f3',
})
const server = createServer(client, {}, { cors: true })
server.listen(8545, '127.0.0.1', () => {
console.log('Trace RPC listening on http://127.0.0.1:8545')
})
process.once('SIGINT', () => {
server.close(() => {
void client.tevmClose()
})
})

Fetch the opcode trace over standard JSON-RPC 2.0 and format each step for inspection:

fetch-trace.ts
export type TraceStep = {
pc: number
op: string
gas: number
gasCost: number
depth: number
stack: string[]
memory?: string[]
returnData?: string
}
export type DebugTrace = {
gas: number
failed: boolean
returnValue: string
truncated: boolean
structLogs: TraceStep[]
}
export async function traceCall(
rpcUrl: string,
to: `0x${string}`,
data: `0x${string}` = '0x',
): Promise<DebugTrace> {
const response = await fetch(rpcUrl, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'debug_traceCall',
params: [{ to, data }, 'latest', { enableMemory: true, enableReturnData: true }],
}),
})
if (!response.ok) {
throw new Error(`RPC HTTP error: ${response.status}`)
}
const payload = (await response.json()) as {
result?: DebugTrace
error?: { code: number; message: string }
}
if (payload.error) {
throw new Error(`RPC error ${payload.error.code}: ${payload.error.message}`)
}
return payload.result!
}
render-trace.ts
import { traceCall } from './fetch-trace'
const trace = await traceCall('http://127.0.0.1:8545', '0x0000000000000000000000000000000000000123')
console.log(`Gas: ${trace.gas}, Failed: ${trace.failed}, Return: ${trace.returnValue}`)
if (trace.truncated) {
console.warn('Trace reached the 100,000-step native retention cap.')
}
for (const step of trace.structLogs) {
console.log(
`pc=${String(step.pc).padStart(4)} ${step.op.padEnd(10)} gas=${step.gas} (-${step.gasCost}) stack=[${step.stack.join(', ')}]`,
)
}