Skip to content

Debug.traceCall

Runs a call without sending a transaction and traces it, on the state of a block you name, with state and block overrides if you give them.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Debug } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const program = Effect.gen(function* () {
const trace = yield* Debug.traceCall({ from: FROM, to: TO, value: 1n }, "latest");
return { gas: trace.gas, failed: trace.failed, logs: trace.structLogs.length };
});
const traced = await program.pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// { gas: 21000, failed: false, logs: 0 }

A transfer to an account without code runs no opcode, so the struct logger has nothing to log. The examples below leave the transport out.

The third argument is the options. They select the tracer, as for Debug.traceTransaction.

import { Debug } from "@ethect/core/rpc";
import type { TransactionRequest } from "@ethect/core/rpc";
// Effect<CallFrame.CallFrame, …>: the calls the request would make
const calls = (request: TransactionRequest.TransactionRequest) =>
Debug.traceCall(request, "latest", {
tracer: "callTracer",
tracerConfig: { withLog: true },
});

stateOverrides changes accounts for the duration of the call. Here it puts code at an address that has none.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Debug } from "@ethect/core/rpc";
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const opcodes = Effect.gen(function* () {
const trace = yield* Debug.traceCall({ to: TO }, "latest", {
// runtime code that returns the block number:
// NUMBER PUSH0 MSTORE PUSH1 32 PUSH0 RETURN
stateOverrides: { [TO]: { code: "0x435f5260205ff3" } },
});
return trace.structLogs.map((log) => log.op);
// ["NUMBER", "PUSH0", "MSTORE", "PUSH1", "PUSH0", "RETURN"]
});

blockOverrides changes fields of the block the call runs in.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Debug } from "@ethect/core/rpc";
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const output = Effect.gen(function* () {
const frame = yield* Debug.traceCall({ to: TO }, "latest", {
tracer: "callTracer",
stateOverrides: { [TO]: { code: "0x435f5260205ff3" } },
blockOverrides: { number: 100n },
});
return frame.output;
// "0x0000000000000000000000000000000000000000000000000000000000000064"
});

The code returns the block number, and the call sees block 100.

By default the call runs on the state the block left. txIndex runs it on the state a transaction of that block ran on instead: 0 is the state before the first one.

import { Debug } from "@ethect/core/rpc";
import type { TransactionRequest } from "@ethect/core/rpc";
const before = (request: TransactionRequest.TransactionRequest, block: bigint) =>
Debug.traceCall(request, block, {
tracer: "prestateTracer",
// on the state the first transaction of the block ran on
txIndex: 0,
});

Effect<StructLog.Trace, Method.Error, Transport>

That is the type without options, a StructLog.Trace. With a tracer, the success type is the one that tracer has: the table is on Debug.traceTransaction.

The call to trace, as for Eth.call. Without from, the zero address is the caller.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Debug } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const trace = Effect.gen(function* () {
const request = { from: FROM, to: TO, data: `0x${"07".padStart(64, "0")}` } as const;
return yield* Debug.traceCall(request, "latest");
});

The block whose state the call runs on: a number, a tag or an object that names the block by hash. Left out, the param is not sent: see Naming a block.

import { Debug } from "@ethect/core/rpc";
import type { TransactionRequest } from "@ethect/core/rpc";
const atBlock = (request: TransactionRequest.TransactionRequest) =>
Debug.traceCall(request, 19_000_000n);
  • Type: an object, whose fields depend on the tracer

The options of Debug.traceTransaction, plus three fields that only a call has.

Field Type Description
stateOverrides StateOverride (optional) accounts to change for the call: balance, nonce, code, storage
blockOverrides BlockOverride (optional) fields of the block the call runs in
txIndex number (optional) run the call before that transaction of the block, and not after the last
import { Address } from "@ethect/core";
import { Debug } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const funded = Debug.traceCall({ from: FROM, to: TO, value: 10n ** 18n }, "latest", {
tracer: "callTracer",
stateOverrides: { [FROM]: { balance: 2n * 10n ** 18n } },
});
  • Options need a block. The options are the third param, so the block has to be given before them: pass "latest". Debug.traceCall(request, undefined, options) compiles, and fails with a CodecError before anything is sent.
  • Nothing is sent to the chain. The call runs and its effects are discarded, as with Eth.call.
  • A revert is a result, not a failure. Where Eth.call fails with ExecutionReverted, Debug.traceCall succeeds: the struct logger reports failed: true with the revert data in returnValue, and callTracer puts error: "execution reverted" and the data in output.
  • Struct logs without a flag. anvil traces a call as it runs it, so structLogs is filled on a default node. The --steps-tracing flag only matters for mined transactions.
  • Override names are plural here. The fields are stateOverrides and blockOverrides, inside the options. Eth.call takes its overrides as params of their own.

The three errors every method can fail with. The cases specific to this method, as anvil 1.9 reports them:

Error Reason When
RpcError InvalidParams the block number is above the chain’s head
RpcError InvalidParams txIndex is not a position of the block
RpcError InvalidParams tracer is "4byteTracer": anvil counts selectors for a mined transaction, not for a call
CodecError on: "params" options are given without a block: nothing is sent

debug_traceCall