3

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

require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-truffle5");
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..', () => { expect(true).to.true; }); });

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?

Majd TL
  • 3,217
  • 3
  • 17
  • 36

1 Answers1

0

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>

Majd TL
  • 3,217
  • 3
  • 17
  • 36