Moved advanceBlock to time. (#1523)

* Added advanceBlock to time, moved tests around.

* Removed the standalone advanceBlock.

* Removed the 'id' field

* Fixed linter error.

* Removed the 'latest' test, since it only works if time hasn't been fast-forwarded.

* Removed .only directive.
This commit is contained in:
Nicolás Venturo
2018-11-29 18:23:20 -03:00
committed by GitHub
parent 7ef2730506
commit 6fd0010325
10 changed files with 63 additions and 98 deletions

View File

@ -1,4 +1,12 @@
const { ethGetBlock } = require('./web3');
const { promisify } = require('util');
function advanceBlock () {
return promisify(web3.currentProvider.sendAsync)({
jsonrpc: '2.0',
method: 'evm_mine',
});
}
// Returns the time of the last mined block in seconds
async function latest () {
@ -7,29 +15,16 @@ async function latest () {
}
// Increases ganache time by the passed duration in seconds
function increase (duration) {
const id = Date.now();
async function increase (duration) {
if (duration < 0) throw Error(`Cannot increase time by a negative amount (${duration})`);
return new Promise((resolve, reject) => {
if (duration < 0) throw Error(`Cannot increase time by a negative amount (${duration})`);
web3.currentProvider.sendAsync({
jsonrpc: '2.0',
method: 'evm_increaseTime',
params: [duration],
id: id,
}, err1 => {
if (err1) return reject(err1);
web3.currentProvider.sendAsync({
jsonrpc: '2.0',
method: 'evm_mine',
id: id + 1,
}, (err2, res) => {
return err2 ? reject(err2) : resolve(res);
});
});
await promisify(web3.currentProvider.sendAsync)({
jsonrpc: '2.0',
method: 'evm_increaseTime',
params: [duration],
});
await advanceBlock();
}
/**
@ -57,6 +52,7 @@ const duration = {
};
module.exports = {
advanceBlock,
latest,
increase,
increaseTo,