Skip to content

Time

The seven methods that read and move the clock of a test node: the timestamp its next blocks get, and the real time at which it mined the latest one.

Method JSON-RPC Returns Description
getGenesisTime anvil_getGenesisTime bigint The timestamp of the genesis block, in seconds.
getLastBlockWallTime anvil_getLastBlockWallTime number The real time at which the latest block became the head, in milliseconds.
increaseTime anvil_increaseTime number Moves the clock forward by a number of seconds.
setTime anvil_setTime number Sets the clock to a timestamp.
setNextBlockTimestamp anvil_setNextBlockTimestamp null Sets the timestamp of the next block.
setBlockTimestampInterval anvil_setBlockTimestampInterval null Makes every block’s timestamp a fixed number of seconds after the one before.
removeBlockTimestampInterval anvil_removeBlockTimestampInterval boolean Removes that interval.

Each returns an Effect<…, Method.Error, Transport>. Moving the clock mines nothing: the new time shows in the timestamp of the next block, so mine one with Anvil.mine.

Returns the timestamp of the genesis block, in seconds.

import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";
const age = Effect.gen(function* () {
const genesis = yield* Anvil.getGenesisTime();
const block = yield* Eth.getBlockByNumber("latest", false);
return block === null ? 0n : block.timestamp - genesis;
});
  • JSON-RPC: anvil_getGenesisTime
  • Parameters: none.
  • Returns: bigint, in seconds since the Unix epoch, like the timestamp of a block.

Returns the real time at which the latest block became the head, in milliseconds.

import { Effect } from "effect";
import { Anvil } from "@ethect/core/rpc";
const sinceLastBlock = Anvil.getLastBlockWallTime().pipe(
Effect.map((milliseconds) => Date.now() - milliseconds),
);
  • JSON-RPC: anvil_getLastBlockWallTime
  • Parameters: none.
  • Returns: number, in milliseconds since the Unix epoch. It is the time of the machine, not the timestamp of the block: moving the chain’s clock does not change it.

Moves the clock forward by a number of seconds.

import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";
const timestamp = Eth.getBlockByNumber("latest", false).pipe(
Effect.map((block) => block?.timestamp),
);
const later = Effect.gen(function* () {
yield* Anvil.increaseTime(3600n);
yield* Anvil.mine();
return yield* timestamp; // at least an hour after the block before
});
  • JSON-RPC: anvil_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. It is 3600 after the first call on a fresh node and 7200 after a second one, or one second less on a node that has just started.

Sets the clock to a timestamp.

import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";
const timestamp = Eth.getBlockByNumber("latest", false).pipe(
Effect.map((block) => block?.timestamp),
);
const moved = Effect.gen(function* () {
yield* Anvil.setTime(2_000_000_000n);
yield* Anvil.mine();
return yield* timestamp; // 2000000000n
});
  • JSON-RPC: anvil_setTime
  • Parameters: timestamp (bigint), in seconds. A value above 10n ** 12n is read as milliseconds: 3_000_000_000_000n gives a block at 3000000000n.
  • Returns: number. How many seconds forward the clock moved, 0 when it moved back.
  • The clock can be set before the latest block. The next block then has a timestamp lower than its parent’s.

Sets the timestamp of the next block, in seconds.

import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";
const timestamp = Eth.getBlockByNumber("latest", false).pipe(
Effect.map((block) => block?.timestamp),
);
const exact = Effect.gen(function* () {
yield* Anvil.setNextBlockTimestamp(2_000_000_000n);
yield* Anvil.mine();
return yield* timestamp; // 2000000000n
});
  • JSON-RPC: anvil_setNextBlockTimestamp
  • Parameters: timestamp (bigint, in seconds).
  • Returns: null.
  • A timestamp lower than the latest block’s fails with an RpcError of reason InvalidParams. One equal to it is accepted.
  • The clock goes on from there: the block after it is one second later when mined right away.

Makes every block’s timestamp a fixed number of seconds after the one before, whatever the real time between them.

import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";
const timestamp = Eth.getBlockByNumber("latest", false).pipe(
Effect.map((block) => block?.timestamp),
);
const spaced = Effect.gen(function* () {
yield* Anvil.setNextBlockTimestamp(2_000_000_000n);
yield* Anvil.setBlockTimestampInterval(10n);
yield* Anvil.mine(3);
return yield* timestamp; // 2000000020n
});
  • JSON-RPC: anvil_setBlockTimestampInterval
  • Parameters: seconds (bigint).
  • Returns: null.

Removes the interval set by setBlockTimestampInterval.

import { Effect } from "effect";
import { Anvil } from "@ethect/core/rpc";
const removed = Effect.gen(function* () {
yield* Anvil.setBlockTimestampInterval(10n);
const first = yield* Anvil.removeBlockTimestampInterval(); // true
const second = yield* Anvil.removeBlockTimestampInterval(); // false
return { first, second };
});
  • JSON-RPC: anvil_removeBlockTimestampInterval
  • Parameters: none.
  • Returns: boolean. true when an interval was set, false when there was none.
  • Two clocks. The chain’s clock gives blocks their timestamp. It starts at the real time and these methods move it. getLastBlockWallTime reads the real one.
  • Seconds and timestamps are bigints. Every method here takes one. The two results that count seconds, of increaseTime and setTime, are numbers, as is the wall time.
  • Undone by a revert. Anvil.revert also brings the clock back to where it was when the snapshot was taken.