New to blockchain software development? Read my beginners guide here

Ethereum gas guide

Created on June 2022 • Tags: ethereum

A guide to how gas works on Ethereum

Table of Contents for Ethereum gas guide


Ethereum is sometimes known as the ‘world computer’. It is a blockchain that has smart contracts. When you interact with the blockchain (by adding a transaction), there is some movement of Ether and if you interact with a smart contract, some computation that happens.

To run those transactions, you have to pay ‘gas’.

Each transaction has a gas limit (max amount of gas to spend) - a mix of what you decide when creating the transaction, and also hard limits set by the protocol.

You can think of it as a mix of the amount of computation that has to be run (e.g. moving data around in memory or calculating math operataions), the size of any smart contracts that you deploy, and the size/access of stored data on the blockchain.

How gas is measured on Ethereum

You pay gas to run a transaction from your ether balance. If you have a balance of 1 ETH, and you send it, you will need to send a bit more than 1 ETH to cover gas fees.

Gas is measured in gwei. 1 gwei is one-billionth of one ETH.

The gas price (in gwei) is the cost to execute one unit of gas.

A transaction always has a minimum of 21,000 gas (so the minimum would be 21000 * the price in gwei, but you also have to add some extra fees (see below)). If you interact with smart contracts, they can consume a lot of gas if they have a lot of complex code to run, so the total cost for all that gas can be expensive.

How gas fees are calculated

Users (such as you) create a transaction (e.g. transferring ETH, or interacting with a smart contract).

Then the user signs it with their private key (to prove it came from them).

They also set a few variables, which you have probably seen in meta mask:

  • gas limit
  • priority fee

And there is also the current base fee (BASEFEE) which is not configurable, and changes in each block depending on how ‘full’ the previous block was.

EIP 1559

Before EIP 1559, calculating gas price was a bit easier, but it lead to less predictable gas prices.

Before EIP 1559, the whole gas fee would also go to the miner. After EIP 1559 some of the fee is burned (and the rest given to miner).

EIP-1559 introduced a new system to calculate gas, based on the base fee, a priority fee (miners tip) and a max fee (fee cap).

Etherscan shows it as base fee, meta mask has fields for max base fee and priority fee, blocknative shows priority fee and max fee.

EIP-1559 went live in the London fork, on Aug 5th 2021 (see my post on the history of Ethereum forks and upgrades)

The simple calculation of gas price per gwei <= max fee, but there some slightly more confusing variables to consider.


  • the maximum amount of gwei you want to pay per unit of gas.
  • When you send a transaction, you can define this


  • max priority fee per gas (also known as priority fee or miner tip)
    • the max proportion of the max_fee_per_gas that should be a miner tip
    • for example if you had max base fee of 10, but max priority fee per gas as 3, then a max of 3 gwei could go to miners
    • When you send a transaction, you can define this
    • this is a proportion of the max_fee_per_gas (fees paid to miners can never be more than the total max fee)
    • if max_priorityfee < max_fee - BASEFEE then you get a refund of max_fee - BASEFEE - max_priority_fee
      • total transaction fee: BASEFEE + (max_fee - max_priority_fee)
    • if max_priorityfee < max_fee - BASEFEE then you get no refund refund, and the miner gets max_fee - BASEFEE
      • total transaction fee: BASEFEE + max_priority_fee


  • BASEFEE which changes depending on recent block size
  • it is the cost (in gwei) that is added to each unit of gas spent
  • almost always written in a captalized word (not basefee)
  • there is a target of 15 million gas per block, but a max of 30 million. If the previous block had over 15 million, then the base fee is increased (by around 12.5%). If it is under 15 million, the base fee decreases (by around 12.5%).
  • smart contracts can see the current base fee by looking at block.basefee (since 0.8.7 in Solidity)
  • this is not configurable by you - it is decided by the protocol
  • the basefee is burned
  • The max fee (also known as max base fee) has to be larger than BASEFEE. Because the BASEFEE can go up, you often will send a transaction with a higher max fee per gas than what it would cost in the current block, as you don’t know if the next block will have a higher base fee.

How to cancel stuck Ethereum transactions

If you send a transaction but it has too low gas to be picked up and processed, it will just wait around in the mem pool.

You can cancel it by sending another transaction with a faster gas fee.

  • Get the nonce of the stuck transaction
  • Create a new transaction, with 0 ETH, and in your software (e.g. meta mask, under advanced settings when creating transaction) add a custom nonce - and put the same nonce as the stuck one
  • set the gas fee to the higher/fast amount
  • then once it gets accepted this new gas will be used for the transaction

This post is incomplete and a work-in-progress
I'll update it soon and flesh it out with more info!

Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!

See all posts (70+ more)

See all posts (70+ more)

Was this post helpful? 📧

If you liked this content and want to receive emails about future posts like this, enter your email. I'll never spam you.

Or follow me on @CryptoGuide_Dev on twitter

By using this site, you agree that you have read and understand its Privacy Policy and Terms of Use.
Use any information on this site at your own risk, I take no responsibility for the accuracy of safety of the information on this site.