How Gas Fees Are Calculated

Web 3
June 10, 2023

How Gas Fees are Calculated

Transactions are taking place in the blockchain at all times. Unlike traditional banks, the blockchain does not have operating times or hours of business. Similar to banks, though, each transaction may generate a transaction fee. These fees are called “gas” fees when referring to the Ethereum network. In other networks, they are known as “transaction fees.” In this article, we’ll mainly discuss how gas fees work within the Ethereum network but similar principles apply to many other blockchain networks. 

Whether you call it “gas” or simply a transaction fee, this fee helps to successfully conduct blockchain transactions. Gas fees keep the network working efficiently and help to keep away unnecessary transactions. In this article, we’ll take a more detailed look at what a gas fee is, how it is calculated, what it means for investors, and other components regarding transaction fees in the blockchain. 

What is a Gas Fee?

Gas is one of the fundamental elements of the Ethereum network. Gas fees keep the network secure and efficient. So, what is the purpose of a gas fee? “Gas” is the fee users pay to process a transaction in the Ethereum network. Gas prices are denominated by a “nanoether” better known as “gwei.” One gwei is equal to 0.000000001 ETH. Gas fees are estimated by the demand and supply system within the network. The busier the network is, the higher the gas fees will be. We’ll discuss this further in the following sections.

In the Ethereum 1.0 network, the proof of work (PoW) system would use transaction fees to reward miners for their computational efforts in the network. These miners would use special hardware to compete for ordering and processing transactions within the Ethereum network. In exchange for their services, miners would be rewarded with ETH block rewards and transaction fees via gas payments.  

In the latest version of Ethereum, Ethereum 2.0, the network utilizes a proof-of-stake (PoS) mechanism. In this system, instead of miners validating the transactions within the network, users are encouraged to stake their ETH in exchange for block rewards and gas fees. We’ll talk about consensus mechanisms in another article. For now, let’s move on to why blockchain networks need gas fees. 

Why Does a Blockchain Network Need Gas Fees?

When users stake their tokens, those tokens are used to validate the network. Validating the network means that each transaction that passes through the network is true and honest. In other words, network validators keep spammers and unnecessary transactions away.

Network validators keep the network secure with the work that they do. When validators are paid gas fees or block rewards, they are incentivized to continue doing their work. Without those rewards, users would not be incentivized to stake ETH to validate the network. Without validators, the network would be at risk.

Gas fees ensure that unnecessary transactions stay out of the network. They incentivize users to stake their ETH to validate the network, ensuring transactions are honest and secure. By requiring a fee for every computation executed in the network, spammers and hackers are less likely to take part in the network. 

Gas fees also help avoid accidental or hostile loops or other computational wastage in code by limiting how many steps of code execution it can use. 

Who Decides the Gas Fees?

Contrary to popular belief, gas fees are not set by any one entity. Developers, project owners, and miners don’t set any price on what the gas fee will be for a particular transaction. Instead, transaction fees are usually determined by the traffic on that network. If there is less traffic, the fees tend to be less. Most wallets that support the Ethereum network automatically calculate gas fees for users. Otherwise, users can manually set a gas fee for their transactions, but that won’t guarantee a complete transaction if it does not meet the minimum threshold. 

When users initiate a transaction, there is usually a requested or estimated gas fee. The miners then can choose the transactions with the higher gas fees to process first. 

To give a real-world example of this, let’s take for instance a line at a coffee shop. Let’s say, there are ten people waiting in line at a coffee shop. The coffee shop has an auction-like policy that offers less waiting time for customers who are willing to pay a higher transaction fee. Customers that offer higher gas fees will have their transactions completed sooner than those offering low gas fees. So, as a result, the more people there are waiting in line, the higher the transaction fees are likely to be. Conversely, when there are fewer people waiting in line, transaction fees are closer to zero. 

Some networks that are able to handle more network traffic, like Polkadot, can also present lower transaction fees. We’ll talk more about Polkadot and scalability in the blockchain in another article. 

What is Gas Limit?

In order to understand how gas fees are calculated, we must first understand some of the components used in calculations. One of those components is the “gas limit.” The gas limit is the estimated maximum effort a validator will need in a particular transaction. The more complex a transaction is within the network, the more gas it will consume. A simple transfer of ETH from one wallet to another normally requires about 21,000 units of gas. However, more complex transactions involving smart contracts require more computational work thus more gas. 

To execute a transaction in the network, users have the option to specify a maximum limit they are willing to pay for their transactions to be executed. This optional parameter is known as “MaxFeePerGas.” We’ll refer to this fee as “MFPG.”  In order for a transaction to be executed, the MFPG must exceed the sum of the base fee + tip. Once the transaction is complete, the transaction sender will be refunded the difference between the MFPG and the sum of the base fee + tip. 

For example, if a user inputs a gas limit of 50,000 for a simple transaction but the network only consumes 20,000, they will be refunded the difference of 30,000 units of gas. However, if a user inputs too little gas limit, say 20,000 gwei, but the transaction requires 21,000 gwei, then the network will consume the 20,000 gwei while attempting to complete the transaction but it will not be complete. The network then reverts any changes, but since the miner has already consumed 20,000 gwei, that gas will not be refunded. 

