ELI5 : Ethereum Gas | Smart Contract Execution |
This article assumes you have at least a vague idea of what a blockchain is.
We all know that hosting any kind of content on the internet requires some amount of capital. Whether it’s hosting your website or database on a server, it has to be available 24/7 to present the content to anyone on the internet asking it.
Well of course there are some free tier services which offer free hosting up to a limit but let’s say we have a real application that needs to be hosted
When we host content on a cloud service like Google Cloud, we pay a monthly / yearly fee for all the charges incurred in keeping it running day and night. But a blockchain is distributed to all the computers in the network, so questions like these arise :
- Who do you pay?
- How much do you pay?
- How do you calculate how much to pay?
The solution to this scenario is where Gas comes in. But before we dive into the meat of this article let’s make sure you know these 2 basic concepts.
- It’s free to read data on the blockchain
yes, the data on a blockchain is open to everyone and free to read. Probably obvious since every computer in the network has a local copy of the entire blockchain.
- Adding data on the blockchain will incur charges
The reason why addition of data requires money is because the data has to be added onto a block, verified by the consensus mechanism, validated by the network and finally added to the blockchain with a copy sent to all computers, also known as mining which requires some amount of work to be done. And since no one would do that work for free, there has to be a financial incentive where gas fees come into play.
So Ethereum has a public chart in which it lists the gas units required for any type of operation.
Notice how I said Gas Units and not dollars or ETH (which I’ll explain in the end of the article). Also notice I said “any type of operation”, yes that’s because a transaction is classified as a type of operation in the Ethereum blockchain.
“What other operations exist on the blockchain?” you might ask and this is where the key difference between Bitcoin and Ethereum exists. The blockchain of Bitcoin was designed only for transactions, Alice gives Bob $5 then Bob gives $7 to Charles yada yada yada…. all stored on the blockchain, but Vitalik (co-founder of Ethereum) thought storing only transactions was boring and introduced something known as “Smart Contracts”.
Explaining Smart Contracts is out of the scope of this article but let me just try to give you a summary in a few lines.
So Vitalik had an idea 💡, the place in a block where transaction data is stored, he thought of including pieces of code. These pieces of code when put together in a single file would be called Smart Contracts.
These pieces of code or Smart Contracts would have their own address/account where you could deposit some money and write code on how/when/where to distribute it and plenty of other cool stuff.
Since these contracts contained code, It would cost a lot more computing resource for the computers in the network to execute all the code, that is why Vitalik and gang came up with a whole list of operations with their respective gas units that should be charged.
If you’re curious here is the list
So let’s say the Smart Contract contains the code
c = a + b
this is an addition operation and cross-checking the list we can see that it will be charged 3 Gas Units.
In a similar fashion all the code is executed and for every operation Gas Units are deducted. If your “Gas Limit”, meaning the maximum amount of Gas Units you are willing to spend runs out, then the execution is aborted. Also notice it takes 21,000 units of Gas for a single transaction.
Let’s talk about Gas Units now. So the reason why Gas Units are not directly convertible to USD or ETH is to keep it more like an auction instead of a simple price. Let me explain,
So let’s say you want to transfer $10 to your friend, but you’re not in a hurry. And another person Charlie wants to send $10 to his wife who urgently needs the money, so Charlie can offer double the money to include his transaction first, so the miners of the blockchain will first validate his transaction then yours even though you initiated the transfer before him.
This goes both ways, suppose I don't mind if my transaction takes a couple of days, I can opt for giving less gas fees and get away with it too. Kinda like Amazon prime deliveries vs a normal customer.
So to keep this incentive alive the Ethereum blockchain lets you decide how much you want to pay for the gas fees. Consider this table below.
gasPrice : Indicates how much you are willing to pay per Gas Unit
gasLimit : Indicates the maximum limit of Gas Units you are willing to pay
You might think 300 ETH per gas unit is unrealistic and you are right, that is not 300 ETH.
ETH has its own units, see the diagram below to understand this.
So the 300 in the gasPrice row we just saw above was in wei. And 10¹⁸ wei is around 1 ETH so suffice to say it’s a reasonable amount.
In conclusion, this hypothetical operation, can consume a maximum of
300 (gasPrice)* 20 (gasLimit) = 6000 wei
6000 wei as Gas (fees) and not more than that.
And since we discussed the auction like nature of Ethereum Gas Fees, when there’s high traffic and lots of transactions going through, the fees can skyrocket to even $50 when converted to USD !
So the next time your crypto friend complains of high gas, you know he’s not talking about his stomach but gas fees. Kudos till next time !
I am a Software Developer who is obsessed with producing clean and maintainable code following well-known Software Architectures. I write about problems I have faced myself and how I improved overtime. If you’d like to see more such articles, make sure to follow me.