1+ import math
12from abc import ABC , abstractmethod
3+ from typing import List , Union
24
35from google .protobuf import any_pb2
46
57from pyinjective .proto .cosmos .authz .v1beta1 import tx_pb2 as cosmos_authz_tx_pb
68from pyinjective .proto .cosmos .gov .v1beta1 import tx_pb2 as gov_tx_pb
79from 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
1124class 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
6184class DefaultGasLimitEstimator (GasLimitEstimator ):
6285 DEFAULT_GAS_LIMIT = 150_000
@@ -74,8 +97,6 @@ def _message_class(self, message: any_pb2.Any):
7497
7598
7699class 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
97121class 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
118140class 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
139164class 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
160183class 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
210248class 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
299337class 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
0 commit comments