dsa-connectors/test/mainnet/ubiquity/utils.ts

56 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-12-12 22:37:25 +00:00
import hre, { ethers, network } from "hardhat";
import hardhatConfig from "../../../hardhat.config";
2021-10-11 16:47:51 +00:00
2021-12-12 22:37:25 +00:00
export async function forkReset(blockNumber: any) {
2021-10-11 16:47:51 +00:00
await hre.network.provider.request({
method: "hardhat_reset",
params: [
{
forking: {
2021-12-12 22:37:25 +00:00
// @ts-ignore
2021-10-11 16:47:51 +00:00
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
2021-12-12 22:37:25 +00:00
export async function mineBlock(timestamp: any) {
2021-10-16 09:21:41 +00:00
await network.provider.request({
method: "evm_mine",
params: [timestamp]
});
}
2021-12-12 22:37:25 +00:00
export async function sendEth(from: any, to: any, amount: any) {
2021-10-11 16:47:51 +00:00
await from.sendTransaction({
to: to,
2021-10-16 09:21:41 +00:00
value: ethers.BigNumber.from(10).pow(18).mul(amount)
});
}
2021-12-12 22:37:25 +00:00
export async function mineNBlock(blockCount: any, secondsBetweenBlock: any) {
2021-10-16 09:21:41 +00:00
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
2021-12-12 22:37:25 +00:00
const minings: any = [maxMinedBlockPerBatch].map((_v, i) => {
2021-10-16 09:21:41 +00:00
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);
}
2021-12-12 22:37:25 +00:00
const minings = [blockToMine].map((_v, i) => {
2021-10-16 09:21:41 +00:00
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
}