Skip to content

Commit 9286d63

Browse files
authored
Merge pull request #385 from InjectiveLabs/fix/rename_v1_markets_module
fix: renamed market modules
2 parents c86686f + 40916af commit 9286d63

15 files changed

+272
-272
lines changed

pyinjective/async_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
from pyinjective.client.model.pagination import PaginationOption
2020
from pyinjective.composer import Composer
2121
from pyinjective.constant import GAS_PRICE
22-
from pyinjective.core.chain_formatted_market import BinaryOptionMarket, DerivativeMarket, SpotMarket
2322
from pyinjective.core.ibc.channel.grpc.ibc_channel_grpc_api import IBCChannelGrpcApi
2423
from pyinjective.core.ibc.client.grpc.ibc_client_grpc_api import IBCClientGrpcApi
2524
from pyinjective.core.ibc.connection.grpc.ibc_connection_grpc_api import IBCConnectionGrpcApi
2625
from pyinjective.core.ibc.transfer.grpc.ibc_transfer_grpc_api import IBCTransferGrpcApi
26+
from pyinjective.core.market import BinaryOptionMarket, DerivativeMarket, SpotMarket
2727
from pyinjective.core.network import Network
2828
from pyinjective.core.tendermint.grpc.tendermint_grpc_api import TendermintGrpcApi
2929
from pyinjective.core.token import Token

pyinjective/async_client_v2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from pyinjective.core.ibc.client.grpc.ibc_client_grpc_api import IBCClientGrpcApi
2525
from pyinjective.core.ibc.connection.grpc.ibc_connection_grpc_api import IBCConnectionGrpcApi
2626
from pyinjective.core.ibc.transfer.grpc.ibc_transfer_grpc_api import IBCTransferGrpcApi
27-
from pyinjective.core.market import BinaryOptionMarket, DerivativeMarket, SpotMarket
27+
from pyinjective.core.market_v2 import BinaryOptionMarket, DerivativeMarket, SpotMarket
2828
from pyinjective.core.network import Network
2929
from pyinjective.core.tendermint.grpc.tendermint_grpc_api import TendermintGrpcApi
3030
from pyinjective.core.token import Token

pyinjective/composer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from google.protobuf import any_pb2, json_format, timestamp_pb2
99

1010
from pyinjective.constant import ADDITIONAL_CHAIN_FORMAT_DECIMALS, INJ_DECIMALS, INJ_DENOM
11-
from pyinjective.core.chain_formatted_market import BinaryOptionMarket, DerivativeMarket, SpotMarket
11+
from pyinjective.core.market import BinaryOptionMarket, DerivativeMarket, SpotMarket
1212
from pyinjective.core.token import Token
1313
from pyinjective.ofac import OfacChecker
1414
from pyinjective.proto.cosmos.authz.v1beta1 import authz_pb2 as cosmos_authz_pb, tx_pb2 as cosmos_authz_tx_pb

pyinjective/core/market.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ class SpotMarket:
2222
min_notional: Decimal
2323

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

2929
return extended_chain_formatted_value
3030

