-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmarket.py
More file actions
274 lines (217 loc) · 13.7 KB
/
market.py
File metadata and controls
274 lines (217 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from dataclasses import dataclass
from decimal import ROUND_UP, Decimal
from typing import Optional
from pyinjective.constant import ADDITIONAL_CHAIN_FORMAT_DECIMALS
from pyinjective.core.token import Token
from pyinjective.utils.denom import Denom
@dataclass(eq=True, frozen=True)
class SpotMarket:
id: str
status: str
ticker: str
base_token: Token
quote_token: Token
maker_fee_rate: Decimal
taker_fee_rate: Decimal
service_provider_fee: Decimal
min_price_tick_size: Decimal
min_quantity_tick_size: Decimal
min_notional: Decimal
def quantity_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
chain_formatted_value = human_readable_value * Decimal(f"1e{self.base_token.decimals}")
quantized_value = chain_formatted_value // self.min_quantity_tick_size * self.min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_value
def price_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
decimals = self.quote_token.decimals - self.base_token.decimals
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // self.min_price_tick_size) * self.min_price_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_value
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}")
quantized_balue = chain_formatted_value.quantize(Decimal("1"), rounding=ROUND_UP)
extended_chain_formatted_value = quantized_balue * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_value
def quantity_from_chain_format(self, chain_value: Decimal) -> Decimal:
return chain_value / Decimal(f"1e{self.base_token.decimals}")
def price_from_chain_format(self, chain_value: Decimal) -> Decimal:
decimals = self.base_token.decimals - self.quote_token.decimals
return chain_value * Decimal(f"1e{decimals}")
def notional_from_chain_format(self, chain_value: Decimal) -> Decimal:
return chain_value / Decimal(f"1e{self.quote_token.decimals}")
def quantity_from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return self._from_extended_chain_format(chain_value=self.quantity_from_chain_format(chain_value=chain_value))
def price_from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return self._from_extended_chain_format(chain_value=self.price_from_chain_format(chain_value=chain_value))
def notional_from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return self._from_extended_chain_format(chain_value=self.notional_from_chain_format(chain_value=chain_value))
def _from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return chain_value / Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
@dataclass(eq=True, frozen=True)
class DerivativeMarket:
id: str
status: str
ticker: str
oracle_base: str
oracle_quote: str
oracle_type: str
oracle_scale_factor: int
initial_margin_ratio: Decimal
maintenance_margin_ratio: Decimal
quote_token: Token
maker_fee_rate: Decimal
taker_fee_rate: Decimal
service_provider_fee: Decimal
min_price_tick_size: Decimal
min_quantity_tick_size: Decimal
min_notional: Decimal
def quantity_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
# Derivative markets do not have a base market to provide the number of decimals
chain_formatted_value = human_readable_value
quantized_value = chain_formatted_value // self.min_quantity_tick_size * self.min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_value
def price_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
decimals = self.quote_token.decimals
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // self.min_price_tick_size) * self.min_price_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_value
def margin_to_chain_format(self, human_readable_value: Decimal) -> Decimal:
return self.notional_to_chain_format(human_readable_value=human_readable_value)
def calculate_margin_in_chain_format(
self, human_readable_quantity: Decimal, human_readable_price: Decimal, leverage: Decimal
) -> Decimal:
chain_formatted_quantity = human_readable_quantity
chain_formatted_price = human_readable_price * Decimal(f"1e{self.quote_token.decimals}")
margin = (chain_formatted_price * chain_formatted_quantity) / leverage
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
# in the chain (it might be changed to a min_notional in the future)
quantized_margin = (margin // self.min_quantity_tick_size) * self.min_quantity_tick_size
extended_chain_formatted_margin = quantized_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_margin
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}")
quantized_notional = chain_formatted_value.quantize(Decimal("1"), rounding=ROUND_UP)
extended_chain_formatted_value = quantized_notional * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_value
def quantity_from_chain_format(self, chain_value: Decimal) -> Decimal:
return chain_value
def price_from_chain_format(self, chain_value: Decimal) -> Decimal:
return chain_value * Decimal(f"1e-{self.quote_token.decimals}")
def margin_from_chain_format(self, chain_value: Decimal) -> Decimal:
return self.notional_from_chain_format(chain_value=chain_value)
def notional_from_chain_format(self, chain_value: Decimal) -> Decimal:
return chain_value / Decimal(f"1e{self.quote_token.decimals}")
def quantity_from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return self._from_extended_chain_format(chain_value=self.quantity_from_chain_format(chain_value=chain_value))
def price_from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return self._from_extended_chain_format(chain_value=self.price_from_chain_format(chain_value=chain_value))
def margin_from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return self.notional_from_extended_chain_format(chain_value=chain_value)
def notional_from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return self._from_extended_chain_format(chain_value=self.notional_from_chain_format(chain_value=chain_value))
def _from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return chain_value / Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
@dataclass(eq=True, frozen=True)
class BinaryOptionMarket:
id: str
status: str
ticker: str
oracle_symbol: str
oracle_provider: str
oracle_type: str
oracle_scale_factor: int
expiration_timestamp: int
settlement_timestamp: int
quote_token: Token
maker_fee_rate: Decimal
taker_fee_rate: Decimal
service_provider_fee: Decimal
min_price_tick_size: Decimal
min_quantity_tick_size: Decimal
min_notional: Decimal
settlement_price: Optional[Decimal] = None
def quantity_to_chain_format(self, human_readable_value: Decimal, special_denom: Optional[Denom] = None) -> Decimal:
# Binary option markets do not have a base market to provide the number of decimals
decimals = 0 if special_denom is None else special_denom.base
min_quantity_tick_size = (
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
)
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = chain_formatted_value // min_quantity_tick_size * min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_value
def price_to_chain_format(self, human_readable_value: Decimal, special_denom: Optional[Denom] = None) -> Decimal:
decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
min_price_tick_size = self.min_price_tick_size if special_denom is None else special_denom.min_price_tick_size
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // min_price_tick_size) * min_price_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_value
def margin_to_chain_format(self, human_readable_value: Decimal, special_denom: Optional[Denom] = None) -> Decimal:
decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
min_quantity_tick_size = (
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
)
chain_formatted_value = human_readable_value * Decimal(f"1e{decimals}")
quantized_value = (chain_formatted_value // min_quantity_tick_size) * min_quantity_tick_size
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_value
def calculate_margin_in_chain_format(
self,
human_readable_quantity: Decimal,
human_readable_price: Decimal,
is_buy: bool,
special_denom: Optional[Denom] = None,
) -> Decimal:
quantity_decimals = 0 if special_denom is None else special_denom.base
price_decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
min_quantity_tick_size = (
self.min_quantity_tick_size if special_denom is None else special_denom.min_quantity_tick_size
)
price = human_readable_price if is_buy else 1 - human_readable_price
chain_formatted_quantity = human_readable_quantity * Decimal(f"1e{quantity_decimals}")
chain_formatted_price = price * Decimal(f"1e{price_decimals}")
margin = chain_formatted_price * chain_formatted_quantity
# We are using the min_quantity_tick_size to quantize the margin because that is the way margin is validated
# in the chain (it might be changed to a min_notional in the future)
quantized_margin = (margin // min_quantity_tick_size) * min_quantity_tick_size
extended_chain_formatted_margin = quantized_margin * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_margin
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}")
quantized_value = chain_formatted_value.quantize(Decimal("1"), rounding=ROUND_UP)
extended_chain_formatted_value = quantized_value * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return extended_chain_formatted_value
def quantity_from_chain_format(self, chain_value: Decimal, special_denom: Optional[Denom] = None) -> Decimal:
# Binary option markets do not have a base market to provide the number of decimals
decimals = 0 if special_denom is None else special_denom.base
return chain_value * Decimal(f"1e-{decimals}")
def price_from_chain_format(self, chain_value: Decimal, special_denom: Optional[Denom] = None) -> Decimal:
decimals = self.quote_token.decimals if special_denom is None else special_denom.quote
return chain_value * Decimal(f"1e-{decimals}")
def margin_from_chain_format(self, chain_value: Decimal) -> Decimal:
return self.notional_from_chain_format(chain_value=chain_value)
def notional_from_chain_format(self, chain_value: Decimal) -> Decimal:
return chain_value / Decimal(f"1e{self.quote_token.decimals}")
def quantity_from_extended_chain_format(
self, chain_value: Decimal, special_denom: Optional[Denom] = None
) -> Decimal:
return self._from_extended_chain_format(
chain_value=self.quantity_from_chain_format(chain_value=chain_value, special_denom=special_denom)
)
def price_from_extended_chain_format(self, chain_value: Decimal, special_denom: Optional[Denom] = None) -> Decimal:
return self._from_extended_chain_format(
chain_value=self.price_from_chain_format(chain_value=chain_value, special_denom=special_denom)
)
def margin_from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return self.notional_from_extended_chain_format(chain_value=chain_value)
def notional_from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return self._from_extended_chain_format(chain_value=self.notional_from_chain_format(chain_value=chain_value))
def _from_extended_chain_format(self, chain_value: Decimal) -> Decimal:
return chain_value / Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")