What are Gas Fees on Ethereum, and How are They Calculated

What are Gas Fees on Ethereum, and How are They Calculated

If you've ever transacted on Ethereum or its Layer 2 solutions, you've encountered the unavoidable "gas fees." And even on centralized exchanges (CEXs) like Binance, you've likely noticed charges labeled as "network fees." But what exactly are these fees? How are they calculated, and why are they necessary?

In this post, we delve into the essentials of gas fees—also known as network fees on CEXs—exploring their calculation and the reasons behind their existence.

Let's dive right in!

What are Gas Fees

Gas fees are the fees used to incentivize the validators to pick up your submitted transaction and put it into the next block. 

On a high level, the gas fees are calculated by the number of gas used multiplied by the gas price. The following are plain English explanations:

  • The number of gas used:
    You can imagine a “gas” as a unit of compute resource. The more complex a transaction is, the more gas it will cost. And no, there is no unit for gas. Gas is the unit. For instance, transferring ETH is the simplest thing you can do on Ethereum, costing 21,000 gas. On the other hand, swapping two tokens can cost you tenfold more than that. 
  • The Gas Price:
    The gas price is the price per unit of gas (remember: it’s a unit!) that a user is willing to pay for a transaction. People can set the price they will pay when submitting a transaction. Like other communities, market demand affects the price. The gas price will increase if many people want their transactions included in the next block - as the storage space in a block is inelastic.   

The gas fees are also known as the “network fees” on centralized exchanges like Binance or Bybit when withdrawing assets from them - as they need to send the requested assets from their address to yours on a blockchain. 

How Is Gas Fees Calculated in Details

In the previous section, we discussed the high level of how gas fees are calculated. In this section, we’ll examine the actual formula in use.  

The number of gas used: 

As mentioned earlier, the number of gas used affects the gas fees you pay for a transaction. But how do you, as a user, know how much gas you will pay? Luckily, wallets like MetaMask or Rabby, as well as blockchain applications nowadays, provide that for you. The number is usually called the “gas limit” on your wallet.  The “gas limit,” as the name implies, means the maximum number of gas (compute unit) that I’m willing to spend on this transaction. Usually, wallets and applications set the number higher than what will eventually be used (see below)

A screenshot from Etherscan showing gas limit and the exact number used
A screenshot from Etherscan showing gas limit and the exact number used

And for those wondering: if the gas limit is lower than the required gas for executing a transaction, the transaction will fail with an “out of gas” error (gas fees will still be charged using the formula below).

The Gas Price:

As for gas prices, it consists of two parts: 

  • Base Fee:
    Ethereum network dynamically adjusts the base fee based on the congestion level of the blockchain. This adjustment aims to maintain the amount of gas used per block around an optimal level, known as the “target gas used,” which is set at 15 million gas. The base fee for the next block is adjusted up or down depending on how much gas is used in the current block compared to this target. If a block uses more gas than the target, indicating high network demand, the base fee increases for the next block, and vice versa. This part of the fees will be burned, i.e., taken out of circulation. 
  • Priority Fee:
    The optional tip you want to pay to the validators to skip the queue.

Both fees are usually denominated in gwei per gas. A gwei is 0.000000001 of a ETH. To learn more about the unit that the Ethereum blockchain uses, you can try out the Ethereum Unit Converter.

A screenshot of the Unit Converter
A screenshot of the Unit Converter

When submitting a transaction, you can state the maximum gas price you will pay. That number is the maximum gas price, or “Max,” as shown in the screenshot below. Whenever the max gas price exceeds the combination of the base fee and priority fee, the transaction will be put inside the latest block.

We’ve come a long way. 

The exact formula for calculating the total gas fees is to add the Base Fee (“Base” below) to the Priority Fee (“Max Priority” below) and multiply it by the unit of the gas fees used. For instance, the following transaction costs 0.004773801554865945 ETH, which comes by adding the Base (20.816167787 gwei per gas) to the Max Priority (0.01 gwei per gas) and then multiplying it by the amount of gas used (228,235 gas). Please note that the result is in gwei, which needs to be converted to ETH. You can do it with a unit converter.

A screenshot of a transaction on Ethereum from Etherscan
A screenshot of a transaction on Ethereum from Etherscan

Why do We Need to Pay Gas Fees

You should have understood how to calculate gas fees by now. But why do we need it, anyway? 

The reason is simple. Because the blockchain is a public database/ledger where miners/validators update it at intervals, there must be a way to disincentivize bad actors from spamming the network by sending gazillions of transactions at once.  

That’s where gas fees come in. 

As each transaction comes with a cost, a potential attacker must weigh the benefits and the associated costs before executing its plan. Furthermore, after the EIP-1559 upgrade, the more block space is used, the more base fees will increase; hence, more ETH will be burned. As the supply of ETH gradually decreases due to the burns, so should the price of ETH (economics 101)

In other words, if an attacker tries to censor the network by flooding transactions, the cost of the attack per block will increase dramatically due to 1) the surging base fees and 2) the dwindling ETH supply.