Skip to content

Commit 67fbafb

Browse files
Merge pull request #2394 from valory-xyz/feat/gas_estimate_multiplier
Gas estimate multiplier on txSettler
2 parents 5a4b83a + 342f5e5 commit 67fbafb

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

autonomy/chain/tx.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
"""Tx settlement helper."""
2121

22+
import math
2223
import time
2324
from datetime import datetime
2425
from typing import Any, Callable, Dict, Optional, TYPE_CHECKING, cast
@@ -47,6 +48,7 @@
4748
DEFAULT_ON_CHAIN_INTERACT_TIMEOUT = 60.0
4849
DEFAULT_ON_CHAIN_INTERACT_RETRIES = 5
4950
DEFAULT_ON_CHAIN_INTERACT_SLEEP = 3.0
51+
DEFAULT_GAS_ESTIMATE_MULTIPLIER = 1.0
5052
DEFAULT_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,

docs/api/chain/tx.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def __init__(ledger_api: LedgerApi,
5555
tx_builder: Callable[[], Dict],
5656
timeout: Optional[float] = None,
5757
retries: Optional[int] = None,
58-
sleep: Optional[float] = None) -> None
58+
sleep: Optional[float] = None,
59+
gas_estimate_multiplier: Optional[float] = None) -> None
5960
```
6061

6162
Initialize object.

0 commit comments

Comments
 (0)