Merged latestTime, increaseTime and duration into a time helper. (#1364)
This commit is contained in:
@ -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,
|
||||
};
|
||||
@ -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,
|
||||
};
|
||||
Reference in New Issue
Block a user