#[repr(C)]pub struct Money {
pub raw: MoneyRaw,
pub currency: Currency,
}Expand description
Fields§
§raw: MoneyRawRepresents the raw fixed-point amount, with currency.precision defining the number of decimal places.
currency: CurrencyThe currency denomination associated with the monetary amount.
Implementations§
Source§impl Money
impl Money
Sourcepub fn new_checked(amount: f64, currency: Currency) -> Result<Self>
pub fn new_checked(amount: f64, currency: Currency) -> Result<Self>
Sourcepub fn new(amount: f64, currency: Currency) -> Self
pub fn new(amount: f64, currency: Currency) -> Self
Creates a new Money instance.
§Panics
Panics if a correctness check fails. See Money::new_checked for more details.
Sourcepub fn from_raw(raw: MoneyRaw, currency: Currency) -> Self
pub fn from_raw(raw: MoneyRaw, currency: Currency) -> Self
Creates a new Money instance from the given raw fixed-point value and the specified currency.
§Panics
Panics if raw is outside the representable range [MONEY_RAW_MIN, MONEY_RAW_MAX].
Panics if currency.precision exceeds FIXED_PRECISION.
Sourcepub fn zero(currency: Currency) -> Self
pub fn zero(currency: Currency) -> Self
Creates a new Money instance with a value of zero with the given Currency.
§Panics
Panics if a correctness check fails. See Money::new_checked for more details.
Sourcepub fn as_f64(&self) -> f64
pub fn as_f64(&self) -> f64
Returns the value of this instance as an f64.
§Panics
Panics if precision is beyond MAX_FLOAT_PRECISION (16).
Sourcepub fn as_decimal(&self) -> Decimal
pub fn as_decimal(&self) -> Decimal
Returns the value of this instance as a Decimal.
Sourcepub fn to_formatted_string(&self) -> String
pub fn to_formatted_string(&self) -> String
Returns a formatted string representation of this instance.
Sourcepub fn from_decimal(decimal: Decimal, currency: Currency) -> Result<Self>
pub fn from_decimal(decimal: Decimal, currency: Currency) -> Result<Self>
Creates a new Money from a Decimal value with specified currency.
This method provides more reliable parsing by using Decimal arithmetic to avoid floating-point precision issues during conversion.
§Errors
Returns an error if:
- The decimal value cannot be converted to the raw representation.
- Overflow occurs during scaling.
Source§impl Money
impl Money
Sourcepub fn from_wei<U>(raw_wei: U, currency: Currency) -> Selfwhere
U: Into<U256>,
pub fn from_wei<U>(raw_wei: U, currency: Currency) -> Selfwhere
U: Into<U256>,
Creates a new Money instance from raw wei value with 18-decimal precision.
This method is specifically designed for DeFi applications where values are represented in wei (the smallest unit of Ether, 1 ETH = 10^18 wei).
§Panics
Panics if the raw wei value exceeds 128-bit range.
§Examples
use nautilus_model::types::{Money, Currency};
use nautilus_model::enums::CurrencyType;
use alloy_primitives::U256;
use rust_decimal_macros::dec;
let eth = Currency::new("ETH", 18, 0, "Ethereum", CurrencyType::Crypto);
let money = Money::from_wei(U256::from(1_000_000_000_000_000_000_u64), eth); // 1 ETH
assert_eq!(money.as_decimal(), dec!(1.0));Sourcepub fn to_wei(&self) -> U256
pub fn to_wei(&self) -> U256
Converts this Money instance to raw wei value.
Only valid for prices with precision 18. For other precisions convert to precision 18 first.
§Returns
The raw wei value as a U256.
§Examples
use nautilus_model::types::{Money, Currency};
use nautilus_model::enums::CurrencyType;
use alloy_primitives::U256;
let eth = Currency::new("ETH", 18, 0, "Ethereum", CurrencyType::Crypto);
// Construct via raw wei to ensure correct 18-dec precision.
let money = Money::from_wei(U256::from(1_000_000_000_000_000_000_u64), eth); // 1 ETH
let wei_value = money.to_wei();
assert_eq!(wei_value, U256::from(1_000_000_000_000_000_000_u64));Trait Implementations§
Source§impl AddAssign for Money
impl AddAssign for Money
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+= operation. Read moreSource§impl<'de> Deserialize<'de> for Money
impl<'de> Deserialize<'de> for Money
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'py> IntoPyObject<'py> for Money
impl<'py> IntoPyObject<'py> for Money
Source§impl Ord for Money
impl Ord for Money
Source§impl PartialOrd for Money
impl PartialOrd for Money
Source§impl PyClassImpl for Money
impl PyClassImpl for Money
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§const RAW_DOC: &'static CStr = /// Represents an amount of money in a specified currency denomination.
///
/// - [`MONEY_MAX`] - Maximum representable money amount
/// - [`MONEY_MIN`] - Minimum representable money amount
const RAW_DOC: &'static CStr = /// Represents an amount of money in a specified currency denomination. /// /// - [`MONEY_MAX`] - Maximum representable money amount /// - [`MONEY_MIN`] - Minimum representable money amount
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type ThreadChecker = SendablePyClass<Money>
type ThreadChecker = SendablePyClass<Money>
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::ImmutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::ImmutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
Source§impl PyClassNewTextSignature for Money
impl PyClassNewTextSignature for Money
const TEXT_SIGNATURE: &'static str = "(amount, currency)"
Source§impl PyMethods<Money> for PyClassImplCollector<Money>
impl PyMethods<Money> for PyClassImplCollector<Money>
fn py_methods(self) -> &'static PyClassItems
Source§impl PyTypeInfo for Money
impl PyTypeInfo for Money
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type or a subclass of this type.§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type.Source§impl SubAssign for Money
impl SubAssign for Money
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
-= operation. Read moreimpl Copy for Money
impl DerefToPyAny for Money
impl Eq for Money
Auto Trait Implementations§
impl Freeze for Money
impl RefUnwindSafe for Money
impl Send for Money
impl Sync for Money
impl Unpin for Money
impl UnwindSafe for Money
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> FromPyObject<'_> for Twhere
T: PyClass + Clone,
impl<T> FromPyObject<'_> for Twhere
T: PyClass + Clone,
§fn extract_bound(obj: &Bound<'_, PyAny>) -> Result<T, PyErr>
fn extract_bound(obj: &Bound<'_, PyAny>) -> Result<T, PyErr>
§impl<'py, T> FromPyObjectBound<'_, 'py> for Twhere
T: FromPyObject<'py>,
impl<'py, T> FromPyObjectBound<'_, 'py> for Twhere
T: FromPyObject<'py>,
§fn from_py_object_bound(ob: Borrowed<'_, 'py, PyAny>) -> Result<T, PyErr>
fn from_py_object_bound(ob: Borrowed<'_, 'py, PyAny>) -> Result<T, PyErr>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self into an owned Python object, dropping type information.§fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
self into an owned Python object, dropping type information and unbinding it
from the 'py lifetime.§fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
self into a Python object. Read more§impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
§fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
Source§impl<T> Separable for Twhere
T: Display,
impl<T> Separable for Twhere
T: Display,
Source§fn separate_by_policy(&self, policy: SeparatorPolicy<'_>) -> String
fn separate_by_policy(&self, policy: SeparatorPolicy<'_>) -> String
SeparatorPolicy. Read more