Skip to content

Commit b2758b7

Browse files
authored
Merge pull request #299 from InjectiveLabs/feat/update_gas_cost_in_gas_estimator
Feat/update gas cost in gas estimator
2 parents 6c47732 + 1d66710 commit b2758b7

File tree

289 files changed

+6079
-6134
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

289 files changed

+6079
-6134
lines changed

CHANGELOG.md

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

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

5+
## [1.2.0] - 2024-01-25
6+
### Changed
7+
- Updated reference gas cost for all messages in the gas estimator
8+
- Included different calculation for Post Only orders
9+
- Updated all proto definitions for Injective Core 1.12.1
10+
511
## [1.1.1] - 2024-01-18
612
### Changed
713
- Updated the logic to create a `MsgLiquidatePosition` message

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ clean-all:
2828
$(call clean_repos)
2929

3030
clone-injective-core:
31-
git clone https://github.com/InjectiveLabs/injective-core.git -b v1.12.0 --depth 1 --single-branch
31+
git clone https://github.com/InjectiveLabs/injective-core.git -b v1.12.1 --depth 1 --single-branch
3232

3333
clone-injective-indexer:
3434
git clone https://github.com/InjectiveLabs/injective-indexer.git -b v1.12.79.1 --depth 1 --single-branch

pyinjective/core/gas_limit_estimator.py

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
import math
12
from abc import ABC, abstractmethod
3+
from typing import List, Union
24

35
from google.protobuf import any_pb2
46

57
from pyinjective.proto.cosmos.authz.v1beta1 import tx_pb2 as cosmos_authz_tx_pb
68
from pyinjective.proto.cosmos.gov.v1beta1 import tx_pb2 as gov_tx_pb
79
from pyinjective.proto.cosmwasm.wasm.v1 import tx_pb2 as wasm_tx_pb
8-
from pyinjective.proto.injective.exchange.v1beta1 import tx_pb2 as injective_exchange_tx_pb
10+
from pyinjective.proto.injective.exchange.v1beta1 import (
11+
exchange_pb2 as injective_exchange_pb,
12+
tx_pb2 as injective_exchange_tx_pb,
13+
)
14+
15+
SPOT_ORDER_CREATION_GAS_LIMIT = 50_000
16+
DERIVATIVE_ORDER_CREATION_GAS_LIMIT = 70_000
17+
SPOT_ORDER_CANCELATION_GAS_LIMIT = 50_000
18+
DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT = 60_000
19+
# 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
922

1023

1124
class GasLimitEstimator(ABC):
12-
GENERAL_MESSAGE_GAS_LIMIT = 5_000
25+
GENERAL_MESSAGE_GAS_LIMIT = 15_000
1326
BASIC_REFERENCE_GAS_LIMIT = 150_000
1427

1528
@classmethod
@@ -57,6 +70,16 @@ def _parsed_message(self, message: any_pb2.Any) -> any_pb2.Any:
5770
parsed_message = message
5871
return parsed_message
5972

73+
def _select_post_only_orders(
74+
self,
75+
orders: List[Union[injective_exchange_pb.SpotOrder, injective_exchange_pb.DerivativeOrder]],
76+
) -> List[Union[injective_exchange_pb.SpotOrder, injective_exchange_pb.DerivativeOrder]]:
77+
return [
78+
order
79+
for order in orders
80+
if order.order_type in [injective_exchange_pb.OrderType.BUY_PO, injective_exchange_pb.OrderType.SELL_PO]
81+
]
82+
6083

6184
class DefaultGasLimitEstimator(GasLimitEstimator):
6285
DEFAULT_GAS_LIMIT = 150_000
@@ -74,8 +97,6 @@ def _message_class(self, message: any_pb2.Any):
7497

7598

7699
class BatchCreateSpotLimitOrdersGasLimitEstimator(GasLimitEstimator):
77-
ORDER_GAS_LIMIT = 45_000
78-
79100
def __init__(self, message: any_pb2.Any):
80101
self._message = self._parsed_message(message=message)
81102

