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?

1

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.