Truffle develop EVM increase time, EVM mine

Hi everyone,

I am dealing with a situation here:
How will I create a timeTravel function that will increase the time in the EVM and mine blocks?

I tried this:

timeTravelFunction
async function timeTravel(){
        await advanceTime(travelTime);
        await advanceBlock();
        return Promise.resolve(web3.eth.getBlock('latest'));
    }

    
    let travelTime = 1000;
    advanceTime = (travelTime) => {
        return new Promise((resolve, reject) => {
            web3.currentProvider.sendAsync({
                jsonrpc: "2.0",
                method: "evm_increaseTime",
                params: [travelTime],
                id: new Date().getTime()
            }, (err, result) => {
                if (err) { return reject(err); }
                return resolve(result);
            });
        });
    }
    
    advanceBlock = () => {
        return new Promise((resolve, reject) => {
            web3.currentProvider.sendAsync({
                jsonrpc: "2.0",
                method: "evm_mine",
                id: new Date().getTime()
            }, (err, result) => {
                if (err) { return reject(err); }
                const newBlockHash = web3.eth.getBlock('latest').hash;
    
                return resolve(newBlockHash)
            });
        });
    }

but got this error:

EVMIncreaseTimeEVMMine

Any idea @dan-i, @thecil, @filip, @ivan, @Malik ?

1 Like

I think it would be easy for you if you learn how to use the test-helpers from openzeppelin, they have a framework to play with “time” .

https://docs.openzeppelin.com/test-helpers/0.5/

image

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like

Thank you Carlos :pray:,
yea I am right now diving into unit testing :slight_smile:
I can see though that my contract’s basic functionality is also working in the browser, but with some disadvantage in testing locally, as truffle develop is not increasing block.timestamp automatically, but only when a new transaction goes through.
I am quite optimistic I will be able to present some good results soon. :sunglasses:

1 Like

Indeed @thecil , OpenZeppelin Test Helpers RULE!
ZeppelinHelpersTimeIncrease

Everything is working as planned so far :star_struck:

1 Like