const { network } = require('hardhat');
const { promisify } = require('util');
const queue = promisify(setImmediate);
async function countPendingTransactions() {
return parseInt(await network.provider.send('eth_getBlockTransactionCountByNumber', ['pending']));
}
async function batchInBlock(txs) {
try {
await network.provider.send('evm_setAutomine', [false]);
const promises = txs.map(fn => fn());
while (txs.length > (await countPendingTransactions())) {
await queue();
}
await network.provider.send('evm_mine');
const receipts = await Promise.all(promises);
const minedBlocks = new Set(receipts.map(({ receipt }) => receipt.blockNumber));
expect(minedBlocks.size).to.equal(1);
return receipts;
} finally {
await network.provider.send('evm_setAutomine', [true]);
}
}
module.exports = {
countPendingTransactions,
batchInBlock,
};