Skip to content

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 anvil_setChainId null Sets the chain id.
setCoinbase anvil_setCoinbase null Sets the address that receives block rewards.
setBlockGasLimit anvil_setBlockGasLimit boolean Sets the gas limit of the blocks to come.
setMinGasPrice anvil_setMinGasPrice null Sets the lowest gas price the node accepts. Refused once EIP-1559 is active.
setNextBlockBaseFeePerGas anvil_setNextBlockBaseFeePerGas null Sets the base fee of the next block.
setNextBlockPrevRandao anvil_setNextBlockPrevRandao null Sets the prevRandao of the next block.
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>. None of them mines a block: mine one with Anvil.mine to see what the next block holds.

Sets the chain id of the node.

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.

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

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).
  • Returns: null.

Sets the gas limit of the blocks to come.

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.

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.

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 then returns the price that was set.

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

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 returns the new value at once, before the block is mined.

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

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, 32 bytes).
  • Returns: null.

Sets the parent beacon block root of the next block.

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, 32 bytes).
  • Returns: null.
  • 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.