What is Ethereum gas?

What you are paying for when a transaction costs money.

Updated

Short answer

Gas is the unit that measures how much work an Ethereum transaction does. Adding two numbers costs a few units and writing to storage costs thousands, and the fee you pay is the gas your transaction consumed multiplied by the price you agreed to pay per unit.

Every Ethereum transaction runs code, and every node on the network runs that same code to check the result. Gas is the unit that measures how much work the code does. Adding two numbers costs a few units. Writing a value into storage costs thousands. The fee you pay is the amount of gas your transaction consumed, multiplied by the price you agreed to pay per unit.

Why it exists

Two reasons, and neither is about revenue. The first is that computation has to cost something. If it were free, anyone could publish a contract that loops forever and the network would stall. Metering the work and charging for it puts a hard ceiling on what a single transaction can do.

The second is ordering. Far more transactions are submitted than fit in a block, so something has to decide which ones go in. Fees are that something, and they also pay the validators doing the work.

Gas used and gas price are separate

Gas used depends only on what the transaction does. A plain ETH transfer costs exactly 21,000 gas, today and five years ago. A token approval costs somewhat more. A swap through several pools can cost hundreds of thousands. Network congestion does not change this number.

Gas price is what you pay per unit, quoted in gwei. This is the part that moves, and it moves a lot. When people say gas is expensive, this is what they mean.

fee = gas used × price per gas

The gas limit is a ceiling, not a bill

Alongside the price, your transaction carries a gas limit: the most gas it is allowed to consume. Wallets estimate it for you by simulating the transaction first.

If the transaction uses less than the limit, you are charged for what it used and nothing more. The leftover is never spent. Raising the limit on a transaction that would have succeeded anyway does not cost you extra, and it does not make anything faster.

Setting it too low is the case that hurts. The transaction runs until it hits the ceiling, then reverts and gets included in a block anyway. You pay for the work done up to that point and get nothing for it.

Where the money goes

The fee splits in two. Part of it is the base fee, which the protocol sets and then destroys. The rest is a tip to whoever proposed the block. EIP-1559 covers how that split works.

Only the second part is up to you. The base fee is the same for everybody in the block whether you like the number or not, so choosing a tip is the whole of what anyone means by setting gas.