Gas Price and Limit | Ethereum Transaction Fees

Share:
Gas Price and Limit | Ethereum Transaction Fees

Gas is a specific unit of measurement for a fee in the Ethereum system.

Before we can start effective development on Ethereum Solidity we need to figure out a few points of important stuff.

This article is going to be about the Gas price and Gas limit, why it is important and how to use it.

You probably know that to make any changes to blockchain we need to pay some fee for it. All fees from the block are gathered up and go to miner who mined this block.

In the Ethereum, the system of collecting commission is a bit more complicated than just taking some Ether from transactions to cover it.


Definition


Gas is a specific unit of measurement of fees in the Ethereum system. Any gas unit is always the same and it is not measured in anything else, one unit of gas is a constant. Also, the number of these gas units (gas amount) for the same operations is always the same, to give an illustration ordinary transaction by sending Ether from one address to the other with no additional info or operations costs 21000 of gas units.

Gas limit is the maximum amount of gas that can be spent for the transaction, it has to be set by the user.

And the Gas price is just the amount of Ether which should be paid per 1 unit of gas.

It can be different due to the situation in the Ether market. Common price can always be seen at https://ethgasstation.info/.

Here is an example of the current gas price: 

Gas price is usually measured in Weis to avoid lots of ‘0’ after the comma in the number because the gas price is usually small enough. Wei is the smallest point of Ethereum 

1 Ether =  1,000,000,000,000,000,000 Wei (1018). 

Here is one example of an ordinary transaction: 

Another thing to say is that the gas limit is not always fully used for transaction execution.

The gas limit is usually set by the user and sometimes happens that the put amount is invalid.

What happens next?

If gas price is too small for complete execution your transaction will still be taken to the network and get started to be processed by miners but once it runs out of gas miners will stop executing the transaction, it will be reverted and amount of ether that was sent will be returned. In the network, the transaction will be marked as failed and all gas that was spent on trying to execute the transaction will be taken by miners and not given back.

If you are using such well-known wallets as Metamask or MyEtherWallet it will be pretty difficult to send not really complicated transactions with less gas limit according to their UI which calculates it before the transaction is sent and deactivates the button if the gas limit is less. But in case of some difficult system of smart contracts sometimes it calculates not correctly and it becomes possible. 

Next case if the gas limit is more than needed. In this situation, the left gas will just be returned to the sender's address. But anyway it is recommended to specify an exactly needed amount of gas if possible.

So the formula of needed Ether for the transaction is next 

Fee = (Gas price) * (Gas limit).

It looks simple, but it is common trouble of understanding how this all stuff works.


An illustrative example


Let’s imagine that somebody needs to get from point A to B in his car. For making it possible car needs to be filled with gasoline. For example, gasoline will cost $10 per gallon. And to cover the same distance always the same amount of gasoline should be spent. Then let it be for covering the distance from A to B 50 gallons of gasoline should be spent which means that for making it possible the amount of $500 should also be spent.

In this example, we have 1 gallon = 1 point of gas and 10$ is a gas price for this one gas point. 

This is probably the easiest example of how Ethereum fee system works. Might be that is why gas in Ethereum is called so.


Gas expenditures


As we know from above the simple transaction costs 21000 of gas. But why could it cost more?

In interaction with the Ethereum Blockchain system, there are few main points of expenditures.

The main point is blockchain memory using. 

As we all probably know Blockchain is a decentralized database. It means that data can be stored there. But taking in mind that all system memory should be stored in every user’s device it does not have to be free to publish information there. So it is costly.

In Smart contracts, there are 3 types of memory saving:

  1. Volatile stack access: Stack

  2. Volatile memory access: Memory

  3. Non-volatile: storage

I will explain the differences between them in detail in the next articles.

Here is a list of prices in gas for using the memory in the blockchain.

Let’s make some symbols to make it more comfortable to understand.

MC = memore cell = 32 bytes value.


Stack POP = 2 gas/MC

Stack PUSH = 3gas/MC

Stack SWAP = 3gas/MC

Stack DUP = 3gas/MC


POP - is a command that gets one last value from the stack.

PUSH - a command that puts value to the stack.

SWAP - a command that changes values in the stack.

DUP - a command that duplicates value in the stack.


Memory LOAD = 3 gas/MC

Memory STORE = 3 gas/MC


LOAD - command that allows us to read one value to the memory.

STORE - command that allows us to write one value to memory.

With storage things are a bit different. Interacting with data in storage is super expensive due to the fact that information in storage will be stored in blockchain history.

So reading one MC from storage will cost 200 gas.

To write something to storage firstly it will be defined whether MC is zero value or not. 

Writing to zero value will cost 20 000 gas.

Writing to non zero value will cost 5 000 gas.

And the most interesting part - changing the value in storage from non zero to zero, in other words, cleaning storage value will refund the amount of 5 000 gas to the user.


 Armen Arakelyan


Comments

Scroll to top