Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/update gas cost in gas estimator #299

Merged
merged 6 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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

## [1.2.0] - 2024-01-25
### Changed
- Updated reference gas cost for all messages in the gas estimator
- Included different calculation for Post Only orders
- Updated all proto definitions for Injective Core 1.12.1

## [1.1.1] - 2024-01-18
### Changed
- Updated the logic to create a `MsgLiquidatePosition` message
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ clean-all:
$(call clean_repos)

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

clone-injective-indexer:
git clone https://github.com/InjectiveLabs/injective-indexer.git -b v1.12.79.1 --depth 1 --single-branch
Expand Down
96 changes: 67 additions & 29 deletions pyinjective/core/gas_limit_estimator.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import math
from abc import ABC, abstractmethod
from typing import List, Union

from google.protobuf import any_pb2

from pyinjective.proto.cosmos.authz.v1beta1 import tx_pb2 as cosmos_authz_tx_pb
from pyinjective.proto.cosmos.gov.v1beta1 import tx_pb2 as gov_tx_pb
from pyinjective.proto.cosmwasm.wasm.v1 import tx_pb2 as wasm_tx_pb
from pyinjective.proto.injective.exchange.v1beta1 import tx_pb2 as injective_exchange_tx_pb
from pyinjective.proto.injective.exchange.v1beta1 import (
exchange_pb2 as injective_exchange_pb,
tx_pb2 as injective_exchange_tx_pb,
)

SPOT_ORDER_CREATION_GAS_LIMIT = 50_000
DERIVATIVE_ORDER_CREATION_GAS_LIMIT = 70_000
SPOT_ORDER_CANCELATION_GAS_LIMIT = 50_000
DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT = 60_000
# POST ONLY orders take around 50% more gas to create than normal orders due to the required validations
SPOT_POST_ONLY_ORDER_MULTIPLIER = 0.5
DERIVATIVE_POST_ONLY_ORDER_MULTIPLIER = 0.5


class GasLimitEstimator(ABC):
GENERAL_MESSAGE_GAS_LIMIT = 5_000
GENERAL_MESSAGE_GAS_LIMIT = 15_000
BASIC_REFERENCE_GAS_LIMIT = 150_000

@classmethod
Expand Down Expand Up @@ -57,6 +70,16 @@ def _parsed_message(self, message: any_pb2.Any) -> any_pb2.Any:
parsed_message = message
return parsed_message

def _select_post_only_orders(
self,
orders: List[Union[injective_exchange_pb.SpotOrder, injective_exchange_pb.DerivativeOrder]],
) -> List[Union[injective_exchange_pb.SpotOrder, injective_exchange_pb.DerivativeOrder]]:
return [
order
for order in orders
if order.order_type in [injective_exchange_pb.OrderType.BUY_PO, injective_exchange_pb.OrderType.SELL_PO]
]


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


class BatchCreateSpotLimitOrdersGasLimitEstimator(GasLimitEstimator):
ORDER_GAS_LIMIT = 45_000

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

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

def gas_limit(self) -> int:
post_only_orders = self._select_post_only_orders(orders=self._message.orders)

total = 0
total += self.GENERAL_MESSAGE_GAS_LIMIT
total += len(self._message.orders) * self.ORDER_GAS_LIMIT
total += len(self._message.orders) * SPOT_ORDER_CREATION_GAS_LIMIT
total += math.ceil(len(post_only_orders) * SPOT_ORDER_CREATION_GAS_LIMIT * SPOT_POST_ONLY_ORDER_MULTIPLIER)

return total

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


class BatchCancelSpotOrdersGasLimitEstimator(GasLimitEstimator):
ORDER_GAS_LIMIT = 45_000

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

Expand All @@ -107,7 +129,7 @@ def applies_to(cls, message: any_pb2.Any):
def gas_limit(self) -> int:
total = 0
total += self.GENERAL_MESSAGE_GAS_LIMIT
total += len(self._message.data) * self.ORDER_GAS_LIMIT
total += len(self._message.data) * SPOT_ORDER_CANCELATION_GAS_LIMIT

return total

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


class BatchCreateDerivativeLimitOrdersGasLimitEstimator(GasLimitEstimator):
ORDER_GAS_LIMIT = 60_000

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

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

