Skip to content

Commit b8beed2

Browse files
committed
(fix) Fixed issues in the V2 messages with Coin parameters, to not add the extra digits in the amounts
1 parent 321a78e commit b8beed2

File tree

9 files changed

+134
-180
lines changed

9 files changed

+134
-180
lines changed

pyinjective/async_client.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,11 +2481,6 @@ async def listen_chain_stream_updates(
24812481
positions_filter: Optional[chain_stream_query.PositionsFilter] = None,
24822482
oracle_price_filter: Optional[chain_stream_query.OraclePriceFilter] = None,
24832483
):
2484-
"""
2485-
This method is deprecated and will be removed soon. Please use `listen_chain_stream_v2_updates` instead
2486-
"""
2487-
warn("This method is deprecated. Use listen_chain_stream_v2_updates instead", DeprecationWarning, stacklevel=2)
2488-
24892484
return await self.chain_stream_api.stream(
24902485
callback=callback,
24912486
on_end_callback=on_end_callback,

pyinjective/composer.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ def order_data(
369369
is_market_order: Optional[bool] = False,
370370
) -> injective_exchange_tx_pb.OrderData:
371371
"""
372-
This method is deprecated and will be removed soon. Please use `create_v2_order_data` instead
372+
This method is deprecated and will be removed soon. Please use `create_order_data_v2` instead
373373
"""
374-
warn("This method is deprecated. Use create_v2_order_data instead", DeprecationWarning, stacklevel=2)
374+
warn("This method is deprecated. Use create_order_data_v2 instead", DeprecationWarning, stacklevel=2)
375375

376376
order_mask = self._order_mask(is_conditional=is_conditional, is_buy=is_buy, is_market_order=is_market_order)
377377

@@ -391,10 +391,10 @@ def order_data_without_mask(
391391
cid: Optional[str] = None,
392392
) -> injective_exchange_tx_pb.OrderData:
393393
"""
394-
This method is deprecated and will be removed soon. Please use `create_v2_order_data_without_mask` instead
394+
This method is deprecated and will be removed soon. Please use `create_order_data_without_mask_v2` instead
395395
"""
396396
warn(
397-
"This method is deprecated. Use create_v2_order_data_without_mask instead", DeprecationWarning, stacklevel=2
397+
"This method is deprecated. Use create_order_data_without_mask_v2 instead", DeprecationWarning, stacklevel=2
398398
)
399399

400400
return injective_exchange_tx_pb.OrderData(
@@ -452,9 +452,9 @@ def spot_order(
452452
trigger_price: Optional[Decimal] = None,
453453
) -> injective_exchange_pb.SpotOrder:
454454
"""
455-
This method is deprecated and will be removed soon. Please use `create_v2_spot_order` instead
455+
This method is deprecated and will be removed soon. Please use `create_spot_order_v2` instead
456456
"""
457-
warn("This method is deprecated. Use create_v2_spot_order instead", DeprecationWarning, stacklevel=2)
457+
warn("This method is deprecated. Use create_spot_order_v2 instead", DeprecationWarning, stacklevel=2)
458458

459459
market = self.spot_markets[market_id]
460460

@@ -532,9 +532,9 @@ def derivative_order(
532532
trigger_price: Optional[Decimal] = None,
533533
) -> injective_exchange_pb.DerivativeOrder:
534534
"""
535-
This method is deprecated and will be removed soon. Please use `create_v2_derivative_order` instead
535+
This method is deprecated and will be removed soon. Please use `create_derivative_order_v2` instead
536536
"""
537-
warn("This method is deprecated. Use create_v2_derivative_order instead", DeprecationWarning, stacklevel=2)
537+
warn("This method is deprecated. Use create_derivative_order_v2 instead", DeprecationWarning, stacklevel=2)
538538

539539
market = self.derivative_markets[market_id]
540540

@@ -597,9 +597,9 @@ def binary_options_order(
597597
denom: Optional[Denom] = None,
598598
) -> injective_exchange_pb.DerivativeOrder:
599599
"""
600-
This method is deprecated and will be removed soon. Please use `create_v2_binary_options_order` instead
600+
This method is deprecated and will be removed soon. Please use `create_binary_options_order_v2` instead
601601
"""
602-
warn("This method is deprecated. Use create_v2_binary_options_order instead", DeprecationWarning, stacklevel=2)
602+
warn("This method is deprecated. Use create_binary_options_order_v2 instead", DeprecationWarning, stacklevel=2)
603603

604604
market = self.binary_option_markets[market_id]
605605

@@ -717,8 +717,7 @@ def MsgSend(self, from_address: str, to_address: str, amount: float, denom: str)
717717
)
718718

719719
def msg_send(self, from_address: str, to_address: str, amount: int, denom: str):
720-
chain_amount = Token.convert_value_to_extended_decimal_format(value=Decimal(str(amount)))
721-
coin = self.coin(amount=int(chain_amount), denom=denom)
720+
coin = self.coin(amount=int(amount), denom=denom)
722721

723722
return cosmos_bank_tx_pb.MsgSend(
724723
from_address=from_address,
@@ -748,8 +747,7 @@ def msg_deposit(
748747
def msg_deposit_v2(
749748
self, sender: str, subaccount_id: str, amount: int, denom: str
750749
) -> injective_exchange_tx_v2_pb.MsgDeposit:
751-
chain_amount = Token.convert_value_to_extended_decimal_format(value=Decimal(str(amount)))
752-
coin = self.coin(amount=int(chain_amount), denom=denom)
750+
coin = self.coin(amount=int(amount), denom=denom)
753751

754752
return injective_exchange_tx_v2_pb.MsgDeposit(
755753
sender=sender,
@@ -780,8 +778,7 @@ def msg_withdraw_v2(
780778
amount: int,
781779
denom: str,
782780
) -> injective_exchange_tx_v2_pb.MsgWithdraw:
783-
chain_amount = Token.convert_value_to_extended_decimal_format(value=Decimal(str(amount)))
784-
coin = self.coin(amount=int(chain_amount), denom=denom)
781+
coin = self.coin(amount=int(amount), denom=denom)
785782

786783
return injective_exchange_tx_v2_pb.MsgWithdraw(
787784
sender=sender,
@@ -1894,8 +1891,7 @@ def msg_subaccount_transfer_v2(
18941891
amount: int,
18951892
denom: str,
18961893
) -> injective_exchange_tx_v2_pb.MsgSubaccountTransfer:
1897-
chain_amount = Token.convert_value_to_extended_decimal_format(value=Decimal(str(amount)))
1898-
amount_coin = self.coin(amount=int(chain_amount), denom=denom)
1894+
amount_coin = self.coin(amount=int(amount), denom=denom)
18991895

19001896
return injective_exchange_tx_v2_pb.MsgSubaccountTransfer(
19011897
sender=sender,
@@ -1934,8 +1930,7 @@ def msg_external_transfer_v2(
19341930
amount: int,
19351931
denom: str,
19361932
) -> injective_exchange_tx_v2_pb.MsgExternalTransfer:
1937-
chain_amount = Token.convert_value_to_extended_decimal_format(value=Decimal(str(amount)))
1938-
coin = self.coin(amount=int(chain_amount), denom=denom)
1933+
coin = self.coin(amount=int(amount), denom=denom)
19391934

19401935
return injective_exchange_tx_v2_pb.MsgExternalTransfer(
19411936
sender=sender,
@@ -2131,13 +2126,13 @@ def msg_decrease_position_margin_v2(
21312126
market_id: str,
21322127
amount: Decimal,
21332128
) -> injective_exchange_tx_v2_pb.MsgDecreasePositionMargin:
2134-
additional_margin = Token.convert_value_to_extended_decimal_format(value=amount)
2129+
margin_to_remove = Token.convert_value_to_extended_decimal_format(value=amount)
21352130
return injective_exchange_tx_v2_pb.MsgDecreasePositionMargin(
21362131
sender=sender,
21372132
source_subaccount_id=source_subaccount_id,
21382133
destination_subaccount_id=destination_subaccount_id,
21392134
market_id=market_id,
2140-
amount=f"{additional_margin.normalize():f}",
2135+
amount=f"{margin_to_remove.normalize():f}",
21412136
)
21422137

21432138
def msg_update_spot_market(
@@ -2324,8 +2319,7 @@ def msg_create_insurance_fund(
23242319
expiry: int,
23252320
initial_deposit: int,
23262321
) -> injective_insurance_tx_pb.MsgCreateInsuranceFund:
2327-
chain_initial_deposit = Token.convert_value_to_extended_decimal_format(value=Decimal(str(initial_deposit)))
2328-
deposit = self.coin(amount=int(chain_initial_deposit), denom=quote_denom)
2322+
deposit = self.coin(amount=int(initial_deposit), denom=quote_denom)
23292323

23302324
return injective_insurance_tx_pb.MsgCreateInsuranceFund(
23312325
sender=sender,
@@ -2365,8 +2359,7 @@ def msg_underwrite(
23652359
quote_denom: str,
23662360
amount: int,
23672361
):
2368-
chain_amount = Token.convert_value_to_extended_decimal_format(value=Decimal(str(amount)))
2369-
coin = self.coin(amount=int(chain_amount), denom=quote_denom)
2362+
coin = self.coin(amount=int(amount), denom=quote_denom)
23702363

23712364
return injective_insurance_tx_pb.MsgUnderwrite(
23722365
sender=sender,
@@ -2426,10 +2419,8 @@ def MsgSendToEth(self, denom: str, sender: str, eth_dest: str, amount: float, br
24262419
)
24272420

24282421
def msg_send_to_eth(self, denom: str, sender: str, eth_dest: str, amount: int, bridge_fee: int):
2429-
chain_amount = Token.convert_value_to_extended_decimal_format(value=Decimal(str(amount)))
2430-
chain_bridge_fee = Token.convert_value_to_extended_decimal_format(value=Decimal(str(bridge_fee)))
2431-
amount_coin = self.coin(amount=int(chain_amount), denom=denom)
2432-
bridge_fee_coin = self.coin(amount=int(chain_bridge_fee), denom=denom)
2422+
amount_coin = self.coin(amount=int(amount), denom=denom)
2423+
bridge_fee_coin = self.coin(amount=int(bridge_fee), denom=denom)
24332424

24342425
return injective_peggy_tx_pb.MsgSendToEth(
24352426
sender=sender,
@@ -3029,9 +3020,9 @@ def _basic_derivative_order(
30293020
chain_trigger_price: Optional[Decimal] = None,
30303021
) -> injective_exchange_pb.DerivativeOrder:
30313022
"""
3032-
This method is deprecated and will be removed soon. Please use `_basic_v2_derivative_order` instead
3023+
This method is deprecated and will be removed soon. Please use `_basic_derivative_order_v2` instead
30333024
"""
3034-
warn("This method is deprecated. Use _basic_v2_derivative_order instead", DeprecationWarning, stacklevel=2)
3025+
warn("This method is deprecated. Use _basic_derivative_order_v2 instead", DeprecationWarning, stacklevel=2)
30353026

30363027
formatted_quantity = f"{chain_quantity.normalize():f}"
30373028
formatted_price = f"{chain_price.normalize():f}"

pyinjective/constant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
GAS_PRICE = 160_000_000
2-
GAS_FEE_BUFFER_AMOUNT = 25_000
2+
GAS_FEE_BUFFER_AMOUNT = 30_000
33
MAX_MEMO_CHARACTERS = 256
44
ADDITIONAL_CHAIN_FORMAT_DECIMALS = 18
55
TICKER_TOKENS_SEPARATOR = "/"

pyinjective/core/market.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

@@ -87,17 +87,17 @@ class DerivativeMarket:
8787

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

9494
return extended_chain_formatted_value
9595

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

102102
return extended_chain_formatted_value
103103

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

118-
return extended_chain_formatted_margin
115+
return self.notional_to_chain_format(human_readable_value=quantized_margin)
119116

120117
def notional_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
121118
decimals = self.quote_token.decimals
@@ -178,18 +175,18 @@ def quantity_to_chain_format(self, human_readable_value: Decimal, special_denom:
178175
min_quantity_tick_size = (
179176
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
180177
)
181-
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
182-
quantized_value = chain_formatted_value // min_quantity_tick_size * min_quantity_tick_size
183-
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
178+
quantized_value = human_readable_value // min_quantity_tick_size * min_quantity_tick_size
179+
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
180+
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
184181

185182
return extended_chain_formatted_value
186183

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

194191
return extended_chain_formatted_value
195192

@@ -198,9 +195,9 @@ def margin_to_chain_format(self, human_readable_value: Decimal, special_denom: O
198195
min_quantity_tick_size = (
199196
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
200197
)
201-
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
202-
quantized_value = (chain_formatted_value // min_quantity_tick_size) * min_quantity_tick_size
203-
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
198+
quantized_value = (human_readable_value // min_quantity_tick_size) * min_quantity_tick_size
199+
chain_formatted_value = quantized_value * Decimal(f"1e{decimals}")
200+
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
204201

205202
return extended_chain_formatted_value
206203

@@ -211,19 +208,17 @@ def calculate_margin_in_chain_format(
211208
is_buy: bool,
212209
special_denom: Optional[Denom] = None,
213210
) -> Decimal:
214-
quantity_decimals = 0 if special_denom is None else special_denom.base
215-
price_decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
211+
quote_decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
216212
min_quantity_tick_size = (
217213
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
218214
)
219215
price = human_readable_price if is_buy else 1 - human_readable_price
220-
chain_formatted_quantity = human_readable_quantity * Decimal(f"1e{quantity_decimals}")
221-
chain_formatted_price = price * Decimal(f"1e{price_decimals}")
222-
margin = chain_formatted_price * chain_formatted_quantity
216+
margin = price * human_readable_quantity
223217
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
224218
# in the chain (it might be changed to a min_notional in the future)
225219
quantized_margin = (margin // min_quantity_tick_size) * min_quantity_tick_size
226-
extended_chain_formatted_margin = quantized_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
220+
chain_formatted_margin = quantized_margin * Decimal(f"1e{quote_decimals}")
221+
extended_chain_formatted_margin = chain_formatted_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
227222

228223
return extended_chain_formatted_margin
229224

0 commit comments

Comments
 (0)