Skip to content

Commit

Permalink
(feat) Updated reference gas cost for messages in the gas limit estim…
Browse files Browse the repository at this point in the history
…ator after chain upgrade v1.13
  • Loading branch information
aarmoa committed Aug 8, 2024
1 parent b57aba9 commit 281936b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 51 deletions.
16 changes: 8 additions & 8 deletions pyinjective/core/gas_limit_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
tx_pb2 as injective_exchange_tx_pb,
)

SPOT_ORDER_CREATION_GAS_LIMIT = 50_000
DERIVATIVE_ORDER_CREATION_GAS_LIMIT = 70_000
SPOT_ORDER_CREATION_GAS_LIMIT = 52_000
DERIVATIVE_ORDER_CREATION_GAS_LIMIT = 84_000
SPOT_ORDER_CANCELATION_GAS_LIMIT = 50_000
DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT = 60_000
DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT = 68_000
# POST ONLY orders take around 50% more gas to create than normal orders due to the required validations
SPOT_POST_ONLY_ORDER_MULTIPLIER = 0.5
DERIVATIVE_POST_ONLY_ORDER_MULTIPLIER = 0.5
SPOT_POST_ONLY_ORDER_MULTIPLIER = 0.62
DERIVATIVE_POST_ONLY_ORDER_MULTIPLIER = 0.35


class GasLimitEstimator(ABC):
GENERAL_MESSAGE_GAS_LIMIT = 15_000
GENERAL_MESSAGE_GAS_LIMIT = 25_000
BASIC_REFERENCE_GAS_LIMIT = 150_000

@classmethod
Expand Down Expand Up @@ -183,7 +183,7 @@ def _message_class(self, message: any_pb2.Any):
class BatchUpdateOrdersGasLimitEstimator(GasLimitEstimator):
CANCEL_ALL_SPOT_MARKET_GAS_LIMIT = 40_000
CANCEL_ALL_DERIVATIVE_MARKET_GAS_LIMIT = 50_000
MESSAGE_GAS_LIMIT = 15_000
MESSAGE_GAS_LIMIT = 30_000

AVERAGE_CANCEL_ALL_AFFECTED_ORDERS = 20

Expand Down Expand Up @@ -246,7 +246,7 @@ def _message_class(self, message: any_pb2.Any):


class ExecGasLimitEstimator(GasLimitEstimator):
DEFAULT_GAS_LIMIT = 8_000
DEFAULT_GAS_LIMIT = 20_000

def __init__(self, message: any_pb2.Any):
self._message = self._parsed_message(message=message)
Expand Down
72 changes: 42 additions & 30 deletions tests/core/test_gas_limit_estimator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
from decimal import Decimal

from pyinjective.composer import Composer
from pyinjective.core.gas_limit_estimator import GasLimitEstimator
from pyinjective.core.gas_limit_estimator import (
DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT,
DERIVATIVE_ORDER_CREATION_GAS_LIMIT,
SPOT_ORDER_CANCELATION_GAS_LIMIT,
SPOT_ORDER_CREATION_GAS_LIMIT,
BatchCancelDerivativeOrdersGasLimitEstimator,
BatchCancelSpotOrdersGasLimitEstimator,
BatchCreateDerivativeLimitOrdersGasLimitEstimator,
BatchCreateSpotLimitOrdersGasLimitEstimator,
BatchUpdateOrdersGasLimitEstimator,
ExecGasLimitEstimator,
GasLimitEstimator,
)
from pyinjective.core.market import BinaryOptionMarket
from pyinjective.proto.cosmos.gov.v1beta1 import tx_pb2 as gov_tx_pb
from pyinjective.proto.cosmwasm.wasm.v1 import tx_pb2 as wasm_tx_pb
Expand Down Expand Up @@ -44,8 +56,8 @@ def test_estimation_for_batch_create_spot_limit_orders(self):
message = composer.msg_batch_create_spot_limit_orders(sender="sender", orders=orders)
estimator = GasLimitEstimator.for_message(message=message)

expected_order_gas_limit = 50000
expected_message_gas_limit = 15000
expected_order_gas_limit = SPOT_ORDER_CREATION_GAS_LIMIT
expected_message_gas_limit = BatchCreateSpotLimitOrdersGasLimitEstimator.GENERAL_MESSAGE_GAS_LIMIT

assert (expected_order_gas_limit * 2) + expected_message_gas_limit == estimator.gas_limit()

