0

Hardhat's test network, defaultNetwork: "hardhat" in config, automatically mines a block every time a transaction is sent by default.

This is problematic for situations where I want to test functionalities where multiple transactions need to be in the same block.

How can I set/configure hardhat to not automatically mine transactions during tests and control when the test network actually mines a block?

1
  • 1
    In order to do it on a single test level and not the entire project configuration you can use ‘evm_setAutomine‘
    – Julissa DC
    Jun 8, 2022 at 16:58

1 Answer 1

1

Set a mining mode in the configuration to tell it how to mine.

Documentation states:

You can configure the mining behavior under your Hardhat Network settings:

networks: {
  hardhat: {
    mining: {
      auto: false,
      interval: 5000
    }
  }
}

In this example, automining is disabled and interval mining is set so that a new block is generated every 5 seconds. You can also configure interval mining to generate a new block after a random delay:

networks: {
  hardhat: {
    mining: {
      auto: false,
      interval: [3000, 6000]
    }
  }
}

In this case, a new block will be mined after a random delay of between 3 and 6 seconds. For example, the first block could be mined after 4 seconds, the second block 5.5 seconds after that, and so on.

Manual mining

You can disable both mining modes like this:

networks: {
  hardhat: {
    mining: {
      auto: false,
      interval: 0
    }
  }
}

This means that no new blocks will be mined by the Hardhat Network, but you can manually mine new blocks using the evm_mine RPC method. This will generate a new block that will include as many pending transactions as possible.

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.