See Virtual TestNets in action! ⚡ Replace slow public testnets in dapp development. Join our Office Hours

Smart Contracts

How Tenderly Enables 100% Accurate Eth Gas Estimations in Transaction Simulations

Learn how Tenderly enables 100% accurate gas estimations in a single run and prevents internal transactions from failing with out-of-gas errors. Start getting the most precise gas estimates with Tenderly Node or Simulation API.

Nebojsa Urosevic
Nebojsa Urosevic
Jun 8, 2023 · 4 min read

In this post

100% Accurate Eth Gas Estimations

The gas estimation endpoint is a crucial component in Web3 because it enables the estimation of the amount of gas required for executing a transaction. Gas represents the computational and storage resources required to execute smart contracts and perform operations on the Ethereum network.

However, obtaining accurate gas estimates is essential for ensuring that transactions have enough gas to complete successfully without running out of computational resources. Without sufficient gas, both Web3 developers and end users can experience transaction failures and waste their resources on reverted transactions. On the other hand, providing more gas than necessary can result in unnecessary fees and inefficiencies.

So, evaluating gas usage precisely is essential for warning your users that their transactions may spend more money than actually needed. However, the current algorithm leaves room for improvement, so we decided to explore an alternative solution. And we’ve found a way to achieve 100% gas estimation accuracy and bring it to Web3 developers.

Want to start getting 100% accurate gas estimates? Just head over to Tenderly Web3 Gateway and run tenderly_estimateGas.

How are gas estimates currently calculated on Ethereum?

The standard implementation of gas estimation on the Ethereum network typically utilizes a binary search algorithm. The goal of this algorithm is to find the optimal amount of gas required for a transaction to be executed successfully.

The binary search nature of this algorithm allows it to quickly converge on the optimal gas limit. By successively narrowing down the range of possible gas limits, the algorithm reduces the number of computations required to find the correct gas estimation from O(N) to O(logN), N being the maximum amount of gas a transaction can have in a block.

However, the nature of the current algorithm has certain downsides:

  • The process is complex and resource-consuming, requiring around 25 transaction executions to estimate the optimal gas limit.
  • Computing gas accurately is also challenging in cases where smart contracts encounter unsuccessful internal transactions in their logic. This means that you still exhaust your resources, yet your transactions get reverted.
  • Gas estimation inaccuracy has a negative effect on end users who may often underpay or overpay for gas, leaving them with unnecessary expenses and unsuccessful transactions.
  • Finally, the network itself doesn’t benefit from a great number of reverted transactions since it still needs to execute them.
ethereum gas estimator
The binary algorithm for calculating gas estimates 

Calculating precise Ethereum gas estimates in a single run

At Tenderly, we’ve always wanted to support the growth and adoption of blockchain technology by supporting innovative teams building Web3 solutions. So, we’re always looking for ways to drive the space forward and provide solutions to the obstacles standing in the way of smart contract engineers.

So, we’re excited to have found a solution capable of finding gas estimates in a single run, eliminating the need for a binary search algorithm. Our solution significantly reduces the computational complexity from O(logN) to O(1), providing a significant advancement in gas estimation efficiency.

By leveraging this new approach, we can estimate the optimal gas limit required for transaction execution without the iterative process of raising or lowering it. This not only saves computational resources but also streamlines the gas estimation process, allowing for faster and more accurate transaction execution on the Ethereum network.

web3 estimate gas with tenderly
Calculating gas estimates in a single run with Tenderly

Preventing internal transactions from failing with “out of gas” errors

To further enhance gas estimations on Ethereum, we implemented a system that also tracks internal transaction failures caused by “out of gas” errors. Additionally, we revised the condition for lowering the gas amount. Rather than increasing the gas limit only when a simulation fails due to an out-of-gas error, we also bump it if any internal transaction fails because of the same issue.

How to get the most accurate gas estimates

In Tenderly, there are two ways to obtain 100% gas estimates for your or your users’ transactions:

1. Run tenderly_estimateGas in Tenderly Node: You can use our node as a service to get comprehensive and accurate gas estimates.

Just head over to the Tenderly Dashboard, open the Node page, and configure the RPC Request Builder to send your tenderly_estimateGas request.

Running eth_estimateGas in Tenderly Web3 Gateway
Running eth_estimateGas in Tenderly Web3 Gateway

2. Simulate transactions using Transaction Simulator: Whether you’re running simulations from the Tenderly Dashboard or you’ve integrated Simulation API, you can get 100% precise gas estimates prior to sending transactions on-chain. Plus, with Simulation API, you can expose this information to your dapp or wallet users, providing them with exact gas estimates and helping them save funds.

When running simulations, just make sure to specify estimate_gas and set it to true. Then, you can check the gas field in the API response for the precise gas estimate.

After getting the exact gas estimate for your smart contract, you can determine whether it spends too much gas. You can head over to Tenderly Gas Profiler and analyze how much gas each function call consumes. This way, you can identify gas-intensive functions and optimize them to reduce your smart contract’s gas usage.

Get 100% accurate gas estimates in Web3

With our new method of calculating the most accurate gas estimates, you can improve the user experience in your dapps. Just run tenderly_estimateGas via Web3 Gateway and eliminate the risk of getting an incorrect response and, ultimately, failed transactions.

You can even offer the same information to your users by integrating Tenderly Simulation API. Your dapp or wallet users can always know the exact gas cost of their transactions even if they don’t come from technical backgrounds. In return, this will also help build confidence in the industry.

So, head over to the Tenderly Dashboard and start getting the most accurate gas estimation in Web3.

Use Tenderly Node Extensions to build custom RPC endpoints that fit your dapp without changing its existing logic. Optimize dapp performance, improve user experience, and get greater control and flexibility both on-chain and during development.

Try Tenderly Node Extensions