def gas_limit(self) -> int:
post_only_orders = self._select_post_only_orders(orders=self._message.orders)

total = 0
total += self.GENERAL_MESSAGE_GAS_LIMIT
total += len(self._message.orders) * self.ORDER_GAS_LIMIT
total += len(self._message.orders) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT
total += math.ceil(
len(post_only_orders) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT * DERIVATIVE_POST_ONLY_ORDER_MULTIPLIER
)

return total

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


class BatchCancelDerivativeOrdersGasLimitEstimator(GasLimitEstimator):
ORDER_GAS_LIMIT = 55_000

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

Expand All @@ -149,7 +172,7 @@ def applies_to(cls, message: any_pb2.Any):
def gas_limit(self) -> int:
total = 0
total += self.GENERAL_MESSAGE_GAS_LIMIT
total += len(self._message.data) * self.ORDER_GAS_LIMIT
total += len(self._message.data) * DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT

return total

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


class BatchUpdateOrdersGasLimitEstimator(GasLimitEstimator):
SPOT_ORDER_CREATION_GAS_LIMIT = 40_000
DERIVATIVE_ORDER_CREATION_GAS_LIMIT = 60_000
SPOT_ORDER_CANCELATION_GAS_LIMIT = 45_000
DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT = 55_000
CANCEL_ALL_SPOT_MARKET_GAS_LIMIT = 35_000
CANCEL_ALL_DERIVATIVE_MARKET_GAS_LIMIT = 45_000
MESSAGE_GAS_LIMIT = 10_000
CANCEL_ALL_SPOT_MARKET_GAS_LIMIT = 40_000
CANCEL_ALL_DERIVATIVE_MARKET_GAS_LIMIT = 50_000
MESSAGE_GAS_LIMIT = 15_000

AVERAGE_CANCEL_ALL_AFFECTED_ORDERS = 20

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

def gas_limit(self) -> int:
post_only_spot_orders = self._select_post_only_orders(orders=self._message.spot_orders_to_create)
post_only_derivative_orders = self._select_post_only_orders(orders=self._message.derivative_orders_to_create)
post_only_binary_options_orders = self._select_post_only_orders(
orders=self._message.binary_options_orders_to_create
)

total = 0
total += self.MESSAGE_GAS_LIMIT
total += len(self._message.spot_orders_to_create) * self.SPOT_ORDER_CREATION_GAS_LIMIT
total += len(self._message.derivative_orders_to_create) * self.DERIVATIVE_ORDER_CREATION_GAS_LIMIT
total += len(self._message.binary_options_orders_to_create) * self.DERIVATIVE_ORDER_CREATION_GAS_LIMIT
total += len(self._message.spot_orders_to_cancel) * self.SPOT_ORDER_CANCELATION_GAS_LIMIT
total += len(self._message.derivative_orders_to_cancel) * self.DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
total += len(self._message.binary_options_orders_to_cancel) * self.DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
total += len(self._message.spot_orders_to_create) * SPOT_ORDER_CREATION_GAS_LIMIT
total += len(self._message.derivative_orders_to_create) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT
total += len(self._message.binary_options_orders_to_create) * DERIVATIVE_ORDER_CREATION_GAS_LIMIT

total += math.ceil(len(post_only_spot_orders) * SPOT_ORDER_CREATION_GAS_LIMIT * SPOT_POST_ONLY_ORDER_MULTIPLIER)
total += math.ceil(
len(post_only_derivative_orders)
* DERIVATIVE_ORDER_CREATION_GAS_LIMIT
* DERIVATIVE_POST_ONLY_ORDER_MULTIPLIER
)
total += math.ceil(
len(post_only_binary_options_orders)
* DERIVATIVE_ORDER_CREATION_GAS_LIMIT
* DERIVATIVE_POST_ONLY_ORDER_MULTIPLIER
)

total += len(self._message.spot_orders_to_cancel) * SPOT_ORDER_CANCELATION_GAS_LIMIT
total += len(self._message.derivative_orders_to_cancel) * DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT
total += len(self._message.binary_options_orders_to_cancel) * DERIVATIVE_ORDER_CANCELATION_GAS_LIMIT

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


class ExecGasLimitEstimator(GasLimitEstimator):
DEFAULT_GAS_LIMIT = 5_000
DEFAULT_GAS_LIMIT = 8_000

