Skip to content

Commit

Permalink
Fix bug for additional Tip (#3311)
Browse files Browse the repository at this point in the history
# Description
I've noticed that non-colocated drivers almost always have `tip` = 1
gwei, even in volatile periods when base fee raises significantly.

The reason is, our first choice gas price estimator almost always
returns tip < 1 gwei, so the `self.min_priority_fee` is always used as a
tip and it's fixed. (The reason why gas price estimator always return
tip <1 gwei will be analyzed independently of this fix, but one of the
reasons can be: using default inclusion time of 30s always, estimating
gas price at the SOLVE time instead of SETTLE time etc, or just not
working properly).

This PR makes the tip always dependable of `additionalTip` config that
is used by infra to additionally tweak gas price estimations if needed.

# Changes
<!-- List of detailed changes (how the change is accomplished) -->

- [ ] Additional tip is always added to final estimated tip.

## How to test
Will try it out on staging.

<!--
## Related Issues

Fixes #
-->
  • Loading branch information
sunce86 authored Mar 6, 2025
1 parent 72fe27e commit 7bb3594
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions crates/driver/src/infra/blockchain/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
/// private submission networks are used.
use {
super::Error,
crate::infra::config::file::GasEstimatorType,
crate::{domain::eth, infra::mempool},
crate::{
domain::eth,
infra::{config::file::GasEstimatorType, mempool},
},
ethcontract::dyns::DynWeb3,
gas_estimation::{GasPriceEstimating, nativegasestimator::NativeGasEstimator},
std::sync::Arc,
Expand All @@ -18,7 +20,7 @@ type AdditionalTip = (MaxAdditionalTip, AdditionalTipPercentage);
pub struct GasPriceEstimator {
//TODO: remove visibility once boundary is removed
pub(super) gas: Arc<dyn GasPriceEstimating>,
additional_tip: Option<AdditionalTip>,
additional_tip: AdditionalTip,
max_fee_per_gas: eth::U256,
min_priority_fee: eth::U256,
}
Expand Down Expand Up @@ -51,7 +53,8 @@ impl GasPriceEstimator {
..
} => (max_additional_tip, additional_tip_percentage),
})
.next();
.next()
.unwrap_or((eth::U256::zero(), 0.));
// Use the lowest max_fee_per_gas of all mempools as the max_fee_per_gas
let max_fee_per_gas = mempools
.iter()
Expand Down Expand Up @@ -81,24 +84,20 @@ impl GasPriceEstimator {
self.gas
.estimate()
.await
.map(|mut estimate| {
let estimate = match self.additional_tip {
Some((max_additional_tip, additional_tip_percentage)) => {
let additional_tip = max_additional_tip
.to_f64_lossy()
.min(estimate.max_fee_per_gas * additional_tip_percentage);
estimate.max_fee_per_gas += additional_tip;
estimate.max_priority_fee_per_gas += additional_tip;
estimate
}
None => estimate,
};
.map(|estimate| {
let (max, percentage) = self.additional_tip;
let additional_tip = max
.to_f64_lossy()
.min(estimate.max_fee_per_gas * percentage);

let tip = std::cmp::max(
self.min_priority_fee + eth::U256::from_f64_lossy(additional_tip),
eth::U256::from_f64_lossy(estimate.max_priority_fee_per_gas + additional_tip),
);

eth::GasPrice::new(
self.max_fee_per_gas.into(),
std::cmp::max(
self.min_priority_fee.into(),
eth::U256::from_f64_lossy(estimate.max_priority_fee_per_gas).into(),
),
tip.into(),
eth::U256::from_f64_lossy(estimate.base_fee_per_gas).into(),
)
})
Expand Down

0 comments on commit 7bb3594

Please sign in to comment.