Estimate Gas Dynamically: Best Practices for Smart Contract Development

Hedera TeamHedera Team
4 min read

By Ed Marquez

Gas is the fuel of smart contract computations, and underestimating it can bring your dApp to a halt. Locking in a hardcoded gas limit might get you across the finish line once (or a few times), but as your contracts grow or conditions shift, that same fixed value can cause unexpected failures in the future.

This article covers why hardcoded gas limits are fragile, how to use dynamic estimation with common EVM tools, practical tips like adding buffers and retry logic, and specific guidance for developers using both JSON-RPC and HAPI (Hedera API).

Why Hardcoded Gas Limits Break

Hardcoding a gas limit is like flying blind. You are guessing how much fuel you’ll need without checking the conditions. If your guess is too low, the transaction hits the gas ceiling and fails, resulting in wasted gas fees without achieving the intended outcome. Even if the hardcoded values worked once, changes in network state or contract logic can turn a “safe” limit into a silent failure point. To make things more specific, some protocol upgrades (e.g., Ethereum’s EIP-1884) can reprice operations, breaking code that assumed a fixed gas amount.

Rather than hoping a constant value remains valid, it’s best to estimate gas needs at runtime.

Use Dynamic Gas Estimation Tools

By "asking" the network first, you avoid flying blind and get a reliable baseline for each transaction. EVM tooling makes dynamic gas estimation simple. For instance:

  • Hardhat & Ethers.js: Default to gas: "auto", which calls estimateGas() and applies an optional multiplier (gasMultiplier).

  • Web3.js: Use web3.eth.estimateGas(txParams) to simulate and return the expected gas usage.

  • Remix IDE: Estimates gas for you in the UI when you run transactions. Just click the gas field and let it fill in.

Practical Tips: Buffers & Retry Logic

Consider the following steps for resilient deployments:

  1. Estimate First: Always call estimateGas() (or let your framework do it for you).

  2. Add a Safety Buffer: Add ~20–25% extra to cover state changes or estimation variance (e.g, gasMultiplier: 1.2 with Hardhat and Ethers.js). On Hedera, up to 20% of unused gas is refunded, so you pay for at least 80% of the gas limit you specify. This incentivizes accurate gas estimates.

  3. Implement Retry Logic: Catch out-of-gas errors, log them, and retry with a higher limit (e.g., increase by another 20%). A couple of retries usually result in a successful transaction.

  4. Monitor and adjust: Track the actual gas used in production, then tweak your buffer or estimation strategy as your contracts evolve.

Estimates may not always be perfect, but this approach helps avoid surprises in your application with out-of-gas failures.

Working with JSON-RPC & HAPI on Hedera

Hedera is EVM-compatible and open source, so you can use standard EVM tools that you're already familiar with.

Hedera offers capabilities beyond the EVM, so it provides SDKs in JavaScript, Java, Go, Rust, and other languages for developers who want to incorporate web3 components in their applications without having to learn or code smart contracts.

Keep in mind that you can observe the Gas Limit, Gas Used, and Gas Consumed in the Contract Results section of a transaction on HashScan. Alternatively, you can use contractCallResult.gasUsed in the SDK.

Gas Hash Scan

In Summary...

Hardcoding gas limits sets you up for brittle, failure-prone deployments. The optimal approach, which applies to all EVM networks, is to estimate dynamically, apply a buffer, and build in a safety net. Use tools like Hardhat, Remix, or Web3.js to call eth_estimateGas and add a 20–25% buffer to stay ahead of shifting network conditions. When using HAPI on Hedera, you can accurately estimate gas using the MirrorNodeContractEstimateQuery via the SDK or by directly calling the /contracts/call endpoint of the Mirror Node REST API.

These best practices not only prevent costly failures but also future-proof your dApps as they evolve and scale. Whether you're building with smart contracts on Hedera or another EVM chain, dynamic gas estimation leads to more reliable contract execution.

Ready to put it into practice?

Try deploying your contract on Hedera in seconds with the Hedera Contract Builder. Be sure to join the Hedera Discord server to stay in touch with experts and the community!

0
Subscribe to my newsletter

Read articles from Hedera Team directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hedera Team
Hedera Team