CoinsBench

Where blockchain developers share their stories, experiences & ideas.

Follow publication

Exploring Ethereum Transaction Fees: An In-Depth Analysis of EIP-1559 and Costing

--

Gas is the fee required to successfully conduct a transaction or execute a contract on the Ethereum blockchain platform. This fee is to incentivize the miners who validate and process the transaction and it’s measured in Ether (ETH). In the past, the gas fee was determined by the Legacy gas pricing system, but now it’s calculated through the EIP-1559 system. In this article, we’ll explore both Legacy and EIP-1559.

Exploring Ethereum Transaction Fees: An In-Depth Analysis of EIP-1559 and Costing
Exploring Ethereum Transaction Fees: An In-Depth Analysis of EIP-1559 and Costing

Legacy Gas Pricing System:

In the past, the Legacy gas pricing system was the only pricing system. This system used a simple formula for calculating transaction fees based on the gas limit and gas price. The gas price was set entirely by the sender of the transaction, and there was no dynamic adjustment of fees based on network demand. Consequently, this led to high fees and network congestion during peak network usage, as users competed to pay higher fees to ensure their transactions were processed on time. Here’s the formula for the Legacy gas pricing system:

Transaction Fees = Gas Limit * Gas Price

Where Gas Limit is the maximum amount of gas that a transaction can consume and Gas Price is the price of each unit of gas in ETH.

EIP-1559:

EIP-1559 is the new pricing system that was included in the London hard fork, which was activated on August 5th, 2021. The base fee in EIP-1559 is a dynamically adjusting fee that is automatically adjusted by the Ethereum network based on network demand. The goal of this mechanism is to ensure that blocks remain close to their gas limits without causing major fluctuations in gas prices.

When we perform a transaction, we send the gas limit, but the total transaction cost is calculated using gas used * gas cost, so the basic formula goes as follows:

Transaction Cost = Gas Used * Gas Price

Gas limit is a cap or limit on the maximum amount of gas that a transaction can consume. When a transaction is submitted to the Ethereum network, it includes a gas limit specified by the sender. This is to ensure that the transaction does not consume too much gas and potentially cause network congestion or even crash the network. If a transaction exceeds its gas limit, the transaction will fail, and all the gas will be consumed. On the other hand, if the gas limit is set too high, the sender will end up paying more fees than necessary.

“But if we can estimate the gas used using a formula, why then do we set a gas limit?” you may ask. While we can calculate the gas used, Ethereum cannot calculate it beforehand because it depends on the state of the network at the time of execution. This can be influenced by various factors, such as some operations requiring more gas than others or unexpected errors occurring during the transaction’s execution, requiring additional gas for error handling. Therefore, the gas limit acts as a safeguard to prevent transactions from consuming more gas than the user is willing to spend. If the transaction exceeds the gas limit, it will be automatically reverted.

Now, let’s delve into Gas Used and Gas Prices individually:

Gas Used:

The gas used in an Ethereum transaction is a measure of the computational work required to execute the transaction on the Ethereum network. The amount of gas consumed by a transaction is determined by its complexity and the resources it requires to execute. For a rough understanding, the formula for Gas Used goes as follows:

Gas Used = Base Number of Gas + Transaction Data + Opcodes Cost
  • According to Ethereum Yellow Paper, the Base Number of Gas is denoted with Gtransaction and is fixed at 21000 and it is paid for every EOA transaction.
  • Transaction Data refers to any additional information or messages that are included in a transaction apart from the recipient address, value to be sent, and gas parameters. This data can be used to execute smart contracts, store additional information about the transaction, or carry out other functions on the Ethereum network. Any zero-byte of data costs 4 gas and any non-zero-byte costs 16 gas.
  • The Opcodes Cost is the gas per opcode * number of opcodes. For example, the PUSH opcode costs 3 gas, the CALL opcode costs 700 gas, etc.

So, in the end, the formula becomes:

Gas Used = 21000 + Transaction Data + Opcodes Cost

Case 1: If we send only ethers without sending any data and executing opcodes to EOA using Metamask the gas used will be:

Gas Used = 21000 + 0 + 0 = 21000

Case 2: If we now send an ether and a zero-byte of transaction data to EOA using Metamask then the gas used will be:

Gas Used = 21000 + 4 + 0 = 21004

