I'm trying to test a contract in which functions should require certain timestamps. Parts of code are eliminated for readability:

Here's the problematic part of the contract :


pragma solidity ^0.7.0;

contract PCE_Contract{

constructor(uint256 newStartTime, uint256 PERIOD)
_PERIOD     = PERIOD * 1 days  ;
 initiation = newStartTime     ;

 function enter() payable public
    require(( block.timestamp < (initiation + _PERIOD) ),
     "Period for this function has ended." );
    // do stuff

I'm simulating time using the info found in this article.


const expected_period           = 7     ;
const SECONDS_IN_DAY            = 86400 ;

const Mon_01_Jun_00_00_UTC_2020 = 1590969600; // 06-01-2020 @ 12.00 am (UTC)
const Mon_09_Jun_13_00_UTC_2020 = 1591660800; // 06-09-2020 @ 01.00 pm (UTC) 

contract('PCE_Contract', async (accounts) =>
  before( async () => {
    instance_1 = await PCE_Contract.new(Mon_01_Jun_00_00_UTC_2020 ,expected_period );
    instance_2 = await PCE_Contract.new(Mon_09_Jun_13_00_UTC_2020 ,expected_period );
const expected_address          = accounts[1];

// more tests using instance_1

  it("No entry after period is over, OK", async() => 
    await test_helper.advanceTimeAndBlock(SECONDS_IN_DAY * (expected_period ));

    await instance_2.enter({ from: expected_address, value: String(1e18)});


I expected the test to fail, since _PERIOD time has passed since instance_1. However, in the first test run the test passes. The interesting part is that when I run the test for a second time (which will start from the timestamp 1st test ended at) The test fails (expected behavior).

Why does the 1st test's time-simulation fail even though I can see time has passed in ganache-cli ? How can I fix the issue so that I can test functions in two time periods ?

ganache-cli output for two tests that have time-jump between:

  Transaction: 0xbb9e5b0e3ee1bc266d79ebe169061259c4832cbbc175b63ff1719aa125cf06a3
  Gas usage: 40655
  Block Number: 10
  Block Time: Mon Jun 01 2020 03:00:24 GMT+0300 (GMT+03:00)
  Runtime Error: revert
  Revert reason: ERROR


  Transaction: 0x43578162490a3edf734c17462056f2090b3775a2a90228a0097da53c42319afb
  Gas usage: 112357
  Block Number: 12
  Block Time: Tue Jun 09 2020 03:00:24 GMT+0300 (GMT+03:00) 

Truffle v5.1.49
Ganache CLI v6.12.1 (ganache-core: 2.13.1)
Node v12.18.4

  • Try to check the behavior of ganache returned timestamp with a dedicated “print” at solidity level using remix-ide.
    – Rick Park
    Nov 13 '20 at 12:26

The code sets instance_1 initiation to June 1, and instance_2 initiation to June 9.

In the logs the time has advanced to June 9. So tests should fail for instance_1 but succeed for instance_2.

Since the test checks for instance_2 it is correct for the test to succeed.

  • 1
    Hey thanks a lot. Not sleeping & coffee fried up my brain a bit. Totally missed it out.
    – newbnoob
    Nov 13 '20 at 18:52

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.