Expand All @@ -72,8 +84,8 @@ def test_estimation_for_batch_cancel_spot_orders(self):
message = composer.msg_batch_cancel_spot_orders(sender="sender", orders_data=orders)
estimator = GasLimitEstimator.for_message(message=message)

expected_order_gas_limit = 50000
expected_message_gas_limit = 15000
expected_order_gas_limit = SPOT_ORDER_CANCELATION_GAS_LIMIT
expected_message_gas_limit = BatchCancelSpotOrdersGasLimitEstimator.GENERAL_MESSAGE_GAS_LIMIT

assert (expected_order_gas_limit * 3) + expected_message_gas_limit == estimator.gas_limit()

Expand Down Expand Up @@ -103,8 +115,8 @@ def test_estimation_for_batch_create_derivative_limit_orders(self):
message = composer.msg_batch_create_derivative_limit_orders(sender="sender", orders=orders)
estimator = GasLimitEstimator.for_message(message=message)

expected_order_gas_limit = 70_000
expected_message_gas_limit = 15000
expected_order_gas_limit = DERIVATIVE_ORDER_CREATION_GAS_LIMIT
expected_message_gas_limit = BatchCreateDerivativeLimitOrdersGasLimitEstimator.GENERAL_MESSAGE_GAS_LIMIT

assert (expected_order_gas_limit * 2) + expected_message_gas_limit == estimator.gas_limit()

Expand All @@ -131,8 +143,8 @@ def test_estimation_for_batch_cancel_derivative_orders(self):
message = composer.msg_batch_cancel_derivative_orders(sender="sender", orders_data=orders)
estimator = GasLimitEstimator.for_message(message=message)

expected_order_gas_limit = 60_000
expected_message_gas_limit = 15000
expected_order_gas_limit = DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
expected_message_gas_limit = BatchCancelDerivativeOrdersGasLimitEstimator.GENERAL_MESSAGE_GAS_LIMIT

assert (expected_order_gas_limit * 3) + expected_message_gas_limit == estimator.gas_limit()

Expand Down Expand Up @@ -166,8 +178,8 @@ def test_estimation_for_batch_update_orders_to_create_spot_orders(self):
)
estimator = GasLimitEstimator.for_message(message=message)

expected_order_gas_limit = 50_000
expected_message_gas_limit = 15_000
expected_order_gas_limit = SPOT_ORDER_CREATION_GAS_LIMIT
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT

assert (expected_order_gas_limit * 2) + expected_message_gas_limit == estimator.gas_limit()

Expand Down Expand Up @@ -203,8 +215,8 @@ def test_estimation_for_batch_update_orders_to_create_derivative_orders(self):
)
estimator = GasLimitEstimator.for_message(message=message)

expected_order_gas_limit = 70_000
expected_message_gas_limit = 15_000
expected_order_gas_limit = DERIVATIVE_ORDER_CREATION_GAS_LIMIT
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT

assert (expected_order_gas_limit * 2) + expected_message_gas_limit == estimator.gas_limit()

Expand Down Expand Up @@ -260,8 +272,8 @@ def test_estimation_for_batch_update_orders_to_create_binary_orders(self, usdt_t
)
estimator = GasLimitEstimator.for_message(message=message)

expected_order_gas_limit = 70_000
expected_message_gas_limit = 15_000
expected_order_gas_limit = DERIVATIVE_ORDER_CREATION_GAS_LIMIT
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT

assert (expected_order_gas_limit * 2) + expected_message_gas_limit == estimator.gas_limit()

Expand Down Expand Up @@ -294,8 +306,8 @@ def test_estimation_for_batch_update_orders_to_cancel_spot_orders(self):
)
estimator = GasLimitEstimator.for_message(message=message)

expected_order_gas_limit = 50_000
expected_message_gas_limit = 15_000
expected_order_gas_limit = SPOT_ORDER_CANCELATION_GAS_LIMIT
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT

assert (expected_order_gas_limit * 3) + expected_message_gas_limit == estimator.gas_limit()

Expand Down Expand Up @@ -328,8 +340,8 @@ def test_estimation_for_batch_update_orders_to_cancel_derivative_orders(self):
)
estimator = GasLimitEstimator.for_message(message=message)

expected_order_gas_limit = 60_000
expected_message_gas_limit = 15_000
expected_order_gas_limit = DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT

assert (expected_order_gas_limit * 3) + expected_message_gas_limit == estimator.gas_limit()

