Setup
Before using this utility, create a folder calledgas_calculations
in the root folder of the directory (same level as the src
folder) and ensure that you have the right permissions settings set in your foundry.toml
file:
Write the test
In the test function(s) that you want to perform, callinstance.log4337Gas("identifier")
before a UserOperation
is executed. This will log the gas used for the UserOperation
and use the identifier as the filename for the gas report. Make sure to use a unique identifier for each UserOp, otherwise the gas reports will be overwritten. For example:
Run the tests
To run the tests and log gas, run the following command:Read the gas report
After the tests have finished, the gas reports will be written to thegas_calculations
folder in the root of the project. A gas report will look like this:
(diff: ...)
that shows the difference, in gas units between the calculations.
Convert the gas units
All numbers are in gas units. To calculate the cost in gwei, follow the instructions below:- On L1: multiply the total gas used by the gas price in gwei:
totalGasUsed * gasPrice
- On an L2: multiply the total gas used by the L2 gas price in gwei and then add it to to the result of the relevant L2-L1 gas cost muliplied by the L1 gas cost in gwei:
totalGasUsed * l2GasPrice + callDataGasUsed * l1GasPrice