Skip to content

Commit 6eefa4c

Browse files
author
abel
committed
(fix) Added extra logic to increase the gas cost for post only order
1 parent ae051f4 commit 6eefa4c

File tree

4 files changed

+65
-33
lines changed

4 files changed

+65
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
## [1.2.0] - 2024-01-24
66
### Changed
77
- Updated reference gas cost for all messages in the gas estimator
8+
- Included different calculation for Post Only orders
89
- Updated all proto definitions for Injective Core 1.12.1
910

1011
## [1.1.1] - 2024-01-18

pyinjective/core/gas_limit_estimator.py

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
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 = 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

1123
class 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

6183
class DefaultGasLimitEstimator(GasLimitEstimator):
6284
DEFAULT_GAS_LIMIT = 150_000
@@ -74,8 +96,6 @@ def _message_class(self, message: any_pb2.Any):
7496

7597

7698
class 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

97120
class 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

118139
class 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

139161
class 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

160180
class 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)

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.2.0-rc1"
3+
version = "1.2.0-rc2"
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def test_estimation_for_batch_create_spot_limit_orders(self):
4646
message = composer.MsgBatchCreateSpotLimitOrders(sender="sender", orders=orders)
4747
estimator = GasLimitEstimator.for_message(message=message)
4848

49-
expected_order_gas_limit = 50000
50-
expected_message_gas_limit = 10000
49+
expected_order_gas_limit = 45000
50+
expected_message_gas_limit = 15000
5151

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

@@ -75,7 +75,7 @@ def test_estimation_for_batch_cancel_spot_orders(self):
7575
estimator = GasLimitEstimator.for_message(message=message)
7676

7777
expected_order_gas_limit = 50000
78-
expected_message_gas_limit = 10000
78+
expected_message_gas_limit = 15000
7979

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

@@ -107,8 +107,8 @@ def test_estimation_for_batch_create_derivative_limit_orders(self):
107107
message = composer.MsgBatchCreateDerivativeLimitOrders(sender="sender", orders=orders)
108108
estimator = GasLimitEstimator.for_message(message=message)
109109

110-
expected_order_gas_limit = 66_000
111-
expected_message_gas_limit = 10000
110+
expected_order_gas_limit = 70_000
111+
expected_message_gas_limit = 15000
112112

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

@@ -136,7 +136,7 @@ def test_estimation_for_batch_cancel_derivative_orders(self):
136136
estimator = GasLimitEstimator.for_message(message=message)
137137

138138
expected_order_gas_limit = 60_000
139-
expected_message_gas_limit = 10000
139+
expected_message_gas_limit = 15000
140140

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

@@ -211,7 +211,7 @@ def test_estimation_for_batch_update_orders_to_create_derivative_orders(self):
211211
)
212212
estimator = GasLimitEstimator.for_message(message=message)
213213

214-
expected_order_gas_limit = 66_000
214+
expected_order_gas_limit = 70_000
215215
expected_message_gas_limit = 15_000
216216

217217
assert (expected_order_gas_limit * 2) + expected_message_gas_limit == estimator.gas_limit()
@@ -269,7 +269,7 @@ def test_estimation_for_batch_update_orders_to_create_binary_orders(self, usdt_t
269269
)
270270
estimator = GasLimitEstimator.for_message(message=message)
271271

272-
expected_order_gas_limit = 66_000
272+
expected_order_gas_limit = 70_000
273273
expected_message_gas_limit = 15_000
274274

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

0 commit comments

Comments
 (0)