Blockchain | 5 MIN READ

Demystifying ‘Gas’ in Ethereum

If you are working on an ethereum based Dapp, then the term ‘gas’ is a word you cannot miss. But what makes ‘Gas’ more important in ethereum environment is its usage. It is defined as the fuel that drives the ethereum environment.

Though Gas is just an arbitrary number and unit of measurement and not the real ether we need to spend, it indeed determines how much ether we will have to spend.

Before diving into the need for gas, let me make clear that, any ethereum based Dapp or token have to pay some amount of ether for each transaction or contract deployment to the miners who verify your contract or transaction and add it to the live chain.

And here comes ‘Gas’, which will determine how much ether you are going to pay to miners to make your work done.

The term ‘Gas’ can be split into three main components for each transaction that takes place.

  • Gas Limit
  • Gas Price
  • Gas Used

And out of this, Gas Limit and Gas Price are controlled by the owner of the transaction. They set the value for these two. Whereas, the real devil is the Gas Used, which is not exactly predictable. The value will be revealed only after the transaction is completed and added to the chain. And your transaction fee will be paid out in ether based on gas used.

Let us deep dive into the definition for each term.

Gas Limit – It is the maximum no. of gas that the sender is ready to spend for that particular transaction to be mined. It is like setting a credit limit for your credit card. Ex: If you set gas limit for a transaction as 40000, then you are sending only 40000 gas along with the transaction to the mining pool.

By this way, you make sure that you are not spending too much for that transaction without your control. It is like, you send your child to a nearby grocery shop but will not give your entire wallet to him, instead send him with few dollars which should be high enough for the items he needs to buy.

Gas Price – It is the ether per gas you are ready to offer to the miner who mines your transaction. It is specified in gweis, the one billionth fraction of an ether. The miners will ideally pick up transactions in decreasing order of this gas price for them to be profitable. Hence, assigning more gas price will ensure your transaction be pushed to the front of the queue.

Gas Used – This is the actual gas that your transaction has consumed. As I said earlier, this is not predictable and will be available only after the transaction is mined. And after your transaction is mined,

A transaction fee of  gasUsed * gasPrice goes to the miner

How gas is used?

          Though it is impossible to predict the accurate gas amount that a transaction is going to consume, it is possible to decode how a transaction is going to consume gas.

         It is nothing but the amount of work that is needed to execute a method in the contract corresponding to the transaction. This involves various factors like, the input given to the method by that tx, the state of the chain at that moment, the byte code of the compiled contract etc.

For example, consider the below code:

     for(i=0; i<10; i++) {

            if(input == i)




Let us say, ethereum demands 100 gas for looping, hence if we call the above code with input as 1, then there will be 2 loops and hence will cost 200 gas. If the input is 4, it will cost 5 loops and hence will cost 500 gas.

Similarly in ethereum each operation like, storage, assignment, looping etc will consume some predefined amount of gas. As a contract developer, it is our responsibility to write an optimized code that will utilize gas economically, else at each execution your code will get penalized by unnecessary usage of gas.

Currently, for transferring an ether in a tx, 21000 gas will be used since the smart contract is predefined. And for transferring any other tokens, it may range from 30000 to few hundred thousands.

What if I set gas limit higher than gas used?

    If you have assigned gas limit higher than the actual gas needed for your tx, no worries. Ethereum will return back the unused gas to your address.

What if I set gas limit lesser than gas used?

    Here is where you get screwed. Or, you get screwed by yourself. Suppose if you specify gas limit as 4000, and gas price as 100 gweis and the tx after its execution took 5k gas. Then ethereum fails the transaction with an ‘Out of gas’ error. And you have to resend the tx. But, all the 4k gas you sent along with your tx will not be returned to you. Because a miner has done some work to verify your tx and hence Ethereum feels it is not ethically right to penalize the miner for the mistake done by you. He has to be paid for what he did. Hence all your 4k gas will be consumed as tx fee. Hence, you will lose 4k * 100 (gas price set by you) =  400000 gweis from your wallet and also your tx will fail.

Hence make sure you send sufficient amount of gas limit with your tx. Do not be too conservative and end up having your hard earned ethers gone in vain. Anyhow the unused gas will be returned back, hence no worries.

Vitality of Gas in the future

In the near future, ethereum is likely to replace the PoW consensus with PoS and in that case, the ethers that are rewarded to miners on appending one successful block may get removed. Hence the miners’ profit entirely depends on the tx fees they receive and hence the specification of gas limit, gas price and the optimal gas usage will entirely decide the likeliness of the tx being added to the chain sooner.

Leave a Reply

Your email address will not be published. Required fields are marked *

SHARE linkedin twitter