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.
getGenesisTime
Section titled “getGenesisTime”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 thetimestampof a block.
getLastBlockWallTime
Section titled “getLastBlockWallTime”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 thetimestampof the block: moving the chain’s clock does not change it.
increaseTime
Section titled “increaseTime”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 is3600after the first call on a fresh node and7200after a second one, or one second less on a node that has just started.
setTime
Section titled “setTime”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 above10n ** 12nis read as milliseconds:3_000_000_000_000ngives a block at3000000000n. - Returns:
number. How many seconds forward the clock moved,0when it moved back. - The clock can be set before the latest block. The next block then has a
timestamplower than its parent’s.
setNextBlockTimestamp
Section titled “setNextBlockTimestamp”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
RpcErrorof reasonInvalidParams. One equal to it is accepted. - The clock goes on from there: the block after it is one second later when mined right away.
setBlockTimestampInterval
Section titled “setBlockTimestampInterval”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.
removeBlockTimestampInterval
Section titled “removeBlockTimestampInterval”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.truewhen an interval was set,falsewhen there was none.
Behavior
Section titled “Behavior”- Two clocks. The chain’s clock gives blocks their
timestamp. It starts at the real time and these methods move it.getLastBlockWallTimereads the real one. - Seconds and timestamps are
bigints. Every method here takes one. The two results that count seconds, ofincreaseTimeandsetTime, arenumbers, as is the wall time. - Undone by a revert.
Anvil.revertalso brings the clock back to where it was when the snapshot was taken.