cgewecke / eth-gas-reporter

Gas usage per unit test. Average gas usage per method. A mocha reporter.
MIT License
603 stars 94 forks source link

Feature request: Optimism support #273

Closed andrewshvv closed 6 months ago

andrewshvv commented 2 years ago

@cgewecke

Hey, pretty new in web3 space, I am wondering - can the eth-gas-reporter support Optimism?

I am not familiar with eth-gas-reporter internals and thought that maybe you can give me initial guidance on what kind of changes are necessary to make it work and the place where to start looking?

Depending on the work required I may try to implement it.

From an initial naive glance it seems we need:

About Optimism fees: https://community.optimism.io/docs/developers/l2/new-fees.html#frontmatter-title

cgewecke commented 2 years ago

Yes, that looks right to me.

As a practical matter this tool is usually run with HardhatEVM or ganache-cli (both ethereumjs-vm based) It would nice to know if gasUsedL2 on an optimism vm is reasonably similar to what is used on an L1 ethereum vm.

Do you know if that's the case?

cgewecke commented 2 years ago

Cross-linking relevant issue at hardhat-gas-reporter

https://github.com/cgewecke/hardhat-gas-reporter/issues/78

Will think a little bit about how this should be done....if we take the simplest approach and don't worry about representing the different layer costs in the table, most of the code changes would be in this file where the utilities to fetch and calculate gasPrices are:

https://github.com/cgewecke/eth-gas-reporter/blob/master/lib/utils.js

andrewshvv commented 2 years ago

Cool, glad to hear that!

It would nice to know if gasUsedL2 on an optimism vm is reasonably similar to what is used on an L1 ethereum VM. Do you know if that's the case?

By reading this it seems so https://research.paradigm.xyz/optimism But I will take a deeper dive in a couple of days.

Cross-linking relevant issue at hardhat-gas-reporter

Yeah, hardhat-gas-reporter is what I am actually using.

https://github.com/cgewecke/eth-gas-reporter/blob/master/lib/utils.js

Alright, I will take a look in utils.js, optimism rpc node documentation, ethereumjs-vm docs.

andrewshvv commented 2 years ago

(in progress, will update with time)

Optimism research:

eth-gas-reporter research:

Open questions:

smartcontracts commented 2 years ago

FYI, we just updated the Optimism docs to give more information about how to accurately estimate the current L1 data cost for a given transaction: https://community.optimism.io/docs/developers/build/transaction-fees/#estimating-the-l1-data-fee

smartcontracts commented 2 years ago

When computing the total cost for a transaction on Optimism you need to account for two things, the L2 "execution" fee and the L1 "data" fee.

The L2 execution fee can be computed with the standard formula of provider.estimateGas(tx) * provider.getGasPrice().

The L1 data fee can be most easily estimated by making a query to OVM_GasPriceOracle.getL1Fee(serialize(tx)).

andrewshvv commented 2 years ago

First calculation:

Screen Shot 2022-01-24 at 6 29 43 AM
math77 commented 1 year ago

this was implemented?

andrewshvv commented 1 year ago

@math77 nah, sorry, never got it working properly I ended up rewriting too much, and got stuck introducing new bugs It is open for anyone to take

cgewecke commented 6 months ago

Hi! eth-gas-reporter is being deprecated in favor of hardhat-gas-reporter.

In the latest version at Hardhat, Optimism Ecotone l1GasUsed and total cost are available by setting the L2 option to "optimism". The reporter synthesizes these costs locally so there's no performance hit for collecting this data while running your tests.

https://github.com/cgewecke/hardhat-gas-reporter/releases/tag/v2.0.0