# Simulate

The types of [`Eth.simulateV1`](https://ethect.dev/rpc/eth/simulate-v1/): a payload says which calls to run
in which made-up blocks, and the result is those blocks with the outcome of each call.

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

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

const outcome = Effect.gen(function* () {
  const payload: Simulate.Payload = {
    blockStateCalls: [
      {
        blockOverrides: { time: 2_000_000_000n },
        calls: [{ from: FROM, to: TO, value: 1n }],
      },
    ],
  };
  const blocks: Simulate.Simulate = yield* Eth.simulateV1(payload, "latest");
  const call: Simulate.Call | undefined = blocks[0]?.calls[0];
  return call?.status; // "success"
});
```

## Schemas

| Schema | Type | Describes | Used by |
| --- | --- | --- | --- |
| `Simulate.payloadSchema` | `Simulate.Payload` | what to simulate, for blocks with their transactions as hashes | the first param of `Eth.simulateV1` |
| `Simulate.fullPayloadSchema` | `Simulate.FullPayload` | the same with `returnFullTransactions: true` | the first param of `Eth.simulateV1` |
| `Simulate.schema` | `Simulate.Simulate` | the simulated blocks, with `transactions` as hashes | the result for a `Simulate.Payload` |
| `Simulate.fullSchema` | `Simulate.Full` | the simulated blocks, with `transactions` in full | the result for a `Simulate.FullPayload` |
| `Simulate.callSchema` | `Simulate.Call` | the outcome of one call | the `calls` of a simulated block |

## Payload fields

`Simulate.Payload` and `Simulate.FullPayload`.

| Field | Type | Notes |
| --- | --- | --- |
| `blockStateCalls` | `ReadonlyArray` of blocks | the blocks to simulate, in order |
| `traceTransfers` | `boolean` (optional) | report transfers of ether as ERC-20 `Transfer` logs |
| `validation` | `boolean` (optional) | check nonces, balances and fees as a real block would |
| `returnFullTransactions` | `false` (optional) in `Simulate.Payload` | |
| `returnFullTransactions` | `true` in `Simulate.FullPayload` | return the transactions of each block in full |

Each entry of `blockStateCalls` is one block. Its three fields are optional.

| Field | Type | Notes |
| --- | --- | --- |
| `blockOverrides` | [`BlockOverride`](https://ethect.dev/rpc/types/block-override/) | header fields of the simulated block |
| `stateOverrides` | [`StateOverride`](https://ethect.dev/rpc/types/state-override/) | state changed before the calls of the block run |
| `calls` | `ReadonlyArray<`[`TransactionRequest`](https://ethect.dev/rpc/types/transaction-request/)`>` | the calls of the block, in order |

## Call fields

`Simulate.Call`, the outcome of one call.

| Field | Type | Notes |
| --- | --- | --- |
| `status` | `"success" \| "failure"` | |
| `returnData` | `Hex` | what the call returned, `"0x"` for nothing |
| `gasUsed` | `bigint` | |
| `maxUsedGas` | `bigint` (optional) | the most gas the call held at once, before refunds. anvil 1.9 sends it. |
| `logs` | `ReadonlyArray<`[`Log`](https://ethect.dev/rpc/types/log/)`>` (optional) | the logs the call emitted |
| `error` | `{ code: number, message: string, data?: unknown }` (optional) | on a failed call: the JSON-RPC error a real call would fail with |

## Block fields

An entry of `Simulate.Simulate` has every field of a [`Block.Block`](https://ethect.dev/rpc/types/block/),
plus `calls`. An entry of `Simulate.Full` has every field of a `Block.Full`, plus `calls`.

| Field | Type | Notes |
| --- | --- | --- |
| `calls` | `ReadonlyArray<Simulate.Call>` | one per call of the block, in order |
| `transactions` | `ReadonlyArray<Hash>` in `Simulate.Simulate` | the simulated transactions, one per call |
| `transactions` | `ReadonlyArray<`[`Transaction`](https://ethect.dev/rpc/types/transaction/)`>` in `Simulate.Full` | |

## Behavior

- **The payload chooses the result.** `Eth.simulateV1` is an overload: a payload with
  `returnFullTransactions: true` gives a `Simulate.Full`, any other gives a
  `Simulate.Simulate`.
- **A failed call is a value.** It has the status `"failure"` and an `error`. A revert has the
  code `3` and its revert data in `error.data`.
- **`status` is a name.** The node answers `"0x1"` or `"0x0"`, decoded to `"success"` or
  `"failure"`. A [`Receipt`](https://ethect.dev/rpc/types/receipt/) says `"reverted"` where a simulated call
  says `"failure"`.
- **`error` is the node's error object.** It is not an
  [`RpcError`](https://ethect.dev/reference/errors/rpc-error/) and has no `reason`. Pass it to
  [`RpcError.fromWire`](https://ethect.dev/reference/errors/rpc-error/#fromwire) to classify it: a revert gives
  `ExecutionReverted` with its data.
- **Simulated transactions are not signed.** In a `Simulate.Full`, anvil fills the signature
  fields of a transaction with placeholders.
