1use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19};
20
21use nautilus_core::{
22 from_pydict,
23 python::{IntoPyObjectNautilusExt, serialization::from_dict_pyo3, to_pyvalue_err},
24};
25use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
26use rust_decimal::Decimal;
27
28use crate::{
29 enums::AssetClass,
30 identifiers::{InstrumentId, Symbol},
31 instruments::TokenizedAsset,
32 types::{Currency, Money, Price, Quantity},
33};
34
35#[pymethods]
36#[pyo3_stub_gen::derive::gen_stub_pymethods]
37impl TokenizedAsset {
38 #[expect(clippy::too_many_arguments)]
44 #[new]
45 #[pyo3(signature = (instrument_id, raw_symbol, asset_class, base_currency, quote_currency, price_precision, size_precision, price_increment, size_increment, ts_event, ts_init, isin=None, multiplier=None, lot_size=None, max_quantity=None, min_quantity=None, max_notional=None, min_notional=None, max_price=None, min_price=None, margin_init=None, margin_maint=None, maker_fee=None, taker_fee=None, info=None))]
46 fn py_new(
47 instrument_id: InstrumentId,
48 raw_symbol: Symbol,
49 asset_class: AssetClass,
50 base_currency: Currency,
51 quote_currency: Currency,
52 price_precision: u8,
53 size_precision: u8,
54 price_increment: Price,
55 size_increment: Quantity,
56 ts_event: u64,
57 ts_init: u64,
58 isin: Option<String>,
59 multiplier: Option<Quantity>,
60 lot_size: Option<Quantity>,
61 max_quantity: Option<Quantity>,
62 min_quantity: Option<Quantity>,
63 max_notional: Option<Money>,
64 min_notional: Option<Money>,
65 max_price: Option<Price>,
66 min_price: Option<Price>,
67 margin_init: Option<Decimal>,
68 margin_maint: Option<Decimal>,
69 maker_fee: Option<Decimal>,
70 taker_fee: Option<Decimal>,
71 info: Option<Py<PyDict>>,
72 ) -> PyResult<Self> {
73 let info_map = if let Some(info_dict) = info {
74 Python::attach(|py| from_pydict(py, info_dict))?
75 } else {
76 None
77 };
78
79 Self::new_checked(
80 instrument_id,
81 raw_symbol,
82 asset_class,
83 base_currency,
84 quote_currency,
85 isin.map(|s| ustr::Ustr::from(&s)),
86 price_precision,
87 size_precision,
88 price_increment,
89 size_increment,
90 multiplier,
91 lot_size,
92 max_quantity,
93 min_quantity,
94 max_notional,
95 min_notional,
96 max_price,
97 min_price,
98 margin_init,
99 margin_maint,
100 maker_fee,
101 taker_fee,
102 info_map,
103 ts_event.into(),
104 ts_init.into(),
105 )
106 .map_err(to_pyvalue_err)
107 }
108
109 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
110 match op {
111 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
112 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
113 _ => py.NotImplemented(),
114 }
115 }
116
117 fn __hash__(&self) -> isize {
118 let mut hasher = DefaultHasher::new();
119 self.hash(&mut hasher);
120 hasher.finish() as isize
121 }
122
123 #[getter]
124 fn type_name(&self) -> &'static str {
125 stringify!(TokenizedAsset)
126 }
127
128 #[getter]
129 #[pyo3(name = "id")]
130 fn py_id(&self) -> InstrumentId {
131 self.id
132 }
133
134 #[getter]
135 #[pyo3(name = "raw_symbol")]
136 fn py_raw_symbol(&self) -> Symbol {
137 self.raw_symbol
138 }
139
140 #[getter]
141 #[pyo3(name = "asset_class")]
142 fn py_asset_class(&self) -> AssetClass {
143 self.asset_class
144 }
145
146 #[getter]
147 #[pyo3(name = "base_currency")]
148 fn py_base_currency(&self) -> Currency {
149 self.base_currency
150 }
151
152 #[getter]
153 #[pyo3(name = "quote_currency")]
154 fn py_quote_currency(&self) -> Currency {
155 self.quote_currency
156 }
157
158 #[getter]
159 #[pyo3(name = "isin")]
160 fn py_isin(&self) -> Option<String> {
161 self.isin.map(|u| u.to_string())
162 }
163
164 #[getter]
165 #[pyo3(name = "price_precision")]
166 fn py_price_precision(&self) -> u8 {
167 self.price_precision
168 }
169
170 #[getter]
171 #[pyo3(name = "size_precision")]
172 fn py_size_precision(&self) -> u8 {
173 self.size_precision
174 }
175
176 #[getter]
177 #[pyo3(name = "price_increment")]
178 fn py_price_increment(&self) -> Price {
179 self.price_increment
180 }
181
182 #[getter]
183 #[pyo3(name = "size_increment")]
184 fn py_size_increment(&self) -> Quantity {
185 self.size_increment
186 }
187
188 #[getter]
189 #[pyo3(name = "multiplier")]
190 fn py_multiplier(&self) -> Quantity {
191 self.multiplier
192 }
193
194 #[getter]
195 #[pyo3(name = "lot_size")]
196 fn py_lot_size(&self) -> Option<Quantity> {
197 self.lot_size
198 }
199
200 #[getter]
201 #[pyo3(name = "max_quantity")]
202 fn py_max_quantity(&self) -> Option<Quantity> {
203 self.max_quantity
204 }
205
206 #[getter]
207 #[pyo3(name = "min_quantity")]
208 fn py_min_quantity(&self) -> Option<Quantity> {
209 self.min_quantity
210 }
211
212 #[getter]
213 #[pyo3(name = "max_notional")]
214 fn py_max_notional(&self) -> Option<Money> {
215 self.max_notional
216 }
217
218 #[getter]
219 #[pyo3(name = "min_notional")]
220 fn py_min_notional(&self) -> Option<Money> {
221 self.min_notional
222 }
223
224 #[getter]
225 #[pyo3(name = "max_price")]
226 fn py_max_price(&self) -> Option<Price> {
227 self.max_price
228 }
229
230 #[getter]
231 #[pyo3(name = "min_price")]
232 fn py_min_price(&self) -> Option<Price> {
233 self.min_price
234 }
235
236 #[getter]
237 #[pyo3(name = "maker_fee")]
238 fn py_maker_fee(&self) -> Decimal {
239 self.maker_fee
240 }
241
242 #[getter]
243 #[pyo3(name = "taker_fee")]
244 fn py_taker_fee(&self) -> Decimal {
245 self.taker_fee
246 }
247
248 #[getter]
249 #[pyo3(name = "margin_maint")]
250 fn py_margin_maint(&self) -> Decimal {
251 self.margin_maint
252 }
253
254 #[getter]
255 #[pyo3(name = "margin_init")]
256 fn py_margin_init(&self) -> Decimal {
257 self.margin_init
258 }
259
260 #[getter]
261 #[pyo3(name = "ts_event")]
262 fn py_ts_event(&self) -> u64 {
263 self.ts_event.as_u64()
264 }
265
266 #[getter]
267 #[pyo3(name = "ts_init")]
268 fn py_ts_init(&self) -> u64 {
269 self.ts_init.as_u64()
270 }
271
272 #[getter]
273 #[pyo3(name = "info")]
274 fn py_info(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
275 if let Some(ref info_map) = self.info {
276 let py_dict = PyDict::new(py);
277
278 for (key, value) in info_map {
279 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
280 let py_value =
281 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
282 py_dict.set_item(key, py_value)?;
283 }
284 Ok(py_dict.unbind())
285 } else {
286 Ok(PyDict::new(py).unbind())
287 }
288 }
289
290 #[staticmethod]
291 #[pyo3(name = "from_dict")]
292 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
293 from_dict_pyo3(py, values)
294 }
295
296 #[pyo3(name = "to_dict")]
297 fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
298 let dict = PyDict::new(py);
299 dict.set_item("type", stringify!(TokenizedAsset))?;
300 dict.set_item("id", self.id.to_string())?;
301 dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
302 dict.set_item("asset_class", self.asset_class.to_string())?;
303 dict.set_item("base_currency", self.base_currency.code.to_string())?;
304 dict.set_item("quote_currency", self.quote_currency.code.to_string())?;
305 match self.isin {
306 Some(value) => dict.set_item("isin", value.to_string())?,
307 None => dict.set_item("isin", py.None())?,
308 }
309 dict.set_item("price_precision", self.price_precision)?;
310 dict.set_item("size_precision", self.size_precision)?;
311 dict.set_item("price_increment", self.price_increment.to_string())?;
312 dict.set_item("size_increment", self.size_increment.to_string())?;
313 dict.set_item("multiplier", self.multiplier.to_string())?;
314 dict.set_item("maker_fee", self.maker_fee.to_string())?;
315 dict.set_item("taker_fee", self.taker_fee.to_string())?;
316 dict.set_item("margin_init", self.margin_init.to_string())?;
317 dict.set_item("margin_maint", self.margin_maint.to_string())?;
318
319 if let Some(ref info_map) = self.info {
320 let info_dict = PyDict::new(py);
321
322 for (key, value) in info_map {
323 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
324 let py_value =
325 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
326 info_dict.set_item(key, py_value)?;
327 }
328 dict.set_item("info", info_dict)?;
329 } else {
330 dict.set_item("info", PyDict::new(py))?;
331 }
332 dict.set_item("ts_event", self.ts_event.as_u64())?;
333 dict.set_item("ts_init", self.ts_init.as_u64())?;
334 match self.lot_size {
335 Some(value) => dict.set_item("lot_size", value.to_string())?,
336 None => dict.set_item("lot_size", py.None())?,
337 }
338
339 match self.max_quantity {
340 Some(value) => dict.set_item("max_quantity", value.to_string())?,
341 None => dict.set_item("max_quantity", py.None())?,
342 }
343
344 match self.min_quantity {
345 Some(value) => dict.set_item("min_quantity", value.to_string())?,
346 None => dict.set_item("min_quantity", py.None())?,
347 }
348
349 match self.max_notional {
350 Some(value) => dict.set_item("max_notional", value.to_string())?,
351 None => dict.set_item("max_notional", py.None())?,
352 }
353
354 match self.min_notional {
355 Some(value) => dict.set_item("min_notional", value.to_string())?,
356 None => dict.set_item("min_notional", py.None())?,
357 }
358
359 match self.max_price {
360 Some(value) => dict.set_item("max_price", value.to_string())?,
361 None => dict.set_item("max_price", py.None())?,
362 }
363
364 match self.min_price {
365 Some(value) => dict.set_item("min_price", value.to_string())?,
366 None => dict.set_item("min_price", py.None())?,
367 }
368 Ok(dict.into())
369 }
370}