txpool.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. const { network } = require('hardhat');
  2. const { promisify } = require('util');
  3. const queue = promisify(setImmediate);
  4. async function countPendingTransactions() {
  5. return parseInt(await network.provider.send('eth_getBlockTransactionCountByNumber', ['pending']));
  6. }
  7. async function batchInBlock(txs) {
  8. try {
  9. // disable auto-mining
  10. await network.provider.send('evm_setAutomine', [false]);
  11. // send all transactions
  12. const promises = txs.map(fn => fn());
  13. // wait for node to have all pending transactions
  14. while (txs.length > (await countPendingTransactions())) {
  15. await queue();
  16. }
  17. // mine one block
  18. await network.provider.send('evm_mine');
  19. // fetch receipts
  20. const receipts = await Promise.all(promises);
  21. // Sanity check, all tx should be in the same block
  22. const minedBlocks = new Set(receipts.map(({ receipt }) => receipt.blockNumber));
  23. expect(minedBlocks.size).to.equal(1);
  24. return receipts;
  25. } finally {
  26. // enable auto-mining
  27. await network.provider.send('evm_setAutomine', [true]);
  28. }
  29. }
  30. module.exports = {
  31. countPendingTransactions,
  32. batchInBlock,
  33. };