Skip to content

Commit 5541211

Browse files
authored
Merge pull request #344 from InjectiveLabs/feat/update_gas_estimator_base_values
feat/update_gas_estimator_base_values
2 parents b57aba9 + b0141f3 commit 5541211

File tree

5 files changed

+76
-52
lines changed

5 files changed

+76
-52
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [1.6.3]
6+
### Fixed
7+
- Updated reference gas cost for messages in the gas limit estimator after chain upgrade v1.13
8+
59
## [1.6.2]
610
### Fixed
711
- Fixed issue in the `listen_derivative_market_updates` method in the `AsyncClient` class

pyinjective/core/gas_limit_estimator.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
tx_pb2 as injective_exchange_tx_pb,
1313
)
1414

15-
SPOT_ORDER_CREATION_GAS_LIMIT = 50_000
16-
DERIVATIVE_ORDER_CREATION_GAS_LIMIT = 70_000
15+
SPOT_ORDER_CREATION_GAS_LIMIT = 52_000
16+
DERIVATIVE_ORDER_CREATION_GAS_LIMIT = 84_000
1717
SPOT_ORDER_CANCELATION_GAS_LIMIT = 50_000
18-
DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT = 60_000
18+
DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT = 68_000
1919
# POST ONLY orders take around 50% more gas to create than normal orders due to the required validations
20-
SPOT_POST_ONLY_ORDER_MULTIPLIER = 0.5
21-
DERIVATIVE_POST_ONLY_ORDER_MULTIPLIER = 0.5
20+
SPOT_POST_ONLY_ORDER_MULTIPLIER = 0.62
21+
DERIVATIVE_POST_ONLY_ORDER_MULTIPLIER = 0.35
2222

2323

2424
class GasLimitEstimator(ABC):
25-
GENERAL_MESSAGE_GAS_LIMIT = 15_000
25+
GENERAL_MESSAGE_GAS_LIMIT = 25_000
2626
BASIC_REFERENCE_GAS_LIMIT = 150_000
2727

2828
@classmethod
@@ -183,7 +183,7 @@ def _message_class(self, message: any_pb2.Any):
183183
class BatchUpdateOrdersGasLimitEstimator(GasLimitEstimator):
184184
CANCEL_ALL_SPOT_MARKET_GAS_LIMIT = 40_000
185185
CANCEL_ALL_DERIVATIVE_MARKET_GAS_LIMIT = 50_000
186-
MESSAGE_GAS_LIMIT = 15_000
186+
MESSAGE_GAS_LIMIT = 30_000
187187

188188
AVERAGE_CANCEL_ALL_AFFECTED_ORDERS = 20
189189

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

247247

248248
class ExecGasLimitEstimator(GasLimitEstimator):
249-
DEFAULT_GAS_LIMIT = 8_000
249+
DEFAULT_GAS_LIMIT = 20_000
250250

251251
def __init__(self, message: any_pb2.Any):
252252
self._message = self._parsed_message(message=message)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "injective-py"
3-
version = "1.6.2"
3+
version = "1.6.3"
44
description = "Injective Python SDK, with Exchange API Client"
55
authors = ["Injective Labs <[email protected]>"]
66
license = "Apache-2.0"

tests/core/test_gas_limit_estimator.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
from decimal import Decimal
22

33
from pyinjective.composer import Composer
4-
from pyinjective.core.gas_limit_estimator import GasLimitEstimator
4+
from pyinjective.core.gas_limit_estimator import (
5+
DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT,
6+
DERIVATIVE_ORDER_CREATION_GAS_LIMIT,
7+
SPOT_ORDER_CANCELATION_GAS_LIMIT,
8+
SPOT_ORDER_CREATION_GAS_LIMIT,
9+
BatchCancelDerivativeOrdersGasLimitEstimator,
10+
BatchCancelSpotOrdersGasLimitEstimator,
11+
BatchCreateDerivativeLimitOrdersGasLimitEstimator,
12+
BatchCreateSpotLimitOrdersGasLimitEstimator,
13+
BatchUpdateOrdersGasLimitEstimator,
14+
ExecGasLimitEstimator,
15+
GasLimitEstimator,
16+
)
517
from pyinjective.core.market import BinaryOptionMarket
618
from pyinjective.proto.cosmos.gov.v1beta1 import tx_pb2 as gov_tx_pb
719
from pyinjective.proto.cosmwasm.wasm.v1 import tx_pb2 as wasm_tx_pb
@@ -44,8 +56,8 @@ def test_estimation_for_batch_create_spot_limit_orders(self):
4456
message = composer.msg_batch_create_spot_limit_orders(sender="sender", orders=orders)
4557
estimator = GasLimitEstimator.for_message(message=message)
4658

