# Fork

The two methods that make a running test node a fork of another chain, or start its chain anew:
the endpoint to fork from, and the reset that applies it.

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`setRpcUrl`](#setrpcurl) | `anvil_setRpcUrl` | `null` | Sets the endpoint the node forks from. |
| [`reset`](#reset) | `anvil_reset` | `null` | Starts the chain anew, as a fork or not. |

Each returns an [`Effect<null, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method).

## setRpcUrl

Sets the endpoint the node forks from. On a node that is not a fork it changes nothing by
itself: the next [`reset`](#reset) with options uses it.

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

const forked = Effect.gen(function* () {
  yield* Anvil.setRpcUrl("https://rpc.example.com");
  yield* Anvil.reset({ blockNumber: 19_000_000n });
  return yield* Eth.blockNumber(); // 19000000n
});
```

- **JSON-RPC:** `anvil_setRpcUrl`
- **Parameters:** `url` (`string`), the JSON-RPC endpoint of the chain to fork.
- **Returns:** `null`.

## reset

Starts the chain anew. Without an argument the node stops being a fork and goes back to its
genesis. With options it forks an endpoint at a block.

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

const program = Effect.gen(function* () {
  yield* Anvil.reset({
    jsonRpcUrl: "https://rpc.example.com",
    blockNumber: 19_000_000n,
  });
  const forkedAt = yield* Eth.blockNumber(); // 19000000n
  yield* Anvil.reset();
  const fresh = yield* Eth.blockNumber(); // 0n
  return { forkedAt, fresh };
});
```

- **JSON-RPC:** `anvil_reset`
- **Parameters:** `options` (optional), an object with two optional fields.
- **Returns:** `null`.

| Field | Type | Description |
| --- | --- | --- |
| `jsonRpcUrl` | `string` (optional) | the endpoint to fork. Left out, the endpoint set before: by `setRpcUrl`, by an earlier `reset` or by `anvil --fork-url`. |
| `blockNumber` | `bigint` (optional) | the block to fork at. Left out, the latest block of the endpoint. |

What each call does:

| Call | Result |
| --- | --- |
| `Anvil.reset()` | a chain that is not a fork, back at block `0`, with the development accounts funded again |
| `Anvil.reset({})` | a fork of the endpoint set before, at its latest block |
| `Anvil.reset({ blockNumber })` | a fork of the endpoint set before, at that block |
| `Anvil.reset({ jsonRpcUrl })` | a fork of that endpoint, at its latest block |

## Behavior

- **The head is the fork block.** After a reset with options,
  [`Eth.blockNumber`](https://ethect.dev/rpc/eth/block-number/) returns the block forked at, and state
  reads come from the forked chain.
- **Reading the fork back.** [`Anvil.nodeInfo`](https://ethect.dev/rpc/anvil/node/#nodeinfo) has the
  endpoint and the block under `forkConfig`, and
  [`Anvil.metadata`](https://ethect.dev/rpc/anvil/node/#metadata) has the chain id and the block hash
  under `forkedNetwork`.
- **Local state is dropped.** A reset discards the blocks mined and the state set on the node
  before it.

The refusals, as anvil 1.9 reports them:

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | `InvalidParams` | `reset` has options without `jsonRpcUrl` and no endpoint was set before |
| `RpcError` | `InternalError` | the endpoint does not answer |
