Lesson 3 of 4 Gas and Transaction Fees

Gas Costs of EVM Operations

Knowing which operations are expensive lets you predict costs and design efficient contracts. This lesson covers the intrinsic cost of transactions, storage and memory costs, warm and cold access, logs, calls and deployment, with real measured examples.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the components of intrinsic gas.
  • Explain warm and cold access and the cost of storage operations.
  • Identify the costs of logs, calls and deployment.
  • Explain measured gas figures from their components.

Intrinsic gas

Before executing any code, every transaction pays intrinsic gas. The following table lists its parts.

ComponentGas
Base cost of any transaction21,000
Contract creation (no to address)+32,000
Each zero byte of calldata4
Each non-zero byte of calldata16
Each address or storage key in an access list2,400 / 1,900

A plain ether transfer to an externally owned account uses exactly 21,000 gas. Since the Pectra upgrade (EIP-7623), transactions whose cost is dominated by calldata pay a higher minimum rate per byte, which encourages rollups to use blobs instead of calldata.

Fill in the number

Intrinsic gas

Exam skills 5.2.1

Add up the intrinsic gas a transaction pays before any code runs.

Log in to try this activity Interactive activities are free with an account.

Storage costs

Storage is the dominant cost of most contracts, because every node must keep it permanently. Since the Berlin upgrade (EIP-2929), the first access to a storage slot in a transaction is cold and costs extra; later accesses to the same slot are warm. The following table gives the main costs.

OperationGas
SLOAD, cold2,100
SLOAD, warm100
SSTORE zero → non-zero20,000 (+2,100 if cold)
SSTORE non-zero → different non-zero2,900 (+2,100 if cold)
SSTORE to the same value100 (+2,100 if cold)
SSTORE non-zero → zero2,900 (+2,100 if cold), with a 4,800 refund
TSTORE / TLOAD (transient storage)100

Refunds are paid at the end of the transaction and are capped at one fifth of the gas used (EIP-3529), so clearing storage reduces but never eliminates the cost of a transaction.

Fill in the number

Storage costs

Exam skills 5.2.1

Calculate the gas cost of storage operations, including cold access.

Log in to try this activity Interactive activities are free with an account.

Other operations

The following table lists other costs a developer meets often.

OperationGas
ADD, SUB, comparisons3
MUL, DIV5
KECCAK25630 + 6 per 32-byte word
Memory3 per word, plus a quadratic term as memory grows
LOG (event)375 + 375 per topic + 8 per byte of data
Call to a cold address2,600 (100 if warm)
Sending value with a call+9,000 (+25,000 if it creates a new account)
Code deposit at deployment200 per byte of runtime code

Runtime code is limited to 24,576 bytes (EIP-170) and creation code to 49,152 bytes (EIP-3860). Contracts that grow too large must be split, for example into libraries or several contracts.

Scenario questions

Costs of other operations

Exam skills 5.2.1

Compare the costs of common operations and the code size limits.

Log in to try this activity Interactive activities are free with an account.
Worked example

Measuring a counter

A Counter contract has uint256 public x;, and inc() increments x and emits Increment(uint256 by). A Hardhat gas report shows that the first inc() after deployment uses 44,448 gas, and a later incBy(7) uses 27,624 gas. Explain the difference.

The first call writes slot 0 from zero to non-zero while it is cold: 20,000 + 2,100 = 22,100 gas. Adding 21,000 base, about 1,000 for the event (375 + 375 for one topic + 8 × 32 bytes of data) and 64 for four bytes of calldata gives about 44,170, and the remaining few hundred gas are ordinary opcodes. The later call changes a non-zero value: 2,900 + 2,100 cold = 5,000 instead of 22,100, which explains most of the 16,824 gas difference. These figures were measured with npx hardhat test --gas-stats.

Worked example

Deployment cost

The same report shows that deploying Counter used 150,163 gas and that its runtime bytecode is 448 bytes. Where does the gas go?

21,000 base + 32,000 contract creation = 53,000. Storing 448 bytes of runtime code costs 448 × 200 = 89,600. That accounts for 142,600 gas; the rest is the calldata of the creation code (charged per byte) and executing the constructor and copying code. Code size is therefore a major part of deployment cost, which is why the optimizer's low-runs setting reduces it.

Check yourself

How much gas does a plain ether transfer between two externally owned accounts use?

Show answer

Exactly 21,000 gas: the base cost of a transaction, with no calldata and no code to execute.

Exam Tips

Remember for the exam

  • Every transaction pays 21,000 gas; contract creation adds 32,000; calldata costs 4 or 16 per byte.
  • Cold SLOAD 2,100, warm 100; new storage value 20,000; updating 2,900 (plus 2,100 if cold).
  • Refunds are capped at one fifth of gas used.
  • Deployment pays 200 gas per byte of runtime code; the code size limit is 24,576 bytes.