
I will give you an overview of the course and make sure it is right for you.
In this video we learn how to predict the cost of an Ethereum transfer in dollars.
An overview of example transaction fees for various smart contracts part 1
An overview of example transaction fees for various smart contracts part 2
We create our own example smart contract to further develop our intuition about smart contract transaction fees
Explore how Ethereum limits blocks by total gas, 30 million, shaping throughput, gas wars, and price volatility; large transactions may revert or fit only a few per block.
Learn why gas optimization matters even on ethereum-compatible chains. Analyze gas block limits, transaction sizing, and how adoption and decentralization influence gas costs, with avalanche as an example.
Delve into how opcodes power the Etherium computer by compiling Solidity into stack-based operations, loads, stores, and arithmetic that affect gas costs.
Explore how Ethereum opcodes determine gas costs through stack operations, sload usage, and cold access penalties, and compare heavy versus light functions.
Learn how function selectors in bytecode are four-byte hashes of function names, computed from exact signatures with event types, not variable names (as shown with blue, gray, green, yellow).
Explore the op codes of a real smart contract by examining a do-nothing payable function, memory scratch space, call data size checks, and a gas cost of 21,138.
Copy contract op codes into a spreadsheet to visualize executed instructions. Pre-populate gas costs and account for storage and memory usage that explain discrepancies in total gas.
Analyze how the data portion of a transaction drives gas costs. Zero bytes incur small increases; non-zero bytes cost 16 gas per byte, four bytes total 64 gas.
Explains the unchecked block in Solidity, why it exists, and how pre-0.8 overflow and underflow could misbehave; shows how 0.8 adds checks and reverts on invalid arithmetic.
Explore unchecked blocks, their gas savings, and the security risks of overflow and underflow; see practical use in OpenZeppelin counters and ERC-20 transfers, with gas analysis via opcodes.
Explore how the gas limit, gas price, and intrinsic gas determine transaction costs and why Ethereum enforces a 21,000 gas baseline. Note how unused gas is refunded.
Learn how EIP-1559 changes gas costs by burning the base fee, splitting fees into max priority fee per gas and max fee per gas, and adjusting base fees per block.
Explore how EIP-1559 pricing works, clarifying max base fee, max fee per gas, and max priority fee, and show how base fees are burned and refunds or miner tips arise.
Explore how the solidity optimizer trades deployment size for execution cost by adjusting the runs parameter, and balance gas costs for common tokens.
Analyze storage gas costs in Ethereum, including zero to non-zero (20,000 gas plus 2,100 cold storage access fee), non-zero to zero refunds, and the impact of cold storage access fee.
Explain why the 5000 gas cost appears when changing storage from non-zero to non-zero, detailing cold storage load and gas reset, and noting first access adds the cold storage load.
Explore how storage uses 32-byte slots in solidity, causing small integers to incur full-slot gas costs. See examples with 128-to-128 mappings, uint16 arrays, and uint8 values costing around 43,000 gas.
Discover how assigning an unchanged storage value still consumes gas, analyze cold storage vs store costs, and learn a caching-then-conditional update pattern to save gas in Solidity contracts.
Explain how Solidity stores dynamic arrays and the gas costs of array operations, including transaction initiation, storing length, and writing elements, with tricks like cold storage and op-code stepping.
Examine how gas refunds relate to storage changes in ethereum. Reference the yellow paper and IP 3529 adjustments, including the removal of self-destruct refunds and the cap at one fifth of costs.
Examine how refunds influence gas costs when setting variables to zero, including array deletions, and learn to count down rather than up to maximize refunds.
Compare ERC20 transfers and gas costs across zero-to-nonzero, nonzero-to-zero, and nonzero-to-nonzero scenarios to surface storage-operations costs and refunds. Understand how events increase gas usage in benchmarks.
Explore how Solidity stores structs and strings in storage slots, revealing gas costs, packing behavior, and cold versus warm storage effects.
Discover how variable packing stores two values in a single 32-byte slot, uses offsets and masking to read A and B, and when packing saves gas in Solidity.
Explore memory cost explosion in Ethereum and the quadratic gas costs of large memory allocations, including 256 bit numbers.
Solidity memory is never cleared, so allocating memory in a transaction raises gas costs and can hit the quadratic portion of the formula; avoid loop allocations.
Learn how function names affect gas costs through selectors and dispatch. Each extra function adds about 22 gas from push, checks, and jumps, so place gas-sensitive selectors near the top.
Compare less than and less than or equal to in Solidity to understand how opcodes impact gas costs and why choosing cheaper equivalents improves efficiency.
Explore bit shifting in Solidity: left and right shifts represent multiply and divide by two, compare gas costs of shifting versus multiplication, and review overflow risks and benchmark results.
Compare reverting early versus after storage writes to minimize gas costs, showing that reverts undo all state changes and charge gas only up to the revert point.
Explore short circuiting in Solidity, where or and and operators skip evaluation to save gas. Compare memory reads costing 2100 gas with timestamp checks costing two gas, and decide presale ordering.
Analyze how precomputing constant expressions in Solidity bytecode can lead to hard-coded results, using pushes and a function selector, while noting where optimization may fail and affect gas costs.
Practice problems for gas optimization
Ever been surprised by the gas costs in a smart contract? The Ethereum Virtual Machine calculates these costs deterministically, but understanding how is a bit obscure unless you want to comb through the dense yellowpaper! This course helps you understand where every last bit of gas is coming from and the gotchas that can make transactions surprisingly expensive.
This course is for solidity developers looking to progress from beginner to advanced intermediate. Knowledge about Solidity, OpenZeppelin, and Remix is assumed. You should know how to deploy and test a smart contract, and ideally, you've created a token on the mainnet before (and experienced how expensive it can be!).
I’m the author of ERC1155D, arguably the most efficient NFT contract created. I’m here to share my learnings with you!
Some of the most enjoyable years of my life was teaching computer science at Columbia University, so I’m happy to relive those years virtually!
Topics covered include:
A deep dive into the solidity compiler
A deep dive into the Ethereum Virtual Machine
How to understand the output from the solidity compiler
How to track down wasteful computation
Understanding gas cost specifications
Techniques for improving gas costs
How memory, transaction data, state changes, and computational cost affect transaction cost
Bad designs to avoid
Where the Yellowpaper is wrong!
Follow me on Twitter: @jeyffre