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.
With a tracer
Section titled “With a tracer”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 makeconst calls = (request: TransactionRequest.TransactionRequest) => Debug.traceCall(request, "latest", { tracer: "callTracer", tracerConfig: { withLog: true }, });On overridden state
Section titled “On overridden state”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"]});In an overridden block
Section titled “In an overridden block”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.
Before a transaction of the block
Section titled “Before a transaction of the block”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, });Returns
Section titled “Returns”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.
Parameters
Section titled “Parameters”request
Section titled “request”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");});block (optional)
Section titled “block (optional)”- Type:
BlockId.BlockId
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);options (optional)
Section titled “options (optional)”- 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 } },});Behavior
Section titled “Behavior”- 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 aCodecErrorbefore 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.callfails withExecutionReverted,Debug.traceCallsucceeds: the struct logger reportsfailed: truewith the revert data inreturnValue, andcallTracerputserror: "execution reverted"and the data inoutput. - Struct logs without a flag. anvil traces a call as it runs it, so
structLogsis filled on a default node. The--steps-tracingflag only matters for mined transactions. - Override names are plural here. The fields are
stateOverridesandblockOverrides, inside the options.Eth.calltakes its overrides as params of their own.
Errors
Section titled “Errors”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 |
JSON-RPC method
Section titled “JSON-RPC method”debug_traceCall