11from decimal import Decimal
22from math import floor
3+ from typing import Union
34
45"""
56One thing you may need to pay more attention to is how to deal with decimals on the Injective Exchange.
@@ -56,9 +57,13 @@ def binary_options_quantity_to_backend(quantity, denom) -> int:
5657 return int (exchange_quantity )
5758
5859def derivative_margin_to_backend (price , quantity , leverage , denom ) -> int :
59- price_tick_size = Decimal (denom .min_price_tick_size ) / pow (10 , denom .quote )
60- margin = (price * quantity ) / leverage
61- exchange_margin = floor_to (margin , float (price_tick_size )) * pow (10 , 18 + denom .quote )
60+ chain_format_price = Decimal (str (price )) * Decimal (f"1e{ denom .quote } " )
61+ chain_format_quantity = Decimal (str (quantity )) * Decimal (f"1e{ denom .base } " )
62+ decimal_leverage = Decimal (str (leverage ))
63+ margin = (chain_format_price * chain_format_quantity ) / decimal_leverage
64+ # We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
65+ # in the chain (it might be changed to a min_notional in the future)
66+ exchange_margin = floor_to (margin , denom .min_quantity_tick_size ) * Decimal (f"1e18" )
6267 return int (exchange_margin )
6368
6469def binary_options_buy_margin_to_backend (price , quantity , denom ) -> int :
@@ -82,7 +87,7 @@ def amount_to_backend(amount, decimals) -> int:
8287 be_amount = amount * pow (10 , decimals )
8388 return int (be_amount )
8489
85- def floor_to (value : float , target : float ) -> Decimal :
90+ def floor_to (value : Union [ float , Decimal ], target : Union [ float , Decimal ] ) -> Decimal :
8691 value_tmp = Decimal (str (value ))
8792 target_tmp = Decimal (str (target ))
8893 result = int (floor (value_tmp / target_tmp )) * target_tmp
0 commit comments