Gas in Ethereum: Everything You Need to Know About Transaction Fees

Gas in Ethereum: Everything You Need to Know About Transaction Fees

The Key to Successful Transactions on the Blockchain.

Introduction

Gas is a term you need to understand to understand the Ethereum network.

Let's start with a basic analogy:

As car needs gasoline to run, similarly Ethereum needs Gas to operate it.

Now some questions every crypto & blockchain beginner have:

  • How is the transaction fee calculated?

  • How much ETH do you need to pay for a transaction?

  • Why are some transactions more expensive than others?

  • Why do Gas Fees exist?

Let's understand the concept behind Gas to answer all your brainy questions.

Gas General Concept

Like any other quantity, Gas too have some units, for example, seconds are units of time, metres are units of distance, similarly gas by itself is a unit of computation on the Ethereum Network.

Gas fees are paid in Ether or ETH.

Upgrades in Gas

Before starting to understand Gas, you should know that there was a recent upgrade- the London Upgrade of August 2021, which slightly changed how transaction fees are calculated and how gas works.

The concept of gas before this was- Pre-London Upgrade and later one is known as the Post-London upgrade.

How Gas fees are calculated?

Pre-London Upgrade

Before the London Upgrade, a straightforward formula was used to determine how much ether you had to spend for a transaction.

gas fees = gas spent * gas price

  • Gas Spent is the total amount of gas (in gas units) that were used to execute the transaction

  • Gas Price is the amount of ether you're willing to pay per gas unit of execution

Gas prices are denominated in gwei - a denomination of ETH.

1 Gwei = 0.000000001 ETH

1 ETH = 10^9 Gwei

Gwei stands for Giga-Wei, which is equal to 1,000,000,000 (10^9) wei. Wei is the smallest denomination of ETH. 1 ETH = 10^18 Wei.

Gas Cost Calculation

A smart contract is compiled down into OPCODES before deployment to the Ethereum network. These are nothing, but basic operations like ADD, SUB, MUL, DIV, etc.

Each OPCODE has a fixed gas cost. The gas cost of a specific function within the smart contract is the sum of the gas costs of all its OPCODES.

So, now see the formula which was used before London upgrade-

gas fees = gas spent * gas price

Gas Limits

Now that, we know the gas fee depends on the functions of the smart contract. But if we have more complex functions, like those involving loops, or rely on user input, it will be difficult to estimate the correct price.

As such, instead of specifying the exact gas cost when deciding how much fees to pay for the transaction, you can specify an upper bound limit.

Gas Limit refers to the maximum amount of gas (units) you're willing to use for the transaction. This is set by the user. Wallets like Metamask already estimate reasonably for us.

If your transaction uses less gas than your limit, the unspent gas is refunded to your account.

To pay for gas while submitting the transaction, your wallet must have gas limit * gas price ether. After the transaction is processed and mined, any unspent gas will be refunded.

If, however, your transaction uses more gas than your limit, the transaction will fail and your gas will be gone.

Block Gas Limits

The Ethereum network has a limit for the maximum amount of gas(in units) allowed in a single block, it is the block gas limit.

This is done to guarantee that each block stays within a reasonable computational cost range. Because more complicated transactions take longer to complete, this ensures that nodes do not go out of sync with the rest of the network as computational complexity increases.

Post-London Upgrade

After 5th August 2021, London Upgrade was implemented on the Ethereum network. So, all the gas fees are calculated with this method now.

This upgrade introduced these benefits:

  • Better gas fees estimation

  • Quicker transaction inclusion

  • Burning a percentage of ETH being used as transaction fees

Prior to the London upgrade, the wallets like Metamask used to show the estimates of gas prices based on past network activity, ie. it scans the last 1000 blocks on ETH and predicts the gas price for us.

But now the method is changed, and every block is set to have a base gas price fee, this is the minimum amount required to get your transaction included within the blocks.

This is calculated by the network based on the amount of demand for the block space. This specific amount is burnt by the Ethereum network, therefore forever getting rid of that ETH to offset issuance.

Do you know?

Bitcoin has a maximum supply of 21M bitcoins, whereas etherum doesn't have any overall limit.

So, to get control over the ETH supply and not inflate it to infinite, burning is very important and reaching equilibrium.

Hmm... now we have one term- Base gas price. The second term introduced was the concept of Tipping(priority fees).

We work to get profit, simple. That's why miners work too. As the base fees got burnt, the tip is there to compensate miners for executing and propagating user transactions. Most wallets set this automatically, however, you may change it manually if you like. Higher tip transactions are usually prioritized.

With this upgrade, the formula to calculate gas fees changed to the following:

gas fees = gas spent * (base fees + priority fees)

Variable Block Sizes

Prior to the London Upgrade, all blocks had the same block gas limit. Each block may hold up to 15M of gas. During times of high demand, this resulted in a poor user experience since blocks were operating at maximum capacity, and users had to wait for demand to decrease before being included in a block.

Ethereum now has variable-size blocks as a result of the update. Each block now has a goal gas limit of 15M gas, but the size can change in response to network demand, up to a maximum of 30M gas.

By adjusting the block size and basic fees, the network often reaches equilibrium at 15M gas.

The base fees for the following block are raised if the block gas is higher than the 15M threshold. Similarly, the base fees for the following block are reduced if the block gas is less than the 15M threshold. The deviation of the block gas from the 15M goal determines how much the base fees are increased.

What is the need for GAS?

The Ethereum network is more secure thanks to gas costs. Bad actors are stopped from spamming the network by mandating a price for each computation carried out on it.

Gas restrictions on transactions place a cap on how much processing a transaction can require in order to prevent accidental or malicious infinite loops in smart contracts, which would keep all Ethereum nodes stuck indefinitely.

Conclusion

That's all for this blog!

Make sure you read more about Gas & its code implementation. Below are some of the resources:

Ethereum Gas Explained

The London Upgrade

Gas Optimizations in Smart Contract

Sources: LW3 & my learning from it!

Thanks for Reading!😄

Keep Learning!

Keep Hustling!🫡

Did you find this article valuable?

Support Agrim Sharma by becoming a sponsor. Any amount is appreciated!