47-
expected_order_gas_limit = 50000
48-
expected_message_gas_limit = 15000
59+
expected_order_gas_limit = SPOT_ORDER_CREATION_GAS_LIMIT
60+
expected_message_gas_limit = BatchCreateSpotLimitOrdersGasLimitEstimator.GENERAL_MESSAGE_GAS_LIMIT
4961

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

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

75-
expected_order_gas_limit = 50000
76-
expected_message_gas_limit = 15000
87+
expected_order_gas_limit = SPOT_ORDER_CANCELATION_GAS_LIMIT
88+
expected_message_gas_limit = BatchCancelSpotOrdersGasLimitEstimator.GENERAL_MESSAGE_GAS_LIMIT
7789

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

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

106-
expected_order_gas_limit = 70_000
107-
expected_message_gas_limit = 15000
118+
expected_order_gas_limit = DERIVATIVE_ORDER_CREATION_GAS_LIMIT
119+
expected_message_gas_limit = BatchCreateDerivativeLimitOrdersGasLimitEstimator.GENERAL_MESSAGE_GAS_LIMIT
108120

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

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

134-
expected_order_gas_limit = 60_000
135-
expected_message_gas_limit = 15000
146+
expected_order_gas_limit = DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
147+
expected_message_gas_limit = BatchCancelDerivativeOrdersGasLimitEstimator.GENERAL_MESSAGE_GAS_LIMIT
136148

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

@@ -166,8 +178,8 @@ def test_estimation_for_batch_update_orders_to_create_spot_orders(self):
166178
)
167179
estimator = GasLimitEstimator.for_message(message=message)
168180

169-
expected_order_gas_limit = 50_000
170-
expected_message_gas_limit = 15_000
181+
expected_order_gas_limit = SPOT_ORDER_CREATION_GAS_LIMIT
182+
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT
171183

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

@@ -203,8 +215,8 @@ def test_estimation_for_batch_update_orders_to_create_derivative_orders(self):
203215
)
204216
estimator = GasLimitEstimator.for_message(message=message)
205217

206-
expected_order_gas_limit = 70_000
207-
expected_message_gas_limit = 15_000
218+
expected_order_gas_limit = DERIVATIVE_ORDER_CREATION_GAS_LIMIT
219+
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT
208220

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

@@ -260,8 +272,8 @@ def test_estimation_for_batch_update_orders_to_create_binary_orders(self, usdt_t
260272
)
261273
estimator = GasLimitEstimator.for_message(message=message)
262274

263-
expected_order_gas_limit = 70_000
264-
expected_message_gas_limit = 15_000
275+
expected_order_gas_limit = DERIVATIVE_ORDER_CREATION_GAS_LIMIT
276+
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT
265277

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

@@ -294,8 +306,8 @@ def test_estimation_for_batch_update_orders_to_cancel_spot_orders(self):
294306
)
295307
estimator = GasLimitEstimator.for_message(message=message)
296308

297-
expected_order_gas_limit = 50_000
298-
expected_message_gas_limit = 15_000
309+
expected_order_gas_limit = SPOT_ORDER_CANCELATION_GAS_LIMIT
310+
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT
299311

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

@@ -328,8 +340,8 @@ def test_estimation_for_batch_update_orders_to_cancel_derivative_orders(self):
328340
)
329341
estimator = GasLimitEstimator.for_message(message=message)
330342

331-
expected_order_gas_limit = 60_000
332-
expected_message_gas_limit = 15_000
343+
expected_order_gas_limit = DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
344+
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT
333345

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

@@ -363,8 +375,8 @@ def test_estimation_for_batch_update_orders_to_cancel_binary_orders(self):
363375
)
364376
estimator = GasLimitEstimator.for_message(message=message)
365377

366-
expected_order_gas_limit = 60_000
367-
expected_message_gas_limit = 15_000
378+
expected_order_gas_limit = DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
379+
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT
368380

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

@@ -383,8 +395,8 @@ def test_estimation_for_batch_update_orders_to_cancel_all_for_spot_market(self):
383395
)
384396
estimator = GasLimitEstimator.for_message(message=message)
385397

386-
expected_gas_limit = 40_000 * 20
387-
expected_message_gas_limit = 15_000
398+
expected_gas_limit = BatchUpdateOrdersGasLimitEstimator.CANCEL_ALL_SPOT_MARKET_GAS_LIMIT * 20
399+
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT
388400

389401
assert expected_gas_limit + expected_message_gas_limit == estimator.gas_limit()
390402

@@ -403,8 +415,8 @@ def test_estimation_for_batch_update_orders_to_cancel_all_for_derivative_market(
403415
)
404416
estimator = GasLimitEstimator.for_message(message=message)
405417

406-
expected_gas_limit = 50_000 * 20
407-
expected_message_gas_limit = 15_000
418+
expected_gas_limit = BatchUpdateOrdersGasLimitEstimator.CANCEL_ALL_DERIVATIVE_MARKET_GAS_LIMIT * 20
419+
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT
408420

409421
assert expected_gas_limit + expected_message_gas_limit == estimator.gas_limit()
410422

