# Mining

The eight methods that decide when blocks are mined: per transaction, on a timer or on request,
with mining paused or not.

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`getAutomine`](#getautomine) | `anvil_getAutomine` | `boolean` | Whether a block is mined as soon as a transaction arrives. |
| [`setAutomine`](#setautomine) | `anvil_setAutomine` | `null` | Turns mining a block per transaction on or off. |
| [`getIntervalMining`](#getintervalmining) | `anvil_getIntervalMining` | `number \| null` | The seconds between two blocks, when blocks are mined on a timer. |
| [`setIntervalMining`](#setintervalmining) | `anvil_setIntervalMining` | `null` | Mines a block every given number of seconds. |
| [`getMiningPaused`](#getminingpaused) | `anvil_getMiningPaused` | `boolean` | Whether mining is paused. |
| [`setMiningPaused`](#setminingpaused) | `anvil_setMiningPaused` | `null` | Pauses or resumes mining. |
| [`mine`](#mine) | `anvil_mine` | `null` | Mines blocks, one by default. |
| [`mineDetailed`](#minedetailed) | `anvil_mine_detailed` | `MinedBlock[]` | Mines blocks and returns them, with what each transaction did. |

Each returns an [`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method). A
node started with plain `anvil` mines a block per transaction: `getAutomine` is `true`,
`getIntervalMining` is `null` and `getMiningPaused` is `false`.

## getAutomine

Returns whether a block is mined as soon as a transaction arrives.

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

const automine = Anvil.getAutomine().pipe(
  Effect.map((on) => (on ? "a block per transaction" : "on a timer or on request")),
);
```

- **JSON-RPC:** `anvil_getAutomine`
- **Parameters:** none.
- **Returns:** `boolean`.

## setAutomine

Turns mining a block per transaction on or off.

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

const batched = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  yield* Anvil.setAutomine(false);
  yield* Eth.sendTransaction({ from, to, value: 1n });
  yield* Eth.sendTransaction({ from, to, value: 2n });
  yield* Anvil.mine();
  const block = yield* Eth.getBlockByNumber("latest", false);
  return block?.transactions.length; // 2
});
```

- **JSON-RPC:** `anvil_setAutomine`
- **Parameters:** `enabled` (`boolean`).
- **Returns:** `null`.
- `true` also stops the timer of [`setIntervalMining`](#setintervalmining).

## getIntervalMining

Returns the seconds between two blocks when blocks are mined on a timer.

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

const interval = Anvil.getIntervalMining().pipe(
  Effect.map((seconds) => (seconds === null ? "no timer" : `every ${seconds} s`)),
);
```

- **JSON-RPC:** `anvil_getIntervalMining`
- **Parameters:** none.
- **Returns:** `number | null`. The interval in seconds, or `null` when blocks are not mined
  on a timer.

## setIntervalMining

Mines a block every given number of seconds, and no longer one per transaction.

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

const timed = Effect.gen(function* () {
  yield* Anvil.setIntervalMining(60);
  const interval = yield* Anvil.getIntervalMining(); // 60
  const automine = yield* Anvil.getAutomine(); // false
  return { interval, automine };
});
```

- **JSON-RPC:** `anvil_setIntervalMining`
- **Parameters:** `interval` (`number`, in seconds). `0` stops the timer.
- **Returns:** `null`.
- Stopping the timer with `0` does not turn automine back on. Nothing is mined until
  [`mine`](#mine) is called or `setAutomine(true)` is.

## getMiningPaused

Returns whether mining is paused.

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

const paused = Anvil.getMiningPaused().pipe(
  Effect.map((on) => (on ? "paused" : "mining")),
);
```

- **JSON-RPC:** `anvil_getMiningPaused`
- **Parameters:** none.
- **Returns:** `boolean`.

## setMiningPaused

Pauses or resumes mining, and leaves the mode as it is.

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

const held = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  yield* Anvil.setMiningPaused(true);
  const hash = yield* Eth.sendTransaction({ from, to, value: 1n });
  // the transaction waits in the pool
  yield* Anvil.setMiningPaused(false);
  return hash;
});
```

- **JSON-RPC:** `anvil_setMiningPaused`
- **Parameters:** `paused` (`boolean`).
- **Returns:** `null`.
- While mining is paused, `getAutomine` still returns `true` on a node that mines per
  transaction, and [`mine`](#mine) still mines.
- Resuming mines what waited in the pool.

## mine

Mines blocks: one by default.

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

const mined = Effect.gen(function* () {
  yield* Anvil.mine();
  // three blocks, their timestamps 12 seconds apart
  yield* Anvil.mine(3, 12n);
  return yield* Eth.blockNumber(); // 4n
});
```

| Parameter | Type | Description |
| --- | --- | --- |
| `blocks` (optional) | `number` | how many blocks to mine. One when left out. |
| `interval` (optional) | `bigint` | the seconds between the timestamps of two blocks, the first one included |

- **JSON-RPC:** `anvil_mine`
- **Returns:** `null`.
- The transactions waiting in the pool go into the first block mined.

## mineDetailed

Mines blocks and returns them in full, each transaction with what its execution returned.

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

const outputs = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  yield* Anvil.setAutomine(false);
  yield* Eth.sendTransaction({ from, to, value: 1n });
  const [block] = yield* Anvil.mineDetailed();
  return block?.transactions.map((transaction) => transaction.output); // ["0x"]
});
```

- **JSON-RPC:** `anvil_mine_detailed`
- **Parameters:** `options` (optional, [`MineOptions`](https://ethect.dev/rpc/types/mine-options/)): how
  many blocks to mine and the timestamp of the first.
- **Returns:** `ReadonlyArray<MinedBlock>`, one [`MinedBlock`](https://ethect.dev/rpc/types/mined-block/)
  per block mined. A transaction that reverted carries a `revertReason`.
- A `timestamp` lower than the latest block's fails with an `RpcError` of reason
  `InvalidParams`.

## Behavior

- **Three modes.** A node mines per transaction (automine), on a timer (interval mining), or
  only on request. Turning one of the first two on turns the other off, and turning both off
  leaves [`mine`](#mine) as the only way to get a block.
- **A transaction is answered before its block.** With any mode,
  [`Eth.sendTransaction`](https://ethect.dev/rpc/eth/send-transaction/) returns the hash as soon as the
  transaction is in the pool.
  [`Eth.sendTransactionSync`](https://ethect.dev/rpc/eth/send-transaction-sync/) waits for the receipt.
- **Empty blocks.** `mine` mines a block even when the pool is empty. `mine(0)` mines nothing.
