cover-img

What are Gas Fees, and How do you calculate them? - Web3 Oversimplified

Everything you need to know about gas and gas fees!

28 September, 2022

11

11

0

Contributors

Introduction

For months, gas fees have surged, hindering trade and creating an economic incentive for miners on the Ethereum blockchain. These fees would make one wonder, "What are Gas Fees? and how are they calculated on trade.
This article will explain why we need gas on the Ethereum blockchain. You would best understand the words you will read in this article if you've read about What Web3 is, as it will only touch on terms relating to Ethereum Gas Fees.
From here on forward, we will step on the gas :D

What is Gas

Gas in Ethereum is the unit cost required to successfully process a transaction or execute a Smart Contract on the Ethereum network.
Smart Contracts are programs created using a blockchain programming language like Solidity and executed on a blockchain. You can learn more about Smart Contracts and Solidity by reading Smart Contracts: What are they.
Smart Contracts are an essential topic when talking about gas and Gas Fees because most gas spent daily on the Ethereum network is spent on Smart Contracts. More on this later.
An essential factor to note is that *** gas *** and Gas fees are not the same.
Gas fees are defined as the cost per gas unit paid to the miners by the users who want their transactions processed.
Gas fees are set by the miners on the Ethereum network
Gas fees are paid in Ethereum's native currency known as Wei. Wei is the smallest denomination of ether. Kind of like how pennies are the smallest denominations of the United States Dollar.
Wei takes its name after the renowned computer engineer Wei Dai, who was one of 2 people hired by Satoshi Nakamoto to develop the Bitcoin blockchain.

Fun fact: Despite sharing the other half of Wei Dai's name, the Dai ERC-20 token is not named after Wei Dai.

Back to Ethereum; Since Wei is a denomination of ether, it is represented as 1018. Don't let the math scare you; this just means that 1 ETH in Wei is one quintillion.

A quintillion is a number with eighteen zeros.

Ether/Wei representation:

One Ether (1 ETH) = 1,000,000,000,000,000,000

Gwei or Giga-Wei is the preferred unit cost for calculating Gas fees because they are easier to understand and represent.
1 ETH = 1000000000 GWEI
Next, we will look at how gas fees work and why they are a precious resource on the Ethereum network.

How Gas Work

The Ethereum blockchain introduced Smart Contracts to the blockchain ball game. When the Ethereum developers introduced Smart Contracts, they needed a way to be able to:

Limit the behavior of a Smart Contract.

Compensate miners for confirming Smart Contract Transactions.
You may wonder, "Why is any of these necessary?" and the answer, my friendlies in the problems they could potentially pose to the network.
Gas works just like the gas or fuel in your car. It was created as a limited resource that could "Run Out." This means that just like gas in your vehicle, your transactions and Smart Contracts could run out of gas.
Unlike a native blockchain which is secured and hack-proof, Smart Contracts are third-party applications created by random developers. Smart Contracts are as secured as the developer making the Smart Contract wants them to be. This one fact poses a risk to the Ethereum network, one of the primary reasons for introducing a Gas Limit.
A gas limit is the maximum amount of gas units used during transactions.
If the gas limit supplied is less than the required amount of gas units needed to process a transaction, the transaction will fail, and an "Out Of Gas" exception will be raised.
If the gas limit exceeds the required amount of gas units needed to process a transaction, the unused gas will be sent back to the sender.
The Gas limits put in place for smart contracts can act as a :

Terminator: to stop devs from writing silly and potentially dangerous code that could slow down the network (i.e., forever loops)

Deterrent: since Smart Contract codes can influence the gas prices, they can deter developers from writing shitty code.
You may be surprised to find out that your solidity code can also influence the gas limit. The more the computational steps present in your code, the higher the gas limit.

Computational steps are just a fancy term and can include code sections that loop or perform some math calculations.

Other factors of your solidity code that can influence the gas limit include:

Data Types.

