Giter Club home page Giter Club logo

ganachetimetraveler's Introduction

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

ganachetimetraveler's People

Contributors

brianwchou avatar chanhosuh avatar codenutt avatar dependabot[bot] avatar ejwessel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ganachetimetraveler's Issues

truffle-config.js should stay as default settings

This module only works with ganache, why don't we keep truffle-config.js to default settings since we're not planning to use it with other ethereum clients. The only thing I can think of for opening the developer port is the case that truffle tests do not run on the deterministic version of ganache.

Clarify Medium article and Readme

For somebody who is not technical it should be very clear the value this brings.
It's packaging the tools that allow you to move time which in turn can allow you to write more isolated test.

How to and README

I'd like more information on how to use this. As specific as possible.

  • how do I add to package.json
  • what I need to do to include in tests: require(...)
  • what I need to do to use in tests: beforeEach and afterEach

`await Promise.all` causes deadlock when measuring test coverage

Not entirely sure if the issue is related to ganache-time-tralever or sol-coverage, but I said to start here.

Say you have this in "beforeEach":

await Promise.all(
  new Array(5).fill().map(() => {
    return traveler.advanceBlock();
  }),
);

Testing works fine (truffle test). However, when I plug sol-coverage, things stop working. The coverage process ends up in standstill, doing nothing after a test suite with the code above gets executed.

I can solve this easily by using a traditional for loop, or just listing the block advancements inline. But I wanted to flag the issue, in case you may know what's going on. I suspect that "Promise.all" batches promises differently and ganache ends up in a recursive loop somehow.

TypeScript support

It would be great to have a typescript definition file so this could be used in TS tests.

Tests

We need tests and information on how to run said test
I'd assume that we want to show time moving forward and time moving backward.

update evm_mine to mine to particular time

Per ganache-cli documents

evm_mine : Force a block to be mined. Takes one optional parameter, which is the timestamp a block should setup as the mining time. Mines a block independent of whether or not mining is started or stopped.

# Ex: new Date("2009-01-03T18:15:05+00:00").getTime()
curl -H "Content-Type: application/json" -X POST --data \
        '{"id":1337,"jsonrpc":"2.0","method":"evm_mine","params":[1231006505000]}' \
        http://localhost:8545

per Paul Berg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.