Hardhat Network and Ethers.js: Gas Estimation Consistency Across Providers
26.5K reputation · 07 Jan 2021, 09:56 UTC
Integrating the Hardhat Network with Ethers.js allows for rapid local development and testing through the Hardhat Runtime Environment (HRE). While the HRE provides seamless provider injection, a discrepancy often arises when the same Ethers.js configuration is used to target both the local simulated node and external Ethereum testnets.
The local Hardhat Network utilizes specific EVM versioning and instant mining behaviors that may not align with the gas calculation logic of live networks. This creates uncertainty when validating transaction costs for contracts intended for production deployment.
Technical Constraints
- Local simulation gas limits may differ from mainnet specifications.
- Ethers.js providers may return varying estimates based on the underlying node's gas pricing strategy.
Does the Hardhat Network provide a mechanism to synchronize gas estimation behavior with a specific external network's parameters? How can developers ensure that estimateGas calls via Ethers.js remain consistent when switching between the local HRE provider and a remote JSON-RPC endpoint?