Skip to main content

Trace a failed transaction

Enable tracing on the node:

const anvil = useAnvil(
new AnvilContainer(
new AnvilOptions().evm
.withPrintTraces() // call traces in anvil's log output
.withStepsTracing() // opcode-level debug_traceTransaction
.logs.verboseLogs(LogVerbosity.Three)
.withColor(Color.Never), // plain text, easy to search
).withTestClient((client) => client.withAccount(0)),
{
setup: async (node) => {
const [, alice] = await node.addresses();
token = await deployToken(node.testClient);
vault = await deployVault(node.testClient, token, 0n);
await mint(node.testClient, token, alice, parseEther("10"));
},
},
);

Tested in examples/test/e2e/debugging-and-tracing.e2e.test.ts:44

The failing inner call​

const trace = await anvil.rpc<CallFrame>("debug_traceTransaction", [
hash,
{ tracer: "callTracer" },
]);
expect(trace.to.toLowerCase()).toBe(vault.toLowerCase());
expect(trace.error).toBe("execution reverted");

// The revert originated one level down, in the token:
const [inner] = trace.calls!;
expect(inner.to.toLowerCase()).toBe(token.toLowerCase());
const cause = decodeErrorResult({
abi: exampleToken.abi,
data: inner.output!,
});
expect(cause.errorName).toBe("InsufficientAllowance");
expect(cause.args).toEqual([vault, 0n, 1n]); // spender, allowance, needed

Tested in examples/test/e2e/debugging-and-tracing.e2e.test.ts:82

Opcodes (--steps-tracing)​

const { structLogs } = await anvil.rpc<{
structLogs: { op: string; depth: number }[];
}>("debug_traceTransaction", [hash, {}]);
expect(structLogs.length).toBeGreaterThan(100);
expect(structLogs.filter((step) => step.op === "REVERT")).toHaveLength(2); // token, then vault

Tested in examples/test/e2e/debugging-and-tracing.e2e.test.ts:104

A call, without sending it​

const trace = await anvil.rpc<CallFrame>("debug_traceCall", [
{
from: alice,
to: token,
data: encodeFunctionData({
abi: exampleToken.abi,
functionName: "transfer",
args: [bob, 1n],
}),
},
"latest",
{ tracer: "callTracer" },
]);
expect(trace.type).toBe("CALL");
expect(trace.error).toBeUndefined();

Tested in examples/test/e2e/debugging-and-tracing.e2e.test.ts:115

anvil's own trace output (--print-traces)​

const logs = await anvil.logText(200); // last 200 lines of anvil's output
const selector = "0x192b9e4e"; // InsufficientAllowance(address,uint256,uint256)
expect(logs).toContain(`[Revert] custom error ${selector}`);

Tested in examples/test/e2e/debugging-and-tracing.e2e.test.ts:145

trace_transaction (Parity style) works through anvil.rpc() too.

Concepts​