@@ -423,8 +435,8 @@ def test_estimation_for_batch_update_orders_to_cancel_all_for_binary_options_mar
423435
)
424436
estimator = GasLimitEstimator.for_message(message=message)
425437

426-
expected_gas_limit = 50_000 * 20
427-
expected_message_gas_limit = 15_000
438+
expected_gas_limit = BatchUpdateOrdersGasLimitEstimator.CANCEL_ALL_DERIVATIVE_MARKET_GAS_LIMIT * 20
439+
expected_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT
428440

429441
assert expected_gas_limit + expected_message_gas_limit == estimator.gas_limit()
430442

@@ -452,9 +464,9 @@ def test_estimation_for_exec_message(self):
452464

453465
estimator = GasLimitEstimator.for_message(message=message)
454466

455-
expected_order_gas_limit = 50_000
456-
expected_inner_message_gas_limit = 15_000
457-
expected_exec_message_gas_limit = 8_000
467+
expected_order_gas_limit = SPOT_ORDER_CREATION_GAS_LIMIT
468+
expected_inner_message_gas_limit = BatchUpdateOrdersGasLimitEstimator.MESSAGE_GAS_LIMIT
469+
expected_exec_message_gas_limit = ExecGasLimitEstimator.DEFAULT_GAS_LIMIT
458470

459471
assert (
460472
expected_order_gas_limit + expected_inner_message_gas_limit + expected_exec_message_gas_limit

tests/core/test_message_based_transaction_fee_calculator.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
from pyinjective.async_client import AsyncClient
88
from pyinjective.composer import Composer
99
from pyinjective.core.broadcaster import MessageBasedTransactionFeeCalculator
10+
from pyinjective.core.gas_limit_estimator import (
11+
DefaultGasLimitEstimator,
12+
ExecGasLimitEstimator,
13+
GenericExchangeGasLimitEstimator,
14+
PrivilegedExecuteContractGasLimitEstimator,
15+
)
1016
from pyinjective.core.network import Network
1117
from pyinjective.proto.cosmos.gov.v1beta1 import tx_pb2 as gov_tx_pb2
1218
from pyinjective.proto.cosmwasm.wasm.v1 import tx_pb2 as wasm_tx_pb2
@@ -31,8 +37,10 @@ async def test_gas_fee_for_privileged_execute_contract_message(self):
3137

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

34-
expected_transaction_gas_limit = 60_000
35-
expected_gas_limit = math.ceil(Decimal(6) * 150_000 + expected_transaction_gas_limit)
40+
expected_transaction_gas_limit = MessageBasedTransactionFeeCalculator.TRANSACTION_GAS_LIMIT
41+
expected_gas_limit = math.ceil(
42+
PrivilegedExecuteContractGasLimitEstimator.BASIC_REFERENCE_GAS_LIMIT * 6 + expected_transaction_gas_limit
43+
)
3644
assert expected_gas_limit == transaction.fee.gas_limit
3745
assert str(expected_gas_limit * 5_000_000) == transaction.fee.amount[0].amount
3846

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

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

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

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

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

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

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

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

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

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

165-
expected_transaction_gas_limit = 60_000
166-
expected_inner_message_gas_limit = Decimal(1) * 120_000
167-
expected_exec_message_gas_limit = 8_000
173+
expected_transaction_gas_limit = MessageBasedTransactionFeeCalculator.TRANSACTION_GAS_LIMIT
174+
expected_inner_message_gas_limit = GenericExchangeGasLimitEstimator.BASIC_REFERENCE_GAS_LIMIT
175+
expected_exec_message_gas_limit = ExecGasLimitEstimator.DEFAULT_GAS_LIMIT
168176
expected_gas_limit = math.ceil(
169177
expected_exec_message_gas_limit + expected_inner_message_gas_limit + expected_transaction_gas_limit
170178
)
@@ -200,10 +208,10 @@ async def test_gas_fee_for_two_messages_in_one_transaction(self):
200208

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

203-
expected_transaction_gas_limit = 60_000
204-
expected_inner_message_gas_limit = Decimal(1) * 120_000
205-
expected_exec_message_gas_limit = 8_000
206-
expected_send_message_gas_limit = 150_000
211+
expected_transaction_gas_limit = MessageBasedTransactionFeeCalculator.TRANSACTION_GAS_LIMIT
212+
expected_inner_message_gas_limit = GenericExchangeGasLimitEstimator.BASIC_REFERENCE_GAS_LIMIT
213+
expected_exec_message_gas_limit = ExecGasLimitEstimator.DEFAULT_GAS_LIMIT
214+
expected_send_message_gas_limit = DefaultGasLimitEstimator.DEFAULT_GAS_LIMIT
207215
expected_gas_limit = math.ceil(
208216
expected_exec_message_gas_limit
209217
+ expected_inner_message_gas_limit

0 commit comments

Comments
 (0)