Skip to main content

xrpl_common_stdlib/objects/generated/
loan.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::number::Number;
10use crate::types::uint::Hash256;
11
12/// Trait providing access to fields specific to Loan objects in any ledger.
13pub trait LoanFields: LedgerObjectCommonFields {
14    /// Identifies the transaction ID that most recently modified this object.
15    fn previous_txn_id(&self) -> Result<Hash256> {
16        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
17    }
18
19    /// The sequence of the ledger that contains the transaction that most recently modified this
20    /// object.
21    fn previous_txn_lgr_seq(&self) -> Result<u32> {
22        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
23    }
24
25    /// Identifies the page where this item is referenced in the owner's directory.
26    fn owner_node(&self) -> Result<u64> {
27        ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
28    }
29
30    /// Identifies the page where this item is referenced in the `LoanBroker` owner directory.
31    fn loan_broker_node(&self) -> Result<u64> {
32        ledger_object::get_field(self.get_slot_num(), sfield::LoanBrokerNode)
33    }
34
35    /// The ID of the _Loan Broker_ associated with this loan.
36    fn loan_broker_id(&self) -> Result<Hash256> {
37        ledger_object::get_field(self.get_slot_num(), sfield::LoanBrokerID)
38    }
39
40    /// The sequence number of the loan.
41    fn loan_sequence(&self) -> Result<u32> {
42        ledger_object::get_field(self.get_slot_num(), sfield::LoanSequence)
43    }
44
45    /// The account address of the _Borrower_.
46    fn borrower(&self) -> Result<AccountID> {
47        ledger_object::get_field(self.get_slot_num(), sfield::Borrower)
48    }
49
50    /// The amount paid to the _Loan Broker_, taken from the principal loan at creation.
51    fn loan_origination_fee(&self) -> Result<Option<Number>> {
52        ledger_object::get_field_optional(self.get_slot_num(), sfield::LoanOriginationFee)
53    }
54
55    /// The amount paid to the _Loan Broker_ with each loan payment.
56    fn loan_service_fee(&self) -> Result<Option<Number>> {
57        ledger_object::get_field_optional(self.get_slot_num(), sfield::LoanServiceFee)
58    }
59
60    /// The amount paid to the _Loan Broker_ for each late payment.
61    fn late_payment_fee(&self) -> Result<Option<Number>> {
62        ledger_object::get_field_optional(self.get_slot_num(), sfield::LatePaymentFee)
63    }
64
65    /// The amount paid to the _Loan Broker_ when a full early payment is made.
66    fn close_payment_fee(&self) -> Result<Option<Number>> {
67        ledger_object::get_field_optional(self.get_slot_num(), sfield::ClosePaymentFee)
68    }
69
70    /// The fee charged on overpayments, in units of 1/10th basis points. Valid values are 0 to
71    /// 100000 (inclusive), representing 0% to 100%.
72    fn overpayment_fee(&self) -> Result<Option<u32>> {
73        ledger_object::get_field_optional(self.get_slot_num(), sfield::OverpaymentFee)
74    }
75
76    /// The annualized interest rate of the loan, in 1/10th basis points.
77    fn interest_rate(&self) -> Result<Option<u32>> {
78        ledger_object::get_field_optional(self.get_slot_num(), sfield::InterestRate)
79    }
80
81    /// The premium added to the interest rate for late payments, in units of 1/10th basis points.
82    /// Valid values are 0 to 100000 (inclusive), representing 0% to 100%.
83    fn late_interest_rate(&self) -> Result<Option<u32>> {
84        ledger_object::get_field_optional(self.get_slot_num(), sfield::LateInterestRate)
85    }
86
87    /// The interest rate charged for repaying the loan early, in units of 1/10th basis points.
88    /// Valid values are 0 to 100000 (inclusive), representing 0% to 100%.
89    fn close_interest_rate(&self) -> Result<Option<u32>> {
90        ledger_object::get_field_optional(self.get_slot_num(), sfield::CloseInterestRate)
91    }
92
93    /// The interest rate charged on overpayments, in units of 1/10th basis points. Valid values are
94    /// 0 to 100000 (inclusive), representing 0% to 100%.
95    fn overpayment_interest_rate(&self) -> Result<Option<u32>> {
96        ledger_object::get_field_optional(self.get_slot_num(), sfield::OverpaymentInterestRate)
97    }
98
99    /// The timestamp of when the loan started, in seconds since the Ripple Epoch.
100    fn start_date(&self) -> Result<u32> {
101        ledger_object::get_field(self.get_slot_num(), sfield::StartDate)
102    }
103
104    /// The number of seconds between loan payments.
105    fn payment_interval(&self) -> Result<u32> {
106        ledger_object::get_field(self.get_slot_num(), sfield::PaymentInterval)
107    }
108
109    /// The number of seconds after a loan payment is due before the loan defaults.
110    fn grace_period(&self) -> Result<Option<u32>> {
111        ledger_object::get_field_optional(self.get_slot_num(), sfield::GracePeriod)
112    }
113
114    /// The timestamp of when the previous payment was made, in seconds since the Ripple Epoch.
115    fn previous_payment_due_date(&self) -> Result<Option<u32>> {
116        ledger_object::get_field_optional(self.get_slot_num(), sfield::PreviousPaymentDueDate)
117    }
118
119    /// The timestamp of when the next payment is due, in seconds since the Ripple Epoch.
120    fn next_payment_due_date(&self) -> Result<Option<u32>> {
121        ledger_object::get_field_optional(self.get_slot_num(), sfield::NextPaymentDueDate)
122    }
123
124    /// The number of payments remaining on the loan.
125    fn payment_remaining(&self) -> Result<Option<u32>> {
126        ledger_object::get_field_optional(self.get_slot_num(), sfield::PaymentRemaining)
127    }
128
129    /// The amount due for each payment interval.
130    fn periodic_payment(&self) -> Result<Number> {
131        ledger_object::get_field(self.get_slot_num(), sfield::PeriodicPayment)
132    }
133
134    /// The principal amount still owed on the loan.
135    fn principal_outstanding(&self) -> Result<Option<Number>> {
136        ledger_object::get_field_optional(self.get_slot_num(), sfield::PrincipalOutstanding)
137    }
138
139    /// The total amount owed on the loan, including remaining principal and fees.
140    fn total_value_outstanding(&self) -> Result<Option<Number>> {
141        ledger_object::get_field_optional(self.get_slot_num(), sfield::TotalValueOutstanding)
142    }
143
144    /// The remaining management fee owed to the loan broker.
145    fn management_fee_outstanding(&self) -> Result<Option<Number>> {
146        ledger_object::get_field_optional(self.get_slot_num(), sfield::ManagementFeeOutstanding)
147    }
148
149    /// The scale factor that ensures all computed amounts are rounded to the same number of decimal
150    /// places. It is based on the total loan value at creation time.
151    fn loan_scale(&self) -> Result<Option<i32>> {
152        ledger_object::get_field_optional(self.get_slot_num(), sfield::LoanScale)
153    }
154}
155
156/// Trait providing access to fields specific to the current Loan object.
157pub trait CurrentLoanFields: CurrentLedgerObjectCommonFields {
158    /// Identifies the transaction ID that most recently modified this object.
159    fn previous_txn_id(&self) -> Result<Hash256> {
160        current_ledger_object::get_field(sfield::PreviousTxnID)
161    }
162
163    /// The sequence of the ledger that contains the transaction that most recently modified this
164    /// object.
165    fn previous_txn_lgr_seq(&self) -> Result<u32> {
166        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
167    }
168
169    /// Identifies the page where this item is referenced in the owner's directory.
170    fn owner_node(&self) -> Result<u64> {
171        current_ledger_object::get_field(sfield::OwnerNode)
172    }
173
174    /// Identifies the page where this item is referenced in the `LoanBroker` owner directory.
175    fn loan_broker_node(&self) -> Result<u64> {
176        current_ledger_object::get_field(sfield::LoanBrokerNode)
177    }
178
179    /// The ID of the _Loan Broker_ associated with this loan.
180    fn loan_broker_id(&self) -> Result<Hash256> {
181        current_ledger_object::get_field(sfield::LoanBrokerID)
182    }
183
184    /// The sequence number of the loan.
185    fn loan_sequence(&self) -> Result<u32> {
186        current_ledger_object::get_field(sfield::LoanSequence)
187    }
188
189    /// The account address of the _Borrower_.
190    fn borrower(&self) -> Result<AccountID> {
191        current_ledger_object::get_field(sfield::Borrower)
192    }
193
194    /// The amount paid to the _Loan Broker_, taken from the principal loan at creation.
195    fn loan_origination_fee(&self) -> Result<Option<Number>> {
196        current_ledger_object::get_field_optional(sfield::LoanOriginationFee)
197    }
198
199    /// The amount paid to the _Loan Broker_ with each loan payment.
200    fn loan_service_fee(&self) -> Result<Option<Number>> {
201        current_ledger_object::get_field_optional(sfield::LoanServiceFee)
202    }
203
204    /// The amount paid to the _Loan Broker_ for each late payment.
205    fn late_payment_fee(&self) -> Result<Option<Number>> {
206        current_ledger_object::get_field_optional(sfield::LatePaymentFee)
207    }
208
209    /// The amount paid to the _Loan Broker_ when a full early payment is made.
210    fn close_payment_fee(&self) -> Result<Option<Number>> {
211        current_ledger_object::get_field_optional(sfield::ClosePaymentFee)
212    }
213
214    /// The fee charged on overpayments, in units of 1/10th basis points. Valid values are 0 to
215    /// 100000 (inclusive), representing 0% to 100%.
216    fn overpayment_fee(&self) -> Result<Option<u32>> {
217        current_ledger_object::get_field_optional(sfield::OverpaymentFee)
218    }
219
220    /// The annualized interest rate of the loan, in 1/10th basis points.
221    fn interest_rate(&self) -> Result<Option<u32>> {
222        current_ledger_object::get_field_optional(sfield::InterestRate)
223    }
224
225    /// The premium added to the interest rate for late payments, in units of 1/10th basis points.
226    /// Valid values are 0 to 100000 (inclusive), representing 0% to 100%.
227    fn late_interest_rate(&self) -> Result<Option<u32>> {
228        current_ledger_object::get_field_optional(sfield::LateInterestRate)
229    }
230
231    /// The interest rate charged for repaying the loan early, in units of 1/10th basis points.
232    /// Valid values are 0 to 100000 (inclusive), representing 0% to 100%.
233    fn close_interest_rate(&self) -> Result<Option<u32>> {
234        current_ledger_object::get_field_optional(sfield::CloseInterestRate)
235    }
236
237    /// The interest rate charged on overpayments, in units of 1/10th basis points. Valid values are
238    /// 0 to 100000 (inclusive), representing 0% to 100%.
239    fn overpayment_interest_rate(&self) -> Result<Option<u32>> {
240        current_ledger_object::get_field_optional(sfield::OverpaymentInterestRate)
241    }
242
243    /// The timestamp of when the loan started, in seconds since the Ripple Epoch.
244    fn start_date(&self) -> Result<u32> {
245        current_ledger_object::get_field(sfield::StartDate)
246    }
247
248    /// The number of seconds between loan payments.
249    fn payment_interval(&self) -> Result<u32> {
250        current_ledger_object::get_field(sfield::PaymentInterval)
251    }
252
253    /// The number of seconds after a loan payment is due before the loan defaults.
254    fn grace_period(&self) -> Result<Option<u32>> {
255        current_ledger_object::get_field_optional(sfield::GracePeriod)
256    }
257
258    /// The timestamp of when the previous payment was made, in seconds since the Ripple Epoch.
259    fn previous_payment_due_date(&self) -> Result<Option<u32>> {
260        current_ledger_object::get_field_optional(sfield::PreviousPaymentDueDate)
261    }
262
263    /// The timestamp of when the next payment is due, in seconds since the Ripple Epoch.
264    fn next_payment_due_date(&self) -> Result<Option<u32>> {
265        current_ledger_object::get_field_optional(sfield::NextPaymentDueDate)
266    }
267
268    /// The number of payments remaining on the loan.
269    fn payment_remaining(&self) -> Result<Option<u32>> {
270        current_ledger_object::get_field_optional(sfield::PaymentRemaining)
271    }
272
273    /// The amount due for each payment interval.
274    fn periodic_payment(&self) -> Result<Number> {
275        current_ledger_object::get_field(sfield::PeriodicPayment)
276    }
277
278    /// The principal amount still owed on the loan.
279    fn principal_outstanding(&self) -> Result<Option<Number>> {
280        current_ledger_object::get_field_optional(sfield::PrincipalOutstanding)
281    }
282
283    /// The total amount owed on the loan, including remaining principal and fees.
284    fn total_value_outstanding(&self) -> Result<Option<Number>> {
285        current_ledger_object::get_field_optional(sfield::TotalValueOutstanding)
286    }
287
288    /// The remaining management fee owed to the loan broker.
289    fn management_fee_outstanding(&self) -> Result<Option<Number>> {
290        current_ledger_object::get_field_optional(sfield::ManagementFeeOutstanding)
291    }
292
293    /// The scale factor that ensures all computed amounts are rounded to the same number of decimal
294    /// places. It is based on the total loan value at creation time.
295    fn loan_scale(&self) -> Result<Option<i32>> {
296        current_ledger_object::get_field_optional(sfield::LoanScale)
297    }
298}
299
300#[derive(Debug, Clone, Copy, Eq, PartialEq)]
301pub struct Loan {
302    pub(crate) slot_num: i32,
303}
304
305impl Loan {
306    /// Binds this handle to a host-managed slot holding a Loan ledger object.
307    pub fn new(slot_num: i32) -> Self {
308        Self { slot_num }
309    }
310}
311
312impl LedgerObjectCommonFields for Loan {
313    fn get_slot_num(&self) -> i32 {
314        self.slot_num
315    }
316}
317
318impl LoanFields for Loan {}
319
320#[cfg(test)]
321mod tests {
322    use super::*;
323    use crate::host::host_bindings_trait::MockHostBindings;
324    use crate::host::setup_mock;
325    use crate::objects::test_utils::*;
326
327    #[test]
328    fn read_all_fields() {
329        let mut mock = MockHostBindings::new();
330        mock_all_fields_present(&mut mock);
331        let _guard = setup_mock(mock);
332
333        let obj = Loan::new(0);
334
335        assert!(obj.previous_txn_id().is_ok());
336        assert!(obj.previous_txn_lgr_seq().is_ok());
337        assert!(obj.owner_node().is_ok());
338        assert!(obj.loan_broker_node().is_ok());
339        assert!(obj.loan_broker_id().is_ok());
340        assert!(obj.loan_sequence().is_ok());
341        assert!(obj.borrower().is_ok());
342        assert!(obj.start_date().is_ok());
343        assert!(obj.payment_interval().is_ok());
344        assert!(obj.periodic_payment().is_ok());
345        assert!(obj.loan_origination_fee().is_ok());
346        assert!(obj.loan_service_fee().is_ok());
347        assert!(obj.late_payment_fee().is_ok());
348        assert!(obj.close_payment_fee().is_ok());
349        assert!(obj.overpayment_fee().is_ok());
350        assert!(obj.interest_rate().is_ok());
351        assert!(obj.late_interest_rate().is_ok());
352        assert!(obj.close_interest_rate().is_ok());
353        assert!(obj.overpayment_interest_rate().is_ok());
354        assert!(obj.grace_period().is_ok());
355        assert!(obj.previous_payment_due_date().is_ok());
356        assert!(obj.next_payment_due_date().is_ok());
357        assert!(obj.payment_remaining().is_ok());
358        assert!(obj.principal_outstanding().is_ok());
359        assert!(obj.total_value_outstanding().is_ok());
360        assert!(obj.management_fee_outstanding().is_ok());
361        assert!(obj.loan_scale().is_ok());
362    }
363
364    #[test]
365    fn optional_fields_none() {
366        let mut mock = MockHostBindings::new();
367        mock_all_fields_not_found(&mut mock);
368        let _guard = setup_mock(mock);
369
370        let obj = Loan::new(0);
371
372        assert!(obj.overpayment_fee().unwrap().is_none());
373        assert!(obj.interest_rate().unwrap().is_none());
374        assert!(obj.late_interest_rate().unwrap().is_none());
375        assert!(obj.close_interest_rate().unwrap().is_none());
376        assert!(obj.overpayment_interest_rate().unwrap().is_none());
377        assert!(obj.grace_period().unwrap().is_none());
378        assert!(obj.previous_payment_due_date().unwrap().is_none());
379        assert!(obj.next_payment_due_date().unwrap().is_none());
380        assert!(obj.payment_remaining().unwrap().is_none());
381        assert!(obj.loan_scale().unwrap().is_none());
382    }
383}