What is Gas? No, Not that Gas…
Many people do not understand the world of crypto, and I am setting out to educate you on a number of topics so that at worst, you can win a blockchain and crypto quiz!
By Aviva Õunap
Many people have heard of gas when overhearing about Ethereum (ETH) transactions — but what exactly IS gas, Well, it is easier to understand than you may think,
Ethereum is a blockchain-based, open-source smart contract platform, meaning that while the early cryptocurrencies, like Bitcoin, acted only as a store of value that could be transacted with, Ethereum allows users to create smart contracts, issue their own tokens and run decentralized apps (DApps), making it possible to do a lot more things on the blockchain than before.
Smart contracts are contracts running on the blockchain that execute themselves upon certain actions. You can compare smart contracts with vending machines. You insert some money into the vending machine, which triggers the ability to choose the beverage of your choice, your choice triggers the machine to release your drink. Smart contracts work similarly but instead of granting you a refreshing break, they execute code on the blockchain allowing users to create different decentralized applications.
These applications consist of different smart contracts working in harmony, which are executed by the Ethereum Virtual Machine (EVM). It takes the code from these applications and broadcasts it to the miners who run these codes.
These miners who keep the network running, run the code, and allow users to do all of these things on the Ethereum blockchain have to be motivated by something so that they’ll want to use their computing power to keep their mining operations up. And this is where gas comes in.
What is gas?
Sending gas is a sacrificial ritual to the Ethereum gods (otherwise known as miners) who make it possible for mere mortals to use the magical powers of the Ethereum blockchain. Or in other words, it is the transaction fee paid to the miners in exchange for their computing power that keeps the network running and allows users to transact. Gas is the thing that makes it profitable for miners to use their computing power to mine.
But why does Ethereum use gas instead of ETH? Is this some sort of an elaborate joke made by the Ethereum team to make things unnecessarily difficult?
Actually not, there’s a very simple explanation for that. While the price of ETH fluctuates quite drastically almost every day, the cost of computing remains constant. If the transaction fee would be a certain amount of ETH that would mean: a) that the network would constantly have to adjust to the new price of ETH, which would be really inefficient b) if the network doesn’t adjust to the new price, then during price drops mining would become unprofitable and the network would have a lot less (if any) miners. On the other hand, if the price increases drastically, transacting on Ethereum would become too expensive.
To solve this problem Ethereum uses gas. Gas is not a separate token that is used to pay the fee, instead, it is paid in ETH, but is calculated by multiplying gas limit and gas price. This disconnects the price of ETH from the fee that’s paid to the miners, which keeps the mining operations profitable to miners at all times and keeps the network stable.
Both gas limit and gas price can be changed when making a transaction, so why would anyone want to spend more money on a transaction if it’s not mandatory?
Gas price is the value of one unit of gas. So everyone could change the gas price, why would someone want to increase it, if they could just keep it low?
When someone makes a transaction on Ethereum blockchain (it is similar in almost all blockchains), the transaction is first checked to confirm that the account actually has enough funds to make such a transaction. If everything is OK, the transaction is added to a pool that contains all of the unmined transactions (in the senders perspective, this is where your transaction waits if you see that it is “pending”).
When miners put together the blocks they are mining they fill them with the transactions that are in this pool. All of these transactions have gas attached to them which will be given to the miners if they include these transactions to their blocks. In the process of adding transactions to their blocks, miners prioritize transactions with higher gas price, as these are more profitable to them.
Long story short, gas price determines how quickly your transaction is included in a block. Higher gas price ensures a faster transaction, while lower gas price saves you some money, but is added to a block much slower.
At the time of writing the standard gas price is around 4 Gwei (0.000000004 ETH), which will get your transaction confirmed in less than 5 minutes. IF you’re in a hurry, setting your gas price at 25 Gwei will get your transaction confirmed in less than 2 minutes, but if time is not of the essence, you can set your gas price lower. These gas prices can be checked here.
When miners set up their mining operations they create a set of rules for it. They can define the minimum gas price of transactions that they’ll add to their blocks. Setting a threshold for the gas price allows miners to only concentrate on profitable transactions, maximizing their profits. There’s no point in mining transactions that result in a low profit or in a loss.
Gas limit is the maximum amount of gas you are willing to spend on a transaction.
Smart contracts run until they are either completed or when they run out of gas. If there wouldn’t be a gas limit, then there would be nothing that would stop a contract with an error for running forever.
Gas limit is there to protect the network from DDoS attacks, making sure that the network isn’t spammed with a bunch of looping contracts that waste resources and clog up the network, and it also avoids situations where a contract with an error keeps running forever, devouring the creator’s valuable resources.
All of the functions that make up smart contracts have a set amount of gas they use, which are written down in the Ethereum yellow paper. For example, a simple ETH transaction between two wallets uses 21,000 gas.
If someone tries to create a transaction with an insufficient gas limit, then the transaction fails due to an “out of gas” error and the funds are not sent. While the funds will not leave the wallet, the gas attached to the transaction is given to the miners regardless whether the transaction succeeds or not. Miners still had to spend their computing power to process your failed transaction.
Now let’s quickly illustrate how gas is calculated and spent.
If you want to send 1 ETH to your friend, you would have to pay an additional 21,000 (gas limit for an ETH transaction) x 0.000000004 ETH (4 Gwei, the standard gas price) = 0.000084 ETH for gas.
If you want to make sure that the transaction is completed as soon as possible you might want to increase the gas price to make it more attractive to the miners to add it to their blocks. Setting your gas price at 25 Gwei means that your transaction gets completed in less than 2 minutes and you’ll have to pay 21,000 x 0.000000025 ETH = 0.000525 ETH.
If you would have set the gas limit at 20,000 and keep the gas price the same, your 1 ETH would not reach your friend, but the 20,000 x 0.000000004 ETH = 0.00008 ETH will still be paid to the miners.
If you would have set your gas limit at 30,000 instead and kept the price the same, your 1 ETH will reach your friend, 21,000 x 0.000000004 ETH = 0.000084 ETH will be given to the miners, but the remaining 9,000 x 0.000000004 ETH = 0.000036 ETH will be refunded to you.
Why does the Ethereum network get clogged up?
Like we talked before, all of the unmined/pending transactions are in a pool, waiting for their time to be added to blocks. During normal times everything works just fine, the amount of new transactions being broadcast to the pool is around the same as the number of transactions getting mined and added into blocks.
But during hectic periods, like ICOs, or when someone creates an app that uses a lot of micro-transactions, like Cryptokitties, then the network can clog up quite fast. This is because the amount of transactions entering the mining pool exceeds the number of transactions getting mined. It’s like an actual pool at a waterpark, where there are hundreds of people sliding down the waterslide, but the pool only has one ladder out, and the pool quickly gets filled with people.
This is one of the main problems with the current blockchain platforms that needs to be solved before the technology becomes viable for everyday use. For example, Status ICO created delays in the network lasting for hours and even days, and Cryptokitties quickly filled up the mining pool, causing over 10,000 pending transactions within a couple of days after going live.
To get a transaction mined during busy times there are mainly two ways. One way is to increase your gas price so that the miners are incentivized to include your transactions to their blocks. And the other is for the miners to increase their block gas limit, this means that more transactions fit into the blocks.
Block gas limit is the maximum amount of gas that can be included in a block. It is similar to the 1MB bitcoin block size, but since Ethereum can do a lot more than Bitcoin, measuring the blocks in terms of kilobytes doesn’t work. Some contracts that have a small size could run forever and waste a lot of resources. This is why Ethereum uses block gas limit instead of a certain block size.
Participating in an ICO
One of the most popular use cases for Ethereum is issuing your own token and launching an ICO. Tokens created on Ethereum are called ERC20 tokens. These tokens, as well as their ICOs, are based on Ethereum’s smart contracts.
Like we talked before the amount of gas used for a transaction depends on the amount of code in the contract. While regular ETH transactions use a constant 21,000 gas, token transactions can range anywhere from 50,000 to 200,000 or even more gas.
ICOs are usually time-bound, meaning that there’s a certain timeframe when these tokens can be purchased. And since these token transactions tend to clog up the Ethereum network, it is often suggested to use a higher gas price. All of the information about the gas limit and gas price is usually provided by the ICO creator when announcing the launch of the ICO.
If you set your gas limit too low when purchasing ICO tokens, this can result in an “out of gas” error and you’ll lose the transaction fee, or if you don’t have a high enough gas price, your transaction could take a long time before getting confirmed.
• Gas is the transaction fee that is paid to the miners in exchange for their computing power, which keeps the network running, processes data, runs code and validates transactions;
• Gas is not a separate token, it is paid in ETH;
• The transaction fees are presented in gas instead of ETH because while the price of computing remains constant, the price of ETH fluctuates quite a bit. Disconnecting the price of ETH from the cost of computing creates more stability and flexibility;
• Transaction fee = gas limit x gas price;
• Gas price is the value of one unit of gas.
o Higher gas price ensures that your transaction is prioritized by the miners and processed faster;
o Lower gas price will save you some money, but your transaction gets mined slower;
• Gas limit is the amount of gas you are willing to spend on a transaction. Ethereum’s smart contracts consist of different functions that use a predetermined amount of gas for running and everyone who wants to transact with these contracts has to provide the sufficient amount of gas to get the transaction completed.
o If the gas limit is set too low, then the transaction fails due to “out of gas” error;
o If the gas limit is set higher than necessary, then the transaction is completed and the remaining, unused gas is refunded to the sender;
• During hectic periods, like during popular ICOs or when there’re a lot of micro-transactions (Cryptokitties), then the Ethereum network can get clogged up and create delays lasting for hours or even days. Increasing your gas price to take into account the network congestion, will get your transaction completed faster;
• ICOs are different from regular ETH transactions. They use more functions than regular transactions and thus require more gas to run. These amounts are usually presented by the creator of the ICO. Due to increased amounts of transactions, it’s also suggested to increase the gas price to ensure a faster confirmation time.
So, this is a little lesson about gas and a chapter from our new ebook and online course — so I hope you enjoy it!
I would also like to thank Mihkel Šorin for being the lyrical master that he is.