Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ejwessel/ganachetimetraveler
Web3 ganache-cli commands that allow moving forward and backward in time
https://github.com/ejwessel/ganachetimetraveler
developer-tools ethereum testing truffle
Last synced: about 18 hours ago
JSON representation
Web3 ganache-cli commands that allow moving forward and backward in time
- Host: GitHub
- URL: https://github.com/ejwessel/ganachetimetraveler
- Owner: ejwessel
- Created: 2019-06-25T18:17:47.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-07-12T00:39:07.000Z (over 1 year ago)
- Last Synced: 2024-11-03T07:03:48.759Z (5 days ago)
- Topics: developer-tools, ethereum, testing, truffle
- Language: JavaScript
- Homepage:
- Size: 146 KB
- Stars: 48
- Watchers: 2
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ganache-time-traveler
A ganache utility that simplifies writing time dependent or stateless tests on a local Ethereum blockchain.- [Read my Medium Post](https://medium.com/fluidity/standing-the-time-of-test-b906fcc374a9)
- [Watch my Presentation](https://photos.app.goo.gl/6qkd5AN2BthxkY2K6)
- [Time Contract Example](https://github.com/ejwessel/TimeContract)
NOTE:
- this only works with ganache-cli
- this only works locally## Tool Dependencies
- [ganache-cli](https://github.com/trufflesuite/ganache-cli)
- [truffle](https://www.trufflesuite.com/docs/truffle/getting-started/installation)## Install
- `npm i ganache-time-traveler`## Usage
The general outline is to add `require` at the top of your tests
```javascript
const timeMachine = require('ganache-time-traveler');
```add the `beforeEach` and `afterEach` hooks into your truffle test file
```javascript
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()`
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()`
Advances the block forward by 1 and **sets** the time to a new time.### `advanceTimeAndBlock()`
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()`
Revert the state of the blockchain to a previous snapshot. Takes a single parameter, which is the snapshot id to revert to.## Resources
- https://github.com/trufflesuite/ganache-cli
- https://www.trufflesuite.com
- [Icon created by Focus Lab from Noun Project](https://thenounproject.com/search/?q=space%20and%20time&i=547869)