Hardhat Network Forking: Deterministic Block Timestamp Constraint
0 reputation · 02 Feb 2022, 14:42 UTC
0 reputation · 02 Feb 2022, 14:42 UTC
Hardhat’s --fork flag captures the latest block from a live network, including its block.timestamp. This value is used as the starting point for all forked test runs.
When multiple forks or parallel test executions occur, the forked block’s timestamp is taken directly from the source chain’s most recent block. Hardhat does not expose a configuration option to set a fixed or deterministic timestamp for the forked state. Consequently, contracts that depend on block.timestamp—such as auctions, vesting schedules, or rate‑limit logic—can behave nondeterministically in forked environments.
Developers currently rely on manual overrides (e.g., JSON‑RPC evm_setTime) or console adjustments, but these are not integrated into the fork configuration. The lack of an official flag leaves a design gap in Hardhat’s documentation.
block.timestamp in forked tests without manual overrides?34775 reputation · 02 Feb 2022, 15:54 UTC
Hardhat does not currently expose a configuration flag to fix the block.timestamp of a forked network. The fork inherits the timestamp from the latest block of the RPC endpoint, so running the fork at different times yields different values.
--fork option copies the latest block, including its block.timestamp, from the supplied RPC URL.hardhat.config.js) exists to override or fix that timestamp.evm_setTime (accessible via hardhat.provider.send) can set the blockchain clock for subsequent blocks.The missing flag is likely omitted because Hardhat treats the fork as a snapshot of a live chain; adding a timestamp override would diverge from that snapshot semantics. Future releases may consider such a flag, but no public roadmap or issue indicates an imminent implementation.
const { network } = require('hardhat');
beforeEach hook), set the desired timestamp:
beforeEach(async () => {
// Example: set timestamp to 1 Jan 2023 00:00:00 UTC
const targetTimestamp = 1672531200;
await network.provider.send('evm_setTime', [targetTimestamp]);
// Optionally mine a block to advance to the new timestamp
await network.provider.send('evm_mine', []);
});
block.timestamp reads will reflect the fixed value.After applying the override, log await network.provider.send('eth_getBlockByNumber', ['latest', false]) and confirm the timestamp matches the value you set.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.