Skip to content
Web3 ganache-cli commands that allow moving forward and backward in time
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
test
.gitignore
README.md
blackhole.png
package.json
truffle-config.js
utils.js
yarn.lock

README.md

ganache-time-traveler

A testing toolset that allows developers to write unit tests for the Ethereum blockchain.

Read my Medium Post

Watch my Presentation

Time Contract Example

NOTE:

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

Tool Dependencies

Install

  • npm i ganache-time-traveler

Usage

add require at the top of your tests

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

add the beforeEach and afterEach hooks into your test file

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

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

Breakdown of 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.