Refactor time helper and remove custom error helper. (#4803)
Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
@ -1,27 +1,39 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { time, mineUpTo } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
const { time, mine, mineUpTo } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
const { mapValues } = require('./iterate');
|
||||
|
||||
const clock = {
|
||||
blocknumber: () => time.latestBlock(),
|
||||
timestamp: () => time.latest(),
|
||||
};
|
||||
const clockFromReceipt = {
|
||||
blocknumber: receipt => Promise.resolve(receipt.blockNumber),
|
||||
timestamp: receipt => ethers.provider.getBlock(receipt.blockNumber).then(block => block.timestamp),
|
||||
};
|
||||
const increaseBy = {
|
||||
blockNumber: mine,
|
||||
timestamp: (delay, mine = true) =>
|
||||
time.latest().then(clock => increaseTo.timestamp(clock + ethers.toNumber(delay), mine)),
|
||||
};
|
||||
const increaseTo = {
|
||||
blocknumber: mineUpTo,
|
||||
timestamp: (to, mine = true) => (mine ? time.increaseTo(to) : time.setNextBlockTimestamp(to)),
|
||||
};
|
||||
const duration = time.duration;
|
||||
|
||||
module.exports = {
|
||||
clock: {
|
||||
blocknumber: () => time.latestBlock(),
|
||||
timestamp: () => time.latest(),
|
||||
},
|
||||
clockFromReceipt: {
|
||||
blocknumber: receipt => Promise.resolve(receipt.blockNumber),
|
||||
timestamp: receipt => ethers.provider.getBlock(receipt.blockNumber).then(block => block.timestamp),
|
||||
},
|
||||
forward: {
|
||||
blocknumber: mineUpTo,
|
||||
timestamp: (to, mine = true) => (mine ? time.increaseTo(to) : time.setNextBlockTimestamp(to)),
|
||||
},
|
||||
duration: time.duration,
|
||||
clock,
|
||||
clockFromReceipt,
|
||||
increaseBy,
|
||||
increaseTo,
|
||||
duration,
|
||||
};
|
||||
|
||||
// TODO: deprecate the old version in favor of this one
|
||||
module.exports.bigint = {
|
||||
clock: mapValues(module.exports.clock, fn => () => fn().then(ethers.toBigInt)),
|
||||
clockFromReceipt: mapValues(module.exports.clockFromReceipt, fn => receipt => fn(receipt).then(ethers.toBigInt)),
|
||||
forward: module.exports.forward,
|
||||
duration: mapValues(module.exports.duration, fn => n => ethers.toBigInt(fn(ethers.toNumber(n)))),
|
||||
clock: mapValues(clock, fn => () => fn().then(ethers.toBigInt)),
|
||||
clockFromReceipt: mapValues(clockFromReceipt, fn => receipt => fn(receipt).then(ethers.toBigInt)),
|
||||
increaseBy: increaseBy,
|
||||
increaseTo: increaseTo,
|
||||
duration: mapValues(duration, fn => n => ethers.toBigInt(fn(ethers.toNumber(n)))),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user