Skip to content

The evm_ methods (Evm)

The evm_ namespace: the names Ganache gave to driving a test node, as anvil serves them. All but mine are an Anvil method under another name.

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: see Testing.

A method takes its JSON-RPC params in order and returns an Effect<…, Method.Error, Transport>. The name is the wire name without its prefix: evm_snapshot is Evm.snapshot, and evm_mine_detailed is Evm.mineDetailed.

Method JSON-RPC Returns Description
setAutomine evm_setAutomine null Turns mining a block per transaction on or off. Same as Anvil.setAutomine.
setIntervalMining evm_setIntervalMining null Mines a block every given number of seconds. Same as Anvil.setIntervalMining.
mine evm_mine string Mines blocks. The answer is always "0x0".
mineDetailed evm_mine_detailed MinedBlock[] Mines blocks and returns them. Same as Anvil.mineDetailed.
Method JSON-RPC Returns Description
setAccountNonce evm_setAccountNonce null Sets the nonce of an account. Same as Anvil.setNonce.
setBlockGasLimit evm_setBlockGasLimit boolean Sets the gas limit of the blocks to come. Same as Anvil.setBlockGasLimit.
Method JSON-RPC Returns Description
increaseTime evm_increaseTime number Moves the clock forward by a number of seconds. Same as Anvil.increaseTime.
setTime evm_setTime number Sets the clock to a timestamp. Same as Anvil.setTime.
setNextBlockTimestamp evm_setNextBlockTimestamp null Sets the timestamp of the next block. Same as Anvil.setNextBlockTimestamp.
Method JSON-RPC Returns Description
snapshot evm_snapshot Hex Saves the state of the chain and returns an id. Same as Anvil.snapshot.
revert evm_revert boolean Brings the chain back to a snapshot. Same as Anvil.revert.

Turns mining a block per transaction on or off.

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.

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

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.

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

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): 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 to get them.
  • It is the one method here that is not an Anvil method under another name: Anvil.mine takes a count and an interval, and answers null.

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

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]
});

Sets the nonce of an account.

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
});

Sets the gas limit of the blocks to come.

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.

Moves the clock forward by a number of seconds.

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.

Sets the clock to a timestamp.

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.

Sets the timestamp of the next block, in seconds.

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
});

Saves the state of the chain and returns the id to revert to.

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, the id of the snapshot.
  • Same as: Anvil.snapshot.

Brings the chain back to a snapshot.

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), as returned by snapshot.
  • Returns: boolean. true when the chain was brought back, false when the node does not know the snapshot.
  • Same as: Anvil.revert.
  • 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.