Merged latestTime, increaseTime and duration into a time helper. (#1364)

This commit is contained in:
Nicolás Venturo
2018-10-02 07:07:11 -03:00
committed by GitHub
parent 6ae041bca6
commit 34bc709bc2
10 changed files with 84 additions and 95 deletions

View File

@ -1,11 +0,0 @@
const { ethGetBlock } = require('./web3');
// Returns the time of the last mined block in seconds
async function latestTime () {
const block = await ethGetBlock('latest');
return block.timestamp;
}
module.exports = {
latestTime,
};

View File

@ -1,7 +1,13 @@
const { latestTime } = require('./latestTime');
const { ethGetBlock } = require('./web3');
// Returns the time of the last mined block in seconds
async function latest () {
const block = await ethGetBlock('latest');
return block.timestamp;
}
// Increases ganache time by the passed duration in seconds
function increaseTime (duration) {
function increase (duration) {
const id = Date.now();
return new Promise((resolve, reject) => {
@ -31,12 +37,12 @@ function increaseTime (duration) {
*
* @param target time in seconds
*/
async function increaseTimeTo (target) {
const now = (await latestTime());
async function increaseTo (target) {
const now = (await latest());
if (target < now) throw Error(`Cannot increase current time(${now}) to a moment in the past(${target})`);
const diff = target - now;
return increaseTime(diff);
return increase(diff);
}
const duration = {
@ -49,7 +55,8 @@ const duration = {
};
module.exports = {
increaseTime,
increaseTimeTo,
latest,
increase,
increaseTo,
duration,
};