# StructLog

The result of the struct logger, the tracer a `Debug.trace*` call runs when its options name
none: one log per opcode executed, and the settings that say what each log captures.

```ts
import { Effect } from "effect";
import { Debug } from "@ethect/core/rpc";
import type { TransactionRequest } from "@ethect/core/rpc";

const writes = (request: TransactionRequest.TransactionRequest) =>
  Debug.traceCall(request, "latest").pipe(
    // trace: StructLog.Trace
    Effect.map((trace) => trace.structLogs.filter((log) => log.op === "SSTORE")),
  );
```

## Schemas

| Schema | Type | Describes | Used by |
| --- | --- | --- | --- |
| `StructLog.traceSchema` | `StructLog.Trace` | the trace of one execution | the result of [`Debug.traceTransaction`](https://ethect.dev/rpc/debug/trace-transaction/) and [`Debug.traceCall`](https://ethect.dev/rpc/debug/trace-call/) without a `tracer` |
| `StructLog.schema` | `StructLog.StructLog` | one executed opcode | each entry of `structLogs` |
| `StructLog.configSchema` | `StructLog.Config` | what each log captures | the options of a `Debug.trace*` call without a `tracer` |

## Trace fields

`StructLog.Trace`, the result.

| Field | Type | Notes |
| --- | --- | --- |
| `gas` | `number` | the gas the execution used |
| `failed` | `boolean` | `true` when the execution reverted |
| `returnValue` | `Hex` | what it returned, or the revert data |
| `structLogs` | `ReadonlyArray<StructLog>` | one log per opcode, in the order of execution |

## Log fields

`StructLog.StructLog`, one executed opcode with the state it ran on.

| Field | Type | Notes |
| --- | --- | --- |
| `pc` | `number` | the position of the opcode in the code |
| `op` | `string` | the name of the opcode, such as `"SSTORE"` |
| `gas` | `number` | the gas left before the opcode |
| `gasCost` | `number` | the gas the opcode costs |
| `depth` | `number` | the depth of the call it runs in, `1` for the top call |
| `error` | `string` (optional) | |
| `refund` | `number` (optional) | |
| `stack` | `ReadonlyArray<bigint>` (optional) | from bottom to top. Absent with `disableStack`. |
| `memory` | `ReadonlyArray<Hex>` (optional) | in 32-byte words. Only with `enableMemory`. |
| `returnData` | `Hex` (optional) | Only with `enableReturnData`. |
| `storage` | `Record<Hash, Hash>` (optional) | on `SLOAD` and `SSTORE` only: every slot the contract touched so far. Absent with `disableStorage`. |

A decoded log, from a call that stores the word `7` in slot `0`:

```ts
import type { StructLog } from "@ethect/core/rpc";

const sstore: StructLog.StructLog = {
  pc: 21,
  op: "SSTORE",
  gas: 23127,
  gasCost: 22100,
  depth: 1,
  refund: 0,
  stack: [7n, 7n, 0n],
  storage: {
    "0x0000000000000000000000000000000000000000000000000000000000000000":
      "0x0000000000000000000000000000000000000000000000000000000000000007",
  },
};
```

## Config fields

`StructLog.Config`. The fields go at the top level of the options, next to `timeout` and
`reexec`, and every one is optional.

| Field | Type | Notes |
| --- | --- | --- |
| `enableMemory` | `boolean` (optional) | add the memory to each log |
| `disableStack` | `boolean` (optional) | leave the stack out |
| `disableStorage` | `boolean` (optional) | leave the storage out |
| `enableReturnData` | `boolean` (optional) | add the return data to each log |
| `debug` | `boolean` (optional) | have the node print the logs as it runs |
| `limit` | `number` (optional) | the most logs to return |

```ts
import { Debug } from "@ethect/core/rpc";
import type { Hash } from "@ethect/core";
import type { StructLog } from "@ethect/core/rpc";

const config: StructLog.Config = {
  enableMemory: true,
  disableStack: true,
  limit: 100,
};
const trace = (hash: Hash.Hash) => Debug.traceTransaction(hash, config);
```

## Behavior

- **Stack and storage by default, memory and return data on request.** Without settings a log
  has its `stack`, and its `storage` on the opcodes that read or write it. `memory` and
  `returnData` appear only when enabled.
- **Gas is a `number` here.** The node sends `gas` and `gasCost` as JSON numbers, not as hex
  quantities, so they are not `bigint`. The stack values are quantities and are `bigint`.
- **Calls are flattened.** The logs of an inner call are in the same array, with a `depth` one
  higher. For the calls as a tree, use [`callTracer`](https://ethect.dev/rpc/debug/tracers/#calltracer).
- **Empty for a mined transaction on a default anvil.** anvil records the struct logs of the
  transactions it mines only when started with `anvil --steps-tracing`. Without it,
  `structLogs` is `[]` and the three other fields are still right. A traced call is not
  affected.
- **No opcode, no log.** A transfer to an account without code has an empty `structLogs`.