Expand Down Expand Up @@ -363,8 +375,8 @@ def test_estimation_for_batch_update_orders_to_cancel_binary_orders(self):
)
estimator = GasLimitEstimator.for_message(message=message)

expected_order_gas_limit = 60_000
expected_message_gas_limit = 15_000
expected_order_gas_limit = DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT

assert (expected_order_gas_limit * 3) + expected_message_gas_limit == estimator.gas_limit()

Expand All @@ -383,8 +395,8 @@ def test_estimation_for_batch_update_orders_to_cancel_all_for_spot_market(self):
)
estimator = GasLimitEstimator.for_message(message=message)

expected_gas_limit = 40_000 * 20
expected_message_gas_limit = 15_000
expected_gas_limit = BatchUpdateOrdersGasLimitEstimator.CANCEL_ALL_SPOT_MARKET_GAS_LIMIT * 20
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT

assert expected_gas_limit + expected_message_gas_limit == estimator.gas_limit()

Expand All @@ -403,8 +415,8 @@ def test_estimation_for_batch_update_orders_to_cancel_all_for_derivative_market(
)
estimator = GasLimitEstimator.for_message(message=message)

expected_gas_limit = 50_000 * 20
expected_message_gas_limit = 15_000
expected_gas_limit = BatchUpdateOrdersGasLimitEstimator.CANCEL_ALL_DERIVATIVE_MARKET_GAS_LIMIT * 20
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT

assert expected_gas_limit + expected_message_gas_limit == estimator.gas_limit()

Expand All @@ -423,8 +435,8 @@ def test_estimation_for_batch_update_orders_to_cancel_all_for_binary_options_mar
)
estimator = GasLimitEstimator.for_message(message=message)

expected_gas_limit = 50_000 * 20
expected_message_gas_limit = 15_000
expected_gas_limit = BatchUpdateOrdersGasLimitEstimator.CANCEL_ALL_DERIVATIVE_MARKET_GAS_LIMIT * 20
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT

assert expected_gas_limit + expected_message_gas_limit == estimator.gas_limit()

Expand Down Expand Up @@ -452,9 +464,9 @@ def test_estimation_for_exec_message(self):

estimator = GasLimitEstimator.for_message(message=message)

expected_order_gas_limit = 50_000
expected_inner_message_gas_limit = 15_000
expected_exec_message_gas_limit = 8_000
expected_order_gas_limit = SPOT_ORDER_CREATION_GAS_LIMIT
expected_inner_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT
expected_exec_message_gas_limit = ExecGasLimitEstimator.DEFAULT_GAS_LIMIT