@@ -84,9 +105,12 @@ def applies_to(cls, message: any_pb2.Any):
84105
return cls.message_type(message=message).endswith("MsgBatchCreateSpotLimitOrders")
85106

86107
def gas_limit(self) -> int:
108+
post_only_orders = self._select_post_only_orders(orders=self._message.orders)
109+
87110
total = 0
88111
total += self.GENERAL_MESSAGE_GAS_LIMIT
89-
total += len(self._message.orders) * self.ORDER_GAS_LIMIT
112+
total += len(self._message.orders) * SPOT_ORDER_CREATION_GAS_LIMIT
113+
total += math.ceil(len(post_only_orders) * SPOT_ORDER_CREATION_GAS_LIMIT * SPOT_POST_ONLY_ORDER_MULTIPLIER)
90114

91115
return total
92116

@@ -95,8 +119,6 @@ def _message_class(self, message: any_pb2.Any):
95119

96120

97121
class BatchCancelSpotOrdersGasLimitEstimator(GasLimitEstimator):
98-
ORDER_GAS_LIMIT = 45_000
99-
100122
def __init__(self, message: any_pb2.Any):
101123
self._message = self._parsed_message(message=message)
102124

@@ -107,7 +129,7 @@ def applies_to(cls, message: any_pb2.Any):
107129
def gas_limit(self) -> int:
108130
total = 0
109131
total += self.GENERAL_MESSAGE_GAS_LIMIT
110-
total += len(self._message.data) * self.ORDER_GAS_LIMIT
132+
total += len(self._message.data) * SPOT_ORDER_CANCELATION_GAS_LIMIT
111133

112134
return total
113135

@@ -116,8 +138,6 @@ def _message_class(self, message: any_pb2.Any):
116138

117139

118140
class BatchCreateDerivativeLimitOrdersGasLimitEstimator(GasLimitEstimator):
119-
ORDER_GAS_LIMIT = 60_000
120-
121141
def __init__(self, message: any_pb2.Any):
122142
self._message = self._parsed_message(message=message)
123143

@@ -126,9 +146,14 @@ def applies_to(cls, message: any_pb2.Any):
126146
return cls.message_type(message=message).endswith("MsgBatchCreateDerivativeLimitOrders")
127147

128148
def gas_limit(self) -> int:
149+
post_only_orders = self._select_post_only_orders(orders=self._message.orders)
150+
129151
total = 0
130152
total += self.GENERAL_MESSAGE_GAS_LIMIT
131-
total += len(self._message.orders) * self.ORDER_GAS_LIMIT
153+
total += len(self._message.orders) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT
154+
total += math.ceil(
155+
len(post_only_orders) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT * DERIVATIVE_POST_ONLY_ORDER_MULTIPLIER
156+
)
132157

133158
return total
134159

@@ -137,8 +162,6 @@ def _message_class(self, message: any_pb2.Any):
137162

138163

139164
class BatchCancelDerivativeOrdersGasLimitEstimator(GasLimitEstimator):
140-
ORDER_GAS_LIMIT = 55_000
141-
142165
def __init__(self, message: any_pb2.Any):
143166
self._message = self._parsed_message(message=message)
144167

@@ -149,7 +172,7 @@ def applies_to(cls, message: any_pb2.Any):
149172
def gas_limit(self) -> int:
150173
total = 0
151174
total += self.GENERAL_MESSAGE_GAS_LIMIT
152-
total += len(self._message.data) * self.ORDER_GAS_LIMIT
175+
total += len(self._message.data) * DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
153176

154177
return total
155178

@@ -158,13 +181,9 @@ def _message_class(self, message: any_pb2.Any):
158181

159182

160183
class BatchUpdateOrdersGasLimitEstimator(GasLimitEstimator):
161-
SPOT_ORDER_CREATION_GAS_LIMIT = 40_000
162-
DERIVATIVE_ORDER_CREATION_GAS_LIMIT = 60_000
163-
SPOT_ORDER_CANCELATION_GAS_LIMIT = 45_000
164-
DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT = 55_000
165-
CANCEL_ALL_SPOT_MARKET_GAS_LIMIT = 35_000
166-
CANCEL_ALL_DERIVATIVE_MARKET_GAS_LIMIT = 45_000
167-
MESSAGE_GAS_LIMIT = 10_000
184+
CANCEL_ALL_SPOT_MARKET_GAS_LIMIT = 40_000
185+
CANCEL_ALL_DERIVATIVE_MARKET_GAS_LIMIT = 50_000
186+
MESSAGE_GAS_LIMIT = 15_000
168187

