In this article I will cover few tricky questions to those who are new to Ethereum. What is gas? Why do you have to pay for Ethereum transaction in gas instead of directly in ether? What is Gas Price? Why can you set it yourself and why would you want to do it? And what the heck is Gas Limit? By reading the following article sending ether transaction will become obvious and easy.
We will start with the basics. Bitcoin and Ethereum are minable cryptocurrencies which means that the computing power of mining nodes via proof-of-work is used to secure their networks. Obviously, mining right now is a serious business which requires, as of Jan 2018, 41 Terawatt-hours (TWh) for the Bitcoin network1 and 11TwH2 for the Ethereum network. Miners for their work are rewarded with tokens from block rewards and transactions fees. Whereas Bitcoin transactions fees are taken simply in bitcoins and denominated in bitcoins, in Ethereum fees for transactions and smart contracts are taken in ether 3 but denominated in gas.
Gas – internal pricing for sending a transaction or running smart contracts on Ethereum used by Ethereum Virtual Machine. When you make a new transaction, you need to set a Gas Limit and Gas Price (denominated in ether) and the total fees will be calculated by the formula: Gas Limit * Gas Price.
Why can’t you set fees simply in ether?
Why Ethereum requires gas in the first place? A fairly good question, I would say. In Bitcoin network you set a fraction of a bitcoin as a fee4. Everything is quite simple and intuitive. Why then with Ethereum you need to deal with mysterious Gas Limit and Gas Price? To answer that question we first need to look at two different purposes of Bitcoin and Ethereum systems. In short, Bitcoin is for transactions and Ethereum for smart contracts. It is true that Bitcoin protocol allows some smart contracts but it doesn’t directly support them. Programmers who want to create for example an escrow contract on Bitcoin blockchain must use the Bitcoin scripting. It may come as a surprise to you but the scripting language used by the Bitcoin network is very limited and you can’t do much with it. It wasn’t a mistake. Bitcoin scripting system was purposely designed that way to make it Turing non-complete. Why? For deterministic reasons. Bitcoin scripting doesn’t allow any loops so you always know when and how a given program will end. That simplicity ensures maximum security and stability.
Ethereum, on the other hand, is designed completely different. The main Ethereum feature are programmable agreements enforced through blockchain technology, commonly called smart contracts. Ethereum platform also provides their own programming language, called Solidity, designed only for writing smart contracts .
Solidity is a contract-oriented programming language for writing smart contracts. It was proposed by a British programmer and Ethereum co-founder Gavin Wood.
Solidity enables to write smart contracts with only a few lines of code. Probably the fewer lines, the better because every single one not only counts but also costs! Validating smart contracts requires computational power which depends on a contract’s complexity. The shorter the contract, the less computational power it will use and therefore it will cost less gas.
Gas is simply a way to decouple the market value of ether from the price of running and validating Ethereum transactions and smart contracts. Setting a fixed amount of ether (like 0.0001 ETH for a line of smart contract) is not flexible enough because the actual cost depends on the market variables (what is the total of the actual computational power of all Ethereum mining nodes divided by the number of transactions and smart contracts waiting for validation). To make it even simpler to understand, you can think of gas as a unit of computational power similar to Kilowatt (kW) for electric power. Before sending a transaction a user can set the Gas Price for a single gas he is willing to pay for his transaction to be validated. A miner on the other end sets his own minimum Gas Price for which he is willing to validate transactions, so the higher the Gas Price, the quicker your transaction will be validated (because more miners will be happy to pick it up from the mempool and validate it). It is always a good habit to check the current gas prices (link) before creating a new Ethereum transaction. As a time of writing you should set the Gas Price to 20 gwei if you want your transactions to be validated in less than 2 minutes, 3 gwei for less than 5 minutes. And if you are not in a hurry and willing to wait up to half an hour, only 2 gwei are needed. Now what is gwei? It is a funny name for denominating ether (like cents to pounds). One ether equals to 1bln gwei. There are more ether denominations apart from gwei, for example one gether which is a 1000 ether (you probably didn’t know that if you only have 1 gether you are quite a wealthy person5). If you are curious about all ether denominations like wei, szabo, finney, tether go here.
That’s all really cool but what the heck is Gas Limit?
I hope I’ve clearly explained what gas and Gas Price are and why we need to use them. But this knowledge is yet not everything when it comes to sending Ether transactions. There is also a little bit mysterious element called Gas Limit. Why would we need to set a maximum amount of gas which we are willing to pay for a transaction? Shouldn’t we know how much gas a given transaction requires? The answer is yes and no. A simple transaction from wallet A to wallet B requires exactly 21000 gas. Not a single gas less or more. If you put more, remaining gas will be refunded to you. On the other hand, if you put less than 21k, gas transaction won’t be validated and you lose your gas. The need of setting Gas Limit occurs when you send your ethers for a smart contract, like ICO. As I already mentioned, Ethereum provides their own programming language for smart contracts – Solidity. It is a Turing complete language6 which means it has loops7. Every single programmer knows what a possible problem with a loop is – if a code has a bug, it may never end! In the most of programs that is not a disaster. A program simply freezes and all you have to do is to press restart. With smart contracts though you have to pay for every single instruction and therefore any restart might be costly. Like really costly. Theoretically a buggy loop may have an infinite amount of iteration which means it would cost you an infinite amount of gas. I can assume you wouldn’t happily pay all your ethers only because a smart contract programmer had a bad day. Gas Limit was created exactly in order to protect you from that. By setting it, you state what is the maximum amount of gas you are willing to pay. If smart contract execution exceeds your set limit, it is automatically halted. All smart contracts must provide the Gas Limit which you should set to be sure that your transaction will be handled.
As the old saying goes – one image is worth a thousand words so, to sum this information up, I will provide you with a graphic with all the needed fields marked. Now you’ve gained new Ethereum Jedi Power!
- Which is close to the energy consumption of New Zealand; source: https://digiconomist.net/bitcoin-energy-consumption
- Which is close to the energy consumption of Lithuania; source: https://digiconomist.net/ethereum-energy-consumption
- Ethers are tokens in the Ethereum Network parallel to bitcoins in the Bitcoin Network.
- As of Jan 2018 average fee equals to 0.001BTC. You can check the current different fees and times of transaction confirmation here: link
- According to the current ether prices Feb 2018, one gether costs almost a million dollars
- Programming language that can simulate any Turing machine more info
- For non-programmers: loops are iteration which allows iterations of the same code