-
Notifications
You must be signed in to change notification settings - Fork 166
Migrate gas estimation to match alloy's type #4054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
2b6bf23
Migrate shared off of ethcontract
jmg-duarte f439af1
Vendor gas-estimation crate into shared module
jmg-duarte 5e7e7a2
remove unused fn from trait
jmg-duarte fa90a4c
Merge branch 'main' into jmgd/alloy/gas
jmg-duarte ea800d9
address gemini comment
jmg-duarte 1e2e165
compilation error
jmg-duarte 9323557
wip
jmg-duarte a6a467c
Address river error
jmg-duarte 5065765
address clippy
jmg-duarte 05c2c01
Update crates/driver/src/tests/setup/solver.rs
jmg-duarte aa62ec2
address comments
jmg-duarte 3dd9f64
alleviate issue with the magic numbers
jmg-duarte 8a6a305
Merge branch 'jmgd/alloy/gas' into jmgd/alloy/gas-2
jmg-duarte ebf73de
wip
jmg-duarte a507ca9
is_approx_eq
jmg-duarte 442db6d
Merge branch 'jmgd/alloy/gas' into jmgd/alloy/gas-2
jmg-duarte ec38cd7
approx_eq improvements
jmg-duarte 876fd43
clippy
jmg-duarte 49def52
Merge branch 'jmgd/alloy/gas' into jmgd/alloy/gas-2
jmg-duarte 1138858
cleanup
jmg-duarte c553a87
clippy
jmg-duarte f42e13a
Merge branch 'main' into jmgd/alloy/gas-2
jmg-duarte 6737b8c
wip
jmg-duarte e3767cc
Precision loss fix
jmg-duarte 4ff8baa
Update crates/driver/src/domain/eth/gas.rs
jmg-duarte 75ba211
Merge Eip1559Estimation extension traits
jmg-duarte c2e52c2
Replace GasPriceResponse with the equivalent Eip1559Estimation
jmg-duarte 387ce1f
Provide base fee in driver mempool
jmg-duarte b4a7291
Fix approx_eq logs and tighten comparison margin
jmg-duarte a28e5b5
docs
jmg-duarte 16b9b07
Merge branch 'main' into jmgd/alloy/gas-2
jmg-duarte 55c216d
fix basis points
jmg-duarte 26fbe9d
Replace the GasPrice struct with Eip1559Estimation for mempool
jmg-duarte 5e6f105
Make base_fee mandatory
jmg-duarte 61d7778
Rename PML -> PERMIL
jmg-duarte 8e26a48
Make base_fee mandatory for effective()
jmg-duarte 457826b
Add base_fee and effective_gas_price to estimators
jmg-duarte b36d30c
wip
jmg-duarte 45b02cf
tracing for gas estimates
jmg-duarte 51d6acb
proper metric
jmg-duarte 1e48694
use effective instead of estimate
jmg-duarte 671b2d2
fmt
jmg-duarte 1f59ff7
clippy
jmg-duarte d474798
Merge branch 'main' into jmgd/alloy/gas-2
jmg-duarte a667918
apply suggestions
jmg-duarte eb96262
Merge branch 'main' into jmgd/alloy/gas-2
jmg-duarte a343fb5
Merge branch 'main' into jmgd/alloy/gas-2
jmg-duarte bfcc808
address comments
jmg-duarte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,23 @@ | ||
| use { | ||
| crate::{ | ||
| domain::eth, | ||
| infra::{Ethereum, api::error::Error}, | ||
| util::serialize, | ||
| }, | ||
| crate::infra::{Ethereum, api::error::Error}, | ||
| alloy::eips::eip1559::Eip1559Estimation, | ||
| axum::Json, | ||
| serde::{Deserialize, Serialize}, | ||
| serde_with::serde_as, | ||
| tracing::instrument, | ||
| }; | ||
|
|
||
| pub(in crate::infra::api) fn gasprice(app: axum::Router<Ethereum>) -> axum::Router<Ethereum> { | ||
| app.route("/gasprice", axum::routing::get(route)) | ||
| } | ||
|
|
||
| /// Gas price components in EIP-1559 format. | ||
| #[serde_as] | ||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct GasPriceResponse { | ||
| #[serde_as(as = "serialize::U256")] | ||
| pub max_fee_per_gas: eth::U256, | ||
| #[serde_as(as = "serialize::U256")] | ||
| pub max_priority_fee_per_gas: eth::U256, | ||
| #[serde_as(as = "serialize::U256")] | ||
| pub base_fee_per_gas: eth::U256, | ||
| } | ||
|
|
||
| #[instrument(skip(eth))] | ||
| async fn route( | ||
| eth: axum::extract::State<Ethereum>, | ||
| ) -> Result<Json<GasPriceResponse>, (hyper::StatusCode, axum::Json<Error>)> { | ||
| ) -> Result<Json<Eip1559Estimation>, (hyper::StatusCode, axum::Json<Error>)> { | ||
| // For simplicity we use the default time limit (None) | ||
| let gas_price = eth.gas_price().await?; | ||
|
|
||
| Ok(Json(GasPriceResponse { | ||
| max_fee_per_gas: gas_price.max().0.0, | ||
| max_priority_fee_per_gas: gas_price.tip().0.0, | ||
| base_fee_per_gas: gas_price.base().0.0, | ||
| Ok(Json(Eip1559Estimation { | ||
| max_fee_per_gas: gas_price.max_fee_per_gas, | ||
| max_priority_fee_per_gas: gas_price.max_priority_fee_per_gas, | ||
| })) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ use { | |
| domain::eth, | ||
| infra::{config::file::GasEstimatorType, mempool}, | ||
| }, | ||
| alloy::eips::eip1559::Eip1559Estimation, | ||
| anyhow::anyhow, | ||
| ethrpc::Web3, | ||
| shared::gas_price_estimation::{ | ||
| GasPriceEstimating, | ||
|
|
@@ -84,20 +86,34 @@ impl GasPriceEstimator { | |
| /// If additional tip is configured, it will be added to the gas price. This | ||
| /// is to increase the chance of a transaction being included in a block, in | ||
| /// case private submission networks are used. | ||
| pub async fn estimate(&self) -> Result<eth::GasPrice, Error> { | ||
| pub async fn estimate(&self) -> Result<Eip1559Estimation, Error> { | ||
| let estimate = self.gas.estimate().await.map_err(Error::GasPrice)?; | ||
|
|
||
| let max_priority_fee_per_gas = { | ||
| // the driver supports tweaking the tx gas price tip in case the gas | ||
| // price estimator is systematically too low => compute configured tip bump | ||
| let (max_additional_tip, tip_percentage_increase) = self.additional_tip; | ||
| let additional_tip = f64::from(max_additional_tip) | ||
| .min(estimate.max_priority_fee_per_gas * tip_percentage_increase); | ||
|
|
||
| // Calculate additional tip in integer space to avoid precision loss | ||
| // Convert percentage to basis points (multiply by 10000) to maintain precision | ||
| // e.g., tip_percentage_increase = 0.125 (12.5%) becomes 1250 | ||
| let overflow_err = || { | ||
| Error::GasPrice(anyhow!( | ||
| "overflow on multiplication (max_priority_fee_per_gas * tip_percentage_as_bps)" | ||
| )) | ||
| }; | ||
| let tip_percentage_as_bps = tip_percentage_increase * 10000.0; | ||
| let calculated_tip = eth::U256::from(estimate.max_priority_fee_per_gas) | ||
| .checked_mul(eth::U256::from(tip_percentage_as_bps)) | ||
| .ok_or_else(overflow_err)? | ||
| / eth::U256::from(10000u128); | ||
|
Comment on lines
+106
to
+109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This math looks wrong to me. This should use a |
||
|
|
||
| let additional_tip = max_additional_tip.min(calculated_tip); | ||
|
|
||
| // make sure we tip at least some configurable minimum amount | ||
| std::cmp::max( | ||
| self.min_priority_fee, | ||
| eth::U256::from(estimate.max_priority_fee_per_gas + additional_tip), | ||
| eth::U256::from(estimate.max_priority_fee_per_gas) + additional_tip, | ||
| ) | ||
| }; | ||
|
|
||
|
|
@@ -113,10 +129,11 @@ impl GasPriceEstimator { | |
| ))); | ||
| } | ||
|
|
||
| Ok(eth::GasPrice::new( | ||
| suggested_max_fee_per_gas.into(), | ||
| max_priority_fee_per_gas.into(), | ||
| eth::U256::from(estimate.base_fee_per_gas).into(), | ||
| )) | ||
| Ok(Eip1559Estimation { | ||
| max_fee_per_gas: u128::try_from(suggested_max_fee_per_gas) | ||
| .map_err(|err| Error::GasPrice(err.into()))?, | ||
| max_priority_fee_per_gas: u128::try_from(max_priority_fee_per_gas) | ||
| .map_err(|err| Error::GasPrice(err.into()))?, | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.