Case 3: If we send an ether and a byte of non-zero transaction data to EOA using Metamask then the gas used will be:

Gas Used = 21000 + 16 + 0 = 21016

Case 4: If we send an ether and two bytes of non-zero transaction data to EOA using Metamask then the gas used will be:

Gas Used = 21000 + (2 * 16) + 0 = 21000 + 32 = 21032

Case 5: Consider the following function which we will execute in a solidity contract that only transfers the ether:

function doNothing() external payable {}

This contract is compiled at the optimization of 1000 and the assembly code for the above function will look as follows:

0:
.code
PUSH 80
PUSH 40
MSTORE
PUSH 4
CALLDATASIZE
LT
PUSH [tag] 1
JUMPI
PUSH 0
CALLDATALOAD
PUSH E0
SHR
DUP1
PUSH 2F576F20
EQ
PUSH [tag] 2
JUMPI
tag 1
JUMPDEST
PUSH 0
DUP1
REVERT
tag 2
JUMPDEST
STOP
.data

The gas calculation will be as follows for the opcodes:

All the operations till offset number 33 are carried out before every function and since this is an empty function so after jumping to the function it will stop. So, according to the table, the total cost for opcode is 74. Since we are also storing transaction data which costs 16 gas per byte for non-zero and function name hash is always 4 bytes (32 bits), so it will cause 64 gas. So, in the end, the calculation will be as follows:

Total Gas Used = 21000 + 64 + 74 = 21138

Gas Price:

Gas price is the amount of Ether (ETH) that a user is willing to pay per unit of gas when they submit a transaction or a smart contract function to be executed. The gas price is specified in units of Gwei (10⁹ wei), which is a smaller denomination of Ether. EIP 1559 has only two fees and one global variable:

max_fee_per_gas (Max Base Fee): most Gwei per gas you are willing to pay.

max_priority_fee_per_gas (Priority Fee): it is the portion of the max_fees_per_gas you want to be a miner tip. It is the fee you are willing to pay to the miner and it is subtracted from the max_fee. For understanding let support your max fee is 5 and you set max_priority_fee to 1.5 then after the BASEFEE is subtracted the max_priority fee is given from that leftover.

  • If max_priority_fee < leftover then after paying the miner’s tip the remaining gas will be refunded.
  • If max_priority_fee >= leftover: leftover will be given to the miner and there will be no refund.
  • If the leftover is 0 then the miner will not probably accept this transaction because it does not contain a tip.

BASEFEE: Protocol level BASEFEE is the minimum fee required to include a transaction in a block. It is set by the network and is adjusted based on network demand and it is burnt in the end. The calculation of the base fee is done as follows:

  • If the previous block is filled exactly 50%, then the basic fee will remain unchanged.
  • If the previous block is a complete block (100%), the basic fee of the next block will increase by up to 12.5%.
  • If the integrity of the previous block exceeds 50% but is less than 100%, the basic fee will increase by less than 12.5%.
  • If the previous block is a blank block (0%), then the basic cost of the next block will be reduced by up to 12.5%.
  • If the integrity of the previous block exceeds 0% but is less than 50%, the basic fee will be reduced by less than 12.5%.

So, the formula for gas price calculation goes as follows:

Gas Price = BASEFEE + Priority Fee

Total Transaction Cost Calculation:

Here is the image that gives a better overview of how transaction costs occur:

EIP-1559 Transaction Cost
EIP-1559 Transaction Cost

Now if we calculate transaction fees for case 5:

Gas Used = 21138
Gas Price = 29.391636709 Gwei + 1.5 Gwei = 30.891636709 Gwei
Where,
BASEFEE = 29.391636709 Gwei
Priority Fee = 1.5 Gwei

Transaction Fees = 21138 * 30.891636709 Gwei
= 652,987.416754842 Gwei
= 652,987.416754842 / 1000000000
= 0.000652987416754842 ETH

Transaction Cost in USD = Transaction Cost (ETH) * ETH to USD
= 0.000652987416754842 ETH * 1,467.41 USD
= $ 0.95820026522022269922

References:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in CoinsBench

Where blockchain developers share their stories, experiences & ideas.

Written by Farasat Ali

Tech Savvy 💖 Blockchain, Web & Artificial Intelligence. Love to travel, explore culture & watch anime. Visit My Portfolio 👇 https://linktr.ee/faraasat

No responses yet

Write a response