Skip to content

Commit

Permalink
(fix) Removed quantization logic from notional formatting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
aarmoa committed Aug 6, 2024
1 parent dedef04 commit 1bffc7c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
12 changes: 3 additions & 9 deletions pyinjective/core/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ def price_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
def notional_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
decimals = self.quote_token.decimals
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantization_factor = self.min_notional if self.min_notional > Decimal("0") else self.min_quantity_tick_size
quantized_value = (chain_formatted_value // quantization_factor) * quantization_factor
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

Expand Down Expand Up @@ -122,9 +120,7 @@ def calculate_margin_in_chain_format(
def notional_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
decimals = self.quote_token.decimals
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantization_factor = self.min_notional if self.min_notional > Decimal("0") else self.min_quantity_tick_size
quantized_value = (chain_formatted_value // quantization_factor) * quantization_factor
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

Expand Down Expand Up @@ -234,9 +230,7 @@ def calculate_margin_in_chain_format(
def notional_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
decimals = self.quote_token.decimals
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantization_factor = self.min_notional if self.min_notional > Decimal("0") else self.min_quantity_tick_size
quantized_value = (chain_formatted_value // quantization_factor) * quantization_factor
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
extended_chain_formatted_value = chain_formatted_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

return extended_chain_formatted_value

Expand Down
15 changes: 6 additions & 9 deletions tests/core/test_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ def test_convert_notional_to_chain_format(self, inj_usdt_spot_market: SpotMarket
chain_value = inj_usdt_spot_market.notional_to_chain_format(human_readable_value=original_notional)
notional_decimals = inj_usdt_spot_market.quote_token.decimals
expected_value = original_notional * Decimal(f"1e{notional_decimals}")
quantized_value = (expected_value // inj_usdt_spot_market.min_notional) * inj_usdt_spot_market.min_notional
quantized_chain_format_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
expected_chain_format_value = expected_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

assert quantized_chain_format_value == chain_value
assert expected_chain_format_value == chain_value

def test_convert_quantity_from_chain_format(self, inj_usdt_spot_market: SpotMarket):
expected_quantity = Decimal("123.456")
Expand Down Expand Up @@ -157,10 +156,9 @@ def test_convert_notional_to_chain_format(self, btc_usdt_perp_market: Derivative
chain_value = btc_usdt_perp_market.notional_to_chain_format(human_readable_value=original_notional)
notional_decimals = btc_usdt_perp_market.quote_token.decimals
expected_value = original_notional * Decimal(f"1e{notional_decimals}")
quantized_value = (expected_value // btc_usdt_perp_market.min_notional) * btc_usdt_perp_market.min_notional
quantized_chain_format_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
expected_chain_format_value = expected_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

assert quantized_chain_format_value == chain_value
assert expected_chain_format_value == chain_value

def test_convert_quantity_from_chain_format(self, btc_usdt_perp_market: DerivativeMarket):
expected_quantity = Decimal("123.456")
Expand Down Expand Up @@ -430,10 +428,9 @@ def test_convert_notional_to_chain_format(self, first_match_bet_market: BinaryOp
chain_value = first_match_bet_market.notional_to_chain_format(human_readable_value=original_notional)
notional_decimals = first_match_bet_market.quote_token.decimals
expected_value = original_notional * Decimal(f"1e{notional_decimals}")
quantized_value = (expected_value // first_match_bet_market.min_notional) * first_match_bet_market.min_notional
quantized_chain_format_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
expected_chain_format_value = expected_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")

assert quantized_chain_format_value == chain_value
assert expected_chain_format_value == chain_value

def test_convert_quantity_from_chain_format_with_fixed_denom(self, first_match_bet_market: BinaryOptionMarket):
original_quantity = Decimal("123.456789")
Expand Down

0 comments on commit 1bffc7c

Please sign in to comment.