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::{
25        chain::Blockchain,
26        data::PoolLiquidityUpdateType,
27        dex::{AmmType, DexType},
28    },
29    python::common::EnumIterator,
30};
31
32#[pymethods]
33#[pyo3_stub_gen::derive::gen_stub_pymethods]
34impl Blockchain {
35    /// Represents different blockchain networks.
36    #[new]
37    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
38        let t = Self::type_object(py);
39        Self::py_from_str(&t, value)
40    }
41
42    const fn __hash__(&self) -> isize {
43        *self as isize
44    }
45
46    fn __str__(&self) -> String {
47        self.to_string()
48    }
49
50    #[getter]
51    #[must_use]
52    pub fn name(&self) -> String {
53        self.to_string()
54    }
55
56    #[getter]
57    #[must_use]
58    pub fn value(&self) -> u8 {
59        *self as u8
60    }
61
62    #[classmethod]
63    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
64        EnumIterator::new::<Self>(py)
65    }
66
67    #[classmethod]
68    #[pyo3(name = "from_str")]
69    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
70        let data_str: &str = data.extract()?;
71        let tokenized = data_str.to_uppercase();
72        Self::from_str(&tokenized).map_err(to_pyvalue_err)
73    }
74}
75
76#[pymethods]
77#[pyo3_stub_gen::derive::gen_stub_pymethods]
78impl AmmType {
79    /// Represents different types of Automated Market Makers (AMMs) in DeFi protocols.
80    #[new]
81    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
82        let t = Self::type_object(py);
83        Self::py_from_str(&t, value)
84    }
85
86    const fn __hash__(&self) -> isize {
87        *self as isize
88    }
89
90    fn __str__(&self) -> String {
91        self.to_string()
92    }
93
94    #[getter]
95    #[must_use]
96    pub fn name(&self) -> String {
97        self.to_string()
98    }
99
100    #[getter]
101    #[must_use]
102    pub fn value(&self) -> u8 {
103        *self as u8
104    }
105
106    #[classmethod]
107    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
108        EnumIterator::new::<Self>(py)
109    }
110
111    #[classmethod]
112    #[pyo3(name = "from_str")]
113    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
114        let data_str: &str = data.extract()?;
115        Self::from_str(data_str).map_err(to_pyvalue_err)
116    }
117}
118
119#[pymethods]
120#[pyo3_stub_gen::derive::gen_stub_pymethods]
121impl DexType {
122    const fn __hash__(&self) -> isize {
123        *self as isize
124    }
125}
126
127#[pymethods]
128#[pyo3_stub_gen::derive::gen_stub_pymethods]
129impl PoolLiquidityUpdateType {
130    /// Represents the type of liquidity update operation in a DEX pool.
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    const fn __hash__(&self) -> isize {
138        *self as isize
139    }
140
141    fn __str__(&self) -> String {
142        self.to_string()
143    }
144
145    #[getter]
146    #[must_use]
147    pub fn name(&self) -> String {
148        self.to_string()
149    }
150
151    #[getter]
152    #[must_use]
153    pub fn value(&self) -> u8 {
154        *self as u8
155    }
156
157    #[classmethod]
158    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
159        EnumIterator::new::<Self>(py)
160    }
161
162    #[classmethod]
163    #[pyo3(name = "from_str")]
164    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
165        let data_str: &str = data.extract()?;
166        Self::from_str(data_str).map_err(to_pyvalue_err)
167    }
168}