Ethereum Gas Explained

Published time

Gas is a commodity like oil. Your car needs oil to run it. By the same token, the Ethereum Virtual Machine(EVM) needs gas to run it. Gas is used for performing actions in the Ethereum. It's a crypto-fuel that drives the motion of smart contracts.

Just like you pay for the amount of oil you used, you pay for the amount of EVM resources you use. In the case of oil, the unit is gallons. In the case of EVM, it is gas.

At a low level, the Ethereum has an operation code. Each operation in the EVM consumes a fixed amount of gas. This is subject to change through the EIPs, but currently, a multiplication consumes 5 gas, an addition consumes 3 gas, calculating a hash consumes 30 gas, and sending a transaction consumes 21000 gas. Your first instruction might cost 5 gas and the second one might cost 30 gas.

The gas is translated into ether. As a user, you can specify how much ether you are willing to pay for each gas. This is called the gasPrice.

The more you pay, the faster your transaction goes through because miners are economically incentivized to collect a transaction with high gasPrice.

You may ask why we need to have a division between gas and ether. This is because we need to decouple the price of operation and the volatile market price of ether. The price of each operation should remain constant.

As the originator of a transaction, you are in control of another parameter: gasLimit. The gasLimit is the max units of gas originators are willing to spend. If the gasPrice is the cost per gallon of oil, then the gasLimit is the capacity of the oil tank. This limit protects people from erroneous smart contracts that would infinitely spend ether forever until the address is depleted.