Ethereum Gas: understanding gas price and gas limit

Ethereum gas price

And how Crypto APIs calculates it for you

What is gas?

Gas is the unit of measuring the computational work of running transactions, or smart contracts, in the Ethereum network done by miners. 

To make a transaction on the Ethereum network, whether you are doing a simple wallet to wallet transaction, sending tokens or interacting with a smart contract, requires for a miner to pick that transaction and process it. The more complex is the transaction you are making, the more gas you will need for it to be mined.

These transaction costs (transaction fees) are always paid in ETH, the native currency on the Ethereum blockchain.  

How is a transaction fee calculated?

A transaction fee is calculated by multiplying the consumed gas by the gas price.

Gas limit is the maximum amount of gas you’re willing to pay for a transaction. As mentioned above, more complex transactions need much higher gas limits. For example, executing a smart contract requires more computational power, therefore the gas limit will have to be higher than the one required by a standard transaction. A standard ETH transfer has a gas limit of 21,000 units of gas. 

When the gas limit you set is lower than the gas required, the transaction fails, but since it’s still been mined you lose the gas used up to that point. When the gas limit is higher than needed, only the required amount of gas will be considered and the unused gas will be refunded. It’s good to keep in mind that, if all other variables are the same, a transaction with an unnecessarily high gas limit could be less appealing to miners. Therefore it doesn’t make sense to put a high limit even when only the needed amount of gas is deducted.

Gas price is the amount of gwei you are willing to pay per unit of gas. A higher price will get your transaction mined faster. A cheap gas price might leave you with an unmined transaction, as there is no financial benefit for the miner to pick it.

To make an analogy and understand better how it works, imagine that:

  • Gas consumption is the amount of petrol that you need to get from point A to point B in a car. The further you need to go (the more complex the transaction), the more gas you will need. If you need 21,000 liters of petrol to get to point B, but you only have 20,000 you won’t complete your journey. If you have 22,000 litres of petrol, only 21,000 will be consumed.
  • Gas limit is the amount of petrol you will put in the tank. 
  • Gas price is the price you pay for each litre of petrol. The higher the cost of petrol per litre, the faster the car will get to its destination. The lower you pay for your fuel, the slower the car will make it to its destination.

Now that you know how Gas works, you can forget about it.

Why? Because Crypto APIs have created 2 endpoints that will relieve you of the burden of calculating fees for your transactions.

Estimate Transaction GasLimit Endpoint – This endpoint will help you estimate the gas needed for a successful transaction. This will be particularly useful when sending ETH to smart contracts or making a transaction with additional data in it, which is more complex to calculate.

Transactions GasPrice Endpoint –  This is the real jewel of out gas-related endpoints. The endpoint takes in consideration all the transactions and related gas prices included in the last 1,500 blocks. This is approximately all transactions in the last 5-6 hours. An algorithm then calculates different parameters, which you can use for your transactions:

  • min – shows the lowest gas price that was accepted by the miners in the last 1,500 blocks
  • max – is the highest gas price that was paid for a transaction included in the last 1,500 blocks.
  • average – is the average gas price of all transactions included in the last 1,500 blocks.
  • recommended – based on the information of transactions and gas prices in the last 1,500 blocks, our algorithm calculates a gas price that will ensure you to do the cheapest, fastest transactions.

To find out more about Crypto APIs GasPrice and GasLimit endpoints, check out our documentation.