Skip to main content

xrpl_common_stdlib/objects/generated/
vault.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::issue::Issue;
11use crate::types::number::Number;
12use crate::types::uint::{Hash192, Hash256};
13
14/// Trait providing access to fields specific to Vault objects in any ledger.
15pub trait VaultFields: LedgerObjectCommonFields {
16    /// Identifies the transaction ID that most recently modified this object.
17    fn previous_txn_id(&self) -> Result<Hash256> {
18        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
19    }
20
21    /// The sequence of the ledger that contains the transaction that most recently modified this
22    /// object.
23    fn previous_txn_lgr_seq(&self) -> Result<u32> {
24        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
25    }
26
27    /// The transaction sequence number that created the vault.
28    fn sequence(&self) -> Result<u32> {
29        ledger_object::get_field(self.get_slot_num(), sfield::Sequence)
30    }
31
32    /// Identifies the page where this item is referenced in the owner's directory.
33    fn owner_node(&self) -> Result<u64> {
34        ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
35    }
36
37    /// The account address of the Vault Owner.
38    fn owner(&self) -> Result<AccountID> {
39        ledger_object::get_field(self.get_slot_num(), sfield::Owner)
40    }
41
42    /// The address of the vault's pseudo-account.
43    fn account(&self) -> Result<AccountID> {
44        ledger_object::get_field(self.get_slot_num(), sfield::Account)
45    }
46
47    /// Arbitrary metadata, in hex format, about the vault. Limited to 256 bytes. See Data Field
48    /// Format for more information.
49    fn data(&self) -> Result<Option<StandardBlob>> {
50        ledger_object::get_field_optional(self.get_slot_num(), sfield::Data)
51    }
52
53    /// The asset of the vault. The vault supports XRP, trust line tokens, and MPTs.
54    fn asset(&self) -> Result<Issue> {
55        ledger_object::get_field(self.get_slot_num(), sfield::Asset)
56    }
57
58    /// The total value of the vault.
59    fn assets_total(&self) -> Result<Option<Number>> {
60        ledger_object::get_field_optional(self.get_slot_num(), sfield::AssetsTotal)
61    }
62
63    /// The asset amount that is available in the vault.
64    fn assets_available(&self) -> Result<Option<Number>> {
65        ledger_object::get_field_optional(self.get_slot_num(), sfield::AssetsAvailable)
66    }
67
68    /// The maximum asset amount that can be held in the vault. If set to 0, this indicates there is
69    /// no cap.
70    fn assets_maximum(&self) -> Result<Option<Number>> {
71        ledger_object::get_field_optional(self.get_slot_num(), sfield::AssetsMaximum)
72    }
73
74    /// The potential loss amount that is not yet realized, expressed as the vault's asset. Only a
75    /// protocol connected to the vault can modify this attribute.
76    fn loss_unrealized(&self) -> Result<Option<Number>> {
77        ledger_object::get_field_optional(self.get_slot_num(), sfield::LossUnrealized)
78    }
79
80    /// The identifier of the share `MPTokenIssuance` object.
81    fn share_mpt_id(&self) -> Result<Hash192> {
82        ledger_object::get_field(self.get_slot_num(), sfield::ShareMPTID)
83    }
84
85    /// Indicates the withdrawal strategy used by the vault.
86    fn withdrawal_policy(&self) -> Result<u8> {
87        ledger_object::get_field(self.get_slot_num(), sfield::WithdrawalPolicy)
88    }
89
90    /// Specifies decimal precision for share calculations. Assets are multiplied by 10^Scale to
91    /// convert fractional amounts into whole number shares. For example, with a `Scale` of `6`,
92    /// depositing 20.3 units creates 20,300,000 shares (20.3 × 10^Scale). For trust line tokens
93    /// this can be configured at vault creation, and valid values are between 0-18, with the
94    /// default being `6`. For XRP and MPTs, this is fixed at `0`. See Scaling Factor for more
95    /// information.
96    fn scale(&self) -> Result<Option<u8>> {
97        ledger_object::get_field_optional(self.get_slot_num(), sfield::Scale)
98    }
99
100    /// The LEVersion field (Optional).
101    fn le_version(&self) -> Result<Option<u8>> {
102        ledger_object::get_field_optional(self.get_slot_num(), sfield::LEVersion)
103    }
104}
105
106/// Trait providing access to fields specific to the current Vault object.
107pub trait CurrentVaultFields: CurrentLedgerObjectCommonFields {
108    /// Identifies the transaction ID that most recently modified this object.
109    fn previous_txn_id(&self) -> Result<Hash256> {
110        current_ledger_object::get_field(sfield::PreviousTxnID)
111    }
112
113    /// The sequence of the ledger that contains the transaction that most recently modified this
114    /// object.
115    fn previous_txn_lgr_seq(&self) -> Result<u32> {
116        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
117    }
118
119    /// The transaction sequence number that created the vault.
120    fn sequence(&self) -> Result<u32> {
121        current_ledger_object::get_field(sfield::Sequence)
122    }
123
124    /// Identifies the page where this item is referenced in the owner's directory.
125    fn owner_node(&self) -> Result<u64> {
126        current_ledger_object::get_field(sfield::OwnerNode)
127    }
128
129    /// The account address of the Vault Owner.
130    fn owner(&self) -> Result<AccountID> {
131        current_ledger_object::get_field(sfield::Owner)
132    }
133
134    /// The address of the vault's pseudo-account.
135    fn account(&self) -> Result<AccountID> {
136        current_ledger_object::get_field(sfield::Account)
137    }
138
139    /// Arbitrary metadata, in hex format, about the vault. Limited to 256 bytes. See Data Field
140    /// Format for more information.
141    fn data(&self) -> Result<Option<StandardBlob>> {
142        current_ledger_object::get_field_optional(sfield::Data)
143    }
144
145    /// The asset of the vault. The vault supports XRP, trust line tokens, and MPTs.
146    fn asset(&self) -> Result<Issue> {
147        current_ledger_object::get_field(sfield::Asset)
148    }
149
150    /// The total value of the vault.
151    fn assets_total(&self) -> Result<Option<Number>> {
152        current_ledger_object::get_field_optional(sfield::AssetsTotal)
153    }
154
155    /// The asset amount that is available in the vault.
156    fn assets_available(&self) -> Result<Option<Number>> {
157        current_ledger_object::get_field_optional(sfield::AssetsAvailable)
158    }
159
160    /// The maximum asset amount that can be held in the vault. If set to 0, this indicates there is
161    /// no cap.
162    fn assets_maximum(&self) -> Result<Option<Number>> {
163        current_ledger_object::get_field_optional(sfield::AssetsMaximum)
164    }
165
166    /// The potential loss amount that is not yet realized, expressed as the vault's asset. Only a
167    /// protocol connected to the vault can modify this attribute.
168    fn loss_unrealized(&self) -> Result<Option<Number>> {
169        current_ledger_object::get_field_optional(sfield::LossUnrealized)
170    }
171
172    /// The identifier of the share `MPTokenIssuance` object.
173    fn share_mpt_id(&self) -> Result<Hash192> {
174        current_ledger_object::get_field(sfield::ShareMPTID)
175    }
176
177    /// Indicates the withdrawal strategy used by the vault.
178    fn withdrawal_policy(&self) -> Result<u8> {
179        current_ledger_object::get_field(sfield::WithdrawalPolicy)
180    }
181
182    /// Specifies decimal precision for share calculations. Assets are multiplied by 10^Scale to
183    /// convert fractional amounts into whole number shares. For example, with a `Scale` of `6`,
184    /// depositing 20.3 units creates 20,300,000 shares (20.3 × 10^Scale). For trust line tokens
185    /// this can be configured at vault creation, and valid values are between 0-18, with the
186    /// default being `6`. For XRP and MPTs, this is fixed at `0`. See Scaling Factor for more
187    /// information.
188    fn scale(&self) -> Result<Option<u8>> {
189        current_ledger_object::get_field_optional(sfield::Scale)
190    }
191
192    /// The LEVersion field (Optional).
193    fn le_version(&self) -> Result<Option<u8>> {
194        current_ledger_object::get_field_optional(sfield::LEVersion)
195    }
196}
197
198#[derive(Debug, Clone, Copy, Eq, PartialEq)]
199pub struct Vault {
200    pub(crate) slot_num: i32,
201}
202
203impl Vault {
204    /// Binds this handle to a host-managed slot holding a Vault ledger object.
205    pub fn new(slot_num: i32) -> Self {
206        Self { slot_num }
207    }
208}
209
210impl LedgerObjectCommonFields for Vault {
211    fn get_slot_num(&self) -> i32 {
212        self.slot_num
213    }
214}
215
216impl VaultFields for Vault {}
217
218#[cfg(test)]
219mod tests {
220    use super::*;
221    use crate::host::host_bindings_trait::MockHostBindings;
222    use crate::host::setup_mock;
223    use crate::objects::test_utils::*;
224
225    #[test]
226    fn read_all_fields() {
227        let mut mock = MockHostBindings::new();
228        mock_all_fields_present(&mut mock);
229        let _guard = setup_mock(mock);
230
231        let obj = Vault::new(0);
232
233        assert!(obj.previous_txn_id().is_ok());
234        assert!(obj.previous_txn_lgr_seq().is_ok());
235        assert!(obj.sequence().is_ok());
236        assert!(obj.owner_node().is_ok());
237        assert!(obj.owner().is_ok());
238        assert!(obj.account().is_ok());
239        assert!(obj.asset().is_ok());
240        assert!(obj.share_mpt_id().is_ok());
241        assert!(obj.withdrawal_policy().is_ok());
242        assert!(obj.data().is_ok());
243        assert!(obj.assets_total().is_ok());
244        assert!(obj.assets_available().is_ok());
245        assert!(obj.assets_maximum().is_ok());
246        assert!(obj.loss_unrealized().is_ok());
247        assert!(obj.scale().is_ok());
248        assert!(obj.le_version().is_ok());
249    }
250
251    #[test]
252    fn optional_fields_none() {
253        let mut mock = MockHostBindings::new();
254        mock_all_fields_not_found(&mut mock);
255        let _guard = setup_mock(mock);
256
257        let obj = Vault::new(0);
258
259        assert!(obj.scale().unwrap().is_none());
260        assert!(obj.le_version().unwrap().is_none());
261    }
262}