- 1.
evm_increaseTime: increases the time for the next block.
- 2.
evm_mine: mines a new block.
You don't even need a Tardis or a DeLorean for this kind of time travel.
Let me explain how these functions work:
Every time a new block gets mined, the miner adds a timestamp to it. Let's say the transactions that created our zombies got mined in block 5.
Next, we call evm_increaseTime but, since the blockchain is immutable, there is no way of modifying an existing block. So, when the contract checks the time, it will not be increased.
If we run evm_mine, block number 6 gets mined (and timestamped) which means that, when we put the zombies to fight, the smart contract will "see" that a day has passed.
Putting it together, we can fix our test by traveling through time as follows:
await web3.currentProvider.sendAsync({
jsonrpc: "2.0",
method: "evm_increaseTime",
params: [86400], // there are 86400 seconds in a day
id: new Date().getTime()
}, () => { });
web3.currentProvider.send({
jsonrpc: '2.0',
method: 'evm_mine',
params: [],
id: new Date().getTime()
});