Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion pyinjective/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pyinjective.client.model.pagination import PaginationOption
from pyinjective.composer import Composer
from pyinjective.constant import GAS_PRICE
from pyinjective.core.chain_formatted_market import BinaryOptionMarket, DerivativeMarket, SpotMarket
from pyinjective.core.market import BinaryOptionMarket, DerivativeMarket, SpotMarket
from pyinjective.core.ibc.channel.grpc.ibc_channel_grpc_api import IBCChannelGrpcApi
from pyinjective.core.ibc.client.grpc.ibc_client_grpc_api import IBCClientGrpcApi
from pyinjective.core.ibc.connection.grpc.ibc_connection_grpc_api import IBCConnectionGrpcApi
Expand Down
2 changes: 1 addition & 1 deletion pyinjective/async_client_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from pyinjective.core.ibc.client.grpc.ibc_client_grpc_api import IBCClientGrpcApi
from pyinjective.core.ibc.connection.grpc.ibc_connection_grpc_api import IBCConnectionGrpcApi
from pyinjective.core.ibc.transfer.grpc.ibc_transfer_grpc_api import IBCTransferGrpcApi
from pyinjective.core.market import BinaryOptionMarket, DerivativeMarket, SpotMarket
from pyinjective.core.market_v2 import BinaryOptionMarket, DerivativeMarket, SpotMarket
from pyinjective.core.network import Network
from pyinjective.core.tendermint.grpc.tendermint_grpc_api import TendermintGrpcApi
from pyinjective.core.token import Token
Expand Down
2 changes: 1 addition & 1 deletion pyinjective/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from google.protobuf import any_pb2, json_format, timestamp_pb2

from pyinjective.constant import ADDITIONAL_CHAIN_FORMAT_DECIMALS, INJ_DECIMALS, INJ_DENOM
from pyinjective.core.chain_formatted_market import BinaryOptionMarket, DerivativeMarket, SpotMarket
from pyinjective.core.market import BinaryOptionMarket, DerivativeMarket, SpotMarket
from pyinjective.core.token import Token
from pyinjective.ofac import OfacChecker
from pyinjective.proto.cosmos.authz.v1beta1 import authz_pb2 as cosmos_authz_pb, tx_pb2 as cosmos_authz_tx_pb
Expand Down
59 changes: 32 additions & 27 deletions pyinjective/core/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ class SpotMarket:
min_notional: Decimal

