# The evm_ methods (Evm)

The `evm_` namespace: the names Ganache gave to driving a test node, as anvil serves them. All
but [`mine`](#mine) are an [`Anvil`](https://ethect.dev/rpc/anvil/) method under another name.

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { Eth, Evm } from "@ethect/core/rpc";

const program = Effect.gen(function* () {
  const snapshot = yield* Evm.snapshot();
  yield* Evm.mine({ blocks: 3 });
  const mined = yield* Eth.blockNumber();
  yield* Evm.revert(snapshot);
  const reverted = yield* Eth.blockNumber();
  return { mined, reverted };
});

const result = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// { mined: 3n, reverted: 0n }
```

The namespace is for a local test node. A test gets a fresh one from
[`AnvilNode`](https://ethect.dev/reference/testing/anvil-node/): see [Testing](https://ethect.dev/guides/testing/).

A method takes its JSON-RPC params in order and returns an
[`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method). The name is the
wire name without its prefix: `evm_snapshot` is `Evm.snapshot`, and `evm_mine_detailed` is
`Evm.mineDetailed`.

## Mining

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`setAutomine`](#setautomine) | `evm_setAutomine` | `null` | Turns mining a block per transaction on or off. Same as `Anvil.setAutomine`. |
| [`setIntervalMining`](#setintervalmining) | `evm_setIntervalMining` | `null` | Mines a block every given number of seconds. Same as `Anvil.setIntervalMining`. |
| [`mine`](#mine) | `evm_mine` | `string` | Mines blocks. The answer is always `"0x0"`. |
| [`mineDetailed`](#minedetailed) | `evm_mine_detailed` | `MinedBlock[]` | Mines blocks and returns them. Same as `Anvil.mineDetailed`. |

## State

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`setAccountNonce`](#setaccountnonce) | `evm_setAccountNonce` | `null` | Sets the nonce of an account. Same as `Anvil.setNonce`. |
| [`setBlockGasLimit`](#setblockgaslimit) | `evm_setBlockGasLimit` | `boolean` | Sets the gas limit of the blocks to come. Same as `Anvil.setBlockGasLimit`. |

## Time

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`increaseTime`](#increasetime) | `evm_increaseTime` | `number` | Moves the clock forward by a number of seconds. Same as `Anvil.increaseTime`. |
| [`setTime`](#settime) | `evm_setTime` | `number` | Sets the clock to a timestamp. Same as `Anvil.setTime`. |
| [`setNextBlockTimestamp`](#setnextblocktimestamp) | `evm_setNextBlockTimestamp` | `null` | Sets the timestamp of the next block. Same as `Anvil.setNextBlockTimestamp`. |

## History

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`snapshot`](#snapshot) | `evm_snapshot` | `Hex` | Saves the state of the chain and returns an id. Same as `Anvil.snapshot`. |
| [`revert`](#revert) | `evm_revert` | `boolean` | Brings the chain back to a snapshot. Same as `Anvil.revert`. |

## setAutomine

Turns mining a block per transaction on or off.

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

const held = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  yield* Evm.setAutomine(false);
  const hash = yield* Eth.sendTransaction({ from, to, value: 1n });
  yield* Evm.mine();
  return hash;
});
```

- **JSON-RPC:** `evm_setAutomine`
- **Parameters:** `enabled` (`boolean`).
- **Returns:** `null`.
- **Same as:** [`Anvil.setAutomine`](https://ethect.dev/rpc/anvil/mining/#setautomine).

## setIntervalMining

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

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

const timed = Effect.gen(function* () {
  yield* Evm.setIntervalMining(60);
  return yield* Anvil.getIntervalMining(); // 60
});
```

- **JSON-RPC:** `evm_setIntervalMining`
- **Parameters:** `interval` (`number`, in seconds). `0` stops the timer.
- **Returns:** `null`.
- **Same as:** [`Anvil.setIntervalMining`](https://ethect.dev/rpc/anvil/mining/#setintervalmining).

## mine

Mines blocks: one by default, or what the options say.

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

const mined = Effect.gen(function* () {
  yield* Evm.mine();
  yield* Evm.mine({ blocks: 2 });
  yield* Evm.mine({ timestamp: 2_000_000_000n });
  const block = yield* Eth.getBlockByNumber("latest", false);
  return { number: block?.number, timestamp: block?.timestamp };
  // { number: 4n, timestamp: 2000000000n }
});
```

- **JSON-RPC:** `evm_mine`
- **Parameters:** `options` (optional, [`MineOptions`](https://ethect.dev/rpc/types/mine-options/)): how
  many blocks to mine and the timestamp of the first.
- **Returns:** `string`. anvil always answers `"0x0"`, whatever was mined: it says nothing about
  the blocks. Use [`mineDetailed`](#minedetailed) to get them.
- It is the one method here that is not an `Anvil` method under another name:
  [`Anvil.mine`](https://ethect.dev/rpc/anvil/mining/#mine) takes a count and an interval, and answers
  `null`.

## mineDetailed

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

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

const numbers = Effect.gen(function* () {
  const blocks = yield* Evm.mineDetailed({ timestamp: 2_000_000_000n, blocks: 2 });
  return blocks.map((block) => block.number); // [1n, 2n]
});
```

- **JSON-RPC:** `evm_mine_detailed`
- **Parameters:** `options` (optional, [`MineOptions`](https://ethect.dev/rpc/types/mine-options/)).
- **Returns:** `ReadonlyArray<MinedBlock>`, one
  [`MinedBlock`](https://ethect.dev/rpc/types/mined-block/) per block mined.
- **Same as:** [`Anvil.mineDetailed`](https://ethect.dev/rpc/anvil/mining/#minedetailed).

## setAccountNonce

Sets the nonce of an account.

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

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

const next = Effect.gen(function* () {
  yield* Evm.setAccountNonce(ADDRESS, 7);
  return yield* Eth.getTransactionCount(ADDRESS, "latest"); // 7
});
```

- **JSON-RPC:** `evm_setAccountNonce`
- **Parameters:** `address` ([`Address.Address`](https://ethect.dev/reference/primitives/address/)), then `nonce`
  (`number`).
- **Returns:** `null`.
- **Same as:** [`Anvil.setNonce`](https://ethect.dev/rpc/anvil/state/#setnonce).

## setBlockGasLimit

Sets the gas limit of the blocks to come.

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

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

- **JSON-RPC:** `evm_setBlockGasLimit`
- **Parameters:** `gasLimit` (`bigint`).
- **Returns:** `boolean`. anvil answers `true`.
- **Same as:** [`Anvil.setBlockGasLimit`](https://ethect.dev/rpc/anvil/chain/#setblockgaslimit).

## increaseTime

Moves the clock forward by a number of seconds.

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

const later = Effect.gen(function* () {
  const ahead = yield* Evm.increaseTime(3600n); // 3600
  yield* Evm.mine();
  const block = yield* Eth.getBlockByNumber("latest", false);
  return { ahead, timestamp: block?.timestamp };
});
```

- **JSON-RPC:** `evm_increaseTime`
- **Parameters:** `seconds` (`bigint`).
- **Returns:** `number`. How many seconds the chain's clock is now ahead of the real one,
  negative when it is behind.
- **The whole offset of the node, not what was added.** On a node that has just started it
  can be one second less, `3599` here: anvil reads the time twice as it starts, once for its
  first block and once for its clock, and a second can end between the two.
- **Same as:** [`Anvil.increaseTime`](https://ethect.dev/rpc/anvil/time/#increasetime).

## setTime

Sets the clock to a timestamp.

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

const moved = Effect.gen(function* () {
  yield* Evm.setTime(2_000_000_000n);
  yield* Evm.mine();
  const block = yield* Eth.getBlockByNumber("latest", false);
  return block?.timestamp; // 2000000000n
});
```

- **JSON-RPC:** `evm_setTime`
- **Parameters:** `timestamp` (`bigint`), in seconds. A value above `10n ** 12n` is read as
  milliseconds.
- **Returns:** `number`. How many seconds forward the clock moved.
- **Same as:** [`Anvil.setTime`](https://ethect.dev/rpc/anvil/time/#settime).

## setNextBlockTimestamp

Sets the timestamp of the next block, in seconds.

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

const exact = Effect.gen(function* () {
  yield* Evm.setNextBlockTimestamp(2_000_000_000n);
  yield* Evm.mine();
  const block = yield* Eth.getBlockByNumber("latest", false);
  return block?.timestamp; // 2000000000n
});
```

- **JSON-RPC:** `evm_setNextBlockTimestamp`
- **Parameters:** `timestamp` (`bigint`, in seconds).
- **Returns:** `null`.
- **Same as:** [`Anvil.setNextBlockTimestamp`](https://ethect.dev/rpc/anvil/time/#setnextblocktimestamp).

## snapshot

Saves the state of the chain and returns the id to [`revert`](#revert) to.

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

const saved = Effect.gen(function* () {
  const id = yield* Evm.snapshot(); // "0x0"
  yield* Evm.mine({ blocks: 3 });
  return id;
});
```

- **JSON-RPC:** `evm_snapshot`
- **Parameters:** none.
- **Returns:** [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/), the id of the snapshot.
- **Same as:** [`Anvil.snapshot`](https://ethect.dev/rpc/anvil/history/#snapshot).

## revert

Brings the chain back to a snapshot.

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

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

const undone = Effect.gen(function* () {
  const id = yield* Evm.snapshot();
  yield* Anvil.setBalance(ADDRESS, 1n);
  const reverted = yield* Evm.revert(id); // true
  const balance = yield* Eth.getBalance(ADDRESS, "latest"); // 0n
  return { reverted, balance };
});
```

- **JSON-RPC:** `evm_revert`
- **Parameters:** `id` ([`Hex.Hex`](https://ethect.dev/reference/primitives/hex/)), as returned by
  [`snapshot`](#snapshot).
- **Returns:** `boolean`. `true` when the chain was brought back, `false` when the node does
  not know the snapshot.
- **Same as:** [`Anvil.revert`](https://ethect.dev/rpc/anvil/history/#revert).

## Behavior

- **One node, two sets of names.** The `evm_` and `anvil_` names act on the same node state. A
  snapshot taken with `Evm.snapshot` can be reverted with `Anvil.revert`, and
  `Anvil.getAutomine` reads what `Evm.setAutomine` set.
- **The details are on the `Anvil` pages.** What each method does to the node, and how it
  fails, is described with the `Anvil` method of the same name.
