What is Ethereum Gas: Step-By-Step Guide
Ethereum Gas – is the lifeblood of the Ethereum ecosystem, there is no other way of putting that. Gas is a unit that measures the amount of computational effort that it will take to execute certain operations.
Every single operation that takes part in Ethereum, be it a simple transaction, or a smart contract, or even an ICO takes some amount of gas. Gas is what is used to calculate the amount of fees that need to be paid to the network in order to execute an operation.
In this guide, we are going to understand how gas works. But before we do so, there are several concepts that we must learn. So, without further ado, let’s begin our deep dive on Ethereum Gas.
What is Ethereum Gas: Step-By-Step Guide
Bitcoin, Ethereum, and the Advent of Smart Contracts
Bitcoin was created because everyone was asking the same questions.
- Will it be possible to create a form of money which can be transferred between two people without any middleman?
- Will it be possible to create a decentralized money which can function on something like the blockchain?
Satoshi Nakamoto answered these questions when he created bitcoin. We finally had a decentralized monetary system which can transfer money from one person to another.
However, there was a problem with bitcoin which is a problem with all first generation blockchains. They only allowed for monetary transactions, there was no way to add conditions to those transactions.
Alice can send Bob 5 BTC, but she couldn’t impose conditions on those transactions. Eg. She couldn’t tell Bob that he will get the money only if he performed certain tasks.
These conditions would need extremely complicated scripting. Something was required to make the process more seamless.
…And that “something” was a smart contract.
What is a smart contract?
Smart contracts help you exchange money, property, shares, or anything of value in a transparent, conflict-free way while avoiding the services of a middleman.
Vitalik Buterin’s Ethereum is easily the stalwart of this generation. They showed the world how the blockchain can evolve from a simple payment mechanism to something far more meaningful and powerful.
So, what are these “smart contracts” and what’s the big deal?
Smart contracts are automated contracts. They are self-executing with specific instructions written in its code which get executed when certain conditions are made.
You can learn more about smart contracts in our in-depth guide here.
Smart contracts are how things get done in the Ethereum ecosystem. When someone wants to get a particular task done in Ethereum they initiate a smart contract with one or more people.
Smart contracts are a series of instructions, written using the programming language “solidity”, which works on the basis of the IFTTT logic aka the IF-THIS-THEN-THAT logic. Basically, if the first set of instructions are done then execute the next function and after that the next and keep on repeating until you reach the end of the contract.
The best way to understand that is by imagining a vending machine. Each and every step that you take acts like a trigger for the next step to execute itself. It is kinda like the domino effect. So, let’s examine the steps that you will take while interacting with the vending machine:
- Step 1: You give the vending machine some money.
- Step 2: You punch in the button corresponding to the item that you want.
- Step 3: The item comes out and you collect it.
Now look at all those steps and think about it. Will any of the steps work if the previous one wasn’t executed? Each and every one of those steps is directly related to the previous step. There is one more factor to think about, and it is an integral part of smart contracts. You see, in your entire interaction with the vending machine, you (the requestor) were solely working with the machine (the provider). There were absolutely no third parties involved.
So, now how would this transaction have looked like if it happened in the Ethereum network?
Suppose you just bought something from a vending machine in the Ethereum network, how will the steps look like then?
- Step 1: You give the vending machine some money and this gets recorded by all the nodes in the Ethereum network and the transaction gets updated in the ledger.
- Step 2: You punch in the button corresponding to the item that you want and record of that gets updated in the Ethereum network and ledger.
- Step 3: The item comes out and you collect it and this gets recorded by all the nodes and the ledger.
Every transaction that you do through the smart contracts will get recorded and updated by the network. What this does is that it keeps everyone involved with the contract accountable for their actions. It takes away human malice by making every action taken visible to the entire network
What is the Ethereum Virtual Machine?
Before we understand what the Ethereum Virtual Machine (EVM) is, we must understand why a “Virtual Machine” is needed.
So let’s go back to smart contracts.
What are the desirable properties that we want in our smart contract?
Anything that runs on a blockchain needs to be immutable and must have the ability to run through multiple nodes without compromising on its integrity. As a result of which, smart contract functionality needs to be three things:
Feature #1: Deterministic
A program is deterministic if it gives the same output to a given input every single time. Eg. If 3+1 = 4 then 3+1 will ALWAYS be 4 (assuming the same base). So when a program gives the same output to the same set of inputs in different computers, the program is called deterministic.
There are various moments when a program can act in an un-deterministic manner:
- Calling un-deterministic system functions: When a programmer calls an un-deterministic function in their program.
- Un-deterministic data resources: If a program acquires data during runtime and that data source is un-deterministic then the program becomes un-deterministic. Eg. Suppose a program that acquires the top 10 google searches of a particular query. The list may keep changing.
- Dynamic Calls: When a program calls the second program it is called dynamic calling. Since the call target is determined only during execution, it is un-deterministic in nature.
Feature #2: Terminable
In mathematical logic, we have an error called “halting problem”. Basically, it states that there is an inability to know whether or not a given program can execute its function in a time limit. In 1936, Alan Turing deduced, using Cantor’s Diagonal Problem, that there is no way to know whether a given program can finish in a time limit or not.
This is obviously a problem with smart contracts because, contracts by definition, must be capable of termination in a given time limit. There are some measures taken to ensure that there is a way to externally “kill” the contract and to not enter into an endless loop which will drain resources:
- Turing Incompleteness: A Turing Incomplete blockchain will have limited functionality and not be capable of making jumps and/or loops. Hence they can’t enter an endless loop.
- Step and Fee Meter: A program can simply keep track of the number “steps” it has taken, i.e. the number of instructions it has executed, and then terminate once a particular step count has been executed.Another method is the Fee meter. Here the contracts are executed with a pre-paid fee. Every instruction execution requires a particular amount of fee. If the fee spent exceeds the pre-paid fee then the contract is terminated.
- Timer: Here a pre-determined timer is kept. If the contract execution exceeds the time-limit then it is externally aborted.
Feature #3: Isolated
In a blockchain, anyone and everyone can upload a smart contract. However, because of this the contracts may, knowingly and unknowingly contain virus and bugs.
If the contract is not isolated, this may hamper the whole system. Hence, it is critical for a contract to be kept isolated in a sandbox to save the entire ecosystem from any negative effects.
Now that we have seen these features, it is important to know how they are executed. Usually, the smart contracts are run using one of the two systems:
- Virtual Machines: Ethereum uses this.
- Docker: Fabric uses this.
Let’s compare these two and determine which makes for a better ecosystem. For simplicity’s sake, we are going to compare Ethereum (Virtual Machine) to Fabric (Docker).
So, as can be seen, Virtual Machines provide better Deterministic, terminable and isolated environment for the Smart contracts. However, dockers have one distinct advantage. They provide coding language flexibility while in a Virtual Machine (VM) like Ethereum, one needs to learn a whole new language (solidity) to create smart contracts.
The EVM is the virtual machine in which all the smart contracts function in Ethereum. It is a simple yet powerful Turing Complete 256-bit virtual machine. Turing Complete means that given the resources and memory, any program executed in the EVM can solve any problem.
What is Ethereum Gas?
As explained in the introduction, Gas is a unit that measures the amount of computational effort that it will take to execute certain operations.
Note: Before we continue, huge shoutout to Joseph Chow for his amazing presentation on Ethereum gas.
Most of the smart contracts that run in the EVM are coded using Solidity (Ethereum is planning to move on to Viper from Solidity in the future). Each and every line of code in Solidity requires a certain amount of gas to be executed.
The image below has been taken from the Ethereum Yellowpaper and can be used to gain a rough idea of how much specific instructions cost gas-wise. Every transaction requires at least 21,000 gas according to this table:
Image Courtesy: Ethereum Yellow Paper
To better understand how gas works in Ethereum, let’s use an analogy. Suppose you are going on a road trip. Before you do so you go through these steps:
- You go to the gas station and specify how much gas you want to fill up in your car.
- You get that gas filled up in your car.
- You pay the gas station the amount of money you owe them for the gas.
Now, let’s draw parallels with Ethereum.
Driving the car is the operation that you want to execute, like executing a function of a smart contract.
The gas is well….gas.
The gas station is your miner.
The money that you paid them is the miner fees.
All the operations that users want to execute in ethereum must provide gas for the following:
- To cover its data aka intrinsic gas.
- To cover its entire computation.
Now that we have covered the bare basics, you maybe asking the following question.
Why do we have this Gas system?
The answer is simple…incentivization.
Like any proof-of-work peer-to-peer system, Ethereum is heavily dependent on the hashrate of their miners. More the miners, more the hashrate, more secure and fast the system.
In order to attract more miners into the system, they need to make the system as profitable and alluring as possible for the miners. In Ethereum, there are two ways that miners can earn money:
- By mining blocks and getting block rewards.
- By becoming temporary dictators of their mined blocks.
Let’s explore the second point.
The miners are responsible for putting transactions inside their blocks. In order to do so, they must use their computational power to validate smart contracts. The gas system allows them to charge a certain fee for doing so.
This fee is known as the miner’s fee and it helps incentivize them enough to take part actively in the ecosystem.
So, how much fees can they charge? Before we can calculate that let’s understand how we measure gas.
Gas is simply measured in units of gas. A transaction sent to the Ethereum network costs some discrete amount of gas (e.g. 100 gas) depending on how many EVM instructions need to be executed.
So, how do we convert the gas into Ether?
There is no fixed price of conversion. It is up to the sender of a transaction to specify any gas price they like. On the other side, it is up to the miner to verify any transactions they like (usually ones that specify the highest gas price). The average gas price is typically on the order of about 20 Gwei (or 0.00000002 ETH), but can increase during times of high network traffic as there are more transactions competing to be included in the next block.
The following chart shows you the average Ethereum gas price chart.
Image courtesy: Etherscan.
Before we go any further, it is important to know the concept of gas limit.
What is Ethereum Gas Limit?
In order to get an operation done in Ethereum, the sender of the transaction must specify a gas limit before they submit it to the network. The gas limit is the maximum amount of gas the sender is willing to pay for this transaction.
When specifying a gas limit, the following points must be considered:
- Different operations will have different gas costs (as has been shown before).
- The miners will stop executing the moment the gas runs out.
- If there is any gas left over, it will be immediately refunded to the operation generator.
Let’s see this in operation in a hypothetical scenario.
Suppose, we are adding two numbers and for that the contract must do the following actions:
- Storing 10 in a variable. Let’s say this operation costs 45 gas.
- Adding two variables, let’s say this costs 10 gas.
- Storing the result which again costs 45 gas.
Suppose the sender specifies a gas limit of 120 gas.
The total gas used by the miner to run the computation is (45+10+45) = 100 gas.
The fee that is owed to the miner, assuming 1 gas costs 20 Gwei, is (100 * 20 Gwei) = 0.000002 ETH.
Now, how much gas is left over?
120 – 100 = 20 gas.
The 20 unused gas is returned back to the sender (20 * 20 Gwei) = 0.0000004 ETH.
So, having said that, there are two scenarios that one must consider:
- The specified gas limit is too low.
- The specified gas limit is too high.
Scenario #1: The Gas Limit is too low
If an operation runs out of gas, then it is reverted back to its original state like nothing actually happened, however, the operation generator must STILL pay the miners the fee for their computational costs and the operation gets added to the blockchain (even if it has not been executed).
Going back to our road trip analogy, if you haven’t filled up enough gas in your car, then you will not be able to reach your destination, but even then you paid the gas station the money for the fuel right?
Let’s see how this works in our hypothetical smart contract. The steps were:
- Storing 10 in a variable. Let’s say this operation costs 45 gas.
- Adding two variables, let’s say this costs 10 gas.
- Storing the result which again costs 45 gas.
However, this time, the sender sets a gas limit of 90 gas.
Now, we know that the gas that will be required for fulfilling the transaction is 100 gas, but we only specified 90 gas limit.
In this scenario, the miner will do 90 gas worth of computation and then charge the sender fees for the 90 gas which turns out to be (90 * 20 Gwei) = 0.0000018 ETH.
Also, the contract reverts back to its original state and the transaction is included in the blockchain.
Scenario #2: The Gas Limit is too high
So, what if we set the gas limit too high?
That would make sense to do right? Afterall, whatever is leftover gets refunded to the sender right?
That sounds good on paper but it doesn’t really work that well in reality.
Miners are limited by the block gas limit, which we’ll suppose is 6,700,000 gas. A basic transaction (simple transfer of ETH) has at least a gas requirement of 21,000 gas. Miners can only include transactions which add up to be less than or equal to the block gas limit.
Image courtesy: Hackernoon
Suppose there is a transaction A (which does a simple transfer of ETH) and has a specified gas limit of 42,000 and two transactions B and C (also simple transfers of ETH) which have specified gas limits of 21,000.
Which will make more sense for a miner to put in their block?
- Will they put in transaction A and refund back a huge amount of unused gas?
- Or will they put transactions B and C and refund little to nothing back?
The second point makes more sense to them economically right?
This is precisely why having a bloated gas limit is not the sensible way to go. It is more reasonable to set a gas limit which is just a little higher than the required amount of gas for your transaction.
The following is the average gas limit chart.
Image Courtesy: Etherscan
High and Low Gas vs High and Low Fee
It should be clear to you so far that gas and ether are not the same things. Gas is the amount of computational power required while ether is the currency used to pay for that gas.
Now with the knowledge of everything we have obtained so far, let’s go through certain gas and fees scenarios.
If an operation has LOW gas, then the miners won’t even pick it up because it doesn’t have enough gas to finish computation.
If an operation has LOW fees, then it might have just enough gas to cover it but still, the miners won’t be chomping at the bits to pick it up because an operation with low fees isn’t economically appealing for them.
If an operation has HIGH gas, then it means that the operation is bloated with a high gas limit and hence the miners will not pick it up.
If an operation has HIGH fees, then the miners know that they will make a lot of money from it and will be picking it up instantly.
The currently recommended gas prices for different types of transaction speeds, according to ethgasstation are:
What Happens in Gas Refund Scenarios?
In solidity, there are two commands which ensure that you get some gas refund back.
- SUICIDE: This basically kills the smart contract. Doing so will get you back 24000 gas.
- SSTORE: Storage deletion, which gets you back 15,000.
So, if your contract is using up 14,000 gas and deletes a storage then you should get back (15000-14000) 1000 gas refunded to you right?
It isn’t that simple.
If that was the case, then miners will lose all incentive. After all, the miners shouldn’t pay you to do your computations right?
To avoid scenarios like these, a condition was put in.
The refund that has been accumulated cannot exceed half the gas used up during computation.
Let’s take an example to clear this up.
…Suppose we have a smart contract which uses up 14,000 gas.
The gas limit that we have set up is 20,000 gas.
The smart contract also includes an SSTORAGE command.
So, how much gas will the contract creator get back post computation?
Firstly, they will get back (20,000-14,000) = 6,000 units of unused gas.
Now, the SSTORAGE command has also been used, so theoretically they should get back 15,000 gas as well.
However, the amount of gas that has been used in the contract is 14,000 and since 15,000 > 14,000/2, the REFUND generated will be 14,000/2= 7000.
So the total gas that the creator is getting back in the end is 6000+7000 = 13,000.
Let’s take another example.
Suppose this time the contract uses up 70,000 gas and it includes a SUICIDE function.
A SUICIDE function should give you 24,000 gas back which is < 70,000/2.
In this situation, the gas refund will be 24,000 + unused gas.
Criticism of Ethereum Gas. Is it Justified?
Even though the gas system has gotten praise for presenting a smoothly running mechanism which incentivizes the miners pretty positively, it has come under criticism lately for being a tad too expensive for developers and smart contract creators.
Regarding this, Danny Ryan did some interesting studies in his Hackernoon article.
Consider the following scenario:
When two numbers are added a million times in Ethereum it costs ~$26.55 in fees.
Danny Ryan compared that to a standard AWS system. He said that he can add two numbers a million times using python in 0.04 seconds, which going by the $0.0059 hourly Amazon EC2 rate costs $0.000000066.
This means that computation in Ethereum is 400 million times more expensive!
Based on his studies, this is the conclusion he made:
“To be fair, adding two numbers together 1 million times is a bit contrived. A well written contract would likely move such computational complexity off-chain and deal more with updating state in the contract. Storing vast amounts of data to the blockchain is also not an ordinary task. Depending on the task, a user would likely store a cryptographic reference (a hash) of the data on-chain and keep the rest of the data off-chain.
That said, we as developers need to be aware of these costs, and design dApps accordingly. We need to find the balance between on-chain and off-chain complexity, while still leveraging the decentralized capabilities of the blockchain.”