3131
def price_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
32-
quantized_value = (human_readable_value // self.min_price_tick_size) * self.min_price_tick_size
3332
decimals = self.quote_token.decimals - self.base_token.decimals
34-
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
35-
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
33+
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
34+
quantized_value = (chain_formatted_value // self.min_price_tick_size) * self.min_price_tick_size
35+
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
3636

3737
return extended_chain_formatted_value
3838

@@ -88,17 +88,17 @@ class DerivativeMarket:
8888

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

9595
return extended_chain_formatted_value
9696

9797
def price_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
98-
quantized_value = (human_readable_value // self.min_price_tick_size) * self.min_price_tick_size
9998
decimals = self.quote_token.decimals
100-
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
101-
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
99+
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
100+
quantized_value = (chain_formatted_value // self.min_price_tick_size) * self.min_price_tick_size
101+
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
102102

103103
return extended_chain_formatted_value
104104

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

116-
return self.notional_to_chain_format(human_readable_value=quantized_margin)
119+
return extended_chain_formatted_margin
117120

118121
def notional_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
119122
decimals = self.quote_token.decimals
@@ -177,18 +180,18 @@ def quantity_to_chain_format(self, human_readable_value: Decimal, special_denom:
177180
min_quantity_tick_size = (
178181
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
179182
)
180-
quantized_value = human_readable_value // min_quantity_tick_size * min_quantity_tick_size
181-
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
182-
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
183+
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
184+
quantized_value = chain_formatted_value // min_quantity_tick_size * min_quantity_tick_size
185+
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
183186

184187
return extended_chain_formatted_value
185188

186189
def price_to_chain_format(self, human_readable_value: Decimal, special_denom: Optional[Denom] = None) -> Decimal:
187190
decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
188191
min_price_tick_size = self.min_price_tick_size if special_denom is None else special_denom.min_price_tick_size
189-
quantized_value = (human_readable_value // min_price_tick_size) * min_price_tick_size
190-
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
191-
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
192+
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
193+
quantized_value = (chain_formatted_value // min_price_tick_size) * min_price_tick_size
194+
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
192195

193196
return extended_chain_formatted_value
194197

@@ -197,9 +200,9 @@ def margin_to_chain_format(self, human_readable_value: Decimal, special_denom: O
197200
min_quantity_tick_size = (
198201
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
199202
)
200-
quantized_value = (human_readable_value // min_quantity_tick_size) * min_quantity_tick_size
201-
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
202-
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
203+
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
204+
quantized_value = (chain_formatted_value // min_quantity_tick_size) * min_quantity_tick_size
205+
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
203206

204207
return extended_chain_formatted_value
205208

@@ -210,17 +213,19 @@ def calculate_margin_in_chain_format(
210213
is_buy: bool,
211214
special_denom: Optional[Denom] = None,
212215
) -> Decimal:
213-
quote_decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
216+
quantity_decimals = 0 if special_denom is None else special_denom.base
217+
price_decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
214218
min_quantity_tick_size = (
215219
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
216220
)
217221
price = human_readable_price if is_buy else 1 - human_readable_price
218-
margin = price * human_readable_quantity
222+
chain_formatted_quantity = human_readable_quantity * Decimal(f"1e{quantity_decimals}")
223+
chain_formatted_price = price * Decimal(f"1e{price_decimals}")
224+
margin = chain_formatted_price * chain_formatted_quantity
219225
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
220226
# in the chain (it might be changed to a min_notional in the future)
221227
quantized_margin = (margin // min_quantity_tick_size) * min_quantity_tick_size
222-
chain_formatted_margin = quantized_margin * Decimal(f"1e{quote_decimals}")
223-
extended_chain_formatted_margin = chain_formatted_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
228+
extended_chain_formatted_margin = quantized_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
224229

225230
return extended_chain_formatted_margin
226231

pyinjective/core/chain_formatted_market.py renamed to pyinjective/core/market_v2.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ class SpotMarket:
2222
min_notional: Decimal
2323

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

2929
return extended_chain_formatted_value
3030

3131
def price_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
32+
quantized_value = (human_readable_value // self.min_price_tick_size) * self.min_price_tick_size
3233
decimals = self.quote_token.decimals - self.base_token.decimals
33-
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
34-
quantized_value = (chain_formatted_value // self.min_price_tick_size) * self.min_price_tick_size
35-
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
34+
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
35+
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
3636

3737
return extended_chain_formatted_value
3838

@@ -88,17 +88,17 @@ class DerivativeMarket:
8888

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

9595
return extended_chain_formatted_value
9696

9797
def price_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
98+
quantized_value = (human_readable_value // self.min_price_tick_size) * self.min_price_tick_size
9899
decimals = self.quote_token.decimals
99-
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
100-
quantized_value = (chain_formatted_value // self.min_price_tick_size) * self.min_price_tick_size
101-
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
100+
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
101+
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
102102

103103
return extended_chain_formatted_value
104104

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

119-
return extended_chain_formatted_margin
116+
return self.notional_to_chain_format(human_readable_value=quantized_margin)
120117

121118
def notional_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
122119
decimals = self.quote_token.decimals
@@ -180,18 +177,18 @@ def quantity_to_chain_format(self, human_readable_value: Decimal, special_denom:
180177
min_quantity_tick_size = (
181178
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
182179
)
183-
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
184-
quantized_value = chain_formatted_value // min_quantity_tick_size * min_quantity_tick_size
185-
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
180+
quantized_value = human_readable_value // min_quantity_tick_size * min_quantity_tick_size
181+
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
182+
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
186183

187184
return extended_chain_formatted_value
188185

189186
def price_to_chain_format(self, human_readable_value: Decimal, special_denom: Optional[Denom] = None) -> Decimal:
190187
decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
191188
min_price_tick_size = self.min_price_tick_size if special_denom is None else special_denom.min_price_tick_size
192-
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
193-
quantized_value = (chain_formatted_value // min_price_tick_size) * min_price_tick_size
194-
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
189+
quantized_value = (human_readable_value // min_price_tick_size) * min_price_tick_size
190+
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
191+
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
195192

196193
return extended_chain_formatted_value
197194

@@ -200,9 +197,9 @@ def margin_to_chain_format(self, human_readable_value: Decimal, special_denom: O
200197
min_quantity_tick_size = (
201198
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
202199
)
203-
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
204-
quantized_value = (chain_formatted_value // min_quantity_tick_size) * min_quantity_tick_size
205-
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
200+
quantized_value = (human_readable_value // min_quantity_tick_size) * min_quantity_tick_size
201+
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
202+
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
206203

207204
return extended_chain_formatted_value
208205

@@ -213,19 +210,17 @@ def calculate_margin_in_chain_format(
213210
is_buy: bool,
214211
special_denom: Optional[Denom] = None,
215212
) -> Decimal:
216-
quantity_decimals = 0 if special_denom is None else special_denom.base
217-
price_decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
213+
quote_decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
218214
min_quantity_tick_size = (
219215
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
220216
)
221217
price = human_readable_price if is_buy else 1 - human_readable_price
222-
chain_formatted_quantity = human_readable_quantity * Decimal(f"1e{quantity_decimals}")
223-
chain_formatted_price = price * Decimal(f"1e{price_decimals}")
224-
margin = chain_formatted_price * chain_formatted_quantity
218+
margin = price * human_readable_quantity
225219
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
226220
# in the chain (it might be changed to a min_notional in the future)
227221
quantized_margin = (margin // min_quantity_tick_size) * min_quantity_tick_size
228-
extended_chain_formatted_margin = quantized_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
222+
chain_formatted_margin = quantized_margin * Decimal(f"1e{quote_decimals}")
223+
extended_chain_formatted_margin = chain_formatted_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
229224

230225
return extended_chain_formatted_margin
231226

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.11.0-rc3"
3+
version = "1.11.0-rc4"
44
description = "Injective Python SDK, with Exchange API Client"
55
authors = ["Injective Labs <[email protected]>"]
66
license = "Apache-2.0"

tests/core/test_gas_heuristics_gas_limit_estimator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from pyinjective.proto.cosmos.gov.v1beta1 import tx_pb2 as gov_tx_pb
3333
from pyinjective.proto.cosmwasm.wasm.v1 import tx_pb2 as wasm_tx_pb
3434
from pyinjective.proto.injective.exchange.v1beta1 import tx_pb2 as injective_exchange_tx_pb
35-
from tests.model_fixtures.markets_fixtures import ( # noqa: F401
35+
from tests.model_fixtures.markets_v2_fixtures import ( # noqa: F401
3636
btc_usdt_perp_market,
3737
first_match_bet_market,
3838
inj_token,

0 commit comments

Comments
 (0)