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

Fix/sync dev with master 1.12.0 #300

Merged
merged 14 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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

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

## [1.1.0] - 2024-01-15
### Added
- Added new functions in all Market classes to convert values from extended chain format (the ones provided by chain streams) into human-readable format
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
101 changes: 101 additions & 0 deletions examples/chain_client/77_MsgLiquidatePosition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import asyncio
import uuid

from grpc import RpcError

from pyinjective.async_client import AsyncClient
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
from pyinjective.core.network import Network
from pyinjective.transaction import Transaction
from pyinjective.wallet import PrivateKey


async def main() -> None:
# select network: local, testnet, mainnet
network = Network.testnet()

# initialize grpc client
client = AsyncClient(network)
composer = await client.composer()
await client.sync_timeout_height()

# load account
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
await client.fetch_account(address.to_acc_bech32())
subaccount_id = address.get_subaccount_id(index=0)

# prepare trade info
market_id = "0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6"
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
cid = str(uuid.uuid4())

order = composer.DerivativeOrder(
market_id=market_id,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=39.01, # This should be the liquidation price
quantity=0.147,
leverage=1,
cid=cid,
is_buy=False,
)

# prepare tx msg
msg = composer.MsgLiquidatePosition(
sender=address.to_acc_bech32(),
subaccount_id="0x156df4d5bc8e7dd9191433e54bd6a11eeb390921000000000000000000000000",
market_id=market_id,
order=order,
)

# build sim tx
tx = (
Transaction()
.with_messages(msg)
.with_sequence(client.get_sequence())
.with_account_num(client.get_number())
.with_chain_id(network.chain_id)
)
sim_sign_doc = tx.get_sign_doc(pub_key)
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)

# simulate tx
try:
sim_res = await client.simulate(sim_tx_raw_bytes)
except RpcError as ex:
print(ex)
return

sim_res_msg = sim_res["result"]["msgResponses"]
print("---Simulation Response---")
print(sim_res_msg)

# build tx
gas_price = GAS_PRICE
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
fee = [
composer.Coin(
amount=gas_price * gas_limit,
denom=network.fee_denom,
)
]
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(client.timeout_height)
sign_doc = tx.get_sign_doc(pub_key)
sig = priv_key.sign(sign_doc.SerializeToString())
tx_raw_bytes = tx.get_tx_data(sig, pub_key)

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
print("---Transaction Response---")
print(res)
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} INJ".format(gas_fee))
print(f"\n\ncid: {cid}")


if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
10 changes: 7 additions & 3 deletions examples/exchange_client/oracle_rpc/1_StreamPrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ async def main() -> None:
# select network: local, testnet, mainnet
network = Network.testnet()
client = AsyncClient(network)
base_symbol = "INJ"
quote_symbol = "USDT"
oracle_type = "bandibc"
market = (await client.all_derivative_markets())[
"0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6"
]

base_symbol = market.oracle_base
quote_symbol = market.oracle_quote
oracle_type = market.oracle_type

task = asyncio.get_event_loop().create_task(
client.listen_oracle_prices_updates(
Expand Down
13 changes: 8 additions & 5 deletions examples/exchange_client/oracle_rpc/2_Price.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ async def main() -> None:
# select network: local, testnet, mainnet
network = Network.testnet()
client = AsyncClient(network)
base_symbol = "BTC"
quote_symbol = "USDT"
oracle_type = "bandibc"
oracle_scale_factor = 6
market = (await client.all_derivative_markets())[
"0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6"
]

base_symbol = market.oracle_base
quote_symbol = market.oracle_quote
oracle_type = market.oracle_type

oracle_prices = await client.fetch_oracle_price(
base_symbol=base_symbol,
quote_symbol=quote_symbol,
oracle_type=oracle_type,
oracle_scale_factor=oracle_scale_factor,
)
print(oracle_prices)

Expand Down
10 changes: 8 additions & 2 deletions pyinjective/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,15 @@ def MsgBatchUpdateOrders(self, sender: str, **kwargs):
binary_options_orders_to_create=kwargs.get("binary_options_orders_to_create"),
)

def MsgLiquidatePosition(self, sender: str, subaccount_id: str, market_id: str):
def MsgLiquidatePosition(
self,
sender: str,
subaccount_id: str,
market_id: str,
order: Optional[injective_dot_exchange_dot_v1beta1_dot_exchange__pb2.DerivativeOrder] = None,
):
return injective_exchange_tx_pb.MsgLiquidatePosition(
sender=sender, subaccount_id=subaccount_id, market_id=market_id
sender=sender, subaccount_id=subaccount_id, market_id=market_id, order=order
)

def MsgIncreasePositionMargin(
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
Loading
Loading