7

For Ganache, there are several solutions.

What about Hardhat? They implemented their own local blockchain, Hardhat Network, which is different to Ganache.

3
  • 1
    The buidlerevm page says it supports ganache's evm_increaseTime and additionally they have evm_setNextBlockTimestamp. – Ismael Aug 12 '20 at 0:16
  • @Ismael your comment is a legitimate answer. Feel free to post it :) – Paul Razvan Berg Aug 12 '20 at 9:43
  • I can't test right now, but I'll add an answer when I've more time. – Ismael Aug 12 '20 at 21:12
11

There are two relevant RPC methods here: evm_increaseTime and evm_setNextBlockTimestamp. In both cases, they affect the next block but don't mine one.

evm_increaseTime receives a number of seconds that will be added to the timestamp of the latest block. evm_setNextBlockTimestamp receives an absolute UNIX timestamp (again, in seconds), and so it's not affected by the current block.

Examples

evm_increaseTime

// suppose the current block has a timestamp of 01:00 PM
await network.provider.send("evm_increaseTime", [3600])
await network.provider.send("evm_mine") // this one will have 02:00 PM as its timestamp

evm_setNextBlockTimestamp

await network.provider.send("evm_setNextBlockTimestamp", [1625097600])
await network.provider.send("evm_mine") // this one will have 2021-07-01 12:00 AM as its timestamp, no matter what the previous block has

Keep in mind that Hardhat Network validates that the new timestamp is bigger than the previous one, so you can't send any value.

2
  • When using this with hardhat I got this issue: Error: Transaction reverted and Hardhat couldn't infer the reason. Please report this to help us improve Hardhat. – Julien Kode Mar 7 at 17:16
  • Did anyone face the same issue ? – Julien Kode Mar 7 at 17:16
1

What I've been using with typescript and hardhat:

import { ethers, waffle } from 'hardhat';

const time = now + 86400
await ethers.provider.send('evm_setNextBlockTimestamp', [now]); 
await ethers.provider.send('evm_mine');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.