Skip to content

Commit ddaddff

Browse files
committed
use partial gas price percentiles
1 parent 74a4a66 commit ddaddff

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

seth/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ func (m *Client) CalculateGasEstimations(request GasEstimationRequest) GasEstima
934934
defer cancel()
935935

936936
var disableEstimationsIfNeeded = func(err error) {
937-
if strings.Contains(err.Error(), ZeroGasSuggestedErr) {
937+
if strings.Contains(err.Error(), ZeroGasSuggestedErr) || strings.Contains(err.Error(), "Partial data received") {
938938
L.Warn().Msg("Received incorrect gas estimations. Disabling them and reverting to hardcoded values. Remember to update your config!")
939939
m.Cfg.Network.GasPriceEstimationEnabled = false
940940
}

seth/gas.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func (m *GasEstimator) Stats(fromNumber uint64, priorityPerc float64) (GasSugges
4848
}
4949
gasPercs, err := quantilesFromFloatArray(baseFees)
5050
if err != nil {
51-
return GasSuggestions{}, err
51+
L.Debug().Err(err).Msg("Error calculating gas percentiles. Will use partial data.")
52+
// return GasSuggestions{}, err
5253
}
5354
tips := make([]float64, 0)
5455
for _, bf := range hist.Reward {
@@ -64,7 +65,8 @@ func (m *GasEstimator) Stats(fromNumber uint64, priorityPerc float64) (GasSugges
6465
}
6566
tipPercs, err := quantilesFromFloatArray(tips)
6667
if err != nil {
67-
return GasSuggestions{}, err
68+
// return GasSuggestions{}, err
69+
L.Debug().Err(err).Msg("Error calculating tip percentiles. Will use partial data.")
6870
}
6971
suggestedGasPrice, err := m.Client.Client.SuggestGasPrice(context.Background())
7072
if err != nil {
@@ -105,23 +107,28 @@ type GasSuggestions struct {
105107
func quantilesFromFloatArray(fa []float64) (*GasPercentiles, error) {
106108
perMax, err := stats.Max(fa)
107109
if err != nil {
108-
return nil, err
110+
L.Debug().Err(err).Msg("Error calculating max gas price. Continuing")
111+
// return nil, err
109112
}
110113
perc99, err := stats.Percentile(fa, 99)
111114
if err != nil {
112-
return nil, err
115+
L.Debug().Err(err).Msg("Error calculating p99 gas price. Continuing")
116+
// return nil, err
113117
}
114118
perc75, err := stats.Percentile(fa, 75)
115119
if err != nil {
116-
return nil, err
120+
L.Debug().Err(err).Msg("Error calculating p75 gas price. Continuing")
121+
// return nil, err
117122
}
118123
perc50, err := stats.Percentile(fa, 50)
119124
if err != nil {
120-
return nil, err
125+
L.Debug().Err(err).Msg("Error calculating p50 gas price. Continuing")
126+
// return nil, err
121127
}
122128
perc25, err := stats.Percentile(fa, 25)
123129
if err != nil {
124-
return nil, err
130+
L.Debug().Err(err).Msg("Error calculating p25 gas price. Continuing")
131+
// return nil, err
125132
}
126133
return &GasPercentiles{
127134
Max: perMax,

seth/gas_adjuster.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,22 +467,55 @@ func (m *Client) HistoricalFeeData(priority string) (baseFee float64, historical
467467
stats, err := estimator.Stats(m.Cfg.Network.GasPriceEstimationBlocks, 99)
468468
if err != nil {
469469
L.Debug().
470-
Msgf("Failed to get fee history due to: %s. Skipping automation gas estimation", err.Error())
470+
Err(err).
471+
Msgf("Failed to get fee history.. Skipping automation gas estimation")
471472

472473
return
473474
}
474475

475476
switch priority {
476477
case Priority_Degen:
478+
if stats.GasPrice.Max == math.NaN() || stats.TipCap.Max == math.NaN() {
479+
err = fmt.Errorf("Partial data received. Either base fee or suggested tip is 0 for Max values")
480+
L.Debug().
481+
Err(err).
482+
Msgf("Failed to get fee history.. Skipping automation gas estimation")
483+
484+
return
485+
}
477486
baseFee = stats.GasPrice.Max
478487
historicalGasTipCap = stats.TipCap.Max
479488
case Priority_Fast:
489+
if stats.GasPrice.Perc99 == math.NaN() || stats.TipCap.Perc99 == math.NaN() {
490+
err = fmt.Errorf("Partial data received. Either base fee or suggested tip is 0 for Perc99 values")
491+
L.Debug().
492+
Err(err).
493+
Msgf("Failed to get fee history.. Skipping automation gas estimation")
494+
495+
return
496+
}
480497
baseFee = stats.GasPrice.Perc99
481498
historicalGasTipCap = stats.TipCap.Perc99
482499
case Priority_Standard:
500+
if stats.GasPrice.Perc50 == math.NaN() || stats.TipCap.Perc50 == math.NaN() {
501+
err = fmt.Errorf("Partial data received. Either base fee or suggested tip is 0 for Perc50 values")
502+
L.Debug().
503+
Err(err).
504+
Msgf("Failed to get fee history.. Skipping automation gas estimation")
505+
506+
return
507+
}
483508
baseFee = stats.GasPrice.Perc50
484509
historicalGasTipCap = stats.TipCap.Perc50
485510
case Priority_Slow:
511+
if math.IsNaN(stats.GasPrice.Perc25) || math.IsNaN(stats.TipCap.Perc25) {
512+
err = fmt.Errorf("Partial data received. Either base fee or suggested tip is 0 for Perc25 values")
513+
L.Debug().
514+
Err(err).
515+
Msgf("Failed to get fee history.. Skipping automation gas estimation")
516+
517+
return
518+
}
486519
baseFee = stats.GasPrice.Perc25
487520
historicalGasTipCap = stats.TipCap.Perc25
488521
default:

0 commit comments

Comments
 (0)