Ethereum Gas explained in a Nut Shell

Sidharth Pandita
hackerdawn
Published in
4 min readJul 21, 2021
Photo by Executium on Unsplash

Ethereum Gas? What is that, gas is for cars, right?

Well, yes, until sometime ago.

In this story, we will understand what is gas in Ethereum and why is it named that way. You don’t necessarily need to have prior knowledge of Ethereum for this purpose.

What is Gas?

In Ethereum, Gas is a unit that corresponds to the amount of computational effort that it will take to execute certain operations. A simple ETH transfer requires a small amount of gas. More complicated transactions, involving smart contracts, require more computational work and thus higher gas.

Let’s use an real life analogy.

Suppose you are driving a car on the highway and you pay some toll. Now, if you were driving a truck, you would have had to pay more tax as truck puts more stress on the road. Now, let’s compare this scenario to that in Ethereum:

In Ethereum, driving the car is equivalent to executing the smart contract. The toll is equivalent to gas fee you pay, toll pass is equivalent to gas and toll collector is equivalent to the miner.

Note here: Gas fee and gas are two different things. Gas is what you need and gas fee is what you pay to get the gas.

What is a Smart Contract?

You would have come across contracts several times in your life and would have probably signed some of them too. Now, just imagine a contract just like that in a digital form, consisting of lines of code. This is exactly what a smart contract is. A smart contract can sit on an address on Ethereum and does not need a third party like your regular contract. When the conditions coded in the smart contract are fulfilled, it gets executed automatically. Now you can relate more to the word “Smart” in Smart Contract.

Why should I pay the Gas fee?

Ethereum is a decentralized system but like any centralized system, it needs computers to power it. Transactions need to be processed, smart contracts (basically some lines of code) need to be executed. So who will do this? Miners come into play here, providing their computers for the computing needs of people on the Ethereum network. As the miners have to pay for these computers and the electricity required to power them, they need to be compensated in some way.

What is the Math behind it?

The gas fee in Ethereum is paid in units of Gwei. Gwei is a small unit of Ether (ETH) which is the currency of the Ethereum network.

One Gwei equals 0.000000001 ETH. Conversely, 1 ETH equals 1 billion Gwei! Gwei (Giga-wei) further consists of wei which is named after Wei Dai, a computer engineer known for his contributions to cryptography.

To calculate the total fee you need to pay, you have to multiply the gas units you need with the current gas price per unit.

Total fee = Gas units * Gas price per unit

For example, if a person Bob needs 500 gas units and each unit cost 10 Gwei, he needs to pay a fee of 5000 Gwei or 0.000005 ETH.

How do Miners receive this gas fee?

Ethereum miners are awarded this gas fee in return for their computational services like verifying and processing transactions on the network. Miners give preference to transactions with higher gas limits.

If the gas price limit is too low, miners can even choose to ignore such transactions. As a result, the price of a gas unit fluctuates with the supply and demand for processing power.

What is Gas Limit & how should I calculate it?

Gas limit is the maximum gas you are willing to spend on a transaction. A standard ETH transfer requires a gas limit of 21,000 units. The more the number of commands you want to execute, the more gas you have to pay. For example, a large smart contract will need higher gas than a smaller one.

So do you have to calculate the gas required for your transaction manually? No. There are ready-made functions that you can use from libraries like web3.js. There are also some 3rd party tools like https://www.ethgasstation.info/ which you can use for gas limit calculation.

How much Ether (ETH) will I need if I deploy a Dapp?

You might need less gas initially but as your dapp (decentralized-app) functionality grows more complex, the number of operations a smart contract performs grows too leading to higher gas requirements. If the gas demand rises, users can offer a higher gas price to out-bid other users’ transactions, leading to an increase in gas price. The rise in gas required and gas price mean a rise in Ether (ETH) that you need to run your Dapp on the blockchain.

We have come to the end of this story. If there is any other topic that is missed out here, you can head over to https://ethereum.org/, Ethereum’s official website filled with a ton of useful stuff. Happy Learning.

--

--