EVM - How Fee Estimate Works
This guide explains how to interpret and use EVM chains (like Ethereum) Gas Fee Estimates. Tatum provides endpoints to simplify your transaction fee estimates.
Parameters Influencing Transaction Acceptance - by the blockchain
To ensure a transaction is accepted by the blockchain, it is important to properly set the GasLimit
and GasPrice
.
- GasLimit: This is the maximum amount of gas units the user is willing to "pay for" their transaction. It sets an upper limit on how much computational work the transaction can perform. If the transaction execution exceeds this limit, it will fail.
- GasPrice: This is the amount of Ether (ETH) the user is willing to pay per unit of gas. It is measured in Gwei (1 Gwei = 10^-9 ETH). Higher gas prices incentivize miners to include the transaction in a block sooner, especially during periods of high network traffic.
- SmartContracts: Standard ERC-20 transfers or NFT mint are factored in and can make use of the Tatum Fee Estimate.
- If a transaction includes executing a complex Smart Contract method (for example recipient address is an SC with many execution steps), depending on the number of internal execution steps, it can make additional computations or calls to other methods that may require a LOT more gas than usual, thus you will be getting gas too low errors in this situation.
- Complex Smart Contract scenarios require you to simulate the transaction (scroll down)
- Example request:
- v3 REST API endpoint. Standard EVM native asset transaction broadcast:
curl --location 'https://api.tatum.io/v3/ethereum/transaction' \
--header 'Content-Type: application/json' \
--header 'x-api-key: {YOUR_API_KEY}' \
--data '{
"to": "0xd2c4b6434410e4af062ee3c280327c8ece11####",
"currency": "ETH",
"amount": "0.0017",
"fromPrivateKey": "****",
"fee": {
"gasLimit": "21000",
"gasPrice": "44" //unit in Gwei! -> Estimate returned "fast": "43698096524",
}
}'
Transactions broadcasted with an insufficient Fee will be rejected by the blockchain with error message looking as follows or simiar:
"Returned error: gas required exceeds allowance (####)"
The Transaction Fee Formula
Total Transaction Fee = GasUsed Γ GasPrice
- GasUsed: The actual amount of gas a transaction consumes. Depends on EVM step execution complexity.
- Fee Estimates with transaction simulation provide a potential value for
GasUsed
via the returned parameterGasLimit
.
- Fee Estimates with transaction simulation provide a potential value for
- GasPrice: The price per unit of gas, where
GasPrice == "baseFee" + priority fee
.
Example Scenario:
Suppose you are looking forward to sending a transaction and the Gas Fee Estimate for the gas required ( GasUsed
) is 21000 units. To be safe, you set the GasLimit
to 25000 units. You check the current gas price and find that a competitive price is 20 Gwei. Therefore, you set:
GasLimit
= 25000GasPrice
= 20 Gwei
If the actual GasUsed
ends up being 21000 units, your transaction fee will be:
21000 Γ 20 Gwei = 420000 Gwei = 0.00042 ETH
If you set the gasLimit
too low, say 20000 units, the transaction will fail with the error "gas required exceeds allowance (20000)
" if it indeed requires 21000 units.
Tatum EVM REST API transaction broadcast endpoints expect GasPrice in "Gwei" units. Additional information is available at the following article.
Tatum Fee Estimate - EVM
Tatum lets you estimate the cost to broadcast a transaction via the Fee Estimate endpoints. See Ethereum v3 REST API endpoint.
Example Fee Estimate request:
curl --location 'https://api.tatum.io/v3/ethereum/gas' \
--header 'Content-Type: application/json' \
--header 'x-api-key: {YOUR_API_KEY}' \
--data '{
"from": "0x0974557990949352a20369fbe6210b3ee64####",
"to": "0xcdfeaf3b4a7beb3cc1fafb7ea34dc9a40b61####",
"amount": "4.5"
}'
//response:
{
"gasLimit": "24126",
"gasPrice": "12000000000", //This unit is in Wei. Conversion to Gwei may be required
"estimations": {
"safe": "11954831382",
"standard": "11954831382",
"fast": "12000000000",
"baseFee": "11741881149"
}
}
From the request, we get the following estimates:
- gasLimit: This is the estimated maximum amount of gas that the transaction should consume. In this case example, it is estimated at
24126 units
. This value determines how much you will allow the blockchain to charge you. - gasPrice: This is the price per unit of gas in wei (1 wei = 10^-18 ETH). In this case, the gas price is 12000000000 wei (
12 Gwei)
.- Estimations:
- safe: A gas price estimation for transactions that are not time-sensitive, offering a lower fee for potentially slower confirmation. Here, it is 11954831382 wei.
- standard: A standard gas price estimation for typical transactions. It matches the "safe" price in this response: 11954831382 wei.
- fast: A gas price estimation for transactions that need to be confirmed quickly. It is the highest in this response: 12000000000 wei.
- baseFee: This is the minimum gas fee required by the network for the transaction. It is 11741881149 wei.
- Estimations:
Tatum Transaction Simulation - EVM
Some transactions may involve transferring or depositing assets from or into a blockchain address that is a Smart Contract. In those scenarios, whether the transferred asset is native or a token, predicting the cost accurately requires simulating the transaction. A transaction simulation will run all the EVM steps and provide a precise fee estimate in terms of Gas Used (GasLimit).
Find more about Transaction Simulation at the following link.
Alternative - RPC trace calls
EVM traces provide a step-by-step record of what happens during the execution of a transaction.
When you trace a transaction, you get back an array with all EVM traces for said transaction. Each step is represented as an individual object in the array. This can help you estimate the Gas Used for complex Smart Contracts involved in a transaction.
Request Example:
curl --location 'https://api.tatum.io/v3/blockchain/node/ethereum-mainnet/' \
--header 'Content-Type: application/json' \
--header 'x-api-key: {YOUR_API_KEY} \
--data '{
"jsonrpc": "2.0",
"id": 1,
"method": "debug_traceCall",
"params": [
{
"from": "0xaC63F4f45cB60034FD6CCdd5205225Fa0e615B51",
"to": "0x4B01F8382d6333C8cC8087Edcf2D282B86B9F007",
"value": "0x1",
"data": ""
},
"latest"
]
}'
//Response:
...
"memory": []
}
],
"gas": 42973,
"failed": false,
"returnValue": ""
}
}
Analysing trace calls requires in-depth blockchain knowledge. We recommend using Transaction Simulation instead.
Good to know
- If a transaction fails, for any reason, the only option is to retry the transaction with a new fee estimate.
- Fee Estimates are only "valid" for a very short time, in seconds.
- Gas Fees can be extremely volatile depending on blockchain congestion, where you may need to retry the transaction with a new fee estimate and higher allocated fees.
- The values for
GasLimit
andGasUsed
are especially relevant with Smart Contract execution: ERC-20, ERC-721, ERC-1155, or compatible.- With Smart Contracts, part of the fee is paid for the code execution for each EVM step.
- When the Smart Contract execution starts consuming gas to run the code, if the GasLimit value is reached the execution will be reverted/fail.
- If a transaction containing a Smart Contract fails after being initiated, the fee is LOST. This means that the transaction stays in the blockchain, but states Smart Contract Failure in the details.
- Tatum Fee Estimate endpoints return the values "
gasPrice
", "safe
", "standard
" and "fast
" with the "baseFee
" already factored in their amount. - Additional information about Gas can be found in the following official article from the Ethereum consortium.
- When a complex Smart Contract is involved in a transfer, we recommend using trace calls or transaction simulators to figure out the transaction flow and calculate manual gas fees with the base from getblockchainfee.
Make sure the blockchain address that send the funds holds sufficient balance to pay for the transaction (native asset). Additional information is available in the following article.
Updated 5 months ago