@@ -22,17 +22,17 @@ class SpotMarket:
22
22
min_notional : Decimal
23
23
24
24
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 } " )
28
28
29
29
return extended_chain_formatted_value
30
30
31
31
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
33
32
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 } " )
36
36
37
37
return extended_chain_formatted_value
38
38
@@ -88,17 +88,17 @@ class DerivativeMarket:
88
88
89
89
def quantity_to_chain_format (self , human_readable_value : Decimal ) -> Decimal :
90
90
# 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 } " )
94
94
95
95
return extended_chain_formatted_value
96
96
97
97
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
99
98
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 } " )
102
102
103
103
return extended_chain_formatted_value
104
104
@@ -108,12 +108,15 @@ def margin_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
108
108
def calculate_margin_in_chain_format (
109
109
self , human_readable_quantity : Decimal , human_readable_price : Decimal , leverage : Decimal
110
110
) -> 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
112
114
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
113
115
# in the chain (it might be changed to a min_notional in the future)
114
116
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 } " )
115
118
116
- return self . notional_to_chain_format ( human_readable_value = quantized_margin )
119
+ return extended_chain_formatted_margin
117
120
118
121
def notional_to_chain_format (self , human_readable_value : Decimal ) -> Decimal :
119
122
decimals = self .quote_token .decimals
@@ -177,18 +180,18 @@ def quantity_to_chain_format(self, human_readable_value: Decimal, special_denom:
177
180
min_quantity_tick_size = (
178
181
self .min_quantity_tick_size if special_denom is None else special_denom .min_quantity_tick_size
179
182
)
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 } " )
183
186
184
187
return extended_chain_formatted_value
185
188
186
189
def price_to_chain_format (self , human_readable_value : Decimal , special_denom : Optional [Denom ] = None ) -> Decimal :
187
190
decimals = self .quote_token .decimals if special_denom is None else special_denom .quote
188
191
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 } " )
192
195
193
196
return extended_chain_formatted_value
194
197
@@ -197,9 +200,9 @@ def margin_to_chain_format(self, human_readable_value: Decimal, special_denom: O
197
200
min_quantity_tick_size = (
198
201
self .min_quantity_tick_size if special_denom is None else special_denom .min_quantity_tick_size
199
202
)
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 } " )
203
206
204
207
return extended_chain_formatted_value
205
208
@@ -210,17 +213,19 @@ def calculate_margin_in_chain_format(
210
213
is_buy : bool ,
211
214
special_denom : Optional [Denom ] = None ,
212
215
) -> 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
214
218
min_quantity_tick_size = (
215
219
self .min_quantity_tick_size if special_denom is None else special_denom .min_quantity_tick_size
216
220
)
217
221
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
219
225
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
220
226
# in the chain (it might be changed to a min_notional in the future)
221
227
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 } " )
224
229
225
230
return extended_chain_formatted_margin
226
231
0 commit comments