# Chain

The seven methods that change what the chain is and what its next block holds: the chain id,
the coinbase, the gas limit, the fees and two header fields.

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`setChainId`](#setchainid) | `anvil_setChainId` | `null` | Sets the chain id. |
| [`setCoinbase`](#setcoinbase) | `anvil_setCoinbase` | `null` | Sets the address that receives block rewards. |
| [`setBlockGasLimit`](#setblockgaslimit) | `anvil_setBlockGasLimit` | `boolean` | Sets the gas limit of the blocks to come. |
| [`setMinGasPrice`](#setmingasprice) | `anvil_setMinGasPrice` | `null` | Sets the lowest gas price the node accepts. Refused once EIP-1559 is active. |
| [`setNextBlockBaseFeePerGas`](#setnextblockbasefeepergas) | `anvil_setNextBlockBaseFeePerGas` | `null` | Sets the base fee of the next block. |
| [`setNextBlockPrevRandao`](#setnextblockprevrandao) | `anvil_setNextBlockPrevRandao` | `null` | Sets the `prevRandao` of the next block. |
| [`setNextBlockParentBeaconBlockRoot`](#setnextblockparentbeaconblockroot) | `anvil_setNextBlockParentBeaconBlockRoot` | `null` | Sets the parent beacon block root of the next block. |

Each takes one parameter and returns an
[`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method). None of them mines
a block: mine one with [`Anvil.mine`](https://ethect.dev/rpc/anvil/mining/#mine) to see what the next block
holds.

## setChainId

Sets the chain id of the node.

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

const chainId = Effect.gen(function* () {
  yield* Anvil.setChainId(1337);
  return yield* Eth.chainId(); // 1337
});
```

- **JSON-RPC:** `anvil_setChainId`
- **Parameters:** `chainId` (`number`).
- **Returns:** `null`.

## setCoinbase

Sets the address that receives block rewards: the `miner` of the blocks to come.

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

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const coinbase = Effect.gen(function* () {
  yield* Anvil.setCoinbase(ADDRESS);
  return yield* Eth.coinbase(); // "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
});
```

- **JSON-RPC:** `anvil_setCoinbase`
- **Parameters:** `address` ([`Address.Address`](https://ethect.dev/reference/primitives/address/)).
- **Returns:** `null`.

## setBlockGasLimit

Sets the gas limit of the blocks to come.

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

const gasLimit = Effect.gen(function* () {
  yield* Anvil.setBlockGasLimit(20_000_000n);
  yield* Anvil.mine();
  const block = yield* Eth.getBlockByNumber("latest", false);
  return block?.gasLimit; // 20000000n
});
```

- **JSON-RPC:** `anvil_setBlockGasLimit`
- **Parameters:** `gasLimit` (`bigint`).
- **Returns:** `boolean`. anvil answers `true`.

## setMinGasPrice

Sets the lowest gas price the node accepts, in wei. It is a setting of chains without a base
fee, and anvil refuses it once EIP-1559 is active, which it is by default.

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

const outcome = Anvil.setMinGasPrice(1n).pipe(
  Effect.as("set"),
  Effect.catchTag("RpcError", (error) => Effect.succeed(error.message)),
);
// "anvil_setMinGasPrice is not supported when EIP-1559 is active"
```

- **JSON-RPC:** `anvil_setMinGasPrice`
- **Parameters:** `gasPrice` (`bigint`, in wei).
- **Returns:** `null`.
- On a default anvil it fails with an `RpcError` of reason `InvalidParams`.
- A node started on an earlier hard fork, with `anvil --hardfork berlin`, accepts it.
  [`Eth.gasPrice`](https://ethect.dev/rpc/eth/fees/#gasprice) then returns the price that was set.

## setNextBlockBaseFeePerGas

Sets the base fee of the next block, in wei.

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

const baseFee = Effect.gen(function* () {
  yield* Anvil.setNextBlockBaseFeePerGas(2_000_000_000n);
  yield* Anvil.mine();
  const block = yield* Eth.getBlockByNumber("latest", false);
  return block?.baseFeePerGas; // 2000000000n
});
```

- **JSON-RPC:** `anvil_setNextBlockBaseFeePerGas`
- **Parameters:** `baseFeePerGas` (`bigint`, in wei).
- **Returns:** `null`.
- [`Eth.baseFee`](https://ethect.dev/rpc/eth/fees/#basefee) returns the new value at once, before the
  block is mined.

## setNextBlockPrevRandao

Sets the `prevRandao` of the next block, which a block carries in its `mixHash` field.

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

const randao = Effect.gen(function* () {
  const prevRandao = `0x${"01".padStart(64, "0")}` as const;
  yield* Anvil.setNextBlockPrevRandao(prevRandao);
  yield* Anvil.mine();
  const block = yield* Eth.getBlockByNumber("latest", false);
  return block?.mixHash;
  // "0x0000000000000000000000000000000000000000000000000000000000000001"
});
```

- **JSON-RPC:** `anvil_setNextBlockPrevRandao`
- **Parameters:** `prevRandao` ([`Hash`](https://ethect.dev/reference/primitives/hash/), 32 bytes).
- **Returns:** `null`.

## setNextBlockParentBeaconBlockRoot

Sets the parent beacon block root of the next block.

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

const root = Effect.gen(function* () {
  const parentBeaconBlockRoot = `0x${"02".padStart(64, "0")}` as const;
  yield* Anvil.setNextBlockParentBeaconBlockRoot(parentBeaconBlockRoot);
  yield* Anvil.mine();
  const block = yield* Eth.getBlockByNumber("latest", false);
  return block?.parentBeaconBlockRoot;
  // "0x0000000000000000000000000000000000000000000000000000000000000002"
});
```

- **JSON-RPC:** `anvil_setNextBlockParentBeaconBlockRoot`
- **Parameters:** `parentBeaconBlockRoot` ([`Hash`](https://ethect.dev/reference/primitives/hash/), 32 bytes).
- **Returns:** `null`.

## Behavior

- **Next block only.** The three `setNextBlock…` methods apply to one block. The block after it
  gets its base fee from the usual rule, a `prevRandao` of the node's choosing and a zero
  parent beacon block root.
- **Until changed.** The chain id, the coinbase and the gas limit stay as set for every block
  that follows.