assert (
expected_order_gas_limit + expected_inner_message_gas_limit + expected_exec_message_gas_limit
Expand Down
34 changes: 21 additions & 13 deletions tests/core/test_message_based_transaction_fee_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
from pyinjective.async_client import AsyncClient
from pyinjective.composer import Composer
from pyinjective.core.broadcaster import MessageBasedTransactionFeeCalculator
from pyinjective.core.gas_limit_estimator import (
DefaultGasLimitEstimator,
ExecGasLimitEstimator,
GenericExchangeGasLimitEstimator,
PrivilegedExecuteContractGasLimitEstimator,
)
from pyinjective.core.network import Network
from pyinjective.proto.cosmos.gov.v1beta1 import tx_pb2 as gov_tx_pb2
from pyinjective.proto.cosmwasm.wasm.v1 import tx_pb2 as wasm_tx_pb2
Expand All @@ -31,8 +37,10 @@ async def test_gas_fee_for_privileged_execute_contract_message(self):

await calculator.configure_gas_fee_for_transaction(transaction=transaction, private_key=None, public_key=None)

expected_transaction_gas_limit = 60_000
expected_gas_limit = math.ceil(Decimal(6) * 150_000 + expected_transaction_gas_limit)
expected_transaction_gas_limit = MessageBasedTransactionFeeCalculator.TRANSACTION_GAS_LIMIT
expected_gas_limit = math.ceil(
PrivilegedExecuteContractGasLimitEstimator.BASIC_REFERENCE_GAS_LIMIT * 6 + expected_transaction_gas_limit
)
assert expected_gas_limit == transaction.fee.gas_limit
assert str(expected_gas_limit * 5_000_000) == transaction.fee.amount[0].amount

Expand All @@ -57,7 +65,7 @@ async def test_gas_fee_for_execute_contract_message(self):

await calculator.configure_gas_fee_for_transaction(transaction=transaction, private_key=None, public_key=None)

expected_transaction_gas_limit = 60_000
expected_transaction_gas_limit = MessageBasedTransactionFeeCalculator.TRANSACTION_GAS_LIMIT
expected_gas_limit = math.ceil(Decimal(2.5) * 150_000 + expected_transaction_gas_limit)
assert expected_gas_limit == transaction.fee.gas_limit
assert str(expected_gas_limit * 5_000_000) == transaction.fee.amount[0].amount
Expand All @@ -79,7 +87,7 @@ async def test_gas_fee_for_wasm_message(self):

await calculator.configure_gas_fee_for_transaction(transaction=transaction, private_key=None, public_key=None)

expected_transaction_gas_limit = 60_000
expected_transaction_gas_limit = MessageBasedTransactionFeeCalculator.TRANSACTION_GAS_LIMIT
expected_gas_limit = math.ceil(Decimal(1.5) * 150_000 + expected_transaction_gas_limit)
assert expected_gas_limit == transaction.fee.gas_limit
assert str(expected_gas_limit * 5_000_000) == transaction.fee.amount[0].amount
Expand All @@ -101,7 +109,7 @@ async def test_gas_fee_for_governance_message(self):

await calculator.configure_gas_fee_for_transaction(transaction=transaction, private_key=None, public_key=None)

expected_transaction_gas_limit = 60_000
expected_transaction_gas_limit = MessageBasedTransactionFeeCalculator.TRANSACTION_GAS_LIMIT
expected_gas_limit = math.ceil(Decimal(15) * 150_000 + expected_transaction_gas_limit)
assert expected_gas_limit == transaction.fee.gas_limit
assert str(expected_gas_limit * 5_000_000) == transaction.fee.amount[0].amount
Expand Down Expand Up @@ -131,7 +139,7 @@ async def test_gas_fee_for_exchange_message(self):

await calculator.configure_gas_fee_for_transaction(transaction=transaction, private_key=None, public_key=None)

expected_transaction_gas_limit = 60_000
expected_transaction_gas_limit = MessageBasedTransactionFeeCalculator.TRANSACTION_GAS_LIMIT
expected_gas_limit = math.ceil(Decimal(1) * 120_000 + expected_transaction_gas_limit)
assert expected_gas_limit == transaction.fee.gas_limit
assert str(expected_gas_limit * 5_000_000) == transaction.fee.amount[0].amount
Expand Down Expand Up @@ -162,9 +170,9 @@ async def test_gas_fee_for_msg_exec_message(self):

await calculator.configure_gas_fee_for_transaction(transaction=transaction, private_key=None, public_key=None)

expected_transaction_gas_limit = 60_000
expected_inner_message_gas_limit = Decimal(1) * 120_000
expected_exec_message_gas_limit = 8_000
expected_transaction_gas_limit = MessageBasedTransactionFeeCalculator.TRANSACTION_GAS_LIMIT
expected_inner_message_gas_limit = GenericExchangeGasLimitEstimator.BASIC_REFERENCE_GAS_LIMIT
expected_exec_message_gas_limit = ExecGasLimitEstimator.DEFAULT_GAS_LIMIT
expected_gas_limit = math.ceil(
expected_exec_message_gas_limit + expected_inner_message_gas_limit + expected_transaction_gas_limit
)
Expand Down Expand Up @@ -200,10 +208,10 @@ async def test_gas_fee_for_two_messages_in_one_transaction(self):

await calculator.configure_gas_fee_for_transaction(transaction=transaction, private_key=None, public_key=None)

expected_transaction_gas_limit = 60_000
expected_inner_message_gas_limit = Decimal(1) * 120_000
expected_exec_message_gas_limit = 8_000
expected_send_message_gas_limit = 150_000
expected_transaction_gas_limit = MessageBasedTransactionFeeCalculator.TRANSACTION_GAS_LIMIT
expected_inner_message_gas_limit = GenericExchangeGasLimitEstimator.BASIC_REFERENCE_GAS_LIMIT
expected_exec_message_gas_limit = ExecGasLimitEstimator.DEFAULT_GAS_LIMIT
expected_send_message_gas_limit = DefaultGasLimitEstimator.DEFAULT_GAS_LIMIT
expected_gas_limit = math.ceil(
expected_exec_message_gas_limit
+ expected_inner_message_gas_limit
Expand Down

0 comments on commit 281936b

Please sign in to comment.