I'm using hardhat and I added a task to manipulate time in the hardhat.config.js file which is working fine.

const { time } = require('@openzeppelin/test-helpers');

task("increaseDays", "Increase days to currentTime")
  .addParam("days", "The number of days").setAction(
    async (taskArguments) => {
      await time.increase(time.duration.days(Number(taskArguments.days)));
      const newTime = await time.latest();
      console.log('New Time is:', newTime.toString(), new Date(newTime.toString() * 1000));
      return newTime.toString();

The Problem now is that every test script that implements const { time } = require('@openzeppelin/test-helpers'); fails directly when I try to run npx hardhat test --network hardhat ./test/script-test.js

      "before all" hook for "should revert on fallback and receiver function":
     Error: Invalid JSON RPC response: ""
      at Object.InvalidResponse (node_modules\web3-core-helpers\lib\errors.js:43:16)
      at XMLHttpRequest.request.onreadystatechange (node_modules\web3-providers-http\lib\index.js:95:32)
      at XMLHttpRequestEventTarget.dispatchEvent (node_modules\xhr2-cookies\xml-http-request-event-target.ts:44:13)
      at XMLHttpRequest._setReadyState (node_modules\xhr2-cookies\xml-http-request.ts:219:8)
      at XMLHttpRequest._onHttpRequestError (node_modules\xhr2-cookies\xml-http-request.ts:379:8)
      at ClientRequest.<anonymous> (node_modules\xhr2-cookies\xml-http-request.ts:266:37)
      at Socket.socketErrorListener (_http_client.js:406:9)
      at emitErrorNT (internal/streams/destroy.js:92:8)
      at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
      at processTicksAndRejections (internal/process/task_queues.js:80:21)

The test-script

const { expect } = require('chai');
const { time } = require('@openzeppelin/test-helpers');

describe('Test Contract', () => {
  before(async () => {
    const startTime = BigNumber.from((await time.latest()).toString());
  it('should revert on fallback and receiver function..', () => {

I'm using "hardhat": "^2.3.0" and "@openzeppelin/test-helpers": "^0.5.11". Any idea what is the problem and how could I solve it?

  • What's inside script-test.js? Jun 10 '21 at 16:57
  • I thinks it is not important what inside the scripts, I think it crash because of the import and using it inside the script. I will provide what inside the test scripts tomorrow
    – Majd TL
    Jun 10 '21 at 18:37
  • I added the script @scorpion9979
    – Majd TL
    Jun 11 '21 at 7:45
  • 3
    Potentially related: Time-dependent tests with Hardhat?. As far as I know, OpenZeppelin's test helpers are not compatible with Hardhat and Ethers, because each uses a different big number library. Jun 11 '21 at 9:08
  • thanks @PaulRazvanBerg I used the answer from that question to replace the openzeppelin's test helpers from hardhat.config.js
    – Majd TL
    Jun 14 '21 at 14:17

Not The answer but a workaround:

I was not able to find a solution so I got rid of the openzeppelin's test helpers and used the answer of this question Time-dependent tests with Hardhat? to write my hardhat tasks

task('increaseDays', 'Increase days to currentTime')
  .addParam('days', 'The number of days').setAction(
    async (taskArguments) => {
      const blockBefore = await network.provider.send('eth_getBlockByNumber', ['latest', false]);
      console.log('Timestamp before change', blockBefore.timestamp, new Date(blockBefore.timestamp * 1000));
      await network.provider.send('evm_increaseTime', [86400 * Number(taskArguments.days)]);
      await network.provider.send('evm_mine');
      const blockAfter = await network.provider.send('eth_getBlockByNumber', ['latest', false]);
      console.log('Timestamp after change', blockAfter.timestamp, new Date(blockAfter.timestamp * 1000));
      return true;

task('getTime', 'get current time').setAction(
  async () => {
    const lastestBlock = await network.provider.send('eth_getBlockByNumber', ['latest', false]);
    console.log('Current Timestamp is', lastestBlock.timestamp, new Date(lastestBlock.timestamp * 1000));
    return latestTime.toString();

to Run it

npx hardhat --network ganache getTime

npx hardhat --network ganache increaseDays --days <number-of-days>

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.