foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Support suggested priority fee sampling

Open mattsse opened this issue 1 year ago • 3 comments

Component

Anvil

Describe the feature you would like

Currently the maxPriorityFeePerGas returns a simple constant, ideally this should do fee sampling across the recent blockrange.

ref https://github.com/foundry-rs/foundry/issues/7070

The endpoint should look at all the previous transactions in a certain range and do some sampling

ref: https://github.com/paradigmxyz/reth/blob/3c3c7b36adeb0a0491518854a2b13f9f85fc0aff/crates/rpc/rpc/src/eth/gas_oracle.rs#L127-L127

https://github.com/ethereum/go-ethereum/blob/fc380f52ef9778e988266f776b9593ce719cf79d/eth/gasprice/gasprice.go#L144-L150

Additional context

No response

mattsse avatar Feb 10 '24 13:02 mattsse

This is interesting, would love to take this on! Quickly forking mainnet and running a simple Python script gets me this:

- `eth_gasPrice`: 22766662410
- `maxPriorityFeePerGas`: 44533324820
- Difference: 21766662410

So I sent the same request to Goerli, and the difference was a +7. This isn't ideal, and I understand this is the part that needs to be fixed.

  • Retrieves latest block header.
  • Returns cached price if for current block.
  • Sets limit to twice max_block_history or total blocks if less.
  • Iterates over past blocks, fetching or using cached transaction tips.
  • Fills results with last price for empty transactions.
  • Sorts fees, selects fee at configured percentile.
  • Constrains price by maximum if set.
  • Updates cache with new price and block hash, returns suggested gas price.

So had a few questions

  1. Did I get this right?
  2. Do we have an equivalent for the max_block_history
  3. The function I have to work on is max_priority_fee_per_gas (pasted below), right?
pub fn suggested_priority_fee(&self) -> U256 {
        U256::from(1e9 as u64)
    }

AbhinavMir avatar Feb 11 '24 01:02 AbhinavMir

  1. Did I get this right?

this sums it up

  1. Do we have an equivalent for the max_block_history

we don't, I'd recommend to move this impl to the Backend struct instead because this has direct access to all the mined blocks. for forking mode, this should only take into account locally mined blocks.

the logic we need is basically the reth gas oracle

https://github.com/paradigmxyz/reth/blob/3c3c7b36adeb0a0491518854a2b13f9f85fc0aff/crates/rpc/rpc/src/eth/gas_oracle.rs#L216

we calculate this for a configured block range and then sort it and return the configured percentile.

3The function I have to work on is max_priority_fee_per_gas (pasted below), right?

yes this is only used by the rpc endpoint

mattsse avatar Feb 11 '24 14:02 mattsse

Gotcha, starting some work on this!

AbhinavMir avatar Feb 12 '24 01:02 AbhinavMir

probably closed by #7984.

dorlneylon avatar Jun 15 '24 18:06 dorlneylon