Skip to main content

nautilus_dydx/python/
types.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Python bindings for dYdX custom data types.
17
18use 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}