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]
30#[pyo3_stub_gen::derive::gen_stub_pymethods]
31impl DydxOraclePrice {
32    fn __richcmp__(&self, other: &Self, op: pyo3::pyclass::CompareOp, py: Python<'_>) -> Py<PyAny> {
33        match op {
34            pyo3::pyclass::CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
35            pyo3::pyclass::CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
36            _ => py.NotImplemented(),
37        }
38    }
39
40    fn __hash__(&self) -> isize {
41        let mut hasher = DefaultHasher::new();
42        self.hash(&mut hasher);
43        hasher.finish() as isize
44    }
45
46    fn __repr__(&self) -> String {
47        format!(
48            "{}(instrument_id={}, oracle_price={}, ts_event={}, ts_init={})",
49            stringify!(DydxOraclePrice),
50            self.instrument_id,
51            self.oracle_price,
52            self.ts_event,
53            self.ts_init,
54        )
55    }
56
57    fn __str__(&self) -> String {
58        self.__repr__()
59    }
60
61    #[getter]
62    #[pyo3(name = "instrument_id")]
63    const fn py_instrument_id(&self) -> InstrumentId {
64        self.instrument_id
65    }
66
67    #[getter]
68    #[pyo3(name = "oracle_price")]
69    const fn py_oracle_price(&self) -> Price {
70        self.oracle_price
71    }
72
73    #[getter]
74    #[pyo3(name = "ts_event")]
75    const fn py_ts_event(&self) -> u64 {
76        self.ts_event.as_u64()
77    }
78
79    #[getter]
80    #[pyo3(name = "ts_init")]
81    const fn py_ts_init(&self) -> u64 {
82        self.ts_init.as_u64()
83    }
84
85    #[pyo3(name = "to_dict")]
86    pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
87        let dict = PyDict::new(py);
88        dict.set_item("type", stringify!(DydxOraclePrice))?;
89        dict.set_item("instrument_id", self.instrument_id.to_string())?;
90        dict.set_item("oracle_price", self.oracle_price.to_string())?;
91        dict.set_item("ts_event", self.ts_event.as_u64())?;
92        dict.set_item("ts_init", self.ts_init.as_u64())?;
93        Ok(dict.into())
94    }
95}