Gas is computation
Every EVM instruction costs gas. Your transaction sets a gas limit (max units willing to burn) and fee fields that determine how much ETH you pay per unit. Total cost ≈ gasUsed × effective gas price. Unused gas limit is refunded; running out of gas reverts the tx but still charges you for work done.
- Simple ETH transfer: 21,000 gas. Contract calls cost more — often 100k–500k+.
- Storage writes (SSTORE) dominate cost — first write to a slot is ~20k gas.
- Complex DeFi routes can exceed 1M gas during congestion.
// Custom errors save gas vs long revert strings (see gas-golfing article)
error InsufficientBalance(uint256 available, uint256 required);
function withdraw(uint256 amount) external {
if (balances[msg.sender] < amount) {
revert InsufficientBalance(balances[msg.sender], amount);
}
balances[msg.sender] -= amount;
}EIP-1559 base fee and priority tip
Since EIP-1559, each block has a protocol-set base fee that adjusts up or down based on demand. The base fee is burned. You add a priority fee (tip) to incentivize validators to include your transaction sooner. maxFeePerGas caps what you will pay; maxPriorityFeePerGas sets the tip.
- Base fee rises when blocks are >50% full; falls when they are under target.
- Wallets often suggest fees — you can lower them if you are not in a hurry.
- Burned base fee is why ETH can be deflationary during high activity.
Reading a fee on Etherscan
Transaction fee = gasUsed × effectiveGasPrice. Effective gas price combines base and priority components. Internal transactions and token transfers inside a call do not add separate gas bills — they share the outer transaction's gas limit.
- Status 'Fail' still shows a fee — execution reverted partway through.
- Gas limit too low causes 'out of gas' failures.
- L2 fees add an L1 data component — total cost is L2 execution + L1 posting.
What is MEV?
Maximal Extractable Value is profit validators (or builders) extract by ordering, including, or censoring transactions. Common forms: sandwich attacks on swaps (front-run your buy, back-run your sell), liquidation racing in lending, and arbitrage across DEX pools. You pay via worse execution price, not a line item on your receipt.
- Public mempool transactions are visible before inclusion — bots compete on ordering.
- Private RPCs (e.g. Flashbots Protect) route txs away from the public mempool.
- Slippage limits on swaps bound worst-case MEV damage.
Gas optimization mindset
Users pay for your design choices. OpenZeppelin defaults favor safety and clarity over minimal gas — that is correct for most apps. Optimize hot paths (AMM swaps, merkle claims) not admin-only functions. Packing storage, custom errors, and avoiding redundant external calls are the first levers.
- Batch operations amortize base transaction cost (21k) across many actions.
- Events are cheaper than storage for data you only need off-chain.
- Upgradeable proxies add overhead on every call — justify the pattern.
Practical tips for builders and users
Deploy and test on Sepolia or Hoodi before mainnet. Schedule admin tasks off-peak. For users, 'slow' fee tiers save money on non-urgent transfers. Document expected gas ranges in your app so people are not surprised at the wallet confirm screen.
- Testnet ETH is free from faucets — never pay someone for test ETH.
- Contract size limit is 24KB — large OpenZeppelin stacks may need optimization.
- Simulate with eth_call before sending high-value transactions.
