Skip to main content

nautilus_model/python/defi/
enums.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 DeFi enums.
17
18use std::str::FromStr;
19
20use nautilus_core::python::to_pyvalue_err;
21use pyo3::{PyTypeInfo, prelude::*, types::PyType};
22
23use crate::{
24    defi::{chain::Blockchain, data::PoolLiquidityUpdateType, dex::AmmType},
25    python::common::EnumIterator,
26};
27
28#[pymethods]
29impl Blockchain {
30    #[new]
31    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
32        let t = Self::type_object(py);
33        Self::py_from_str(&t, value)
34    }
35
36    fn __hash__(&self) -> isize {
37        *self as isize
38    }
39
40    fn __repr__(&self) -> String {
41        format!(
42            "<{}.{}: '{}'>",
43            stringify!(Blockchain),
44            self.name(),
45            self.value(),
46        )
47    }
48
49    fn __str__(&self) -> String {
50        self.to_string()
51    }
52
53    #[getter]
54    #[must_use]
55    pub fn name(&self) -> String {
56        self.to_string()
57    }
58
59    #[getter]
60    #[must_use]
61    pub fn value(&self) -> u8 {
62        *self as u8
63    }
64
65    #[classmethod]
66    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
67        EnumIterator::new::<Self>(py)
68    }
69
70    #[classmethod]
71    #[pyo3(name = "from_str")]
72    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
73        let data_str: &str = data.extract()?;
74        let tokenized = data_str.to_uppercase();
75        Self::from_str(&tokenized).map_err(to_pyvalue_err)
76    }
77}
78
79#[pymethods]
80impl AmmType {
81    #[new]
82    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
83        let t = Self::type_object(py);
84        Self::py_from_str(&t, value)
85    }
86
87    fn __hash__(&self) -> isize {
88        *self as isize
89    }
90
91    fn __repr__(&self) -> String {
92        format!(
93            "<{}.{}: '{}'>",
94            stringify!(AmmType),
95            self.name(),
96            self.value(),
97        )
98    }
99
100    fn __str__(&self) -> String {
101        self.to_string()
102    }
103
104    #[getter]
105    #[must_use]
106    pub fn name(&self) -> String {
107        self.to_string()
108    }
109
110    #[getter]
111    #[must_use]
112    pub fn value(&self) -> u8 {
113        *self as u8
114    }
115
116    #[classmethod]
117    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
118        EnumIterator::new::<Self>(py)
119    }
120
121    #[classmethod]
122    #[pyo3(name = "from_str")]
123    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
124        let data_str: &str = data.extract()?;
125        Self::from_str(data_str).map_err(to_pyvalue_err)
126    }
127}
128
129#[pymethods]
130impl PoolLiquidityUpdateType {
131    #[new]
132    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
133        let t = Self::type_object(py);
134        Self::py_from_str(&t, value)
135    }
136
137    fn __hash__(&self) -> isize {
138        *self as isize
139    }
140
141    fn __repr__(&self) -> String {
142        format!(
143            "<{}.{}: '{}'>",
144            stringify!(PoolLiquidityUpdateType),
145            self.name(),
146            self.value(),
147        )
148    }
149
150    fn __str__(&self) -> String {
151        self.to_string()
152    }
153
154    #[getter]
155    #[must_use]
156    pub fn name(&self) -> String {
157        self.to_string()
158    }
159
160    #[getter]
161    #[must_use]
162    pub fn value(&self) -> u8 {
163        *self as u8
164    }
165
166    #[classmethod]
167    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
168        EnumIterator::new::<Self>(py)
169    }
170
171    #[classmethod]
172    #[pyo3(name = "from_str")]
173    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
174        let data_str: &str = data.extract()?;
175        Self::from_str(data_str).map_err(to_pyvalue_err)
176    }
177}