def quantity_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
quantized_value = human_readable_value // self.min_quantity_tick_size * self.min_quantity_tick_size
chain_formatted_value = quantized_value * Decimal(f"1e{self.base_token.decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_formatted_value = human_readable_value * Decimal(f"1e{self.base_token.decimals}")
quantized_value = chain_formatted_value // self.min_quantity_tick_size * self.min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

def price_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
quantized_value = (human_readable_value // self.min_price_tick_size) * self.min_price_tick_size
decimals = self.quote_token.decimals - self.base_token.decimals
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // self.min_price_tick_size) * self.min_price_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

Expand Down Expand Up @@ -88,17 +88,17 @@ class DerivativeMarket:

def quantity_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
# Derivative markets do not have a base market to provide the number of decimals
quantized_value = human_readable_value // self.min_quantity_tick_size * self.min_quantity_tick_size
chain_formatted_value = quantized_value
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_formatted_value = human_readable_value
quantized_value = chain_formatted_value // self.min_quantity_tick_size * self.min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

def price_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
quantized_value = (human_readable_value // self.min_price_tick_size) * self.min_price_tick_size
decimals = self.quote_token.decimals
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // self.min_price_tick_size) * self.min_price_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

Expand All @@ -108,12 +108,15 @@ def margin_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
def calculate_margin_in_chain_format(
self, human_readable_quantity: Decimal, human_readable_price: Decimal, leverage: Decimal
) -> Decimal:
margin = (human_readable_price * human_readable_quantity) / leverage
chain_formatted_quantity = human_readable_quantity
chain_formatted_price = human_readable_price * Decimal(f"1e{self.quote_token.decimals}")
margin = (chain_formatted_price * chain_formatted_quantity) / leverage
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
# in the chain (it might be changed to a min_notional in the future)
quantized_margin = (margin // self.min_quantity_tick_size) * self.min_quantity_tick_size
extended_chain_formatted_margin = quantized_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return self.notional_to_chain_format(human_readable_value=quantized_margin)
return extended_chain_formatted_margin

def notional_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
decimals = self.quote_token.decimals
Expand Down Expand Up @@ -177,18 +180,18 @@ def quantity_to_chain_format(self, human_readable_value: Decimal, special_denom:
min_quantity_tick_size = (
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
)
quantized_value = human_readable_value // min_quantity_tick_size * min_quantity_tick_size
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = chain_formatted_value // min_quantity_tick_size * min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

def price_to_chain_format(self, human_readable_value: Decimal, special_denom: Optional[Denom] = None) -> Decimal:
decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
min_price_tick_size = self.min_price_tick_size if special_denom is None else special_denom.min_price_tick_size
quantized_value = (human_readable_value // min_price_tick_size) * min_price_tick_size
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // min_price_tick_size) * min_price_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

Expand All @@ -197,9 +200,9 @@ def margin_to_chain_format(self, human_readable_value: Decimal, special_denom: O
min_quantity_tick_size = (
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
)
quantized_value = (human_readable_value // min_quantity_tick_size) * min_quantity_tick_size
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // min_quantity_tick_size) * min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

Expand All @@ -210,17 +213,19 @@ def calculate_margin_in_chain_format(
is_buy: bool,
special_denom: Optional[Denom] = None,
) -> Decimal:
quote_decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
quantity_decimals = 0 if special_denom is None else special_denom.base
price_decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
min_quantity_tick_size = (
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
)
price = human_readable_price if is_buy else 1 - human_readable_price
margin = price * human_readable_quantity
chain_formatted_quantity = human_readable_quantity * Decimal(f"1e{quantity_decimals}")
chain_formatted_price = price * Decimal(f"1e{price_decimals}")
margin = chain_formatted_price * chain_formatted_quantity
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
# in the chain (it might be changed to a min_notional in the future)
quantized_margin = (margin // min_quantity_tick_size) * min_quantity_tick_size
chain_formatted_margin = quantized_margin * Decimal(f"1e{quote_decimals}")
extended_chain_formatted_margin = chain_formatted_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
extended_chain_formatted_margin = quantized_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_margin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ class SpotMarket:
min_notional: Decimal

def quantity_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
chain_formatted_value = human_readable_value * Decimal(f"1e{self.base_token.decimals}")
quantized_value = chain_formatted_value // self.min_quantity_tick_size * self.min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
quantized_value = human_readable_value // self.min_quantity_tick_size * self.min_quantity_tick_size
chain_formatted_value = quantized_value * Decimal(f"1e{self.base_token.decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

def price_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
quantized_value = (human_readable_value // self.min_price_tick_size) * self.min_price_tick_size
decimals = self.quote_token.decimals - self.base_token.decimals
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // self.min_price_tick_size) * self.min_price_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

Expand Down Expand Up @@ -88,17 +88,17 @@ class DerivativeMarket:

def quantity_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
# Derivative markets do not have a base market to provide the number of decimals
chain_formatted_value = human_readable_value
quantized_value = chain_formatted_value // self.min_quantity_tick_size * self.min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
quantized_value = human_readable_value // self.min_quantity_tick_size * self.min_quantity_tick_size
chain_formatted_value = quantized_value
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

def price_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
quantized_value = (human_readable_value // self.min_price_tick_size) * self.min_price_tick_size
decimals = self.quote_token.decimals
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // self.min_price_tick_size) * self.min_price_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

Expand All @@ -108,15 +108,12 @@ def margin_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
def calculate_margin_in_chain_format(
self, human_readable_quantity: Decimal, human_readable_price: Decimal, leverage: Decimal
) -> Decimal:
chain_formatted_quantity = human_readable_quantity
chain_formatted_price = human_readable_price * Decimal(f"1e{self.quote_token.decimals}")
margin = (chain_formatted_price * chain_formatted_quantity) / leverage
margin = (human_readable_price * human_readable_quantity) / leverage
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
# in the chain (it might be changed to a min_notional in the future)
quantized_margin = (margin // self.min_quantity_tick_size) * self.min_quantity_tick_size
extended_chain_formatted_margin = quantized_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_margin
return self.notional_to_chain_format(human_readable_value=quantized_margin)

def notional_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
decimals = self.quote_token.decimals
Expand Down Expand Up @@ -180,18 +177,18 @@ def quantity_to_chain_format(self, human_readable_value: Decimal, special_denom:
min_quantity_tick_size = (
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
)
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = chain_formatted_value // min_quantity_tick_size * min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
quantized_value = human_readable_value // min_quantity_tick_size * min_quantity_tick_size
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

def price_to_chain_format(self, human_readable_value: Decimal, special_denom: Optional[Denom] = None) -> Decimal:
decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
min_price_tick_size = self.min_price_tick_size if special_denom is None else special_denom.min_price_tick_size
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // min_price_tick_size) * min_price_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
quantized_value = (human_readable_value // min_price_tick_size) * min_price_tick_size
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

Expand All @@ -200,9 +197,9 @@ def margin_to_chain_format(self, human_readable_value: Decimal, special_denom: O
min_quantity_tick_size = (
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
)
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // min_quantity_tick_size) * min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
quantized_value = (human_readable_value // min_quantity_tick_size) * min_quantity_tick_size
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

Expand All @@ -213,19 +210,17 @@ def calculate_margin_in_chain_format(
is_buy: bool,
special_denom: Optional[Denom] = None,
) -> Decimal:
quantity_decimals = 0 if special_denom is None else special_denom.base
price_decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
quote_decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
min_quantity_tick_size = (
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
)
price = human_readable_price if is_buy else 1 - human_readable_price
chain_formatted_quantity = human_readable_quantity * Decimal(f"1e{quantity_decimals}")
chain_formatted_price = price * Decimal(f"1e{price_decimals}")
margin = chain_formatted_price * chain_formatted_quantity
margin = price * human_readable_quantity
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
# in the chain (it might be changed to a min_notional in the future)
quantized_margin = (margin // min_quantity_tick_size) * min_quantity_tick_size
extended_chain_formatted_margin = quantized_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_formatted_margin = quantized_margin * Decimal(f"1e{quote_decimals}")
extended_chain_formatted_margin = chain_formatted_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_margin

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "injective-py"
version = "1.11.0-rc3"
version = "1.11.0-rc4"
description = "Injective Python SDK, with Exchange API Client"
authors = ["Injective Labs <contact@injectivelabs.org>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_gas_heuristics_gas_limit_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
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 tests.model_fixtures.markets_fixtures import ( # noqa: F401
from tests.model_fixtures.markets_v2_fixtures import ( # noqa: F401
btc_usdt_perp_market,
first_match_bet_market,
inj_token,
Expand Down
Loading
Loading