time.js 1.0 KB

123456789101112131415161718192021222324252627
  1. const { ethers } = require('hardhat');
  2. const { time, mineUpTo } = require('@nomicfoundation/hardhat-network-helpers');
  3. const { mapValues } = require('./iterate');
  4. module.exports = {
  5. clock: {
  6. blocknumber: () => time.latestBlock(),
  7. timestamp: () => time.latest(),
  8. },
  9. clockFromReceipt: {
  10. blocknumber: receipt => Promise.resolve(receipt.blockNumber),
  11. timestamp: receipt => ethers.provider.getBlock(receipt.blockNumber).then(block => block.timestamp),
  12. },
  13. forward: {
  14. blocknumber: mineUpTo,
  15. timestamp: (to, mine = true) => (mine ? time.increaseTo(to) : time.setNextBlockTimestamp(to)),
  16. },
  17. duration: time.duration,
  18. };
  19. // TODO: deprecate the old version in favor of this one
  20. module.exports.bigint = {
  21. clock: mapValues(module.exports.clock, fn => () => fn().then(ethers.toBigInt)),
  22. clockFromReceipt: mapValues(module.exports.clockFromReceipt, fn => receipt => fn(receipt).then(ethers.toBigInt)),
  23. forward: module.exports.forward,
  24. duration: mapValues(module.exports.duration, fn => n => ethers.toBigInt(fn(ethers.toNumber(n)))),
  25. };