def __init__(self, message: any_pb2.Any):
self._message = self._parsed_message(message=message)
Expand Down Expand Up @@ -297,7 +335,7 @@ def _message_class(self, message: any_pb2.Any):


class GenericExchangeGasLimitEstimator(GasLimitEstimator):
BASIC_REFERENCE_GAS_LIMIT = 100_000
BASIC_REFERENCE_GAS_LIMIT = 120_000

def __init__(self, message: any_pb2.Any):
self._message = message
Expand Down
13 changes: 0 additions & 13 deletions pyinjective/denoms_devnet.ini
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,6 @@ min_display_price_tick_size = 0.001
min_quantity_tick_size = 10000000000000
min_display_quantity_tick_size = 0.00001

[0xdf9317eac1739a23bc385e264afde5d480c0b3d2322660b5efd206071d4e70b7]
description = 'Devnet Spot NINJA/INJ'
base = 6
quote = 18
min_price_tick_size = 1000000
min_display_price_tick_size = 0.000001
min_quantity_tick_size = 10000000
min_display_quantity_tick_size = 10

[0x1422a13427d5eabd4d8de7907c8340f7e58cb15553a9fd4ad5c90406561886f9]
description = 'Devnet Derivative COMP/USDT PERP'
base = 0
Expand Down Expand Up @@ -308,10 +299,6 @@ decimals = 18
peggy_denom = peggy0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0
decimals = 18

[NINJA]
peggy_denom = factory/inj1xtel2knkt8hmc9dnzpjz6kdmacgcfmlv5f308w/ninja
decimals = 6

[PROJ]
peggy_denom = proj
decimals = 18
Expand Down
31 changes: 31 additions & 0 deletions pyinjective/denoms_mainnet.ini
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,15 @@ min_display_price_tick_size = 0.0001
min_quantity_tick_size = 1000000000000000
min_display_quantity_tick_size = 0.001

[0xb6c24e9a586a50062f2fac059ddd79f8b0cf1c101e263f4b2c7484b0e20d2899]
description = 'Mainnet Spot GINGER/INJ'
base = 6
quote = 18
min_price_tick_size = 100
min_display_price_tick_size = 0.0000000001
min_quantity_tick_size = 10000000000
min_display_quantity_tick_size = 10000

[0x9b13c89f8f10386b61dd3a58aae56d5c7995133534ed65ac9835bb8d54890961]
description = 'Mainnet Spot SNOWY/INJ'
base = 6
Expand Down Expand Up @@ -790,6 +799,24 @@ min_display_price_tick_size = 0.001
min_quantity_tick_size = 0.1
min_display_quantity_tick_size = 0.1

[0x7a70d95e24ba42b99a30121e6a4ff0d6161847d5b86cbfc3d4b3a81d8e190a70]
description = 'Mainnet Derivative ZRO/USDT PERP'
base = 0
quote = 6
min_price_tick_size = 1000
min_display_price_tick_size = 0.001
min_quantity_tick_size = 0.1
min_display_quantity_tick_size = 0.1

[0x03841e74624fd885d1ee28453f921d18c211e78a0d7646c792c7903054eb152c]
description = 'Mainnet Derivative JUP/USDT PERP'
base = 0
quote = 6
min_price_tick_size = 10
min_display_price_tick_size = 0.00001
min_quantity_tick_size = 1
min_display_quantity_tick_size = 1

[AAVE]
peggy_denom = peggy0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9
decimals = 18
Expand Down Expand Up @@ -850,6 +877,10 @@ decimals = 18
peggy_denom = peggy0xAaEf88cEa01475125522e117BFe45cF32044E238
decimals = 18

[GINGER]
peggy_denom = factory/inj172ccd0gddgz203e4pf86ype7zjx573tn8g0df9/GINGER
decimals = 6

[GRT]
peggy_denom = peggy0xc944E90C64B2c07662A292be6244BDf05Cda44a7
decimals = 18
Expand Down
11 changes: 3 additions & 8 deletions pyinjective/proto/amino/amino_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyinjective/proto/cosmos/app/runtime/v1alpha1/module_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyinjective/proto/cosmos/app/v1alpha1/config_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyinjective/proto/cosmos/app/v1alpha1/module_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading