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 = 45_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 validations
20+ POST_ONLY_ORDER_MULTIPLIER = 0.5
921
1022
1123class GasLimitEstimator (ABC ):
12- GENERAL_MESSAGE_GAS_LIMIT = 10_000
24+ GENERAL_MESSAGE_GAS_LIMIT = 15_000
1325 BASIC_REFERENCE_GAS_LIMIT = 150_000
1426
1527 @classmethod
@@ -57,6 +69,16 @@ def _parsed_message(self, message: any_pb2.Any) -> any_pb2.Any:
5769 parsed_message = message
5870 return parsed_message
5971
72+ def _select_post_only_orders (
73+ self ,
74+ orders : List [Union [injective_exchange_pb .SpotOrder , injective_exchange_pb .DerivativeOrder ]],
75+ ) -> List [Union [injective_exchange_pb .SpotOrder , injective_exchange_pb .DerivativeOrder ]]:
76+ return [
77+ order
78+ for order in orders
79+ if order .order_type in [injective_exchange_pb .OrderType .BUY_PO , injective_exchange_pb .OrderType .SELL_PO ]
80+ ]
81+
6082
6183class DefaultGasLimitEstimator (GasLimitEstimator ):
6284 DEFAULT_GAS_LIMIT = 150_000
@@ -74,8 +96,6 @@ def _message_class(self, message: any_pb2.Any):
7496
7597
7698class BatchCreateSpotLimitOrdersGasLimitEstimator (GasLimitEstimator ):
77- ORDER_GAS_LIMIT = 50_000
78-
7999 def __init__ (self , message : any_pb2 .Any ):
80100 self ._message = self ._parsed_message (message = message )
81101
@@ -84,9 +104,12 @@ def applies_to(cls, message: any_pb2.Any):
84104 return cls .message_type (message = message ).endswith ("MsgBatchCreateSpotLimitOrders" )
85105
86106 def gas_limit (self ) -> int :
107+ post_only_orders = self ._select_post_only_orders (orders = self ._message .orders )
108+
87109 total = 0
88110 total += self .GENERAL_MESSAGE_GAS_LIMIT
89- total += len (self ._message .orders ) * self .ORDER_GAS_LIMIT
111+ total += len (self ._message .orders ) * SPOT_ORDER_CREATION_GAS_LIMIT
112+ total += math .ceil (len (post_only_orders ) * SPOT_ORDER_CREATION_GAS_LIMIT * POST_ONLY_ORDER_MULTIPLIER )
90113
91114 return total
92115
@@ -95,8 +118,6 @@ def _message_class(self, message: any_pb2.Any):
95118
96119
97120class BatchCancelSpotOrdersGasLimitEstimator (GasLimitEstimator ):
98- ORDER_GAS_LIMIT = 50_000
99-
100121 def __init__ (self , message : any_pb2 .Any ):
101122 self ._message = self ._parsed_message (message = message )
102123
@@ -107,7 +128,7 @@ def applies_to(cls, message: any_pb2.Any):
107128 def gas_limit (self ) -> int :
108129 total = 0
109130 total += self .GENERAL_MESSAGE_GAS_LIMIT
110- total += len (self ._message .data ) * self . ORDER_GAS_LIMIT
131+ total += len (self ._message .data ) * SPOT_ORDER_CANCELATION_GAS_LIMIT
111132
112133 return total
113134
@@ -116,8 +137,6 @@ def _message_class(self, message: any_pb2.Any):
116137
117138
118139class BatchCreateDerivativeLimitOrdersGasLimitEstimator (GasLimitEstimator ):
119- ORDER_GAS_LIMIT = 66_000
120-
121140 def __init__ (self , message : any_pb2 .Any ):
122141 self ._message = self ._parsed_message (message = message )
123142
@@ -126,9 +145,12 @@ def applies_to(cls, message: any_pb2.Any):
126145 return cls .message_type (message = message ).endswith ("MsgBatchCreateDerivativeLimitOrders" )
127146
128147 def gas_limit (self ) -> int :
148+ post_only_orders = self ._select_post_only_orders (orders = self ._message .orders )
149+
129150 total = 0
130151 total += self .GENERAL_MESSAGE_GAS_LIMIT
131- total += len (self ._message .orders ) * self .ORDER_GAS_LIMIT
152+ total += len (self ._message .orders ) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT
153+ total += math .ceil (len (post_only_orders ) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT * POST_ONLY_ORDER_MULTIPLIER )
132154
133155 return total
134156
@@ -137,8 +159,6 @@ def _message_class(self, message: any_pb2.Any):
137159
138160
139161class BatchCancelDerivativeOrdersGasLimitEstimator (GasLimitEstimator ):
140- ORDER_GAS_LIMIT = 60_000
141-
142162 def __init__ (self , message : any_pb2 .Any ):
143163 self ._message = self ._parsed_message (message = message )
144164
@@ -149,7 +169,7 @@ def applies_to(cls, message: any_pb2.Any):
149169 def gas_limit (self ) -> int :
150170 total = 0
151171 total += self .GENERAL_MESSAGE_GAS_LIMIT
152- total += len (self ._message .data ) * self . ORDER_GAS_LIMIT
172+ total += len (self ._message .data ) * DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
153173
154174 return total
155175
@@ -158,10 +178,6 @@ def _message_class(self, message: any_pb2.Any):
158178
159179
160180class BatchUpdateOrdersGasLimitEstimator (GasLimitEstimator ):
161- SPOT_ORDER_CREATION_GAS_LIMIT = 45_000
162- DERIVATIVE_ORDER_CREATION_GAS_LIMIT = 66_000
163- SPOT_ORDER_CANCELATION_GAS_LIMIT = 50_000
164- DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT = 60_000
165181 CANCEL_ALL_SPOT_MARKET_GAS_LIMIT = 40_000
166182 CANCEL_ALL_DERIVATIVE_MARKET_GAS_LIMIT = 50_000
167183 MESSAGE_GAS_LIMIT = 15_000
@@ -176,14 +192,29 @@ def applies_to(cls, message: any_pb2.Any):
176192 return cls .message_type (message = message ).endswith ("MsgBatchUpdateOrders" )
177193
178194 def gas_limit (self ) -> int :
195+ post_only_spot_orders = self ._select_post_only_orders (orders = self ._message .spot_orders_to_create )
196+ post_only_derivative_orders = self ._select_post_only_orders (orders = self ._message .derivative_orders_to_create )
197+ post_only_binary_options_orders = self ._select_post_only_orders (
198+ orders = self ._message .binary_options_orders_to_create
199+ )
200+
179201 total = 0
180202 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
203+ total += len (self ._message .spot_orders_to_create ) * SPOT_ORDER_CREATION_GAS_LIMIT
204+ total += len (self ._message .derivative_orders_to_create ) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT
205+ total += len (self ._message .binary_options_orders_to_create ) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT
206+
207+ total += math .ceil (len (post_only_spot_orders ) * SPOT_ORDER_CREATION_GAS_LIMIT * POST_ONLY_ORDER_MULTIPLIER )
208+ total += math .ceil (
209+ len (post_only_derivative_orders ) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT * POST_ONLY_ORDER_MULTIPLIER
210+ )
211+ total += math .ceil (
212+ len (post_only_binary_options_orders ) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT * POST_ONLY_ORDER_MULTIPLIER
213+ )
214+
215+ total += len (self ._message .spot_orders_to_cancel ) * SPOT_ORDER_CANCELATION_GAS_LIMIT
216+ total += len (self ._message .derivative_orders_to_cancel ) * DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
217+ total += len (self ._message .binary_options_orders_to_cancel ) * DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
187218
188219 total += (
189220 len (self ._message .spot_market_ids_to_cancel_all )
0 commit comments