1
1
# -*- coding: utf-8 -*-
2
2
# ------------------------------------------------------------------------------
3
3
#
4
- # Copyright 2021-2024 Valory AG
4
+ # Copyright 2021-2025 Valory AG
5
5
# Copyright 2018-2019 Fetch.AI Limited
6
6
#
7
7
# Licensed under the Apache License, Version 2.0 (the "License");
84
84
POLYGON_GAS_ENDPOINT = "https://gasstation-mainnet.matic.network/v2"
85
85
MAX_GAS_FAST = 1500
86
86
RPC_CALL_MAX_WORKERS = 1
87
- N_RETRIES = 3
88
- PERCENTILE_INCREASE = 5
89
87
90
88
# How many blocks to consider for priority fee estimation
91
89
FEE_HISTORY_BLOCKS = 10
104
102
105
103
PRIORITY_FEE_INCREASE_BOUNDARY = 200 # percentage
106
104
105
+ # this is the minimum allowed max fee per gas on Gnosis
106
+ DEFAULT_MIN_ALLOWED_TIP = to_wei (1 , "gwei" )
107
+
107
108
DEFAULT_EIP1559_STRATEGY = {
108
109
"max_gas_fast" : MAX_GAS_FAST ,
109
110
"fee_history_blocks" : FEE_HISTORY_BLOCKS ,
110
111
"fee_history_percentile" : FEE_HISTORY_PERCENTILE ,
111
112
"default_priority_fee" : DEFAULT_PRIORITY_FEE ,
112
113
"fallback_estimate" : FALLBACK_ESTIMATE ,
114
+ "min_allowed_tip" : DEFAULT_MIN_ALLOWED_TIP ,
113
115
"priority_fee_increase_boundary" : PRIORITY_FEE_INCREASE_BOUNDARY ,
114
116
}
115
117
@@ -169,32 +171,33 @@ def estimate_priority_fee(
169
171
default_priority_fee : Optional [int ],
170
172
fee_history_blocks : int ,
171
173
fee_history_percentile : int ,
174
+ min_allowed_tip : int ,
172
175
priority_fee_increase_boundary : int ,
173
176
) -> Optional [int ]:
174
177
"""Estimate priority fee from base fee."""
175
178
176
179
if default_priority_fee is not None :
177
180
return default_priority_fee
178
181
179
- for _ in range (N_RETRIES ):
180
- fee_history = web3_object .eth .fee_history (
181
- fee_history_blocks , block_number , [fee_history_percentile ] # type: ignore
182
- )
183
- # This is going to break if more percentiles are introduced in the future,
184
- # i.e., `fee_history_percentile` param becomes a `List[int]`.
185
- rewards = sorted (
186
- [reward [0 ] for reward in fee_history .get ("reward" , []) if reward [0 ] > 0 ]
187
- )
188
- # we need atleast 2 rewards to proceed further
189
- if len (rewards ) >= 2 :
190
- break
191
- # Increment percentile for next attempt
192
- fee_history_percentile = min (100 , fee_history_percentile + PERCENTILE_INCREASE )
193
-
194
- # Return None if fewer than 2 rewards after retries
195
- if len (rewards ) < 2 :
182
+ fee_history = web3_object .eth .fee_history (
183
+ fee_history_blocks , block_number , [fee_history_percentile ] # type: ignore
184
+ )
185
+
186
+ # This is going to break if more percentiles are introduced in the future,
187
+ # i.e., `fee_history_percentile` param becomes a `List[int]`.
188
+ rewards = sorted (
189
+ [
190
+ reward [0 ]
191
+ for reward in fee_history .get ("reward" , [])
192
+ if reward [0 ] >= min_allowed_tip
193
+ ]
194
+ )
195
+ if len (rewards ) == 0 :
196
196
return None
197
197
198
+ if len (rewards ) == 1 :
199
+ return rewards [0 ]
200
+
198
201
# Calculate percentage increases from between ordered list of fees
199
202
percentage_increases = [
200
203
((j - i ) / i ) * 100 if i != 0 else 0 for i , j in zip (rewards [:- 1 ], rewards [1 :])
@@ -220,6 +223,7 @@ def get_gas_price_strategy_eip1559(
220
223
fee_history_percentile : int ,
221
224
default_priority_fee : Optional [int ],
222
225
fallback_estimate : Dict [str , Wei ],
226
+ min_allowed_tip : int ,
223
227
priority_fee_increase_boundary : int ,
224
228
) -> Callable [[Web3 , TxParams ], Dict [str , Wei ]]:
225
229
"""Get the gas price strategy."""
@@ -253,33 +257,22 @@ def eip1559_price_strategy(
253
257
if base_fee is None or block_number is None :
254
258
return fallback ()
255
259
256
- base_fee_gwei = to_eth_unit (base_fee )
257
-
258
260
estimated_priority_fee = estimate_priority_fee (
259
261
web3 ,
260
262
block_number ,
261
263
default_priority_fee = default_priority_fee ,
262
264
fee_history_blocks = fee_history_blocks ,
263
265
fee_history_percentile = fee_history_percentile ,
266
+ min_allowed_tip = min_allowed_tip ,
264
267
priority_fee_increase_boundary = priority_fee_increase_boundary ,
265
268
)
266
269
267
270
if estimated_priority_fee is None :
268
271
return fallback ()
269
272
270
- multiplier = get_base_fee_multiplier (base_fee_gwei )
271
-
272
- potential_max_fee = base_fee * multiplier
273
- max_fee_per_gas = (
274
- (potential_max_fee + estimated_priority_fee )
275
- if estimated_priority_fee > potential_max_fee
276
- else potential_max_fee
277
- )
273
+ max_fee_per_gas = base_fee + estimated_priority_fee
278
274
279
- if (
280
- to_eth_unit (max_fee_per_gas ) >= max_gas_fast
281
- or to_eth_unit (estimated_priority_fee ) >= max_gas_fast
282
- ):
275
+ if to_eth_unit (max_fee_per_gas ) >= max_gas_fast :
283
276
return fallback (
284
277
"The estimated gas price is larger than the `max_gas_fast`. Falling back."
285
278
)
0 commit comments