1919
2020"""Tx settlement helper."""
2121
22+ import math
2223import time
2324from datetime import datetime
2425from typing import Any , Callable , Dict , Optional , TYPE_CHECKING , cast
4748DEFAULT_ON_CHAIN_INTERACT_TIMEOUT = 60.0
4849DEFAULT_ON_CHAIN_INTERACT_RETRIES = 5
4950DEFAULT_ON_CHAIN_INTERACT_SLEEP = 3.0
51+ DEFAULT_GAS_ESTIMATE_MULTIPLIER = 1.0
5052DEFAULT_MISSING_EVENT_EXCEPTION = Exception (
5153 "Could not verify transaction. Event not found."
5254)
@@ -111,6 +113,7 @@ def __init__(
111113 timeout : Optional [float ] = None ,
112114 retries : Optional [int ] = None ,
113115 sleep : Optional [float ] = None ,
116+ gas_estimate_multiplier : Optional [float ] = None ,
114117 ) -> None :
115118 """Initialize object."""
116119 self .chain_type = chain_type
@@ -121,6 +124,14 @@ def __init__(
121124 self .retries = retries or DEFAULT_ON_CHAIN_INTERACT_RETRIES
122125 self .sleep = sleep or DEFAULT_ON_CHAIN_INTERACT_SLEEP
123126
127+ if gas_estimate_multiplier :
128+ if gas_estimate_multiplier > 2.5 :
129+ logger .warning (f"{ gas_estimate_multiplier = } is unusually high." )
130+ if gas_estimate_multiplier <= 0 :
131+ raise ValueError (f"{ gas_estimate_multiplier = } must be positive." )
132+
133+ self .gas_multiplier = gas_estimate_multiplier or DEFAULT_GAS_ESTIMATE_MULTIPLIER
134+
124135 def _get_preferred_gas_price_strategy (self , tx_dict : dict ) -> str | None :
125136 """Get the preferred gas price strategy based on tx_dict fields."""
126137 if "maxFeePerGas" in tx_dict and "maxPriorityFeePerGas" in tx_dict :
@@ -176,13 +187,14 @@ def transact(self, dry_run: bool = False) -> "TxSettler":
176187 )
177188 if gas_price is not None :
178189 self .tx_dict .update (gas_price )
179-
180190 self .tx_dict = self .ledger_api .update_with_gas_estimate (
181191 {
182192 ** self .tx_dict ,
183193 "gas" : self .tx_dict .get ("gas" , 0 ),
184194 }
185195 )
196+ gas_estimated = int (self .tx_dict .get ("gas" , 0 ))
197+ self .tx_dict ["gas" ] = math .ceil (gas_estimated * self .gas_multiplier )
186198 tx_signed = self .crypto .sign_transaction (transaction = self .tx_dict )
187199 self .tx_hash = self .ledger_api .send_signed_transaction (
188200 tx_signed = tx_signed ,
0 commit comments