# Prestate

The result of the `prestateTracer`: the accounts an execution touched as they were before it
and, in diff mode, what became of the ones it changed.

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

const balanceBefore = (hash: Hash.Hash, account: Address.Address) =>
  Debug.traceTransaction(hash, { tracer: "prestateTracer" }).pipe(
    // prestate: Prestate.Prestate
    Effect.map((prestate) => prestate[account]?.balance ?? 0n),
  );
```

## Schemas

| Schema | Type | Describes | Used by |
| --- | --- | --- | --- |
| `Prestate.schema` | `Prestate.Prestate` | the accounts touched, by address | the result of a `Debug.trace*` call with `tracer: "prestateTracer"` |
| `Prestate.diffSchema` | `Prestate.Diff` | the accounts changed, before and after | the result of the same call in diff mode |
| `Prestate.accountSchema` | `Prestate.Account` | the state of one account | each value of a `Prestate` |
| `Prestate.configSchema` | `Prestate.Config` | the settings of the tracer | the `tracerConfig` of the call |
| `Prestate.diffConfigSchema` | `Prestate.DiffConfig` | the settings of diff mode | the `tracerConfig` that asks for a `Diff` |

## Prestate

`Prestate.Prestate` is a record: `Record<Hex, Prestate.Account>`. A key is the address of an
account, in lowercase, and an [`Address.Address`](https://ethect.dev/reference/primitives/address/) indexes it
directly.

## Account fields

`Prestate.Account`.

| Field | Type | Notes |
| --- | --- | --- |
| `balance` | `bigint` (optional) | in wei |
| `nonce` | `number` (optional) | |
| `code` | `Hex` (optional) | the code of a contract |
| `codeHash` | `Hash` (optional) | |
| `storage` | `Record<Hash, Hash>` (optional) | the slots the execution read or wrote, by slot |

## Diff fields

`Prestate.Diff`, the result in diff mode.

| Field | Type | Notes |
| --- | --- | --- |
| `pre` | `Prestate.Prestate` | the accounts the execution changed, as they were before it |
| `post` | `Prestate.Prestate` | the same accounts after it, with only the fields that changed |

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

const nonces = (hash: Hash.Hash, sender: Address.Address) =>
  Debug.traceTransaction(hash, {
    tracer: "prestateTracer",
    tracerConfig: { diffMode: true },
  }).pipe(
    // diff: Prestate.Diff
    Effect.map((diff) => [diff.pre[sender]?.nonce, diff.post[sender]?.nonce]),
    // for instance [1, 2]
  );
```

## Config fields

`Prestate.Config` and `Prestate.DiffConfig`, passed as `tracerConfig`. They differ by
`diffMode` alone.

| Field | Type | Notes |
| --- | --- | --- |
| `diffMode` | `false` (optional) in `Config`, `true` in `DiffConfig` | `true` asks for a `Diff` |
| `disableCode` | `boolean` (optional) | leave the code out of every account |
| `disableStorage` | `boolean` (optional) | leave the storage out of every account |

## Behavior

- **A field is absent when it is empty.** An account without code has no `code`, and one that
  never sent a transaction has no `nonce`. Read a field with a default:
  `account.nonce ?? 0`.
- **In `post`, absent means unchanged.** The `post` of an account holds only what the
  execution changed. A contract whose storage alone changed has a `storage` and nothing else.
- **Created and destroyed accounts.** An account the execution created is only in `post`. One
  it destroyed is only in `pre`.
- **Storage holds what was touched, not everything.** In a `Prestate`, a slot the execution
  read or wrote is there with the value it had before. The other slots of the contract are
  not.
- **`diffMode` selects the type.** `diffMode: true` gives a `Diff`. Left out or `false`, the
  result is a `Prestate`: see [Tracers](https://ethect.dev/rpc/debug/tracers/#prestatetracer).
- **More than the two parties.** The fee recipient of the block is among the accounts touched,
  with the sender and the account called.
