Skip to main content

nautilus_model/python/types/
currency.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
16use std::str::FromStr;
17
18use nautilus_core::python::{correctness_error_to_pyvalue_err, to_pyruntime_err, to_pyvalue_err};
19use pyo3::{IntoPyObjectExt, prelude::*};
20
21use crate::{enums::CurrencyType, python::currency_lookup_error_to_pyvalue_err, types::Currency};
22
23#[pymethods]
24#[pyo3_stub_gen::derive::gen_stub_pymethods]
25impl Currency {
26    /// Represents a medium of exchange in a specified denomination with a fixed decimal precision.
27    ///
28    /// Handles up to `FIXED_PRECISION` decimals of precision.
29    #[new]
30    fn py_new(
31        code: &str,
32        precision: u8,
33        iso4217: u16,
34        name: &str,
35        currency_type: CurrencyType,
36    ) -> PyResult<Self> {
37        Self::new_checked(code, precision, iso4217, name, currency_type)
38            .map_err(correctness_error_to_pyvalue_err)
39    }
40
41    fn __reduce__(&self, py: Python) -> PyResult<Py<PyAny>> {
42        let unpickle = py.get_type::<Self>().getattr("_unpickle")?;
43        let args = (
44            self.code.to_string(),
45            self.precision,
46            self.iso4217,
47            self.name.to_string(),
48            self.currency_type.to_string(),
49        )
50            .into_py_any(py)?;
51        (unpickle, args).into_py_any(py)
52    }
53
54    #[staticmethod]
55    #[pyo3(name = "_unpickle")]
56    fn py_unpickle(
57        code: &str,
58        precision: u8,
59        iso4217: u16,
60        name: &str,
61        currency_type_str: &str,
62    ) -> PyResult<Self> {
63        let currency_type = CurrencyType::from_str(currency_type_str).map_err(to_pyvalue_err)?;
64        Self::new_checked(code, precision, iso4217, name, currency_type)
65            .map_err(correctness_error_to_pyvalue_err)
66    }
67
68    fn __repr__(&self) -> String {
69        format!("{self:?}")
70    }
71
72    fn __str__(&self) -> &'static str {
73        self.code.as_str()
74    }
75
76    #[getter]
77    #[pyo3(name = "code")]
78    fn py_code(&self) -> &'static str {
79        self.code.as_str()
80    }
81
82    #[getter]
83    #[pyo3(name = "precision")]
84    fn py_precision(&self) -> u8 {
85        self.precision
86    }
87
88    #[getter]
89    #[pyo3(name = "iso4217")]
90    fn py_iso4217(&self) -> u16 {
91        self.iso4217
92    }
93
94    #[getter]
95    #[pyo3(name = "name")]
96    fn py_name(&self) -> &'static str {
97        self.name.as_str()
98    }
99
100    #[getter]
101    #[pyo3(name = "currency_type")]
102    fn py_currency_type(&self) -> CurrencyType {
103        self.currency_type
104    }
105
106    /// Checks if the currency identified by the given `code` is a fiat currency.
107    ///
108    /// # Errors
109    ///
110    /// Returns an error if a currency with the given `code` does not exist.
111    #[staticmethod]
112    #[pyo3(name = "is_fiat")]
113    fn py_is_fiat(code: &str) -> PyResult<bool> {
114        Self::is_fiat(code).map_err(currency_lookup_error_to_pyvalue_err)
115    }
116
117    /// Checks if the currency identified by the given `code` is a cryptocurrency.
118    ///
119    /// # Errors
120    ///
121    /// Returns an error if a currency with the given `code` does not exist.
122    #[staticmethod]
123    #[pyo3(name = "is_crypto")]
124    fn py_is_crypto(code: &str) -> PyResult<bool> {
125        Self::is_crypto(code).map_err(currency_lookup_error_to_pyvalue_err)
126    }
127
128    #[staticmethod]
129    #[pyo3(name = "is_commodity_backed")]
130    fn py_is_commodidity_backed(code: &str) -> PyResult<bool> {
131        Self::is_commodity_backed(code).map_err(currency_lookup_error_to_pyvalue_err)
132    }
133
134    #[staticmethod]
135    #[pyo3(name = "from_str")]
136    #[pyo3(signature = (value, strict = false))]
137    fn py_from_str(value: &str, strict: bool) -> PyResult<Self> {
138        match Self::from_str(value) {
139            Ok(currency) => Ok(currency),
140            Err(e) => {
141                if strict {
142                    Err(currency_lookup_error_to_pyvalue_err(e))
143                } else {
144                    Self::new_checked(value, 8, 0, value, CurrencyType::Crypto)
145                        .map_err(correctness_error_to_pyvalue_err)
146                }
147            }
148        }
149    }
150
151    /// Register the given `currency` in the internal currency map.
152    ///
153    /// - If `overwrite` is `true`, any existing currency will be replaced.
154    /// - If `overwrite` is `false` and the currency already exists, the operation is a no-op.
155    ///
156    /// Registered DeFi tokens retain their first name and amount definition, including when
157    /// `overwrite` is `true`.
158    ///
159    /// # Errors
160    ///
161    /// Returns an error if the amount definition conflicts with a registered DeFi token.
162    #[staticmethod]
163    #[pyo3(name = "register")]
164    #[pyo3(signature = (currency, overwrite = false))]
165    fn py_register(currency: Self, overwrite: bool) -> PyResult<()> {
166        Self::register(currency, overwrite).map_err(to_pyruntime_err)
167    }
168}