OpenZeppelin Test Helpers time

In my contract I have a function that is set to be active after a particular block.timestamp has elapsed.
so in my test, I used

describe("do something at time stated", ()=>{

it("revert after time", async()=>{
const stillValid = await contractInstance.showAtTime() // returns timestamp 7 days from now
await time.increase(stillValid);

await expectRevert(await contractInstance.doAtTime(), "time over")


this above function returned error Error: Cannot increase current time (6465161954) to a moment in the past (1614832558)
after several tries I discovered on every call of increaseTo(timestamp) moves the current block time ahead.

:computer: Environment


:1234: Code to reproduce

1 Like

Hi @makerz,

Can you share a cut down version of your contract with the showAtTime and doAtTime functions?
I assume that the time is passed in as a parameter to the constructor?

Are you using Truffle’s internal network or are you testing on ganache-cli?

1 Like

The time is set like so in the contract after the first deposit:

   contract TimeManager{
            uint256 openPeriod;
            uint256 showAtTime;
            uint256 value;

    function initialize(uint256 _openPeriod) external {
    openPeriod = _openPeriod;

    function deposit(uint256 _value) external {
    value = _value;
    showAtTime= block.timestamp.add(openPerion);`Preformatted text`

    function doAtTime() external {
    require(block.timestamp>= showAtTime,"time over");
    ///other logic...
1 Like

Hi @makerz,

In your test you are using increase but using the value of showAtTime, I think you actually want to use increaseTo

You could have a look at the tests in OpenZeppelin Contracts, such as for TokenTimelock

Thank you @abcoathup I found a way to run the test. I believe it would be great to have helper functions to capture the timestamp before the test and reset the timestamp to the current timestamp after each “it” block. Thank you for this reference :raised_hands:

1 Like

Hi @makerz,

We can’t go backwards in time onchain, so can’t reset the timestamp.

I would recommend passing in the current time (with an offset if required) in a beforeEach in the tests where the contract is created.

1 Like