Diagnosing Gas Estimation and Insufficient Funds Errors in Hardhat
Learn how to distinguish between actual funding shortages and masked Solidity reverts when encountering 'Insufficient funds' and 'Gas estimation failed' errors in Hardhat.
27 Jun 2026, 09:47 UTC

The Problem: Silent Reverts and Funding Gaps
When deploying smart contracts or executing scripts with Hardhat, you will frequently encounter two deceptive errors: ProviderError: Insufficient funds and Gas estimation failed. While the first seems straightforward, the second is often a "masking error"—Hardhat fails to estimate gas because the transaction would revert on-chain, but the provider reports the estimation failure instead of the actual Solidity revert reason.
Diagnostic Matrix
Use this table to identify the likely cause based on the error message and the network you are targeting.
| Error Message | Network | Likely Cause | Diagnostic Signal |
|---|---|---|---|
| Insufficient funds | Testnet/Mainnet | Wallet balance < (Value + Gas) | Balance check via Block Explorer shows 0 or low ETH |
| Gas estimation failed | Any | Contract Logic Revert | Transaction fails immediately during simulation |
| Gas estimation failed | Local/Testnet | Constructor Gas Limit | Contract is exceptionally large or complex |
| Insufficient funds | Hardhat Network | Wrong Account Index | Deploying with an account not in the default 20-account set |
Step-by-Step Resolution Path
1. Verify Account Liquidity
Before debugging code, confirm the account specified in your hardhat.config.js actually holds the native currency. If you are using a public testnet (like Sepolia), verify the address on a block explorer.
To check the balance programmatically within a Hardhat script, use the following check:
// Run via: npx hardhat run scripts/check-balance.js --network
const { ethers } = require("hardhat");
async function main() {
const [deployer] = await ethers.getSigners();
const balance = await ethers.provider.getBalance(deployer.address);
console.log(`Account: ${deployer.address}`);
console.log(`Balance: ${ethers.formatEther(balance)} ETH`);
}
main().catch((error) => console.error(error));
2. Isolate Logic Reverts from Gas Issues
If you see Gas estimation failed, the EVM is likely hitting a require() failure or a revert statement during the simulation. To find the actual cause, move the execution to the local Hardhat Network, which provides more verbose stack traces.
- Run
npx hardhat nodein a separate terminal. - Execute your script against the local node:
npx hardhat run scripts/deploy.js --network localhost. - If the error persists, use
console.loginside your Solidity contract (imported viahardhat/console.sol) to trace exactly which line triggers the revert.
3. Adjust Gas Limits for Complex Constructors
Some complex contracts exceed the default gas estimation. If you are certain the logic is correct but estimation still fails, manually specify a gasLimit in your deployment options. This bypasses the estimation phase and forces the transaction through.
// Example: Forcing a gas limit during deployment
const Contract = await ethers.getContractFactory("MyComplexContract");
const contract = await Contract.deploy(arg1, arg2, {
gasLimit: 5000000 // Manually set to 5 million gas
});
Configuration Safety and Risks
When fixing these issues, avoid the temptation to hardcode private keys in hardhat.config.js to quickly switch accounts. Use a .env file and the dotenv package.
Risk: Manually setting a high gasLimit on a public network does not guarantee success; if the contract logic is flawed, you will still lose the gas spent up to the point of the revert.
Verification and Rollback
To verify the fix, check the transaction status on the network provider. A successful deployment will return a contract address and a transaction hash with a status of Success.
Rollback: Since deployment changes the state of the blockchain, there is no "undo" for a deployed contract. To rollback a failed configuration, revert your hardhat.config.js changes to the previous stable commit and clear your artifacts/ and cache/ folders using npx hardhat clean.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.