# StateOverride

State a call pretends is on chain, per account: a balance, a nonce, the code or storage slots,
for the time of one call. Nothing is written to the chain.

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

const FROM = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const gas = Effect.gen(function* () {
  const stateOverride: StateOverride.StateOverride = {
    [FROM]: { balance: 10n ** 19n },
  };
  const request = { from: FROM, to: TO, value: 10n ** 18n };
  return yield* Eth.estimateGas(request, "latest", stateOverride);
});
// 21000n
```

The account holds nothing on a local anvil, and the estimate runs as if it held ten ether.

## Schemas

| Schema | Type | Describes | Used by |
| --- | --- | --- | --- |
| `StateOverride.schema` | `StateOverride.StateOverride` | overrides by account address | [`Eth.call`](https://ethect.dev/rpc/eth/call/), [`Eth.estimateGas`](https://ethect.dev/rpc/eth/estimate-gas/), the `stateOverrides` of [`Eth.simulateV1`](https://ethect.dev/rpc/eth/simulate-v1/) and of [`Debug.traceCall`](https://ethect.dev/rpc/debug/trace-call/) |
| `StateOverride.accountSchema` | `StateOverride.Account` | what to override on one account | the values of a `StateOverride` |

`StateOverride.StateOverride` is a `Record<Address, StateOverride.Account>`: the keys are
addresses, built as [any address](https://ethect.dev/reference/primitives/address/) is.

## Account fields

Every field of `StateOverride.Account` is optional.

| Field | Type | Notes |
| --- | --- | --- |
| `balance` | `bigint` | in wei |
| `nonce` | `number` | |
| `code` | `Hex` | the runtime code of the account |
| `state` | `Record<Hex, Hex>` | the whole storage of the account: slots not listed read as zero |
| `stateDiff` | `Record<Hex, Hex>` | only the slots listed: the others keep their value |
| `movePrecompileToAddress` | `Address` | where to move the precompile that lives at the overridden address |

## Storage

A storage override maps a slot to a value. Both are words of at most 32 bytes: written short,
they are sent as 32 bytes.

```ts
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const CONTRACT = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const ME = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const owner = Eth.call(
  { to: CONTRACT, data: "0x8da5cb5b" }, // owner()
  "latest",
  { [CONTRACT]: { stateDiff: { "0x0": ME } } },
);
```

For a contract that keeps its owner in its first storage slot, the call answers as if `me` were
the owner. The slot `"0x0"` and the 20-byte address go out as two 32-byte words.

A key that is not a slot, or a slot or value longer than 32 bytes, fails with a `CodecError`
on the params, and nothing is sent.

## Behavior

- **`state` or `stateDiff`, not both.** `state` replaces the whole storage of the account and
  `stateDiff` patches it. A node rejects an account that has both.
- **Code on any address.** `code` makes an account without code behave as a contract for the
  call, which is how to run code that is not deployed.
- **One call only.** The override lasts for the call it is passed to. To change the state of a
  test node for good, use the [`Anvil`](https://ethect.dev/rpc/anvil/state/#setbalance) methods.
- **Lowercase keys.** The accounts are keyed by `Address`, which is lowercase. A key in
  another case fails with a `CodecError` on the params: nothing is left out silently. On the
  wire, each key is in its EIP-55 form.
