nautilus_dydx/python/
types.rs1use std::{
19 collections::hash_map::DefaultHasher,
20 hash::{Hash, Hasher},
21};
22
23use nautilus_core::python::IntoPyObjectNautilusExt;
24use nautilus_model::{identifiers::InstrumentId, types::Price};
25use pyo3::{prelude::*, types::PyDict};
26
27use crate::types::DydxOraclePrice;
28
29#[pymethods]
30impl DydxOraclePrice {
31 fn __richcmp__(&self, other: &Self, op: pyo3::pyclass::CompareOp, py: Python<'_>) -> Py<PyAny> {
32 match op {
33 pyo3::pyclass::CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
34 pyo3::pyclass::CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
35 _ => py.NotImplemented(),
36 }
37 }
38
39 fn __hash__(&self) -> isize {
40 let mut hasher = DefaultHasher::new();
41 self.hash(&mut hasher);
42 hasher.finish() as isize
43 }
44
45 fn __repr__(&self) -> String {
46 format!(
47 "{}(instrument_id={}, oracle_price={}, ts_event={}, ts_init={})",
48 stringify!(DydxOraclePrice),
49 self.instrument_id,
50 self.oracle_price,
51 self.ts_event,
52 self.ts_init,
53 )
54 }
55
56 fn __str__(&self) -> String {
57 self.__repr__()
58 }
59
60 #[getter]
61 #[pyo3(name = "instrument_id")]
62 const fn py_instrument_id(&self) -> InstrumentId {
63 self.instrument_id
64 }
65
66 #[getter]
67 #[pyo3(name = "oracle_price")]
68 const fn py_oracle_price(&self) -> Price {
69 self.oracle_price
70 }
71
72 #[getter]
73 #[pyo3(name = "ts_event")]
74 const fn py_ts_event(&self) -> u64 {
75 self.ts_event.as_u64()
76 }
77
78 #[getter]
79 #[pyo3(name = "ts_init")]
80 const fn py_ts_init(&self) -> u64 {
81 self.ts_init.as_u64()
82 }
83
84 #[pyo3(name = "to_dict")]
85 pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
86 let dict = PyDict::new(py);
87 dict.set_item("type", stringify!(DydxOraclePrice))?;
88 dict.set_item("instrument_id", self.instrument_id.to_string())?;
89 dict.set_item("oracle_price", self.oracle_price.to_string())?;
90 dict.set_item("ts_event", self.ts_event.as_u64())?;
91 dict.set_item("ts_init", self.ts_init.as_u64())?;
92 Ok(dict.into())
93 }
94}