169188
AVERAGE_CANCEL_ALL_AFFECTED_ORDERS = 20
170189

@@ -176,14 +195,33 @@ def applies_to(cls, message: any_pb2.Any):
176195
return cls.message_type(message=message).endswith("MsgBatchUpdateOrders")
177196

178197
def gas_limit(self) -> int:
198+
post_only_spot_orders = self._select_post_only_orders(orders=self._message.spot_orders_to_create)
199+
post_only_derivative_orders = self._select_post_only_orders(orders=self._message.derivative_orders_to_create)
200+
post_only_binary_options_orders = self._select_post_only_orders(
201+
orders=self._message.binary_options_orders_to_create
202+
)
203+
179204
total = 0
180205
total += self.MESSAGE_GAS_LIMIT
181-
total += len(self._message.spot_orders_to_create) * self.SPOT_ORDER_CREATION_GAS_LIMIT
182-
total += len(self._message.derivative_orders_to_create) * self.DERIVATIVE_ORDER_CREATION_GAS_LIMIT
183-
total += len(self._message.binary_options_orders_to_create) * self.DERIVATIVE_ORDER_CREATION_GAS_LIMIT
184-
total += len(self._message.spot_orders_to_cancel) * self.SPOT_ORDER_CANCELATION_GAS_LIMIT
185-
total += len(self._message.derivative_orders_to_cancel) * self.DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
186-
total += len(self._message.binary_options_orders_to_cancel) * self.DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
206+
total += len(self._message.spot_orders_to_create) * SPOT_ORDER_CREATION_GAS_LIMIT
207+
total += len(self._message.derivative_orders_to_create) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT
208+
total += len(self._message.binary_options_orders_to_create) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT
209+
210+
total += math.ceil(len(post_only_spot_orders) * SPOT_ORDER_CREATION_GAS_LIMIT * SPOT_POST_ONLY_ORDER_MULTIPLIER)
211+
total += math.ceil(
212+
len(post_only_derivative_orders)
213+
* DERIVATIVE_ORDER_CREATION_GAS_LIMIT
214+
* DERIVATIVE_POST_ONLY_ORDER_MULTIPLIER
215+
)
216+
total += math.ceil(
217+
len(post_only_binary_options_orders)
218+
* DERIVATIVE_ORDER_CREATION_GAS_LIMIT
219+
* DERIVATIVE_POST_ONLY_ORDER_MULTIPLIER
220+
)
221+
222+
total += len(self._message.spot_orders_to_cancel) * SPOT_ORDER_CANCELATION_GAS_LIMIT
223+
total += len(self._message.derivative_orders_to_cancel) * DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
224+
total += len(self._message.binary_options_orders_to_cancel) * DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
187225

