Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Smart Contracts  ›  Lesson

Gas — Paying for Computation

Ethereum 12 minEvery EVM operation costs gas, the sender pays gas used times the price per gas in the native coin, and running out of gas reverts the call while the gas is still spent.

Gas — Paying for Computation

Asha's call to release is executed by thousands of nodes. If contract calls were free, anyone could send one containing an endless loop, and every node would spin forever. Gas is how Ethereum and the other EVM chains prevent that: every step of computation has a price, and the sender pays it.

The rule

Every step costs gas; the fee is gas used times the price per gas.

  • Gas units measure work. Each EVM operation has a fixed gas cost: adding two numbers is cheap, writing to storage is expensive. A plain ETH transfer between two ordinary accounts costs exactly 21,000 gas.
  • Gas limit. The sender sets the most gas the transaction may use. If execution needs more, it stops with out of gas: every change reverts, and the whole gas limit is spent.
  • Gas price. The native coin paid per unit of gas, quoted in gwei, one billionth of an ETH. On Ethereum it has two parts: a base fee set by the protocol, which rises when blocks are busier than a target and is burned, plus a priority fee, or tip, which goes to the validator.
  • Unused gas is refunded. When a call finishes, or fails a require check, you pay for the gas actually used, not the whole limit.

Each block has a gas limit too, so gas also measures how much of the scarce space in a block you take up. When demand is high, the base fee climbs and cheaper transactions wait.

The shortcut

For fee questions: multiply gas used by gwei per gas, then divide by 1,000,000,000 to get ETH.

What did Asha's release cost?Step 1 of 4
  1. Gas used

    The call used 50,000 gas.

The trap is setting the gas limit too low to save money. The call runs out of gas, nothing it tried to do takes effect, and the gas is spent anyway. Wallets estimate gas by simulating the call first for exactly this reason.

Drills

Answer fast — trust the rule.

quick drill

How much gas does a plain ETH transfer between two ordinary accounts use?

quick drill

A transaction uses 30,000 gas at 10 gwei per gas. What is the fee in gwei?

quick drill

A call runs out of gas halfway through. What happens?

quick drill

Why do EVM chains charge for computation at all?

quick drill

The base fee is 25 gwei and the tip is 1 gwei. A transaction uses 40,000 gas. How much of its fee is burned?

Remember

  • Every step costs gas; the fee is gas used times the price per gas.
  • Shortcut: gas times gwei, then divide by 1,000,000,000 for ETH. The base fee is burned and the tip goes to the validator.
  • Trap: a limit that is too low still costs you. Out of gas reverts everything and keeps the fee.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainToo little gas

Explain why an EVM chain charges gas, work out Asha's fee for a call that uses 50,000 gas at 20 gwei per gas, and explain what would have happened if she had set her gas limit to 30,000.