Working with time on Virtual TestNets
For certain types of tests it’s useful to skip through time. This is possible by relying on 2 Admin RPC methods available on Virtual TestNets: evm_setNextBlockTimestamp
and evm_setNextBlockTimestamp
.
Getting the correct time
During execution of a transaction or eth_call
accesses block.timestamp
within a contract, the value will depend on whether the target block (latest
or pending
) block:
- For
pending
block:block.timestamp == time.now() + adjustment_offset
. This represents the current time adjusted by the time-adjustment methods. - For
latest
block:block.timestamp == latest_block.timestamp
. This is the timestamp of the most recently mined block.
The adjustment_offset
comes from time-adjustment methods:
-
evm_setNextBlockTimestamp
: Skips time to the timestamp for the next block:adjustment_offset = timestamp - time.now()
. -
evm_increaseTime
: Advances time by a relative amount.adjustment_offset += increase_value
Examples
Increase time by an offset
VIRTUAL_TESTNET_RPC=https://YOUR-RPC-URL-HERE
curl -s -X POST \
-H "Content-Type: application/json" \
--data '{
"jsonrpc": "2.0",
"method": "evm_increaseTime",
"params": ["0xe10"],
"id": 2
}' "$VIRTUAL_TESTNET_RPC"
Set timestamp for pending block
VIRTUAL_TESTNET_RPC=https://YOUR-RPC-URL-HERE
# Get the current date in Unix timestamp format
current_date=$(date +%s)
# Use evm_setTime to set the block time to today (current timestamp)
curl -s -X POST \
-H "Content-Type: application/json" \
--data '{
"jsonrpc": "2.0",
"method": "evm_setNextBlockTimestamp",
"params": ["'"$current_date"'"],
"id": 3
}' "$VIRTUAL_TESTNET_RPC"