Skip to content

Migrate gas estimation to match alloy's type#4054

Merged
jmg-duarte merged 48 commits intomainfrom
jmgd/alloy/gas-2
Jan 29, 2026
Merged

Migrate gas estimation to match alloy's type#4054
jmg-duarte merged 48 commits intomainfrom
jmgd/alloy/gas-2

Conversation

@jmg-duarte
Copy link
Copy Markdown
Contributor

@jmg-duarte jmg-duarte commented Jan 15, 2026

Caution

Review with care! The changes are non trivial and there is one breaking change, the gas price returned by the driver no longer includes the base fee!

Description

Refactors gas price handling to use integer arithmetic and alloy's native types instead of floating-point
calculations. This eliminates precision loss in gas price calculations and better aligns with alloy's conventions.

The removal of the base fee is not a true removal, before this change, the base fee was either 0 or the max value available, both leading to inaccurate results. The new code removes the base fee from the type that was being used to describe the estimations (because the base_fee isn't an estimate, it comes in the previous block); but starts querying the chain for the latest block so it's able to get the proper base_fee (if available). The gas estimates themselves should suffer a big change (since the estimate code is the same) but the effective gas price should become more accurate due to the inclusion of the base fee in the calculations.

Changes

  • Replace custom GasPrice1559 with alloy's Eip1559Estimation throughout codebase
  • Change GasPrice::base from FeePerGas to Option to match alloy's base fee representation
  • Migrate gas calculations from f64 to u128/U256 integer arithmetic
  • Implement calc_effective_gas_price from alloy for effective gas price calculations
  • Add base_fee: Option to BlockInfo for proper EIP-1559 support
  • Update API responses to return u128 directly instead of wrapped U256
  • Add scaling helper methods (scaled_by_pct, scaled_by_pml) for clearer gas price adjustments

How to test

  1. Run existing test suite: cargo test
  2. Verify gas price estimation endpoints return expected values
  3. Test refunder gas price calculations with various scenarios (new tx, replacement tx, max gas price limits)
  4. Verify settlement submissions use correct gas parameters

jmg-duarte and others added 21 commits January 12, 2026 15:16
Removed external gas-estimation dependency by vendoring its code into
shared/gas_price_estimation module:
- Inlined GasPriceEstimating and PriorityGasPriceEstimating traits
- Vendored GasPrice1559 type into price module
- Created NodeGasPriceEstimator to replace web3 legacy gas estimation
- Removed NativeGasEstimator and Native gas estimator config option
- Updated driver and refunder crates to use vendored implementations

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Base automatically changed from jmgd/alloy/gas to main January 16, 2026 14:40
@jmg-duarte
Copy link
Copy Markdown
Contributor Author

/gemini review

@jmg-duarte jmg-duarte marked this pull request as ready for review January 16, 2026 16:34
@jmg-duarte jmg-duarte requested a review from a team as a code owner January 16, 2026 16:34
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a significant and valuable refactoring that migrates gas price handling to use integer arithmetic and native alloy types, moving away from floating-point calculations. This is a great improvement for precision and aligns the codebase better with alloy's conventions. The changes are extensive and well-executed across the board. My review focuses on a few areas to enhance correctness and maintainability: I've identified a potential panic due to an unchecked conversion, a compilation error, a possible regression in an API response, and opportunities for better code reuse and consistency.

Comment thread crates/driver/src/domain/eth/gas.rs Outdated
Comment thread crates/driver/src/domain/eth/gas.rs Outdated
Comment thread crates/driver/src/domain/competition/auction.rs
Comment thread crates/driver/src/infra/api/routes/gasprice.rs Outdated
Comment thread crates/refunder/src/submitter.rs Outdated
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a significant and valuable refactoring that migrates gas price handling from floating-point arithmetic to integer-based calculations using alloy's native types. This is a great improvement for precision and consistency throughout the codebase. The changes are extensive and well-implemented.

I've identified one potential panic due to an unchecked type conversion that should be addressed. I've also included a suggestion to improve code organization by moving a new utility trait to a shared location for better reusability.

Overall, this is a solid piece of work that improves the robustness of gas price handling.

Comment thread crates/driver/src/domain/eth/gas.rs Outdated
Comment thread crates/refunder/src/submitter.rs Outdated
@squadgazzz
Copy link
Copy Markdown
Contributor

the gas price returned by the driver no longer includes the base fee!

Could you explain in the PR description why the base fee is no longer included and what exactly will be affected if the base fee is not provided?

Copy link
Copy Markdown
Contributor

@squadgazzz squadgazzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't really think of any issues. I trust your tests 🙂

Comment thread crates/driver/src/domain/competition/solution/settlement.rs Outdated
Comment thread crates/driver/src/infra/blockchain/gas.rs Outdated
@jmg-duarte jmg-duarte requested review from MartinquaXD and removed request for MartinquaXD January 23, 2026 15:15
Comment on lines +100 to +102
let calculated_tip = eth::U256::from(estimate.max_priority_fee_per_gas)
.saturating_sub(eth::U256::from(tip_percentage_as_bps))
/ eth::U256::from(10000u128);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This math looks wrong to me. This should use a checked_mul instead of saturating_sub(), no?

Comment thread crates/driver/src/domain/eth/gas.rs Outdated
/// The current base gas price that will be charged to all accounts on the
/// next block.
base: FeePerGas,
base: Option<u64>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this can now be non-optional. AFAICS only the conversionimpl From<EffectiveGasPrice> for GasPrice would complain but this does not seem to be used anywhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the conversion too, nice find!

@jmg-duarte jmg-duarte added this pull request to the merge queue Jan 29, 2026
Merged via the queue into main with commit ff190ad Jan 29, 2026
19 checks passed
@jmg-duarte jmg-duarte deleted the jmgd/alloy/gas-2 branch January 29, 2026 09:55
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants