Skip to content
Web3 ganache-cli commands that allow moving forward and backward in time
JavaScript
Branch: master
Clone or download

Latest commit

Latest commit 010755c Mar 15, 2020

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci set up yarn and ganache Jul 25, 2019
test comment Aug 16, 2019
.gitignore add more descriptive usage Mar 14, 2020
README.md add missing semicolons Mar 14, 2020
blackhole.png add black hole image Nov 19, 2019
package.json Update npm to 1.0.14 Mar 14, 2020
truffle-config.js test setup Jul 23, 2019
utils.js
yarn.lock update yarn lock Aug 10, 2019

README.md

ganache-time-traveler

A ganache utility that simplifies writing time dependent or stateless tests on a local Ethereum blockchain.

NOTE:

  • this only works with ganache-cli
  • this only works locally

Tool Dependencies

Install

  • npm i ganache-time-traveler

Usage

The general outline is to add require at the top of your tests

const timeMachine = require('ganache-time-traveler');

add the beforeEach and afterEach hooks into your truffle test file

contract('Test', async (accounts) =>  {

   let exampleContract;

   beforeEach(async() => {
       let snapshot = await timeMachine.takeSnapshot();
       snapshotId = snapshot['result'];
   };

   afterEach(async() => {
       await timeMachine.revertToSnapshot(snapshotId);
   });

   before('Deploy Contracts', async() => {
       /* DEPLOY CONTRACTS HERE */
       exampleContract = await ExampleContract.new();
   });

   /* ADD TESTS HERE */

   it('Time Dependent Test', async () => {
       await timeMachine.advanceTimeAndBlock(/* SECONDS TO ADVANCE BY */);
   });
});

Methods

advanceTime(<seconds_to_advance_by>)

Advances the time on the blockchain forward. Takes a single parameter, which is the number of seconds to advance by. Note: for advancetime() to take effect, the block must also be mined using advanceBlock(). See advanceTimeAndBlock() to do both.

advanceBlock()

Mines a new block; advances the block forward by 1 block.

advanceBlockAndSetTime(<new_time>)

Advances the block forward by 1 and sets the time to a new time.

advanceTimeAndBlock(<seconds_to_advance_by>)

Advances the block by 1 in addition to advancing the time on the blockchain forward. Takes a single parameter, which is the number of seconds to advance by.

takeSnapshot()

Snapshot the state of the blockchain at the current block. Takes no parameters. Returns the integer id of the snapshot created.

revertToSnapshot(<id_to_revert_to>)

Revert the state of the blockchain to a previous snapshot. Takes a single parameter, which is the snapshot id to revert to.

Resources

You can’t perform that action at this time.