@@ -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
0 commit comments