0

I am working on a Hardhat project and need to mine multiple transactions in the same block for testing. To achieve this, I disabled auto-mining and interval mining using the following code:

await network.provider.send("evm_setAutomine", [false]);
await network.provider.send("evm_setIntervalMining", [0]);

After doing this, I noticed that when I try to await a transaction receipt, the code hangs indefinitely:

const fooTx = await myContract.foo();
const fooRc = await fooTx.wait(); // Code hangs here

Can anyone explain why this happens and how to resolve it?

MShakeG
  • 1,603
  • 6
  • 37
  • https://ethereum.stackexchange.com/questions/130471/hardhat-how-to-send-multiple-transactions-to-be-mined-in-one-block Does this help? – Rohan Nero Sep 20 '23 at 21:51

1 Answers1

0

The behavior you're experiencing occurs because you've disabled auto-mining. In a Hardhat environment with auto-mining disabled (i.e. evm_setAutomine set to false), transactions get queued and are not automatically mined in new blocks. As a result, await initTx.wait(); hangs because it's waiting for the transaction to be included in a block, and for a receipt to be generated.

To resolve this, you need to manually mine a block after sending the transaction. You can do this using the evm_mine method as follows:

// Send the transaction
const fooTx = await myContract.foo();

// Manually mine the block to include the transaction await network.provider.send("evm_mine"); // alternatively do "await mine();" // mine is a wrapper around evm_mine and can be imported from "@nomicfoundation/hardhat-network-helpers"

// Now this won't hang as the transaction has been mined const fooRc = await fooTx.wait();

This approach allows you to include multiple transactions in the same block by sending multiple transactions before calling evm_mine. It gives you precise control over transaction ordering within a block provided you've configured "fifo" transaction ordering for your hardhat node.

MShakeG
  • 1,603
  • 6
  • 37