Skip to main content

xrpl_common_stdlib/objects/generated/
mptoken.rs

1// GENERATED -- do not hand-edit. Run scripts/generate-ledger-objects.sh to regenerate.
2
3use crate::host::Result;
4use crate::objects::traits::CurrentLedgerObjectCommonFields;
5use crate::objects::traits::LedgerObjectCommonFields;
6use crate::objects::{current_ledger_object, ledger_object};
7use crate::sfield;
8use crate::types::account_id::AccountID;
9use crate::types::blob::StandardBlob;
10use crate::types::uint::{Hash192, Hash256};
11
12/// Trait providing access to fields specific to MPToken objects in any ledger.
13pub trait MPTokenFields: LedgerObjectCommonFields {
14    /// The owner (holder) of these MPTs.
15    fn account(&self) -> Result<AccountID> {
16        ledger_object::get_field(self.get_slot_num(), sfield::Account)
17    }
18
19    /// The `MPTokenIssuance` identifier.
20    fn mptoken_issuance_id(&self) -> Result<Hash192> {
21        ledger_object::get_field(self.get_slot_num(), sfield::MPTokenIssuanceID)
22    }
23
24    /// The amount of tokens currently held by the owner. The minimum is 0 and the maximum is
25    /// 2^63-1.
26    fn mpt_amount(&self) -> Result<Option<u64>> {
27        ledger_object::get_field_optional(self.get_slot_num(), sfield::MPTAmount)
28    }
29
30    /// The amount of tokens currently locked up (for example, in escrow).
31    fn locked_amount(&self) -> Result<Option<u64>> {
32        ledger_object::get_field_optional(self.get_slot_num(), sfield::LockedAmount)
33    }
34
35    /// A hint indicating which page of the owner directory links to this entry, in case the
36    /// directory consists of multiple pages.
37    fn owner_node(&self) -> Result<u64> {
38        ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
39    }
40
41    /// The identifying hash of the transaction that most recently modified this entry.
42    fn previous_txn_id(&self) -> Result<Hash256> {
43        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
44    }
45
46    /// The sequence of the ledger that contains the transaction that most recently modified this
47    /// object.
48    fn previous_txn_lgr_seq(&self) -> Result<u32> {
49        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
50    }
51
52    /// Encrypted inbox balance that receives incoming confidential transfers. Before it can be
53    /// spent, the holder must merge it into their spending balance using the
54    /// ConfidentialMPTMergeInbox transaction. Present when the holder has a confidential balance.
55    fn confidential_balance_inbox(&self) -> Result<Option<StandardBlob>> {
56        ledger_object::get_field_optional(self.get_slot_num(), sfield::ConfidentialBalanceInbox)
57    }
58
59    /// Encrypted spending balance used to generate proofs for outgoing transactions. Present when
60    /// the holder has a confidential balance.
61    fn confidential_balance_spending(&self) -> Result<Option<StandardBlob>> {
62        ledger_object::get_field_optional(self.get_slot_num(), sfield::ConfidentialBalanceSpending)
63    }
64
65    /// Version number that increments each time the spending balance changes. This version is
66    /// cryptographically bound to ZKPs in outgoing transactions to prevent replay attacks and
67    /// ensure proof validity. If the version changes between proof generation and submission, the
68    /// transaction will fail.
69    fn confidential_balance_version(&self) -> Result<Option<u32>> {
70        ledger_object::get_field_optional(self.get_slot_num(), sfield::ConfidentialBalanceVersion)
71    }
72
73    /// Copy of the holder's total confidential balance encrypted for the issuer to audit supply.
74    /// Present when the holder has a confidential balance.
75    fn issuer_encrypted_balance(&self) -> Result<Option<StandardBlob>> {
76        ledger_object::get_field_optional(self.get_slot_num(), sfield::IssuerEncryptedBalance)
77    }
78
79    /// The holder's total confidential balance encrypted under the auditor's key for independent
80    /// auditing. Only present if an auditor is configured.
81    fn auditor_encrypted_balance(&self) -> Result<Option<StandardBlob>> {
82        ledger_object::get_field_optional(self.get_slot_num(), sfield::AuditorEncryptedBalance)
83    }
84
85    /// The holder's ElGamal public key for confidential balances. Present when the holder has a
86    /// confidential balance.
87    fn holder_encryption_key(&self) -> Result<Option<StandardBlob>> {
88        ledger_object::get_field_optional(self.get_slot_num(), sfield::HolderEncryptionKey)
89    }
90}
91
92/// Trait providing access to fields specific to the current MPToken object.
93pub trait CurrentMPTokenFields: CurrentLedgerObjectCommonFields {
94    /// The owner (holder) of these MPTs.
95    fn account(&self) -> Result<AccountID> {
96        current_ledger_object::get_field(sfield::Account)
97    }
98
99    /// The `MPTokenIssuance` identifier.
100    fn mptoken_issuance_id(&self) -> Result<Hash192> {
101        current_ledger_object::get_field(sfield::MPTokenIssuanceID)
102    }
103
104    /// The amount of tokens currently held by the owner. The minimum is 0 and the maximum is
105    /// 2^63-1.
106    fn mpt_amount(&self) -> Result<Option<u64>> {
107        current_ledger_object::get_field_optional(sfield::MPTAmount)
108    }
109
110    /// The amount of tokens currently locked up (for example, in escrow).
111    fn locked_amount(&self) -> Result<Option<u64>> {
112        current_ledger_object::get_field_optional(sfield::LockedAmount)
113    }
114
115    /// A hint indicating which page of the owner directory links to this entry, in case the
116    /// directory consists of multiple pages.
117    fn owner_node(&self) -> Result<u64> {
118        current_ledger_object::get_field(sfield::OwnerNode)
119    }
120
121    /// The identifying hash of the transaction that most recently modified this entry.
122    fn previous_txn_id(&self) -> Result<Hash256> {
123        current_ledger_object::get_field(sfield::PreviousTxnID)
124    }
125
126    /// The sequence of the ledger that contains the transaction that most recently modified this
127    /// object.
128    fn previous_txn_lgr_seq(&self) -> Result<u32> {
129        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
130    }
131
132    /// Encrypted inbox balance that receives incoming confidential transfers. Before it can be
133    /// spent, the holder must merge it into their spending balance using the
134    /// ConfidentialMPTMergeInbox transaction. Present when the holder has a confidential balance.
135    fn confidential_balance_inbox(&self) -> Result<Option<StandardBlob>> {
136        current_ledger_object::get_field_optional(sfield::ConfidentialBalanceInbox)
137    }
138
139    /// Encrypted spending balance used to generate proofs for outgoing transactions. Present when
140    /// the holder has a confidential balance.
141    fn confidential_balance_spending(&self) -> Result<Option<StandardBlob>> {
142        current_ledger_object::get_field_optional(sfield::ConfidentialBalanceSpending)
143    }
144
145    /// Version number that increments each time the spending balance changes. This version is
146    /// cryptographically bound to ZKPs in outgoing transactions to prevent replay attacks and
147    /// ensure proof validity. If the version changes between proof generation and submission, the
148    /// transaction will fail.
149    fn confidential_balance_version(&self) -> Result<Option<u32>> {
150        current_ledger_object::get_field_optional(sfield::ConfidentialBalanceVersion)
151    }
152
153    /// Copy of the holder's total confidential balance encrypted for the issuer to audit supply.
154    /// Present when the holder has a confidential balance.
155    fn issuer_encrypted_balance(&self) -> Result<Option<StandardBlob>> {
156        current_ledger_object::get_field_optional(sfield::IssuerEncryptedBalance)
157    }
158
159    /// The holder's total confidential balance encrypted under the auditor's key for independent
160    /// auditing. Only present if an auditor is configured.
161    fn auditor_encrypted_balance(&self) -> Result<Option<StandardBlob>> {
162        current_ledger_object::get_field_optional(sfield::AuditorEncryptedBalance)
163    }
164
165    /// The holder's ElGamal public key for confidential balances. Present when the holder has a
166    /// confidential balance.
167    fn holder_encryption_key(&self) -> Result<Option<StandardBlob>> {
168        current_ledger_object::get_field_optional(sfield::HolderEncryptionKey)
169    }
170}
171
172#[derive(Debug, Clone, Copy, Eq, PartialEq)]
173pub struct MPToken {
174    pub(crate) slot_num: i32,
175}
176
177impl MPToken {
178    /// Binds this handle to a host-managed slot holding a MPToken ledger object.
179    pub fn new(slot_num: i32) -> Self {
180        Self { slot_num }
181    }
182}
183
184impl LedgerObjectCommonFields for MPToken {
185    fn get_slot_num(&self) -> i32 {
186        self.slot_num
187    }
188}
189
190impl MPTokenFields for MPToken {}
191
192#[cfg(test)]
193mod tests {
194    use super::*;
195    use crate::host::host_bindings_trait::MockHostBindings;
196    use crate::host::setup_mock;
197    use crate::objects::test_utils::*;
198
199    #[test]
200    fn read_all_fields() {
201        let mut mock = MockHostBindings::new();
202        mock_all_fields_present(&mut mock);
203        let _guard = setup_mock(mock);
204
205        let obj = MPToken::new(0);
206
207        assert!(obj.account().is_ok());
208        assert!(obj.mptoken_issuance_id().is_ok());
209        assert!(obj.owner_node().is_ok());
210        assert!(obj.previous_txn_id().is_ok());
211        assert!(obj.previous_txn_lgr_seq().is_ok());
212        assert!(obj.mpt_amount().is_ok());
213        assert!(obj.locked_amount().is_ok());
214        assert!(obj.confidential_balance_inbox().is_ok());
215        assert!(obj.confidential_balance_spending().is_ok());
216        assert!(obj.confidential_balance_version().is_ok());
217        assert!(obj.issuer_encrypted_balance().is_ok());
218        assert!(obj.auditor_encrypted_balance().is_ok());
219        assert!(obj.holder_encryption_key().is_ok());
220    }
221
222    #[test]
223    fn optional_fields_none() {
224        let mut mock = MockHostBindings::new();
225        mock_all_fields_not_found(&mut mock);
226        let _guard = setup_mock(mock);
227
228        let obj = MPToken::new(0);
229
230        assert!(obj.mpt_amount().unwrap().is_none());
231        assert!(obj.locked_amount().unwrap().is_none());
232        assert!(obj.confidential_balance_version().unwrap().is_none());
233    }
234}