Ethereum Gas Fees Explained (For Baby Devs)
Tags: web3, beginners, blockchain
Published: 2023-02-05 15:25:28

For anyone starting their journey exploring the web 3 space, the concept of gas fees can be a hassle to wrap ones head around. I write the article to guide freshmen in web 3 on what gas fees are, why they're of importance and how to calculate them post the Ethereum Merge. 

## **What is Gas and why is it important in Ethereum?**

Gas is the unit of measuring the amount of computational work the Ethereum Virtual Machine (This is what executes operations on the Ethereum network) expends in carrying out operations. Each operation has a corresponding unit of gas that’ll be required to successfully execute it. Basic operations like sending Eth require or consume lesser units of gas, while operations like minting an NFT and swapping tokens require more units to be executed. It is pertinent to know that deploying smart contracts also requires you to pay for gas. The Ethereum Foundation likens this process to your car requiring gas/petrol for its operation. Without sufficient gas, your car cannot perform the computational work of moving you around.

## Okay? But Why do I Have to pay? Why can’t transactions be free?

**Payment of gas fees is required for the following reasons.**

**First**, they are needed because The Ethereum Virtual Machine(henceforth referred to as the EVM) is a Turing complete machine. Turing complete is a term for a computer that can execute any type of computation given to it. The EVM can thus process any computation given to it. From NFTs to complex smart contracts, it can execute it all.  

This sounds like a great thing yeah? Not quite! You see, the thing with Turing complete machines is that they have a halting problem. That is, since they can process any given computation, they can be loaded with endless computations. Turing complete machines like the EVM can be overwhelmed by giving them infinite looped computations which by their design, they cannot stop running. The only way for such machines to come to a halt is by running out of memory and eventually crashing. 

An example of this is your everyday printer, it is a Turing complete machine, in that it can print as many copies as you want it to with enough paper and ink. As such, there’s no way for the printer to stop itself from printing endless copies unless it runs out of memory and crash. The same scenario can be painted for the EVM. One can deploy smart contracts with infinite loops that will eventually overwhelm the Ethereum network. Bad actors can spam and crash the Ethereum blockchain with this. 

To avoid this, there needs to be a way for people to pay for the computational work the EVM executes so as to secure the network and deter bad actors. It is such that, for every work to be executed by the blockchain, there is a required unit of gas. Meaning, if you feed the EVM with an infinite looped smart contract, you’ll pay handsome amounts of gas for every iteration of that loop. This makes any attempt to crash the network a very expensive one.


**Another essential reason** for gas fees is the need to incentives nodes to keep running their nodes in good faith. Before the Ethereum network moved to the Proof of Stake(henceforth referred to as POS) consensus mechanism, transactions on Ethereum were verified by miners who ran nodes. These nodes were expensive to operate as they required heavy hardware and consumed megawatts of electricity. To validate a transaction, nodes solved complex problems using some hashing algorithm to get the right hash that matches a transaction before adding it to the blockchain. 

A reward system existed that incentivized miners to run nodes and keep the system decentralized. Miners who solved the problems first got the gas fees paid by users to have their transactions processed. Post POS, miners no longer run “nodes” to validate transactions. Instead, they stake a certain amount of Ether(32 Ether presently) to become validators on the network. However, they are still incentivised with the gas fees paid by users of the network.

So now you know why gas fees are integral to the success of the Ethereum blockchain  or any other blockchain for that matter.

## How Are Gas Fees Then?

To understand how gas fees are calculated, there is a need to discuss the concept from two different timelines. The time before the Ethereum London upgrade of EIP1559 (Ethereum improvement plan 1559) and the time after the EIP implementation in August of 2021.


Before the London upgrade, the formula for calculating gas fee was;  Gas Unit Limit * gas price(in gwei). Gas limit?  gas price? Gwei? What are all these you might ask.

**<u>Gas Limit:</u>**
The gas limit is the pre-set gas unit for a given transaction. That is, the max amount of gas, the EVM will use in executing the operation. Certain transactions have pre-set gas limits required to execute them. For example, the EVM uses about 21,000 gas to process the transfer of Ether from one wallet to another. For more complex transactions, the gas limit is higher. View this as the amount of gas you’ll need to get to go from point A to point B. If you’re driving interstate to meet family and friends, your car requires way more gas than when you’re going to the mall nearby.

**<u>Gas Price:</u>**
The gas price is the current amount of gwei needed for one’s transaction to be included on the current block. At This juncture, there is a need to explain some terms.

**Gwei:** Gwei stands for gigawei, it is a smaller fragment of Ether. 1 Ether is equal to 1,000,000,000. Yes, one billion gwei. Meaning, 0.5 Ether is 500,000,000 gwei. 0.1 Ether is 100,000,000 Gwei. When you send one Ether to someone, the EVM records it as 1,000,000,000 gwei under the hood. There also is wei, which is the smallest possible unit of value in the Ethereum ecosystem. That is one quintillion Ether or 1 billion gwei. 1 Ether is equal to 1,000,000,000,000,000,000,000 Wei. You only need to care about Gwei, but it’s nice to know it's baby brother.

