Skip to content

Commit 6d3d7da

Browse files
author
abel
committed
(feat) Improved the logic in the gas limit estimator component to consider the cost of each included element for batch messages
1 parent 2164c2c commit 6d3d7da

File tree

4 files changed

+866
-78
lines changed

4 files changed

+866
-78
lines changed

pyinjective/core/broadcaster.py

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABC, abstractmethod
22
from decimal import Decimal
3-
from typing import Callable, List, Optional, Tuple
3+
from typing import List, Optional
44

55
import math
66
from google.protobuf import any_pb2
@@ -9,7 +9,7 @@
99
from pyinjective.async_client import AsyncClient
1010
from pyinjective.composer import Composer
1111
from pyinjective.constant import Network
12-
from pyinjective.proto.cosmos.authz.v1beta1 import tx_pb2 as cosmos_authz_tx_pb
12+
from pyinjective.core.gas_limit_estimator import GasLimitEstimator
1313

1414

1515
class BroadcasterAccountConfig(ABC):
@@ -287,52 +287,25 @@ async def configure_gas_fee_for_transaction(
287287

288288

289289
class MessageBasedTransactionFeeCalculator(TransactionFeeCalculator):
290-
DEFAULT_GAS_LIMIT = 150_000
291-
DEFAULT_EXCHANGE_GAS_LIMIT = 100_000
290+
TRANSACTION_GAS_LIMIT = 60_000
292291

293292
def __init__(
294293
self,
295294
client: AsyncClient,
296295
composer: Composer,
297-
gas_price: Optional[int] = None,
298-
base_gas_limit: Optional[int] = None,
299-
base_exchange_gas_limit: Optional[int] = None):
296+
gas_price: Optional[int] = None):
300297
self._client = client
301298
self._composer = composer
302299
self._gas_price = gas_price or self.DEFAULT_GAS_PRICE
303-
self._base_gas_limit = base_gas_limit or self.DEFAULT_GAS_LIMIT
304-
self._base_exchange_gas_limit = base_exchange_gas_limit or self.DEFAULT_EXCHANGE_GAS_LIMIT
305-
306-
self._gas_limit_per_message_type_rules: List[Tuple[Callable, Decimal]] = [
307-
(
308-
lambda message: "MsgPrivilegedExecuteContract" in self._message_type(message=message),
309-
Decimal("6") * self._base_gas_limit
310-
),
311-
(
312-
lambda message: "MsgExecuteContract" in self._message_type(message=message),
313-
Decimal("2.5") * self._base_gas_limit
314-
),
315-
(
316-
lambda message: "wasm." in self._message_type(message=message),
317-
Decimal("1.5") * self._base_gas_limit
318-
),
319-
(
320-
lambda message: "exchange." in self._message_type(message=message),
321-
Decimal("1") * self._base_exchange_gas_limit
322-
),
323-
(
324-
lambda message: self._is_governance_message(message=message),
325-
Decimal("15") * self._base_gas_limit
326-
),
327-
]
328300

329301
async def configure_gas_fee_for_transaction(
330302
self,
331303
transaction: Transaction,
332304
private_key: PrivateKey,
333305
public_key: PublicKey,
334306
):
335-
transaction_gas_limit = math.ceil(self._calculate_gas_limit(messages=transaction.msgs))
307+
messages_gas_limit = math.ceil(self._calculate_gas_limit(messages=transaction.msgs))
308+
transaction_gas_limit = messages_gas_limit + self.TRANSACTION_GAS_LIMIT
336309

337310
fee = [
338311
self._composer.Coin(
@@ -351,28 +324,11 @@ def _message_type(self, message: any_pb2.Any) -> str:
351324
message_type = message.DESCRIPTOR.full_name
352325
return message_type
353326

354-
def _is_governance_message(self, message: any_pb2.Any) -> bool:
355-
message_type = self._message_type(message=message)
356-
return "gov." in message_type and ("MsgDeposit" in message_type or "MsgSubmitProposal" in message_type)
357-
358327
def _calculate_gas_limit(self, messages: List[any_pb2.Any]) -> int:
359328
total_gas_limit = Decimal("0")
360329

361330
for message in messages:
362-
applying_rule = next(
363-
(rule_tuple for rule_tuple in self._gas_limit_per_message_type_rules
364-
if rule_tuple[0](message)),
365-
None
366-
)
367-
368-
if applying_rule is None:
369-
total_gas_limit += self._base_gas_limit
370-
else:
371-
total_gas_limit += applying_rule[1]
372-
373-
if self._message_type(message=message).endswith("MsgExec"):
374-
exec_message = cosmos_authz_tx_pb.MsgExec.FromString(message.value)
375-
sub_messages_limit = self._calculate_gas_limit(messages=exec_message.msgs)
376-
total_gas_limit += sub_messages_limit
331+
estimator = GasLimitEstimator.for_message(message=message)
332+
total_gas_limit += estimator.gas_limit()
377333

378334
return math.ceil(total_gas_limit)

0 commit comments

Comments
 (0)