Transaction Fees and Gas

Published by Mario Oettler on

As you know, Ethereum provides smart contracts. These scripts are almost Turing complete. But there is a challenge to solve. If a program is Turing complete, it is impossible to prove that it will terminate eventually. This issue is known as the halting problem.

In our highly decentralized setting where everybody can write smart contracts and nodes all over the world execute them, it would be a disaster if there is a script (=smart contracts) that doesn’t come to a halt. It is also critical if it takes very long to come to a halt.

For that reason, Ethereum introduced the gas concept. Every command (or opcode) and storage/memory slot has a price. This price is calculated in units of “gas”. If a user invokes a smart contract, it sends a certain amount of gas with its transaction. If the execution consumes more gas than provided in the transaction, the execution stops.

The gas goes to the miners as a reimbursement for their calculation effort.

Each block has a maximum block gas limit. This means that the gas of all transactions in this block must not exceed this limit. To compare this to bitcoin, the block size in terms of kilobytes can vary because the calculation effort is also taken into account.

The gas costs for each operation (read, write, calculate something, etc.) are given in the Ethereum yellow paper.

The user buys gas with Ether (ETH). This is done automatically by the wallet and in most cases, users don’t need to care about this.

But the important question is, how many ETH do I have to pay for one unit of gas? Well, it depends. Since gas serves as transaction fees, a higher gas price (number of Ether you have to pay for one unit of gas) expedites the processing of a transaction in the network.

For example, if you have a transaction that consumes 21.000 units of gas, and you choose a gas price of 10 GWei, you would have to pay 0.00021 ETH. If you choose a gas price of 20 GWei instead, you would have to pay 0.00042 ETH, but the transaction would be accepted much faster by the miners.

The following formula shows how to calculate the transaction cost in Euro if the amount of gas is known.

amount of gas * gas price in ETH * price of ETH in EUR = Euro price

Calculation of the Gas Price

When Ethereum was invented, the gas price was a first-price auction. This means that transactions that offered a higher gas price were included first into a block. The result was high volatility of the gas price and cloaked blocks.

With EIP1599, a new concept was introduced. It splits the gas price into two parts.

  1. the base fee per gas
  2. the tip (or priority fee)

The base fee per gas moves up and down according to a formula. This formula takes the gas used in the parent block and the gas target of the parent block into account. The base fee per gas rises when blocks are above the gas target and decreases if the blocks are below. With a rising base fee per gas, the cost of a transaction is higher, and the idea is that fewer transactions are created.

The base fee is burned.

With the base fee revolving around a certain value, the blocks will not be completely full. So, there will be room for urgent transactions.

The priority fee can be seen as a tip for the miners as an incentive to process the transaction and include in a block. Since the blocks are never completely full, miners should take every transaction into the block they are currently working on.

Categories:

if()