Skip to main content

xrpl/models/currency/
mpt_currency.rs

1use crate::models::transactions::mptoken_issuance_set::validate_mptoken_issuance_id;
2use crate::models::{Model, XRPLModelResult};
3use alloc::borrow::Cow;
4use serde::{Deserialize, Serialize};
5
6/// An MPT (Multi-Purpose Token) currency identifier.
7///
8/// Identifies a specific MPT issuance as a currency specifier, used
9/// in contexts where XRP or an issued currency could also appear.
10///
11/// JSON shape per XRPL (xrpl.js `MPTCurrency`):
12/// `{"mpt_issuance_id": "<48-hex>"}`
13///
14/// See MPTokenIssuance:
15/// `<https://xrpl.org/docs/references/protocol/ledger-data/ledger-entry-types/mptokenissuance>`
16#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Default)]
17pub struct MPTCurrency<'a> {
18    /// The MPTokenIssuanceID identifying this MPT. Must be a 48-character
19    /// ASCII hex string (24 bytes, Hash192).
20    pub mpt_issuance_id: Cow<'a, str>,
21}
22
23impl<'a> Model for MPTCurrency<'a> {
24    fn get_errors(&self) -> XRPLModelResult<()> {
25        validate_mptoken_issuance_id(self.mpt_issuance_id.as_ref())?;
26        Ok(())
27    }
28}
29
30impl<'a> MPTCurrency<'a> {
31    pub fn new(mpt_issuance_id: Cow<'a, str>) -> Self {
32        Self { mpt_issuance_id }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use crate::models::Model;
39
40    use super::*;
41
42    const VALID_ID: &str = "00000001A407AF5856CEFBF81F3D4A0000000000A407AF58";
43
44    #[test]
45    fn test_mpt_currency_serde_roundtrip() {
46        let cur = MPTCurrency::new(VALID_ID.into());
47        let json = serde_json::to_string(&cur).unwrap();
48        let decoded: MPTCurrency = serde_json::from_str(&json).unwrap();
49        assert_eq!(cur, decoded);
50    }
51
52    #[test]
53    fn test_mpt_currency_get_errors_valid() {
54        assert!(MPTCurrency::new(VALID_ID.into()).get_errors().is_ok());
55    }
56
57    #[test]
58    fn test_mpt_currency_get_errors_bad_id_too_short() {
59        assert!(MPTCurrency::new("XYZ".into()).get_errors().is_err());
60    }
61
62    #[test]
63    fn test_mpt_currency_get_errors_bad_id_non_hex() {
64        let bad_id = "Z".repeat(48);
65        assert!(MPTCurrency::new(bad_id.as_str().into())
66            .get_errors()
67            .is_err());
68    }
69}