**Block:** Think of blocks as memory cards that store different data. It has a specified time with which to fill up its total storage with data, when full, it is added to a library of memory cards where each card connects to one another by referencing the serial number of the one before it, to form a chain of memory cards. This is what a block represents in a blockchain only that it stores records of different verified transactions. Also, the time required for each block to be filled with such transactions on the Ethereum blockchain is twelve(12) seconds. Thus, blocks have 12 seconds with which they can be filled with transactions after which they get added to the chain of blocks with each block referencing the hash of the previous block. 

Ever since the London upgrade, each block has had a target of 15 million units of gas within that 12 seconds timespan. Meaning 15 million units of computation processed by the EVM is expected in any given block. Thus, if only transfers of Ether were included in a given block, it’ll take roughly 714 transactions to fill up that block. I’ll explain more about this target block size further down the road.

Thus, gas price is the amount in gwei it’ll take for a transaction to be included in the current block. This updates every 12 seconds. It fluctuates and the more transactions to be processed, the higher the fee as there are limited block spaces.

**Back to our calculation of gas fees pre-August 2021.**

So let's say John wants to send Jane 0.1 Ether and the gas price at that point in time is 100 gwei (low activity);

Gas Fee is thus;  **21,000 gas units * 100 gwei = 2,100,000 gwei**

To get the Ether value, you just divide it by 1,000,000,000 = 0.0021 Ether. Thus, Jane will pay 0.0021 Ether as gas fees and 0.1002 Ether in total.  To get the dollar value, you just multiply the fee by the current price of Ether.

To illustrate another example, say we want to send 0.2 Ether to our wallet during an anticipated NFT mint, the gas Fee will likely be higher as there will be a fight for block space (I experienced this with the BAYC otherside land mint). As such the gas price may become something like 1000gwei.

Thus gas fee is **21,0000 * 700 /1,000,000,000,000 = 0.021 Ether**.

Following the implementation of EIP1559, although the formula for calculating gas fees remained Gas unit * Gas price, the procedure for determining gas prices changed. Gas prices became a function of base and priority fees. The new formula looks as follows; 

**Gas Limit * gas price(base fee + priority fee)**

Essentially, the 1559 update introduced two new concepts in gas price calculation;  **Base fee and priority fee**.

**<u>Base fee </u>**
This is determined by the block size of the previous block. As highlighted earlier blocks have a target size of 15 million gas. That number can be increased or decreased depending on the network demands. During surge periods, block sizes can be increased up to a max of  30 million gas. When this happens, the base fee will increase up to a maximum of 12.5% per block. Meaning where the target block size of 15 million gas is exceeded by one block, the base fee to have your transaction included on the next block will increase up to a maximum of 12.5%. Similarly, the base fee will be decreased if the block size is less than the target block size. 

Say block A had a size of 15 million with a base fee of 100 gwei, the base fee of block B(the next block) will remain the same even if it uses up the maximum block size of 30 million gas, however, the base fee of block C will be increased up to a max of 12.5% to bring it to 112.5 Gwei. If that block C still uses up more than 15 million gas, the base fee will keep increasing until it gets to a point down the chain where a given block has 15 million or less block size.  All this is done by the Ethereum protocol through a process called _**tâtonnement**_.

It is important to note that validators do not get paid this base fee as it is rather burned after the block has been added to the blockchain(mined). This is done to prevent miners from validating empty blocks and to make Ethereum deflationary.

**<u>Priority Fee:</u>** 
This is the fee paid to incentive a validator to include your transaction on the next block. This is an extra tip given by a user to have their transaction included on the blockchain. This fee is set by the user. The higher the fee, the more likely your transaction will get added to the block, especially during periods of high demand. 

In most cases however users don't get to set the priority Fee as it's handled by their wallet provider. Wallet providers like MetaMask have systems in place to calculate and determine a recommended priority fee. You can still manually set your priority Fee, but be sure it's not below the one auto-set by your wallet provider. It is recommended you don't alter settings with regard to gas units and priority fees unless you absolutely know what you're doing. Priority fees are what validators get paid together with block rewards from Ethereum. 

Let's calculate gas fees at present using our earlier example of John sending Jane 0.1 Ether. Given that the previous block did not exceed the 15 million gas limit and the current base fee is 80 gwei, John decides to tip the validator 10 gwei. The gas fee will be;

**21,000(Gas unit) * (80gwei (Base fee) + 10gwei (Priority fee)) = 1,890,000 gwei**. And of course, you divide by one billion to get the Ether value.


The concept of gas fees can be quite tricky to grasp for newbie smart contract developers and generally for persons new to web 3. I really hope I explained the concept of gas fees to your understanding because the importance of understanding the rationale behind gas fees and how they’re calculated cannot be overemphasized. It will not only help aid your understanding of the Ethereum Virtual Machine but also enable you write smarter contracts.