dsa-connectors/test/ubiquity/utils.js

57 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-10-11 16:47:51 +00:00
const hre = require("hardhat");
const hardhatConfig = require("../../hardhat.config");
async function forkReset(blockNumber) {
await hre.network.provider.request({
method: "hardhat_reset",
params: [
{
forking: {
jsonRpcUrl: hardhatConfig.networks.hardhat.forking.url,
2021-10-16 09:21:41 +00:00
blockNumber
}
}
]
2021-10-11 16:47:51 +00:00
});
}
2021-10-16 09:21:41 +00:00
async function mineBlock(timestamp) {
await network.provider.request({
method: "evm_mine",
params: [timestamp]
});
}
2021-10-11 16:47:51 +00:00
async function sendEth(from, to, amount) {
await from.sendTransaction({
to: to,
2021-10-16 09:21:41 +00:00
value: ethers.BigNumber.from(10).pow(18).mul(amount)
});
}
async function mineNBlock(blockCount, secondsBetweenBlock) {
const blockBefore = await ethers.provider.getBlock("latest");
const maxMinedBlockPerBatch = 1000;
let blockToMine = blockCount;
let blockTime = blockBefore.timestamp;
while (blockToMine > maxMinedBlockPerBatch) {
// eslint-disable-next-line @typescript-eslint/no-loop-func
const minings = [...Array(maxMinedBlockPerBatch).keys()].map((_v, i) => {
const newTs = blockTime + i + (secondsBetweenBlock || 1);
return mineBlock(newTs);
});
// eslint-disable-next-line no-await-in-loop
await Promise.all(minings);
blockToMine -= maxMinedBlockPerBatch;
blockTime = blockTime + maxMinedBlockPerBatch - 1 + maxMinedBlockPerBatch * (secondsBetweenBlock || 1);
}
const minings = [...Array(blockToMine).keys()].map((_v, i) => {
const newTs = blockTime + i + (secondsBetweenBlock || 1);
return mineBlock(newTs);
2021-10-11 16:47:51 +00:00
});
2021-10-16 09:21:41 +00:00
// eslint-disable-next-line no-await-in-loop
await Promise.all(minings);
2021-10-11 16:47:51 +00:00
}
2021-10-16 09:21:41 +00:00
module.exports = { forkReset, sendEth, mineNBlock };