Skip to content

A comprehensive guide to teach you about gas optimization patterns.

Notifications You must be signed in to change notification settings

beskay/gas-guide

Repository files navigation

Gas optimization guide

A comprehensive guide to teach you about gas optimization patterns.

This guide is aimed at developers who are already familiar with Solidity and want to learn how to write more gas efficient smart contracts.

Content

You can use Foundry to look into certain code snippets in detail. Most gas optimization examples have a corresponding source and test file to debug/inspect further.

Useful forge commands for inspecting and debugging contracts in this repository:

  • forge test --mc <contract_name> -vvvv // run tests and show stack traces
  • forge inspect <Contract> ir-optimized // show optimized Yul
  • forge inspect <Contract> methods // show function selectors
  • forge debug --debug <Contract> --sig "foo()" // debug functions

Practice

Practice by completing the challenges in ./src/practice/. The suggested order to complete them is:

  1. ArraySum
  2. Airdrop
  3. BadERC20
  4. BadERC721

ArraySum is an example contract where you can apply various gas optimization patterns. The required gas goal is achieveable by just using the patterns described in the Gas optimization techniques section.

Airdrop, BadERC20 and BadERC721 are inefficient contracts that have been deployed onchain. Try to improve them as much as you can. You can use the following test cases to track your progress:

  • forge test --mc ArraySumTest
  • forge test --mc AirdropTest
  • forge test --mc BadERC20Test -vv
  • forge test --mc BadERC721Test -vv

Notable repositories to use

About

A comprehensive guide to teach you about gas optimization patterns.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published