Importance and Role of Gas in Transactions of Ethereum Blockchain


Written By Dr. Amol Dhumane

 ---------------------------------------------------------------------------------------------------------------------------

 Gas

  1. Every time when you send a transaction to the blockchain, you have to pay ‘Gas’
  2. The important thing is, you only need to pay the ‘gas’ when you modify the data on the blockchain. (If you are reading the data, there is no need to pay ‘gas’)
  3. In the solidity, all the functions with ‘view’ or ‘pure’ keywords are read-only functions. Execution of such functions do not use ‘gas’. 
  4. Gas payer: The sender of the transaction needs to pay the ‘gas’. 
  5. Gas receiver: The miner receives ‘gas’.  Here miners are the entities that add data to the blockchain. For the reward of their work, the gas is paid to them. 
  6. The gas is paid only when the transaction is included in the blockchain. If the miner does not include our transaction in the blockchain, then gas is not paid to the miners. 
  7. Gas is just a measurement. Even though transaction cost is measured in gas, but we actually pay in Ethers. 
  8. When we sign a transaction and send it to Ethereum Network, the Ethereum protocol debit some Ether automatically from our Ethereum address. 
  9. If there is not sufficient Ether at our Ethereum address, so we can’t pay the gas cost for that transaction and it may result into the failure of the transaction.

 ---------------------------------------------------------------------------------------------------------------------------

Why there is a need of Gas:

“Transaction fees are measured in Gas and paid in Ether”

  1. Consider that, there is no need of gas, then users with malicious intentions (attackers) will fill the Ethereum network with malicious smart contracts which may not do something constructive but will use the network resources so that the other good smart contracts will not execute. 
  2. So if we want to avoid this, there is a need of some sort of fees, that will keep the attackers away from doing such types of things as it will not be affordable for them as it will cost them a lot of money. 
  3. Now consider that if the same fee is kept for all types of transactions (simple and complex) execution. So in that case miners will execute simple transactions and not complex ones. So the complex transactions won’t be executed. The solution to this is to make the transaction fee variable instead of keeping it constant. The complex transactions will have more transaction fees than that of simple transactions.  The cost of the transactions depends on their computational complexity. 
  4. Initially, when Ether was launched, it was very cheaper (around a few cents). Then after some time the price of Ether becomes very high. (if the fees were paid in Ether instead of gas, then it might be the case that initially when the prices of Ether were low, the transactions fees were less and when the prices go high, the transaction fees increased a lot. This has created a need to decouple the transaction fees from the price of Ether. For that reason, a measurement unit ‘gas’ is introduced.) 
  5. The gas unit does not provide absolute price; it provides relative price.
  6. The simpler operation (e.g. addition) requires less gas compared to complex operations (such as division). 
  7. The conversion of gas cost into Ether is done on the basis of Network congestion (When there is congestion in the network, the price of gas to Ether goes up and need to pay more Ether for each unit of gas and if the congestion-free network is there then the cost of Ether is less comparatively).

 ---------------------------------------------------------------------------------------------------------------------------

Units of Ether:

  1. Gwei expresses the gas price. 
  2. Remember three units Wei, Gwei (1 Gwei=10^9 Wei) and Ether (1 Ether= 10^18 Wei).

 


 

---------------------------------------------------------------------------------------------------------------------------

How to calculate the Gas cost?

  1. Total Gas cost of any smart contract depends on the usage of opcodes in its equivalent (compiled) assembly language program. 
  2. It is sum of gas costs of all opcode’s execution in that program. 
  3. Refer the following link for more details:

https://fullstacks.org/materials/ethereumbook/18_appdx-evm-opcodes-gas.html

Gas Cost (Ether)= gas_price*Gas Cost(gas)

Ethereum transaction deals with 2 gas parameters: 1.Gas price and 2. Gas limit

We need to set both of these values in a transaction. To set the gas price we can use a website ethgassation.info. This website monitors the blockchain continuously and calculate what is the gas price that is generally accepted by miners. It gives us three types of recommendations: standard, Trader and fast. If you want your transaction mined to be really very fast then set the Gas price as 20 GWei as given in the below screenshot so that it will execute the transaction as soon as possible. If we set the Gas Price as 17.1 GWei the transaction will be executed fast and will be completed in less than 2 min and if you want to execute the transaction by standard mechanism paying 14.1 GWei will execute the transaction within 5 minutes (these prices changes time to time depending on network conditions). So, the more you want to execute the transaction faster, the more amount you need to pay. You can move around this website to get more information.

 

Calculate Gas Limit:

In truffle framework, we use traffic contract. Traffic contract automatically estimate the gas necessary for a transaction and fill the Gas Limit parameters of the transaction, so you don’t need to manage it by yourself. By default, its auto gas feature is turned on, but if we want we can turn it off.  

---------------------------------------------------------------------------------------------------------------------------


 

 

 



Comments