- Notifications
You must be signed in to change notification settings - Fork12
🛠️ Easily track & compare gas costs estimated by Foundry on each of your Pull Requests!
License
Rubilmax/foundry-gas-diff
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
- Easily compare gas reports generated by Foundry automatically on each of your Pull Requests!
- Check out theLive example to see how it looks!
Add a workflow (.github/workflows/foundry-gas-diff.yml):
name:Report gas diffon:push:branches: -mainpull_request:# Optionally configure to run only for changes in specific files. For example:# paths:# - src/**# - test/**# - foundry.toml# - remappings.txt# - .github/workflows/foundry-gas-diff.ymljobs:compare_gas_reports:runs-on:ubuntu-lateststeps: -uses:actions/checkout@v4with:submodules:recursive -name:Install Foundryuses:onbjerg/foundry-toolchain@v1with:version:nightly# Add any step generating a gas report to a temporary file named gasreport.ansi. For example: -name:Run testsrun:forge test --gas-report > gasreport.ansi# <- this file name should be unique in your repository!env:# make fuzzing semi-deterministic to avoid noisy gas cost estimation# due to non-deterministic fuzzing (but still use pseudo-random fuzzing seeds)FOUNDRY_FUZZ_SEED:0x${{ github.event.pull_request.base.sha || github.sha }} -name:Compare gas reportsuses:Rubilmax/foundry-gas-diff@v3with:summaryQuantile:0.9# only display the 10% most significant gas diffs in the summary (defaults to 20%)sortCriteria:avg,max# sort diff rows by criteriasortOrders:desc,asc# and directionsignore:test-foundry/**/*# filter out gas reports from specific paths (test/ is included by default)id:gas_diff -name:Add gas diff to sticky commentif:github.event_name == 'pull_request' || github.event_name == 'pull_request_target'uses:marocchino/sticky-pull-request-comment@v2with:# delete the comment in case changes no longer impact gas costsdelete:${{ !steps.gas_diff.outputs.markdown }}message:${{ steps.gas_diff.outputs.markdown }}
ℹ️An error will appear at first run!
🔴Error: No workflow run found with an artifact named "main.gasreport.ansi"
As the action is expecting a comparative file stored on the base branch and cannot find it (because the action never ran on the target branch and thus has never uploaded any gas report)
Everytime somebody opens a Pull Request, the action expectsFoundryforge to run a test suite, generating a gas report to a temporary file (namedgasreport.ansi by default).
Once generated, the action will fetch the comparative gas report stored as an artifact from previous runs; parse & compare them, storing the results in the action's outputs as shell and as markdown.
You can then do whatever you want with the results!
Our recommandation: Automatically submit a sticky comment displaying the gas diff!
This should correspond to the path of a file where the output of forge's gas report has been logged.Only necessary when generating multiple gas reports on the same repository.
Defaults to:gasreport.ansi
The gas diff reference branch name, used to fetch the previous gas report to compare the freshly generated gas report to.
Defaults to:${{ github.base_ref || github.ref_name }}
The gas diff target branch name, used to upload the freshly generated gas report.
Defaults to:${{ github.head_ref || github.ref_name }}
The github token allowing the action to upload and download gas reports generated by foundry. You should not need to customize this, as the action already has access to the default Github Action token.
Defaults to:${{ github.token }}
The top section displayed in the markdown output. Can be used to identify multiple gas diffs in the same PR or add metadata/information to the markdown output.
Defaults to:
#Changes to gas costThe quantile threshold to filter avg gas cost diffs to display in the summary top section.
Defaults to:0.8
A list of criteria to sort diff rows by in the report table (can bename | min | avg | median | max | calls), separated by a comma.Must have the same length as sortOrders.
Defaults to: name
A list of directions to sort diff rows by in the report table (can beasc | desc), for each sort criterion, separated by a comma.Must have the same length as sortCriteria.
Defaults to: asc
The list of contract paths from which to ignore gas reports, separated by a comma.This allows to clean out gas diffs from dependency contracts impacted by a change (e.g. Proxies, ERC20, ...).
No default assigned: optional opt-in (Please note that node dependencies are always discarded from gas reports)
The list of contract paths of which only to keep gas reports, separated by a comma.This allows to only display gas diff of specific contracts.
No default assigned: optional opt-in
Library gas reports
Forge does not generate library gas reports. You need to wrap their usage in a contract calling the library to be able to compare gas costs of calling the library.
Average gas cost estimation
Average & median gas costs for each function is estimated based on the test suite, which means they are easily impacted by small changes in the tests. We recommend using a separate, specific test suite, rarily updated, designed to perform accurate gas estimations.
Fuzzing impacts gas costs
Fuzzing can lead differences in gas costs estimated each time a test suite is ran. We thus recommend setting a deterministic fuzzing seed via the--fuzz-seedargument.
This repository is maintained independently fromFoundry and may not work as expected with all versions offorge.
About
🛠️ Easily track & compare gas costs estimated by Foundry on each of your Pull Requests!
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.
