Ethereum Gas Explained in Plain English
In this post I will try to explain one of the most confusing aspects of Ethereum — gas. You have probably heard about it but most likely, it did not make much sense to you. The concept of gas is indeed confusing partly because it is a low-level concept used by the Ethereum computing engine (EVM). However, gas is what keeps Ethereum running and it is important to have a clear understanding of this concept. I promise you, after reading this post you will have the clearest understanding of gas.
The first question to ask is why gas exists at all. After all, there is no concept of gas in Bitcoin or in other cryptocurrency for that matter. What makes Ethereum unique and different from Bitcoin is existence of smart contracts. Smart contract is just a fancy name of a computer program.
Forget about Ethereum and smart contracts for a moment. Think about your car. Your car has an engine that requires gasoline to operate. To get from point A to point B, the engine burns some gasoline. The longer the distance, the more gasoline is required. You purchase gasoline at a gas station and pay money for it. However, how do you estimate how much a certain trip will cost you in terms of money? First, you estimate total consumption of gasoline. For this, you take approximate distance of the trip (e.g. in kilometers) and multiply it on average consumption by your car (say 9 liters per 100 km). Further, you multiply total consumption on the price of gasoline (e.g. 1.55 Euro per liter). Thus, you arrive at an approximate cost of the trip.
Ethereum works almost exactly like this. A smart contract contains commands executed by a computer. This means that when a smart contract runs, it consumes someone’s computing power like your car engine consumes gasoline. This someone needs to be paid for the provided computing resources. Thus, every command in a smart contract has a price measured in terms of gas (pretty much like every kilometer covered burns certain amount of gasoline). Some commands cost more, some commands cost less but they are never free. Think about your car again. Your car will consume different amounts of gasoline depending whether you are driving on a highway, going uphill or fighting through a dense traffic in a city. Likewise, different commands in a smart contract put different strain on a computer and thus consume different amounts of gas.
Let us sum up what we have thus far:
- Ethereum is like a car engine.
- Gas in Ethereum is like gasoline in your car.
- In Ethereum you pay Ether to buy gas, in real life you pay money (e.g. dollars) to buy gasoline.
- To execute a smart contract is like to take a trip.
- An execution of a smart contract command is like covering certain distance.
- Smart contract execution price is like total cost of the trip.
Now, an interesting thing about Ethereum is that while each command has a predetermined and fixed cost in terms of gas, gas price is not fixed. When a smart contract is invoked, you specify how much you are willing to pay for gas. On the other hand, people whose computing resources are used (miners) decide whether to accept the proposed price or not. Obviously, miners try to maximize their profit and thus they are willing to accept transactions with higher gas price. Technically, when you invoke an Ethereum smart contract, it may not work if you set gas price very low. Ethereum wallets do suggest reasonable gas prices but again, it is totally up to you to specify gas price. At the same time, you can specify maximum amount of gas that you are willing to spend. Actual gas spent may be lower but it will never be higher than what you are willing to pay.
Finally, it must be noted that gas is used both for regular transactions (i.e. sending Ethereum) and for smart contract invocations.
Did you like this story? Follow me for even more interesting and exclusive stories about Ethereum, Smart Contracts, Blockchain, ICOs and Cryptocurrency.