188226
total += (
189227
len(self._message.spot_market_ids_to_cancel_all)
@@ -208,7 +246,7 @@ def _message_class(self, message: any_pb2.Any):
208246

209247

210248
class ExecGasLimitEstimator(GasLimitEstimator):
211-
DEFAULT_GAS_LIMIT = 5_000
249+
DEFAULT_GAS_LIMIT = 8_000
212250

213251
def __init__(self, message: any_pb2.Any):
214252
self._message = self._parsed_message(message=message)
@@ -297,7 +335,7 @@ def _message_class(self, message: any_pb2.Any):
297335

298336

299337
class GenericExchangeGasLimitEstimator(GasLimitEstimator):
300-
BASIC_REFERENCE_GAS_LIMIT = 100_000
338+
BASIC_REFERENCE_GAS_LIMIT = 120_000
301339

302340
def __init__(self, message: any_pb2.Any):
303341
self._message = message

pyinjective/denoms_devnet.ini

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,6 @@ min_display_price_tick_size = 0.001
169169
min_quantity_tick_size = 10000000000000
170170
min_display_quantity_tick_size = 0.00001
171171

172-
[0xdf9317eac1739a23bc385e264afde5d480c0b3d2322660b5efd206071d4e70b7]
173-
description = 'Devnet Spot NINJA/INJ'
174-
base = 6
175-
quote = 18
176-
min_price_tick_size = 1000000
177-
min_display_price_tick_size = 0.000001
178-
min_quantity_tick_size = 10000000
179-
min_display_quantity_tick_size = 10
180-
181172
[0x1422a13427d5eabd4d8de7907c8340f7e58cb15553a9fd4ad5c90406561886f9]
182173
description = 'Devnet Derivative COMP/USDT PERP'
183174
base = 0
@@ -308,10 +299,6 @@ decimals = 18
308299
peggy_denom = peggy0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0
309300
decimals = 18
310301

311-
[NINJA]
312-
peggy_denom = factory/inj1xtel2knkt8hmc9dnzpjz6kdmacgcfmlv5f308w/ninja
313-
decimals = 6
314-
315302
[PROJ]
316303
peggy_denom = proj
317304
decimals = 18

pyinjective/denoms_mainnet.ini

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,15 @@ min_display_price_tick_size = 0.0001
637637
min_quantity_tick_size = 1000000000000000
638638
min_display_quantity_tick_size = 0.001
639639

640+
[0xb6c24e9a586a50062f2fac059ddd79f8b0cf1c101e263f4b2c7484b0e20d2899]
641+
description = 'Mainnet Spot GINGER/INJ'
642+
base = 6
643+
quote = 18
644+
min_price_tick_size = 100
645+
min_display_price_tick_size = 0.0000000001
646+
min_quantity_tick_size = 10000000000
647+
min_display_quantity_tick_size = 10000
648+
640649
[0x9b13c89f8f10386b61dd3a58aae56d5c7995133534ed65ac9835bb8d54890961]
641650
description = 'Mainnet Spot SNOWY/INJ'
642651
base = 6
@@ -790,6 +799,24 @@ min_display_price_tick_size = 0.001
790799
min_quantity_tick_size = 0.1
791800
min_display_quantity_tick_size = 0.1
792801

802+
[0x7a70d95e24ba42b99a30121e6a4ff0d6161847d5b86cbfc3d4b3a81d8e190a70]
803+
description = 'Mainnet Derivative ZRO/USDT PERP'
804+
base = 0
805+
quote = 6
806+
min_price_tick_size = 1000
807+
min_display_price_tick_size = 0.001
808+
min_quantity_tick_size = 0.1
809+
min_display_quantity_tick_size = 0.1
810+
811+
[0x03841e74624fd885d1ee28453f921d18c211e78a0d7646c792c7903054eb152c]
812+
description = 'Mainnet Derivative JUP/USDT PERP'
813+
base = 0
814+
quote = 6
815+
min_price_tick_size = 10
816+
min_display_price_tick_size = 0.00001
817+
min_quantity_tick_size = 1
818+
min_display_quantity_tick_size = 1
819+
793820
[AAVE]
794821
peggy_denom = peggy0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9
795822
decimals = 18
@@ -850,6 +877,10 @@ decimals = 18
850877
peggy_denom = peggy0xAaEf88cEa01475125522e117BFe45cF32044E238
851878
decimals = 18
852879

880+
[GINGER]
881+
peggy_denom = factory/inj172ccd0gddgz203e4pf86ype7zjx573tn8g0df9/GINGER
882+
decimals = 6
883+
853884
[GRT]
854885
peggy_denom = peggy0xc944E90C64B2c07662A292be6244BDf05Cda44a7
855886
decimals = 18

pyinjective/proto/amino/amino_pb2.py

Lines changed: 3 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyinjective/proto/cosmos/app/runtime/v1alpha1/module_pb2.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyinjective/proto/cosmos/app/v1alpha1/config_pb2.py

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyinjective/proto/cosmos/app/v1alpha1/module_pb2.py

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)