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. Calculated as: `assets available + assets on
59    /// loan`.<ul><li>_Cash-basis vaults_: Potential interest from scheduled, unpaid loans doesn't
60    /// count toward the total. </li><li>_Instant interest recognition vaults_: Potential interest
61    /// from scheduled, unpaid loans does count toward the total.</li></ul>
62    fn assets_total(&self) -> Result<Option<Number>> {
63        ledger_object::get_field_optional(self.get_slot_num(), sfield::AssetsTotal)
64    }
65
66    /// The amount of assets available for loans and withdrawals.
67    fn assets_available(&self) -> Result<Option<Number>> {
68        ledger_object::get_field_optional(self.get_slot_num(), sfield::AssetsAvailable)
69    }
70
71    /// The maximum amount of assets that can be deposited into the vault. Set to `0` for no cap.
72    fn assets_maximum(&self) -> Result<Option<Number>> {
73        ledger_object::get_field_optional(self.get_slot_num(), sfield::AssetsMaximum)
74    }
75
76    /// The potential loss amount that is not yet realized, expressed as the vault's asset. Only a
77    /// protocol connected to the vault can modify this attribute.<ul><li>_Cash-basis vaults_:
78    /// Unrealized losses from interest aren't included in this value. </li><li>_Instant interest
79    /// recognition vaults_: Unrealized losses from interest are included in this value.</li></ul>
80    fn loss_unrealized(&self) -> Result<Option<Number>> {
81        ledger_object::get_field_optional(self.get_slot_num(), sfield::LossUnrealized)
82    }
83
84    /// The identifier of the share `MPTokenIssuance` object.
85    fn share_mpt_id(&self) -> Result<Hash192> {
86        ledger_object::get_field(self.get_slot_num(), sfield::ShareMPTID)
87    }
88
89    /// Indicates the withdrawal strategy used by the vault.
90    fn withdrawal_policy(&self) -> Result<u8> {
91        ledger_object::get_field(self.get_slot_num(), sfield::WithdrawalPolicy)
92    }
93
94    /// Specifies decimal precision for share calculations. Assets are multiplied by 10^Scale to
95    /// convert fractional amounts into whole number shares. For example, with a `Scale` of `6`,
96    /// depositing 20.3 units creates 20,300,000 shares (20.3 × 10^Scale). For trust line tokens
97    /// this can be configured at vault creation, and valid values are between 0-18, with the
98    /// default being `6`. For XRP and MPTs, this is fixed at `0`. See Scaling Factor for more
99    /// information.
100    fn scale(&self) -> Result<Option<u8>> {
101        ledger_object::get_field_optional(self.get_slot_num(), sfield::Scale)
102    }
103
104    /// Indicates what type of accounting the vault uses. `1` indicates the vault uses cash-basis
105    /// accounting. If this field is omitted, the vault uses instant interest recognition
106    /// accounting.
107    fn le_version(&self) -> Result<Option<u8>> {
108        ledger_object::get_field_optional(self.get_slot_num(), sfield::LEVersion)
109    }
110
111    /// Indicates the kind of vault. `1` is a closed-ended vault. If this field is omitted, it's an
112    /// open-ended vault.
113    fn vault_kind(&self) -> Result<Option<u8>> {
114        ledger_object::get_field_optional(self.get_slot_num(), sfield::VaultKind)
115    }
116
117    /// _(Closed-ended vaults only)_ The time, in seconds since the Ripple Epoch, when the vault's
118    /// subscription window closes and its investment period begins.
119    fn subscription_date(&self) -> Result<Option<u32>> {
120        ledger_object::get_field_optional(self.get_slot_num(), sfield::SubscriptionDate)
121    }
122
123    /// _(Closed-ended vaults only)_ The time, in seconds since the Ripple Epoch, when the vault's
124    /// investment period ends and depositors can redeem their shares.
125    fn redemption_date(&self) -> Result<Option<u32>> {
126        ledger_object::get_field_optional(self.get_slot_num(), sfield::RedemptionDate)
127    }
128}
129
130/// Trait providing access to fields specific to the current Vault object.
131pub trait CurrentVaultFields: CurrentLedgerObjectCommonFields {
132    /// Identifies the transaction ID that most recently modified this object.
133    fn previous_txn_id(&self) -> Result<Hash256> {
134        current_ledger_object::get_field(sfield::PreviousTxnID)
135    }
136
137    /// The sequence of the ledger that contains the transaction that most recently modified this
138    /// object.
139    fn previous_txn_lgr_seq(&self) -> Result<u32> {
140        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
141    }
142
143    /// The transaction sequence number that created the vault.
144    fn sequence(&self) -> Result<u32> {
145        current_ledger_object::get_field(sfield::Sequence)
146    }
147
148    /// Identifies the page where this item is referenced in the owner's directory.
149    fn owner_node(&self) -> Result<u64> {
150        current_ledger_object::get_field(sfield::OwnerNode)
151    }
152
153    /// The account address of the Vault Owner.
154    fn owner(&self) -> Result<AccountID> {
155        current_ledger_object::get_field(sfield::Owner)
156    }
157
158    /// The address of the vault's pseudo-account.
159    fn account(&self) -> Result<AccountID> {
160        current_ledger_object::get_field(sfield::Account)
161    }
162
163    /// Arbitrary metadata, in hex format, about the vault. Limited to 256 bytes. See Data Field
164    /// Format for more information.
165    fn data(&self) -> Result<Option<StandardBlob>> {
166        current_ledger_object::get_field_optional(sfield::Data)
167    }
168
169    /// The asset of the vault. The vault supports XRP, trust line tokens, and MPTs.
170    fn asset(&self) -> Result<Issue> {
171        current_ledger_object::get_field(sfield::Asset)
172    }
173
174    /// The total value of the vault. Calculated as: `assets available + assets on
175    /// loan`.<ul><li>_Cash-basis vaults_: Potential interest from scheduled, unpaid loans doesn't
176    /// count toward the total. </li><li>_Instant interest recognition vaults_: Potential interest
177    /// from scheduled, unpaid loans does count toward the total.</li></ul>
178    fn assets_total(&self) -> Result<Option<Number>> {
179        current_ledger_object::get_field_optional(sfield::AssetsTotal)
180    }
181
182    /// The amount of assets available for loans and withdrawals.
183    fn assets_available(&self) -> Result<Option<Number>> {
184        current_ledger_object::get_field_optional(sfield::AssetsAvailable)
185    }
186
187    /// The maximum amount of assets that can be deposited into the vault. Set to `0` for no cap.
188    fn assets_maximum(&self) -> Result<Option<Number>> {
189        current_ledger_object::get_field_optional(sfield::AssetsMaximum)
190    }
191
192    /// The potential loss amount that is not yet realized, expressed as the vault's asset. Only a
193    /// protocol connected to the vault can modify this attribute.<ul><li>_Cash-basis vaults_:
194    /// Unrealized losses from interest aren't included in this value. </li><li>_Instant interest
195    /// recognition vaults_: Unrealized losses from interest are included in this value.</li></ul>
196    fn loss_unrealized(&self) -> Result<Option<Number>> {
197        current_ledger_object::get_field_optional(sfield::LossUnrealized)
198    }
199
200    /// The identifier of the share `MPTokenIssuance` object.
201    fn share_mpt_id(&self) -> Result<Hash192> {
202        current_ledger_object::get_field(sfield::ShareMPTID)
203    }
204
205    /// Indicates the withdrawal strategy used by the vault.
206    fn withdrawal_policy(&self) -> Result<u8> {
207        current_ledger_object::get_field(sfield::WithdrawalPolicy)
208    }
209
210    /// Specifies decimal precision for share calculations. Assets are multiplied by 10^Scale to
211    /// convert fractional amounts into whole number shares. For example, with a `Scale` of `6`,
212    /// depositing 20.3 units creates 20,300,000 shares (20.3 × 10^Scale). For trust line tokens
213    /// this can be configured at vault creation, and valid values are between 0-18, with the
214    /// default being `6`. For XRP and MPTs, this is fixed at `0`. See Scaling Factor for more
215    /// information.
216    fn scale(&self) -> Result<Option<u8>> {
217        current_ledger_object::get_field_optional(sfield::Scale)
218    }
219
220    /// Indicates what type of accounting the vault uses. `1` indicates the vault uses cash-basis
221    /// accounting. If this field is omitted, the vault uses instant interest recognition
222    /// accounting.
223    fn le_version(&self) -> Result<Option<u8>> {
224        current_ledger_object::get_field_optional(sfield::LEVersion)
225    }
226
227    /// Indicates the kind of vault. `1` is a closed-ended vault. If this field is omitted, it's an
228    /// open-ended vault.
229    fn vault_kind(&self) -> Result<Option<u8>> {
230        current_ledger_object::get_field_optional(sfield::VaultKind)
231    }
232
233    /// _(Closed-ended vaults only)_ The time, in seconds since the Ripple Epoch, when the vault's
234    /// subscription window closes and its investment period begins.
235    fn subscription_date(&self) -> Result<Option<u32>> {
236        current_ledger_object::get_field_optional(sfield::SubscriptionDate)
237    }
238
239    /// _(Closed-ended vaults only)_ The time, in seconds since the Ripple Epoch, when the vault's
240    /// investment period ends and depositors can redeem their shares.
241    fn redemption_date(&self) -> Result<Option<u32>> {
242        current_ledger_object::get_field_optional(sfield::RedemptionDate)
243    }
244}
245
246#[derive(Debug, Clone, Copy, Eq, PartialEq)]
247pub struct Vault {
248    pub(crate) slot_num: i32,
249}
250
251impl Vault {
252    /// Binds this handle to a host-managed slot holding a Vault ledger object.
253    pub fn new(slot_num: i32) -> Self {
254        Self { slot_num }
255    }
256}
257
258impl LedgerObjectCommonFields for Vault {
259    fn get_slot_num(&self) -> i32 {
260        self.slot_num
261    }
262}
263
264impl VaultFields for Vault {}
265
266#[cfg(test)]
267mod tests {
268    use super::*;
269    use crate::host::host_bindings_trait::MockHostBindings;
270    use crate::host::setup_mock;
271    use crate::objects::test_utils::*;
272
273    #[test]
274    fn read_all_fields() {
275        let mut mock = MockHostBindings::new();
276        mock_all_fields_present(&mut mock);
277        let _guard = setup_mock(mock);
278
279        let obj = Vault::new(0);
280
281        assert!(obj.previous_txn_id().is_ok());
282        assert!(obj.previous_txn_lgr_seq().is_ok());
283        assert!(obj.sequence().is_ok());
284        assert!(obj.owner_node().is_ok());
285        assert!(obj.owner().is_ok());
286        assert!(obj.account().is_ok());
287        assert!(obj.asset().is_ok());
288        assert!(obj.share_mpt_id().is_ok());
289        assert!(obj.withdrawal_policy().is_ok());
290        assert!(obj.data().is_ok());
291        assert!(obj.assets_total().is_ok());
292        assert!(obj.assets_available().is_ok());
293        assert!(obj.assets_maximum().is_ok());
294        assert!(obj.loss_unrealized().is_ok());
295        assert!(obj.scale().is_ok());
296        assert!(obj.le_version().is_ok());
297        assert!(obj.vault_kind().is_ok());
298        assert!(obj.subscription_date().is_ok());
299        assert!(obj.redemption_date().is_ok());
300    }
301
302    #[test]
303    fn optional_fields_none() {
304        let mut mock = MockHostBindings::new();
305        mock_all_fields_not_found(&mut mock);
306        let _guard = setup_mock(mock);
307
308        let obj = Vault::new(0);
309
310        assert!(obj.scale().unwrap().is_none());
311        assert!(obj.le_version().unwrap().is_none());
312        assert!(obj.vault_kind().unwrap().is_none());
313        assert!(obj.subscription_date().unwrap().is_none());
314        assert!(obj.redemption_date().unwrap().is_none());
315    }
316}