/
time.js
74 lines (62 loc) · 2.03 KB
/
time.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const { BN } = require('./setup');
const { promisify } = require('util');
function advanceBlock () {
return promisify(web3.currentProvider.send.bind(web3.currentProvider))({
jsonrpc: '2.0',
method: 'evm_mine',
});
}
// Returns the time of the last mined block in seconds
async function latest () {
const block = await web3.eth.getBlock('latest');
return new BN(block.timestamp);
}
async function latestBlock () {
const block = await web3.eth.getBlock('latest');
return new BN(block.number);
}
// Increases ganache time by the passed duration in seconds
async function increase (duration) {
if (!BN.isBN(duration)) {
duration = new BN(duration);
}
if (duration.isNeg()) throw Error(`Cannot increase time by a negative amount (${duration})`);
await promisify(web3.currentProvider.send.bind(web3.currentProvider))({
jsonrpc: '2.0',
method: 'evm_increaseTime',
params: [duration.toNumber()],
});
await advanceBlock();
}
/**
* Beware that due to the need of calling two separate ganache methods and rpc calls overhead
* it's hard to increase time precisely to a target point so design your test to tolerate
* small fluctuations from time to time.
*
* @param target time in seconds
*/
async function increaseTo (target) {
if (!BN.isBN(target)) {
target = new BN(target);
}
const now = (await latest());
if (target.lt(now)) throw Error(`Cannot increase current time (${now}) to a moment in the past (${target})`);
const diff = target.sub(now);
return increase(diff);
}
const duration = {
seconds: function (val) { return new BN(val); },
minutes: function (val) { return new BN(val).mul(this.seconds('60')); },
hours: function (val) { return new BN(val).mul(this.minutes('60')); },
days: function (val) { return new BN(val).mul(this.hours('24')); },
weeks: function (val) { return new BN(val).mul(this.days('7')); },
years: function (val) { return new BN(val).mul(this.days('365')); },
};
module.exports = {
advanceBlock,
latest,
latestBlock,
increase,
increaseTo,
duration,
};