Size of .sol file.

Events emission.

Unpacked structs.

How Gas fees are calculated

Gas fees are calculated based on several factors. Gas fees are calculated in Gwei and multiplied by the amount of gas used to process the transaction.
The minimum amount of gas units required to process a transaction is 21,000.
The gas fee equation is calculated as so:
gas fees = (network fee in gwei) x (gas units required)

Network fees are set by the miners on the Ethereum network and fluctuate based on demand. When demand is high, network fees surge and vice versa.

For example:
Let's say John wants to send 2 ETH to his friend Mata and the network fee at the moment is 50 Gwei; the gas fee will be calculated as so:
gas fees = 50 Gwei * 21,000 gas fees = 1,050,000 Gwei
Next, we will convert the gas fees in Gwei to ether:
gas fees = 1,050,000 / 1,000,000,000 ether gas fees = 0.00105 ether
That makes the total transaction = 2 ether + 0.00105 ether = 2.00105 ether.
The total transaction fee that John would have to pay to Mata on 2 ETH is 0.00105 ETH which sums up to 2.00105 ETH sent in total.
The calculations above apply to both funds transfer and smart contract interactions.

Why Gas Fees Are High

Now that you're familiar with what gas is and know how it's calculated, it's time to answer one of the most popular questions surrounding gas fees.
Why are gas fees so freaking high?
To answer this question, we must first understand the economic importance of gas fees to the network miners and how it relates to supply and demand.
Generally, from economics 101, we know that when supply for a product decreases, demand surges, and so does the price of that same product. These principles are the same on the Ethereum network where gas is the product.
When the network gets busy and demand is high, miners can make extra money by increasing their gas fees. This move by the miners can hinder transactions on the network and cause them to delay.
Remember John? When John sent Mata 2 ETH, some other actions occurred that we didn't speak about. Let's discuss them.
First of all, when John made his transaction, his transaction got sent to the network mempool. When a transaction reaches the mempool, it is up to a miner to pick up John's transaction.

The mempool is temporal storage where pending transactions are kept.

When gas prices are increased by the miners, this can cause a bidding war by users on the network to get their transactions confirmed the fastest.
The Ethereum network can currently process 30 transactions per second. Having thousands of pending transactions in the mempool strains the network and slows it down.
When users try to outbid each other to get their transactions in the front seat, they ultimately aid the process of raising the network's gas prices.
This dynamic ultimately creates an economic incentive for the miners while eventually leading to a bad user experience for everyone else.
After John's transaction is processed, Mata gets 2 ETH, and the miner that confirmed John's transaction gets 0.00105 ETH.
Key takeaways:

Miners can raise their gas prices to maximize revenue when the network gets too busy.

Users bidding against each other can unnaturally raise the gas prices.

Miners keep your fees after confirming your transactions.

Continuety

Ethereum 2.0 is finally here! This new and astounding update to the Ethereum network will see that the Proof-of-Work algorithm is replaced with the Proof-of-Stake.
This move will eliminate the need for miners on the network, removing the need for pricey gas fees.
Since miners cannot set the network's gas fees, gas fees will be based on some other random algorithm developed by the network's developers.
It is already rumored that you could make hundreds of transactions on ETH 2.0 and still pay less than a penny on gas fees. While this rumor is not entirely accurate, it's also not far-fetched!
ETH 2.0 will directly change the Ethereum ecosystem, making it a more eco-friendly solution and accessible to everyone.

Conclusions

Thanks for reading! Really hope you enjoyed reading about gas and gas fees!
I hope you can also clearly understand why gas fees are essential and why their status as a precious resource plays a considerable role, negatively and positively, on the Ethereum network.

blockchain

web3

ethereum

crypto

11

11

0

blockchain

web3

ethereum

crypto

Favour
Open Sourcerer | Software Developer | Content Creator | Technical Writer | I am on a journey to make Web3 accessible to everyone 💛

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.