Base Fee

Every block in the network has a base fee. The base fee is determined by the blocks before it- making transactions more predictable for users. Every time a block is mined, a base fee is “burned,” removing it from circulation. 

Table showing the base fee per block for the Ethereum network.

Calculating a Gas Fee

The gas fee is calculated by gas limit x gas price per unit. So, if the gas limit is 20,000 and the gas price per unit is 200, then the calculation would be 20,000 x 200 = 4,000,000 gwei. We mentioned above that one gwei is equal to 0.000000001 ETH. So, this transaction cost would be approximately .004 ETH. 

Before London Upgrade vs After London Upgrade

The London Upgrade on the Ethereum Network took place in August 2021. Before the upgrade, here’s how a transaction would be calculated:

Let’s say Susan wants to pay Joe 1 ETH. The gas limit in this transaction is 21,000 units and the gas price is 200 gwei. So,

Gas units (limit) x gas price per unit would be: 21,000 x 200 = 4,200,000 gwei or .0042 ETH. 

However, after the upgrade, the calculation would be: 

Gas limit = 21,000

Base fee= 10 gwei

Joe adds a tip of 2 gwei - otherwise known as a priority fee 

So, units of gas used x (base fee + priority fee) 

21,000 x (10 + 2) = 252,000 gwei or .000252 ETH.

Why are Gas Fees so High?

Gas fees can change depending on the traffic within the network. The more demand there is within the network, the higher the gas price per unit will be. The gas fee is used for calculations, storing and manipulating data, or transferring tokens. Each of those processes requires a different amount of gas. Users can offer a priority fee, or “tip,” to validators to increase the priority of their transaction so that their transaction is processed faster. The less a user is willing to pay for gas, the slower their transaction may be as miners are less incentivized to process that transaction. 

Strategies to Reduce Gas Fees

While gas fees can’t be completely removed, they can be minimized. The upgrade to Ethereum 2.0 aims to reduce transaction fees for most transactions in the network. It aims to reduce overall network congestion and speed up the transactions in the network. One of the main reasons for Ethereum’s high gas fees is congestion in the network. The Ethereum network has gained a lot of popularity in the past years, with more dApps building on its platform.
One way to reduce these fees is to time transactions so that they take place when network traffic is at its lowest. Timing the transaction so that it takes place when network traffic is at its lowest means there is less demand for validation and more incentive to complete transactions with lower gas fees.

Another way to keep gas fees at a minimum is to earn ETH when you remove storage variables on the blockchain. That’s the foundation of gas tokens. Gas tokens allow you to store gas when it's cheap and use or sell it when it's expensive. However, their use has been heavily criticized for adding to network congestion and they have become less effective since the implementation of EIP-1559. It's also important to note that not all Ethereum users have the technical knowledge to utilize such tokens.. One quick way to mint gas tokens is through GasToken.

N.B. It's important to exercise caution when using services like GasToken, as there can be risks involved. The website itself warns users about potential smart contract and economic risks.

Finally, another way to go around high gas fees is by using a layer 2 application. In other words, applications that are developed on the layer 1 Ethereum platform, like Polygon, will generally have lower gas fees and faster transaction times. The main Ethereum network can often be congested and with large corporations offering higher gas fee bids, retail buyers may be at a disadvantage. In such a case, resorting to a layer 2 protocol may save some time and money. 

What do Gas Fees Mean for Investors?

Gas fees can help keep the network secure. When investors want to initiate transactions in a blockchain network, they will naturally want to work with a blockchain network that is reliable, secure, and efficient. Gas fees give network validators incentive to keep it that way. These fees also ensure proper computation within the network by limiting the number of actions that can take place.  Network fees help filter out spam transactions and ensure that only honest transactions take place. This way, if an attacker were to attempt to instill an endless loop into the network, it would be too costly and not worth the effort. 

Transaction fees in the network help filter out spam transactions and ensure that only honest transactions take place. 

In a Nutshell

In a nutshell, the gas fees or transaction fees in the blockchain are a classic example of a supply and demand economic model. There is a finite supply and fluctuatin demand. Once a transaction is initiated in the blockchain, it goes into a virtual holding place until it is verified and included in the blockchain. Usually, the higher the gas limit is, the quicker the transaction will become finalized. If the gas limit is too low, it could be at risk for remaining in limbo indefinitely. 

Gas fees and transaction fees can be beneficial to the blockchain ecosystem. By requiring a small fee for each transaction, it keeps the blockchain virtually free of attackers or endless error loops. Transaction fees incentivize validators by rewarding them for each block that is validated. These validators keep the blockchain honest and virtually free of error. Naturally, the higher the transaction fee a user is willing to pay, the quicker their transaction will be processed by the validators in the network. 

Essentially, gas fees are the compensation users pay for all of the computation that goes on “behind the scenes.” Similar to how banks charge a fee for bank transfers but different in that crypto transactions can take place at any time of the day, any day of the week, in any part of the world. What’s more, transactions can take place in a matter of seconds from one part of the world to another. Simply put, the transactions happen like “magic” relative to traditional wire transfers. That magic requires a little compensation in the form of “gas fees” and these fees are what keep the network secure, quick, and efficient. 

Recent posts