rippled
Loading...
Searching...
No Matches
LendingHelpers_test.cpp
1#include <xrpl/beast/unit_test/suite.h>
2// DO NOT REMOVE
3#include <test/jtx.h>
4#include <test/jtx/Account.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/mpt.h>
7
8#include <xrpld/app/misc/LendingHelpers.h>
9#include <xrpld/app/misc/LoadFeeTrack.h>
10#include <xrpld/app/tx/detail/Batch.h>
11#include <xrpld/app/tx/detail/LoanSet.h>
12
13#include <xrpl/beast/xor_shift_engine.h>
14#include <xrpl/protocol/SField.h>
15
16#include <string>
17#include <vector>
18
19namespace xrpl {
20namespace test {
21
23{
24 void
26 {
27 using namespace jtx;
28 using namespace xrpl::detail;
29 struct TestCase
30 {
31 std::string name;
32 Number periodicRate;
33 std::uint32_t paymentsRemaining;
34 Number expectedRaisedRate;
35 };
36
37 auto const testCases = std::vector<TestCase>{
38 {
39 .name = "Zero payments remaining",
40 .periodicRate = Number{5, -2},
41 .paymentsRemaining = 0,
42 .expectedRaisedRate = Number{1}, // (1 + r)^0 = 1
43 },
44 {
45 .name = "One payment remaining",
46 .periodicRate = Number{5, -2},
47 .paymentsRemaining = 1,
48 .expectedRaisedRate = Number{105, -2},
49 }, // 1.05^1
50 {
51 .name = "Multiple payments remaining",
52 .periodicRate = Number{5, -2},
53 .paymentsRemaining = 3,
54 .expectedRaisedRate = Number{1157625, -6},
55 }, // 1.05^3
56 {
57 .name = "Zero periodic rate",
58 .periodicRate = Number{0},
59 .paymentsRemaining = 5,
60 .expectedRaisedRate = Number{1}, // (1 + 0)^5 = 1
61 }};
62
63 for (auto const& tc : testCases)
64 {
65 testcase("computeRaisedRate: " + tc.name);
66
67 auto const computedRaisedRate = computeRaisedRate(tc.periodicRate, tc.paymentsRemaining);
68 BEAST_EXPECTS(
69 computedRaisedRate == tc.expectedRaisedRate,
70 "Raised rate mismatch: expected " + to_string(tc.expectedRaisedRate) + ", got " +
71 to_string(computedRaisedRate));
72 }
73 }
74
75 void
77 {
78 using namespace jtx;
79 using namespace xrpl::detail;
80 struct TestCase
81 {
82 std::string name;
83 Number periodicRate;
84 std::uint32_t paymentsRemaining;
85 Number expectedPaymentFactor;
86 };
87
88 auto const testCases = std::vector<TestCase>{
89 {
90 .name = "Zero periodic rate",
91 .periodicRate = Number{0},
92 .paymentsRemaining = 4,
93 .expectedPaymentFactor = Number{25, -2},
94 }, // 1/4 = 0.25
95 {
96 .name = "One payment remaining",
97 .periodicRate = Number{5, -2},
98 .paymentsRemaining = 1,
99 .expectedPaymentFactor = Number{105, -2},
100 }, // 0.05/1 = 1.05
101 {
102 .name = "Multiple payments remaining",
103 .periodicRate = Number{5, -2},
104 .paymentsRemaining = 3,
105 .expectedPaymentFactor = Number{3672085646312450436, -19},
106 }, // from calc
107 {
108 .name = "Zero payments remaining",
109 .periodicRate = Number{5, -2},
110 .paymentsRemaining = 0,
111 .expectedPaymentFactor = Number{0},
112 } // edge case
113 };
114
115 for (auto const& tc : testCases)
116 {
117 testcase("computePaymentFactor: " + tc.name);
118
119 auto const computedPaymentFactor = computePaymentFactor(tc.periodicRate, tc.paymentsRemaining);
120 BEAST_EXPECTS(
121 computedPaymentFactor == tc.expectedPaymentFactor,
122 "Payment factor mismatch: expected " + to_string(tc.expectedPaymentFactor) + ", got " +
123 to_string(computedPaymentFactor));
124 }
125 }
126
127 void
129 {
130 using namespace jtx;
131 using namespace xrpl::detail;
132
133 struct TestCase
134 {
135 std::string name;
136 Number principalOutstanding;
137 Number periodicRate;
138 std::uint32_t paymentsRemaining;
139 Number expectedPeriodicPayment;
140 };
141
142 auto const testCases = std::vector<TestCase>{
143 {
144 .name = "Zero principal outstanding",
145 .principalOutstanding = Number{0},
146 .periodicRate = Number{5, -2},
147 .paymentsRemaining = 5,
148 .expectedPeriodicPayment = Number{0},
149 },
150 {
151 .name = "Zero payments remaining",
152 .principalOutstanding = Number{1'000},
153 .periodicRate = Number{5, -2},
154 .paymentsRemaining = 0,
155 .expectedPeriodicPayment = Number{0},
156 },
157 {
158 .name = "Zero periodic rate",
159 .principalOutstanding = Number{1'000},
160 .periodicRate = Number{0},
161 .paymentsRemaining = 4,
162 .expectedPeriodicPayment = Number{250},
163 },
164 {
165 .name = "Standard case",
166 .principalOutstanding = Number{1'000},
167 .periodicRate = loanPeriodicRate(TenthBips32(100'000), 30 * 24 * 60 * 60),
168 .paymentsRemaining = 3,
169 .expectedPeriodicPayment = Number{389569066396123265, -15}, // from calc
170 },
171 };
172
173 for (auto const& tc : testCases)
174 {
175 testcase("loanPeriodicPayment: " + tc.name);
176
177 auto const computedPeriodicPayment =
178 loanPeriodicPayment(tc.principalOutstanding, tc.periodicRate, tc.paymentsRemaining);
179 BEAST_EXPECTS(
180 computedPeriodicPayment == tc.expectedPeriodicPayment,
181 "Periodic payment mismatch: expected " + to_string(tc.expectedPeriodicPayment) + ", got " +
182 to_string(computedPeriodicPayment));
183 }
184 }
185
186 void
188 {
189 using namespace jtx;
190 using namespace xrpl::detail;
191
192 struct TestCase
193 {
194 std::string name;
195 Number periodicPayment;
196 Number periodicRate;
197 std::uint32_t paymentsRemaining;
198 Number expectedPrincipalOutstanding;
199 };
200
201 auto const testCases = std::vector<TestCase>{
202 {
203 .name = "Zero periodic payment",
204 .periodicPayment = Number{0},
205 .periodicRate = Number{5, -2},
206 .paymentsRemaining = 5,
207 .expectedPrincipalOutstanding = Number{0},
208 },
209 {
210 .name = "Zero payments remaining",
211 .periodicPayment = Number{1'000},
212 .periodicRate = Number{5, -2},
213 .paymentsRemaining = 0,
214 .expectedPrincipalOutstanding = Number{0},
215 },
216 {
217 .name = "Zero periodic rate",
218 .periodicPayment = Number{250},
219 .periodicRate = Number{0},
220 .paymentsRemaining = 4,
221 .expectedPrincipalOutstanding = Number{1'000},
222 },
223 {
224 .name = "Standard case",
225 .periodicPayment = Number{389569066396123265, -15}, // from calc
226 .periodicRate = loanPeriodicRate(TenthBips32(100'000), 30 * 24 * 60 * 60),
227 .paymentsRemaining = 3,
228 .expectedPrincipalOutstanding = Number{1'000},
229 },
230 };
231
232 for (auto const& tc : testCases)
233 {
234 testcase("loanPrincipalFromPeriodicPayment: " + tc.name);
235
236 auto const computedPrincipalOutstanding =
237 loanPrincipalFromPeriodicPayment(tc.periodicPayment, tc.periodicRate, tc.paymentsRemaining);
238 BEAST_EXPECTS(
239 computedPrincipalOutstanding == tc.expectedPrincipalOutstanding,
240 "Principal outstanding mismatch: expected " + to_string(tc.expectedPrincipalOutstanding) + ", got " +
241 to_string(computedPrincipalOutstanding));
242 }
243 }
244
245 void
247 {
248 testcase("computeOverpaymentComponents");
249 using namespace jtx;
250 using namespace xrpl::detail;
251
252 Account const issuer{"issuer"};
253 PrettyAsset const IOU = issuer["IOU"];
254 int32_t const loanScale = 1;
255 auto const overpayment = Number{1'000};
256 auto const overpaymentInterestRate = TenthBips32{10'000}; // 10%
257 auto const overpaymentFeeRate = TenthBips32{50'000}; // 50%
258 auto const managementFeeRate = TenthBips16{10'000}; // 10%
259
260 auto const expectedOverpaymentFee = Number{500}; // 50% of 1,000
261 auto const expectedOverpaymentInterestGross = Number{100}; // 10% of 1,000
262 auto const expectedOverpaymentInterestNet = Number{90}; // 100 - 10% of 100
263 auto const expectedOverpaymentManagementFee = Number{10}; // 10% of 100
264 auto const expectedPrincipalPortion = Number{400}; // 1,000 - 100 - 500
265
266 auto const components = detail::computeOverpaymentComponents(
267 IOU, loanScale, overpayment, overpaymentInterestRate, overpaymentFeeRate, managementFeeRate);
268
269 BEAST_EXPECT(components.untrackedManagementFee == expectedOverpaymentFee);
270
271 BEAST_EXPECT(components.untrackedInterest == expectedOverpaymentInterestNet);
272
273 BEAST_EXPECT(components.trackedInterestPart() == expectedOverpaymentInterestNet);
274
275 BEAST_EXPECT(components.trackedManagementFeeDelta == expectedOverpaymentManagementFee);
276 BEAST_EXPECT(components.trackedPrincipalDelta == expectedPrincipalPortion);
277 BEAST_EXPECT(
278 components.trackedManagementFeeDelta + components.untrackedInterest == expectedOverpaymentInterestGross);
279
280 BEAST_EXPECT(
281 components.trackedManagementFeeDelta + components.untrackedInterest + components.trackedPrincipalDelta +
282 components.untrackedManagementFee ==
284 }
285
286 void
288 {
289 using namespace jtx;
290 using namespace xrpl::detail;
291
292 struct TestCase
293 {
294 std::string name;
295 Number interest;
296 TenthBips16 managementFeeRate;
297 Number expectedInterestPart;
298 Number expectedFeePart;
299 };
300
301 Account const issuer{"issuer"};
302 PrettyAsset const IOU = issuer["IOU"];
303 std::int32_t const loanScale = 1;
304
305 auto const testCases = std::vector<TestCase>{
306 {.name = "Zero interest",
307 .interest = Number{0},
308 .managementFeeRate = TenthBips16{10'000},
309 .expectedInterestPart = Number{0},
310 .expectedFeePart = Number{0}},
311 {.name = "Zero fee rate",
312 .interest = Number{1'000},
313 .managementFeeRate = TenthBips16{0},
314 .expectedInterestPart = Number{1'000},
315 .expectedFeePart = Number{0}},
316 {.name = "10% fee rate",
317 .interest = Number{1'000},
318 .managementFeeRate = TenthBips16{10'000},
319 .expectedInterestPart = Number{900},
320 .expectedFeePart = Number{100}},
321 };
322
323 for (auto const& tc : testCases)
324 {
325 testcase("computeInterestAndFeeParts: " + tc.name);
326
327 auto const [computedInterestPart, computedFeePart] =
328 computeInterestAndFeeParts(IOU, tc.interest, tc.managementFeeRate, loanScale);
329 BEAST_EXPECTS(
330 computedInterestPart == tc.expectedInterestPart,
331 "Interest part mismatch: expected " + to_string(tc.expectedInterestPart) + ", got " +
332 to_string(computedInterestPart));
333 BEAST_EXPECTS(
334 computedFeePart == tc.expectedFeePart,
335 "Fee part mismatch: expected " + to_string(tc.expectedFeePart) + ", got " + to_string(computedFeePart));
336 }
337 }
338
339 void
341 {
342 using namespace jtx;
343 using namespace xrpl::detail;
344 struct TestCase
345 {
346 std::string name;
347 Number principalOutstanding;
348 TenthBips32 lateInterestRate;
349 NetClock::time_point parentCloseTime;
350 std::uint32_t nextPaymentDueDate;
351 Number expectedLateInterest;
352 };
353
354 auto const testCases = std::vector<TestCase>{
355 {
356 .name = "On-time payment",
357 .principalOutstanding = Number{1'000},
358 .lateInterestRate = TenthBips32{10'000}, // 10%
359 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
360 .nextPaymentDueDate = 3'000,
361 .expectedLateInterest = Number{0},
362 },
363 {
364 .name = "Early payment",
365 .principalOutstanding = Number{1'000},
366 .lateInterestRate = TenthBips32{10'000}, // 10%
367 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
368 .nextPaymentDueDate = 4'000,
369 .expectedLateInterest = Number{0},
370 },
371 {
372 .name = "No principal outstanding",
373 .principalOutstanding = Number{0},
374 .lateInterestRate = TenthBips32{10'000}, // 10%
375 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
376 .nextPaymentDueDate = 2'000,
377 .expectedLateInterest = Number{0},
378 },
379 {
380 .name = "No late interest rate",
381 .principalOutstanding = Number{1'000},
382 .lateInterestRate = TenthBips32{0}, // 0%
383 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
384 .nextPaymentDueDate = 2'000,
385 .expectedLateInterest = Number{0},
386 },
387 {
388 .name = "Late payment",
389 .principalOutstanding = Number{1'000},
390 .lateInterestRate = TenthBips32{100'000}, // 100%
391 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
392 .nextPaymentDueDate = 2'000,
393 .expectedLateInterest = Number{317097919837645865, -19}, // from calc
394 },
395 };
396
397 for (auto const& tc : testCases)
398 {
399 testcase("loanLatePaymentInterest: " + tc.name);
400
401 auto const computedLateInterest = loanLatePaymentInterest(
402 tc.principalOutstanding, tc.lateInterestRate, tc.parentCloseTime, tc.nextPaymentDueDate);
403 BEAST_EXPECTS(
404 computedLateInterest == tc.expectedLateInterest,
405 "Late interest mismatch: expected " + to_string(tc.expectedLateInterest) + ", got " +
406 to_string(computedLateInterest));
407 }
408 }
409
410 void
412 {
413 using namespace jtx;
414 using namespace xrpl::detail;
415 struct TestCase
416 {
417 std::string name;
418 Number principalOutstanding;
419 Number periodicRate;
420 NetClock::time_point parentCloseTime;
421 std::uint32_t startDate;
422 std::uint32_t prevPaymentDate;
423 std::uint32_t paymentInterval;
424 Number expectedAccruedInterest;
425 };
426
427 auto const testCases = std::vector<TestCase>{
428 {
429 .name = "Zero principal outstanding",
430 .principalOutstanding = Number{0},
431 .periodicRate = Number{5, -2},
432 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
433 .startDate = 2'000,
434 .prevPaymentDate = 2'500,
435 .paymentInterval = 30 * 24 * 60 * 60,
436 .expectedAccruedInterest = Number{0},
437 },
438 {
439 .name = "Before start date",
440 .principalOutstanding = Number{1'000},
441 .periodicRate = Number{5, -2},
442 .parentCloseTime = NetClock::time_point{NetClock::duration{1'000}},
443 .startDate = 2'000,
444 .prevPaymentDate = 1'500,
445 .paymentInterval = 30 * 24 * 60 * 60,
446 .expectedAccruedInterest = Number{0},
447 },
448 {
449 .name = "Zero periodic rate",
450 .principalOutstanding = Number{1'000},
451 .periodicRate = Number{0},
452 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
453 .startDate = 2'000,
454 .prevPaymentDate = 2'500,
455 .paymentInterval = 30 * 24 * 60 * 60,
456 .expectedAccruedInterest = Number{0},
457 },
458 {
459 .name = "Zero payment interval",
460 .principalOutstanding = Number{1'000},
461 .periodicRate = Number{5, -2},
462 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
463 .startDate = 2'000,
464 .prevPaymentDate = 2'500,
465 .paymentInterval = 0,
466 .expectedAccruedInterest = Number{0},
467 },
468 {
469 .name = "Standard case",
470 .principalOutstanding = Number{1'000},
471 .periodicRate = Number{5, -2},
472 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
473 .startDate = 1'000,
474 .prevPaymentDate = 2'000,
475 .paymentInterval = 30 * 24 * 60 * 60,
476 .expectedAccruedInterest = Number{1929012345679012346, -20}, // from calc
477 },
478 };
479
480 for (auto const& tc : testCases)
481 {
482 testcase("loanAccruedInterest: " + tc.name);
483
484 auto const computedAccruedInterest = loanAccruedInterest(
485 tc.principalOutstanding,
486 tc.periodicRate,
487 tc.parentCloseTime,
488 tc.startDate,
489 tc.prevPaymentDate,
490 tc.paymentInterval);
491 BEAST_EXPECTS(
492 computedAccruedInterest == tc.expectedAccruedInterest,
493 "Accrued interest mismatch: expected " + to_string(tc.expectedAccruedInterest) + ", got " +
494 to_string(computedAccruedInterest));
495 }
496 }
497
498 // This test overlaps with testLoanAccruedInterest, the test cases only
499 // exercise the computeFullPaymentInterest parts unique to it.
500 void
502 {
503 using namespace jtx;
504 using namespace xrpl::detail;
505
506 struct TestCase
507 {
508 std::string name;
509 Number rawPrincipalOutstanding;
510 Number periodicRate;
511 NetClock::time_point parentCloseTime;
512 std::uint32_t paymentInterval;
513 std::uint32_t prevPaymentDate;
514 std::uint32_t startDate;
515 TenthBips32 closeInterestRate;
516 Number expectedFullPaymentInterest;
517 };
518
519 auto const testCases = std::vector<TestCase>{
520 {
521 .name = "Zero principal outstanding",
522 .rawPrincipalOutstanding = Number{0},
523 .periodicRate = Number{5, -2},
524 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
525 .paymentInterval = 30 * 24 * 60 * 60,
526 .prevPaymentDate = 2'000,
527 .startDate = 1'000,
528 .closeInterestRate = TenthBips32{10'000},
529 .expectedFullPaymentInterest = Number{0},
530 },
531 {
532 .name = "Zero close interest rate",
533 .rawPrincipalOutstanding = Number{1'000},
534 .periodicRate = Number{5, -2},
535 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
536 .paymentInterval = 30 * 24 * 60 * 60,
537 .prevPaymentDate = 2'000,
538 .startDate = 1'000,
539 .closeInterestRate = TenthBips32{0},
540 .expectedFullPaymentInterest = Number{1929012345679012346, -20}, // from calc
541 },
542 {
543 .name = "Standard case",
544 .rawPrincipalOutstanding = Number{1'000},
545 .periodicRate = Number{5, -2},
546 .parentCloseTime = NetClock::time_point{NetClock::duration{3'000}},
547 .paymentInterval = 30 * 24 * 60 * 60,
548 .prevPaymentDate = 2'000,
549 .startDate = 1'000,
550 .closeInterestRate = TenthBips32{10'000},
551 .expectedFullPaymentInterest = Number{1000192901234567901, -16}, // from calc
552 },
553 };
554
555 for (auto const& tc : testCases)
556 {
557 testcase("computeFullPaymentInterest: " + tc.name);
558
559 auto const computedFullPaymentInterest = computeFullPaymentInterest(
560 tc.rawPrincipalOutstanding,
561 tc.periodicRate,
562 tc.parentCloseTime,
563 tc.paymentInterval,
564 tc.prevPaymentDate,
565 tc.startDate,
566 tc.closeInterestRate);
567 BEAST_EXPECTS(
568 computedFullPaymentInterest == tc.expectedFullPaymentInterest,
569 "Full payment interest mismatch: expected " + to_string(tc.expectedFullPaymentInterest) + ", got " +
570 to_string(computedFullPaymentInterest));
571 }
572 }
573
574 void
576 {
577 // This test ensures that overpayment with no interest works correctly.
578 testcase("tryOverpayment - No Interest No Fee");
579
580 using namespace jtx;
581 using namespace xrpl::detail;
582
583 Env env{*this};
584 Account const issuer{"issuer"};
585 PrettyAsset const asset = issuer["USD"];
586 std::int32_t const loanScale = -5;
587 TenthBips16 const managementFeeRate{0}; // 0%
588 TenthBips32 const loanInterestRate{0}; // 0%
589 Number const loanPrincipal{1'000};
590 std::uint32_t const paymentInterval = 30 * 24 * 60 * 60;
591 std::uint32_t const paymentsRemaining = 10;
592 auto const periodicRate = loanPeriodicRate(loanInterestRate, paymentInterval);
593 Number const overpaymentAmount{50};
594
595 auto const overpaymentComponents = computeOverpaymentComponents(
596 asset, loanScale, overpaymentAmount, TenthBips32(0), TenthBips32(0), managementFeeRate);
597
598 auto const loanProperties = computeLoanProperties(
599 asset, loanPrincipal, loanInterestRate, paymentInterval, paymentsRemaining, managementFeeRate, loanScale);
600
601 auto const ret = tryOverpayment(
602 asset,
603 loanScale,
604 overpaymentComponents,
605 loanProperties.loanState,
606 loanProperties.periodicPayment,
607 periodicRate,
608 paymentsRemaining,
609 managementFeeRate,
610 env.journal);
611
612 BEAST_EXPECT(ret);
613
614 auto const& [actualPaymentParts, newLoanProperties] = *ret;
615 auto const& newState = newLoanProperties.loanState;
616
617 // =========== VALIDATE PAYMENT PARTS ===========
618 BEAST_EXPECTS(
619 actualPaymentParts.valueChange == 0,
620 " valueChange mismatch: expected 0, got " + to_string(actualPaymentParts.valueChange));
621
622 BEAST_EXPECTS(
623 actualPaymentParts.feePaid == 0,
624 " feePaid mismatch: expected 0, got " + to_string(actualPaymentParts.feePaid));
625
626 BEAST_EXPECTS(
627 actualPaymentParts.interestPaid == 0,
628 " interestPaid mismatch: expected 0, got " + to_string(actualPaymentParts.interestPaid));
629
630 BEAST_EXPECTS(
631 actualPaymentParts.principalPaid == overpaymentAmount,
632 " principalPaid mismatch: expected " + to_string(overpaymentAmount) + ", got " +
633 to_string(actualPaymentParts.principalPaid));
634
635 // =========== VALIDATE STATE CHANGES ===========
636 BEAST_EXPECTS(
637 loanProperties.loanState.interestDue - newState.interestDue == 0,
638 " interest change mismatch: expected 0, got " +
639 to_string(loanProperties.loanState.interestDue - newState.interestDue));
640
641 BEAST_EXPECTS(
642 loanProperties.loanState.managementFeeDue - newState.managementFeeDue == 0,
643 " management fee change mismatch: expected 0, got " +
644 to_string(loanProperties.loanState.managementFeeDue - newState.managementFeeDue));
645
646 BEAST_EXPECTS(
647 actualPaymentParts.principalPaid ==
648 loanProperties.loanState.principalOutstanding - newState.principalOutstanding,
649 " principalPaid mismatch: expected " +
650 to_string(loanProperties.loanState.principalOutstanding - newState.principalOutstanding) + ", got " +
651 to_string(actualPaymentParts.principalPaid));
652 }
653
654 void
656 {
657 testcase("tryOverpayment - No Interest With Overpayment Fee");
658
659 using namespace jtx;
660 using namespace xrpl::detail;
661
662 Env env{*this};
663 Account const issuer{"issuer"};
664 PrettyAsset const asset = issuer["USD"];
665 std::int32_t const loanScale = -5;
666 TenthBips16 const managementFeeRate{0}; // 0%
667 TenthBips32 const loanInterestRate{0}; // 0%
668 Number const loanPrincipal{1'000};
669 std::uint32_t const paymentInterval = 30 * 24 * 60 * 60;
670 std::uint32_t const paymentsRemaining = 10;
671 auto const periodicRate = loanPeriodicRate(loanInterestRate, paymentInterval);
672
673 auto const overpaymentComponents = computeOverpaymentComponents(
674 asset,
675 loanScale,
676 Number{50, 0},
677 TenthBips32(0),
678 TenthBips32(10'000), // 10% overpayment fee
679 managementFeeRate);
680
681 auto const loanProperties = computeLoanProperties(
682 asset, loanPrincipal, loanInterestRate, paymentInterval, paymentsRemaining, managementFeeRate, loanScale);
683
684 auto const ret = tryOverpayment(
685 asset,
686 loanScale,
687 overpaymentComponents,
688 loanProperties.loanState,
689 loanProperties.periodicPayment,
690 periodicRate,
691 paymentsRemaining,
692 managementFeeRate,
693 env.journal);
694
695 BEAST_EXPECT(ret);
696
697 auto const& [actualPaymentParts, newLoanProperties] = *ret;
698 auto const& newState = newLoanProperties.loanState;
699
700 // =========== VALIDATE PAYMENT PARTS ===========
701 BEAST_EXPECTS(
702 actualPaymentParts.valueChange == 0,
703 " valueChange mismatch: expected 0, got " + to_string(actualPaymentParts.valueChange));
704
705 BEAST_EXPECTS(
706 actualPaymentParts.feePaid == 5,
707 " feePaid mismatch: expected 5, got " + to_string(actualPaymentParts.feePaid));
708
709 BEAST_EXPECTS(
710 actualPaymentParts.principalPaid == 45,
711 " principalPaid mismatch: expected 45, got `" + to_string(actualPaymentParts.principalPaid));
712
713 BEAST_EXPECTS(
714 actualPaymentParts.interestPaid == 0,
715 " interestPaid mismatch: expected 0, got " + to_string(actualPaymentParts.interestPaid));
716
717 // =========== VALIDATE STATE CHANGES ===========
718 // With no Loan interest, interest outstanding should not change
719 BEAST_EXPECTS(
720 loanProperties.loanState.interestDue - newState.interestDue == 0,
721 " interest change mismatch: expected 0, got " +
722 to_string(loanProperties.loanState.interestDue - newState.interestDue));
723
724 // With no Loan management fee, management fee due should not change
725 BEAST_EXPECTS(
726 loanProperties.loanState.managementFeeDue - newState.managementFeeDue == 0,
727 " management fee change mismatch: expected 0, got " +
728 to_string(loanProperties.loanState.managementFeeDue - newState.managementFeeDue));
729
730 BEAST_EXPECTS(
731 actualPaymentParts.principalPaid ==
732 loanProperties.loanState.principalOutstanding - newState.principalOutstanding,
733 " principalPaid mismatch: expected " +
734 to_string(loanProperties.loanState.principalOutstanding - newState.principalOutstanding) + ", got " +
735 to_string(actualPaymentParts.principalPaid));
736 }
737
738 void
740 {
741 testcase("tryOverpayment - Loan Interest, No Overpayment Fees");
742
743 using namespace jtx;
744 using namespace xrpl::detail;
745
746 Env env{*this};
747 Account const issuer{"issuer"};
748 PrettyAsset const asset = issuer["USD"];
749 std::int32_t const loanScale = -5;
750 TenthBips16 const managementFeeRate{0}; // 0%
751 TenthBips32 const loanInterestRate{10'000}; // 10%
752 Number const loanPrincipal{1'000};
753 std::uint32_t const paymentInterval = 30 * 24 * 60 * 60;
754 std::uint32_t const paymentsRemaining = 10;
755 auto const periodicRate = loanPeriodicRate(loanInterestRate, paymentInterval);
756
757 auto const overpaymentComponents = computeOverpaymentComponents(
758 asset,
759 loanScale,
760 Number{50, 0},
761 TenthBips32(0), // no overpayment interest
762 TenthBips32(0), // 0% overpayment fee
763 managementFeeRate);
764
765 auto const loanProperties = computeLoanProperties(
766 asset, loanPrincipal, loanInterestRate, paymentInterval, paymentsRemaining, managementFeeRate, loanScale);
767
768 auto const ret = tryOverpayment(
769 asset,
770 loanScale,
771 overpaymentComponents,
772 loanProperties.loanState,
773 loanProperties.periodicPayment,
774 periodicRate,
775 paymentsRemaining,
776 managementFeeRate,
777 env.journal);
778
779 BEAST_EXPECT(ret);
780
781 auto const& [actualPaymentParts, newLoanProperties] = *ret;
782 auto const& newState = newLoanProperties.loanState;
783
784 // =========== VALIDATE PAYMENT PARTS ===========
785 // with no overpayment interest portion, value change should equal
786 // interest decrease
787 BEAST_EXPECTS(
788 (actualPaymentParts.valueChange == Number{-228802, -5}),
789 " valueChange mismatch: expected " + to_string(Number{-228802, -5}) + ", got " +
790 to_string(actualPaymentParts.valueChange));
791
792 // with no fee portion, fee paid should be zero
793 BEAST_EXPECTS(
794 actualPaymentParts.feePaid == 0,
795 " feePaid mismatch: expected 0, got " + to_string(actualPaymentParts.feePaid));
796
797 BEAST_EXPECTS(
798 actualPaymentParts.principalPaid == 50,
799 " principalPaid mismatch: expected 50, got `" + to_string(actualPaymentParts.principalPaid));
800
801 // with no interest portion, interest paid should be zero
802 BEAST_EXPECTS(
803 actualPaymentParts.interestPaid == 0,
804 " interestPaid mismatch: expected 0, got " + to_string(actualPaymentParts.interestPaid));
805
806 // =========== VALIDATE STATE CHANGES ===========
807 BEAST_EXPECTS(
808 actualPaymentParts.principalPaid ==
809 loanProperties.loanState.principalOutstanding - newState.principalOutstanding,
810 " principalPaid mismatch: expected " +
811 to_string(loanProperties.loanState.principalOutstanding - newState.principalOutstanding) + ", got " +
812 to_string(actualPaymentParts.principalPaid));
813
814 BEAST_EXPECTS(
815 actualPaymentParts.valueChange == newState.interestDue - loanProperties.loanState.interestDue,
816 " valueChange mismatch: expected " +
817 to_string(newState.interestDue - loanProperties.loanState.interestDue) + ", got " +
818 to_string(actualPaymentParts.valueChange));
819
820 // With no Loan management fee, management fee due should not change
821 BEAST_EXPECTS(
822 loanProperties.loanState.managementFeeDue - newState.managementFeeDue == 0,
823 " management fee change mismatch: expected 0, got " +
824 to_string(loanProperties.loanState.managementFeeDue - newState.managementFeeDue));
825 }
826
827 void
829 {
830 testcase("tryOverpayment - Loan Interest, Overpayment Interest, No Fee");
831
832 using namespace jtx;
833 using namespace xrpl::detail;
834
835 Env env{*this};
836 Account const issuer{"issuer"};
837 PrettyAsset const asset = issuer["USD"];
838 std::int32_t const loanScale = -5;
839 TenthBips16 const managementFeeRate{0}; // 0%
840 TenthBips32 const loanInterestRate{10'000}; // 10%
841 Number const loanPrincipal{1'000};
842 std::uint32_t const paymentInterval = 30 * 24 * 60 * 60;
843 std::uint32_t const paymentsRemaining = 10;
844 auto const periodicRate = loanPeriodicRate(loanInterestRate, paymentInterval);
845
846 auto const overpaymentComponents = computeOverpaymentComponents(
847 asset,
848 loanScale,
849 Number{50, 0},
850 TenthBips32(10'000), // 10% overpayment interest
851 TenthBips32(0), // 0% overpayment fee
852 managementFeeRate);
853
854 auto const loanProperties = computeLoanProperties(
855 asset, loanPrincipal, loanInterestRate, paymentInterval, paymentsRemaining, managementFeeRate, loanScale);
856
857 auto const ret = tryOverpayment(
858 asset,
859 loanScale,
860 overpaymentComponents,
861 loanProperties.loanState,
862 loanProperties.periodicPayment,
863 periodicRate,
864 paymentsRemaining,
865 managementFeeRate,
866 env.journal);
867
868 BEAST_EXPECT(ret);
869
870 auto const& [actualPaymentParts, newLoanProperties] = *ret;
871 auto const& newState = newLoanProperties.loanState;
872
873 // =========== VALIDATE PAYMENT PARTS ===========
874 // with overpayment interest portion, interest paid should be 5
875 BEAST_EXPECTS(
876 actualPaymentParts.interestPaid == 5,
877 " interestPaid mismatch: expected 5, got " + to_string(actualPaymentParts.interestPaid));
878
879 // With overpayment interest portion, value change should equal the
880 // interest decrease plus overpayment interest portion
881 BEAST_EXPECTS(
882 (actualPaymentParts.valueChange == Number{-205922, -5} + actualPaymentParts.interestPaid),
883 " valueChange mismatch: expected " +
884 to_string(actualPaymentParts.valueChange - actualPaymentParts.interestPaid) + ", got " +
885 to_string(actualPaymentParts.valueChange));
886
887 // with no fee portion, fee paid should be zero
888 BEAST_EXPECTS(
889 actualPaymentParts.feePaid == 0,
890 " feePaid mismatch: expected 0, got " + to_string(actualPaymentParts.feePaid));
891
892 BEAST_EXPECTS(
893 actualPaymentParts.principalPaid == 45,
894 " principalPaid mismatch: expected 45, got `" + to_string(actualPaymentParts.principalPaid));
895
896 // =========== VALIDATE STATE CHANGES ===========
897 BEAST_EXPECTS(
898 actualPaymentParts.principalPaid ==
899 loanProperties.loanState.principalOutstanding - newState.principalOutstanding,
900 " principalPaid mismatch: expected " +
901 to_string(loanProperties.loanState.principalOutstanding - newState.principalOutstanding) + ", got " +
902 to_string(actualPaymentParts.principalPaid));
903
904 // The change in interest is equal to the value change sans the
905 // overpayment interest
906 BEAST_EXPECTS(
907 actualPaymentParts.valueChange - actualPaymentParts.interestPaid ==
908 newState.interestDue - loanProperties.loanState.interestDue,
909 " valueChange mismatch: expected " +
910 to_string(
911 newState.interestDue - loanProperties.loanState.interestDue + actualPaymentParts.interestPaid) +
912 ", got " + to_string(actualPaymentParts.valueChange));
913
914 // With no Loan management fee, management fee due should not change
915 BEAST_EXPECTS(
916 loanProperties.loanState.managementFeeDue - newState.managementFeeDue == 0,
917 " management fee change mismatch: expected 0, got " +
918 to_string(loanProperties.loanState.managementFeeDue - newState.managementFeeDue));
919 }
920
921 void
923 {
924 testcase(
925 "tryOverpayment - Loan Interest and Fee, Overpayment Interest, No "
926 "Fee");
927
928 using namespace jtx;
929 using namespace xrpl::detail;
930
931 Env env{*this};
932 Account const issuer{"issuer"};
933 PrettyAsset const asset = issuer["USD"];
934 std::int32_t const loanScale = -5;
935 TenthBips16 const managementFeeRate{10'000}; // 10%
936 TenthBips32 const loanInterestRate{10'000}; // 10%
937 Number const loanPrincipal{1'000};
938 std::uint32_t const paymentInterval = 30 * 24 * 60 * 60;
939 std::uint32_t const paymentsRemaining = 10;
940 auto const periodicRate = loanPeriodicRate(loanInterestRate, paymentInterval);
941
942 auto const overpaymentComponents = computeOverpaymentComponents(
943 asset,
944 loanScale,
945 Number{50, 0},
946 TenthBips32(10'000), // 10% overpayment interest
947 TenthBips32(0), // 0% overpayment fee
948 managementFeeRate);
949
950 auto const loanProperties = computeLoanProperties(
951 asset, loanPrincipal, loanInterestRate, paymentInterval, paymentsRemaining, managementFeeRate, loanScale);
952
953 auto const ret = tryOverpayment(
954 asset,
955 loanScale,
956 overpaymentComponents,
957 loanProperties.loanState,
958 loanProperties.periodicPayment,
959 periodicRate,
960 paymentsRemaining,
961 managementFeeRate,
962 env.journal);
963
964 BEAST_EXPECT(ret);
965
966 auto const& [actualPaymentParts, newLoanProperties] = *ret;
967 auto const& newState = newLoanProperties.loanState;
968
969 // =========== VALIDATE PAYMENT PARTS ===========
970
971 // Since there is loan management fee, the fee is charged against
972 // overpayment interest portion first, so interest paid remains 4.5
973 BEAST_EXPECTS(
974 (actualPaymentParts.interestPaid == Number{45, -1}),
975 " interestPaid mismatch: expected 4.5, got " + to_string(actualPaymentParts.interestPaid));
976
977 // With overpayment interest portion, value change should equal the
978 // interest decrease plus overpayment interest portion
979 BEAST_EXPECTS(
980 (actualPaymentParts.valueChange == Number{-18533, -4} + actualPaymentParts.interestPaid),
981 " valueChange mismatch: expected " + to_string(Number{-18533, -4} + actualPaymentParts.interestPaid) +
982 ", got " + to_string(actualPaymentParts.valueChange));
983
984 // While there is no overpayment fee, fee paid should equal the
985 // management fee charged against the overpayment interest portion
986 BEAST_EXPECTS(
987 (actualPaymentParts.feePaid == Number{5, -1}),
988 " feePaid mismatch: expected 0.5, got " + to_string(actualPaymentParts.feePaid));
989
990 BEAST_EXPECTS(
991 actualPaymentParts.principalPaid == 45,
992 " principalPaid mismatch: expected 45, got `" + to_string(actualPaymentParts.principalPaid));
993
994 // =========== VALIDATE STATE CHANGES ===========
995 BEAST_EXPECTS(
996 actualPaymentParts.principalPaid ==
997 loanProperties.loanState.principalOutstanding - newState.principalOutstanding,
998 " principalPaid mismatch: expected " +
999 to_string(loanProperties.loanState.principalOutstanding - newState.principalOutstanding) + ", got " +
1000 to_string(actualPaymentParts.principalPaid));
1001
1002 // Note that the management fee value change is not captured, as this
1003 // value is not needed to correctly update the Vault state.
1004 BEAST_EXPECTS(
1005 (newState.managementFeeDue - loanProperties.loanState.managementFeeDue == Number{-20592, -5}),
1006 " management fee change mismatch: expected " + to_string(Number{-20592, -5}) + ", got " +
1007 to_string(newState.managementFeeDue - loanProperties.loanState.managementFeeDue));
1008
1009 BEAST_EXPECTS(
1010 actualPaymentParts.valueChange - actualPaymentParts.interestPaid ==
1011 newState.interestDue - loanProperties.loanState.interestDue,
1012 " valueChange mismatch: expected " +
1013 to_string(newState.interestDue - loanProperties.loanState.interestDue) + ", got " +
1014 to_string(actualPaymentParts.valueChange - actualPaymentParts.interestPaid));
1015 }
1016
1017 void
1019 {
1020 testcase("tryOverpayment - Loan Interest, Fee, Overpayment Interest, Fee");
1021
1022 using namespace jtx;
1023 using namespace xrpl::detail;
1024
1025 Env env{*this};
1026 Account const issuer{"issuer"};
1027 PrettyAsset const asset = issuer["USD"];
1028 std::int32_t const loanScale = -5;
1029 TenthBips16 const managementFeeRate{10'000}; // 10%
1030 TenthBips32 const loanInterestRate{10'000}; // 10%
1031 Number const loanPrincipal{1'000};
1032 std::uint32_t const paymentInterval = 30 * 24 * 60 * 60;
1033 std::uint32_t const paymentsRemaining = 10;
1034 auto const periodicRate = loanPeriodicRate(loanInterestRate, paymentInterval);
1035
1036 auto const overpaymentComponents = computeOverpaymentComponents(
1037 asset,
1038 loanScale,
1039 Number{50, 0},
1040 TenthBips32(10'000), // 10% overpayment interest
1041 TenthBips32(10'000), // 10% overpayment fee
1042 managementFeeRate);
1043
1044 auto const loanProperties = computeLoanProperties(
1045 asset, loanPrincipal, loanInterestRate, paymentInterval, paymentsRemaining, managementFeeRate, loanScale);
1046
1047 auto const ret = tryOverpayment(
1048 asset,
1049 loanScale,
1050 overpaymentComponents,
1051 loanProperties.loanState,
1052 loanProperties.periodicPayment,
1053 periodicRate,
1054 paymentsRemaining,
1055 managementFeeRate,
1056 env.journal);
1057
1058 BEAST_EXPECT(ret);
1059
1060 auto const& [actualPaymentParts, newLoanProperties] = *ret;
1061 auto const& newState = newLoanProperties.loanState;
1062
1063 // =========== VALIDATE PAYMENT PARTS ===========
1064
1065 // Since there is loan management fee, the fee is charged against
1066 // overpayment interest portion first, so interest paid remains 4.5
1067 BEAST_EXPECTS(
1068 (actualPaymentParts.interestPaid == Number{45, -1}),
1069 " interestPaid mismatch: expected 4.5, got " + to_string(actualPaymentParts.interestPaid));
1070
1071 // With overpayment interest portion, value change should equal the
1072 // interest decrease plus overpayment interest portion
1073 BEAST_EXPECTS(
1074 (actualPaymentParts.valueChange == Number{-164737, -5} + actualPaymentParts.interestPaid),
1075 " valueChange mismatch: expected " + to_string(Number{-164737, -5} + actualPaymentParts.interestPaid) +
1076 ", got " + to_string(actualPaymentParts.valueChange));
1077
1078 // While there is no overpayment fee, fee paid should equal the
1079 // management fee charged against the overpayment interest portion
1080 BEAST_EXPECTS(
1081 (actualPaymentParts.feePaid == Number{55, -1}),
1082 " feePaid mismatch: expected 5.5, got " + to_string(actualPaymentParts.feePaid));
1083
1084 BEAST_EXPECTS(
1085 actualPaymentParts.principalPaid == 40,
1086 " principalPaid mismatch: expected 40, got `" + to_string(actualPaymentParts.principalPaid));
1087
1088 // =========== VALIDATE STATE CHANGES ===========
1089
1090 BEAST_EXPECTS(
1091 actualPaymentParts.principalPaid ==
1092 loanProperties.loanState.principalOutstanding - newState.principalOutstanding,
1093 " principalPaid mismatch: expected " +
1094 to_string(loanProperties.loanState.principalOutstanding - newState.principalOutstanding) + ", got " +
1095 to_string(actualPaymentParts.principalPaid));
1096
1097 // Note that the management fee value change is not captured, as this
1098 // value is not needed to correctly update the Vault state.
1099 BEAST_EXPECTS(
1100 (newState.managementFeeDue - loanProperties.loanState.managementFeeDue == Number{-18304, -5}),
1101 " management fee change mismatch: expected " + to_string(Number{-18304, -5}) + ", got " +
1102 to_string(newState.managementFeeDue - loanProperties.loanState.managementFeeDue));
1103
1104 BEAST_EXPECTS(
1105 actualPaymentParts.valueChange - actualPaymentParts.interestPaid ==
1106 newState.interestDue - loanProperties.loanState.interestDue,
1107 " valueChange mismatch: expected " +
1108 to_string(newState.interestDue - loanProperties.loanState.interestDue) + ", got " +
1109 to_string(actualPaymentParts.valueChange - actualPaymentParts.interestPaid));
1110 }
1111
1112public:
1113 void
1133};
1134
1135BEAST_DEFINE_TESTSUITE(LendingHelpers, app, xrpl);
1136
1137} // namespace test
1138} // namespace xrpl
A testsuite class.
Definition suite.h:51
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:147
Number is a floating point type that can represent a wide range of values.
Definition Number.h:207
void run() override
Runs the suite.
Immutable cryptographic account descriptor.
Definition Account.h:19
A transaction testing environment.
Definition Env.h:97
Converts to IOU Issue or STAmount.
ExtendedPaymentComponents computeOverpaymentComponents(Asset const &asset, int32_t const loanScale, Number const &overpayment, TenthBips32 const overpaymentInterestRate, TenthBips32 const overpaymentFeeRate, TenthBips16 const managementFeeRate)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Number loanPeriodicRate(TenthBips32 interestRate, std::uint32_t paymentInterval)
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:597
TenthBips< std::uint32_t > TenthBips32
Definition Units.h:429
LoanProperties computeLoanProperties(Asset const &asset, Number const &principalOutstanding, TenthBips32 interestRate, std::uint32_t paymentInterval, std::uint32_t paymentsRemaining, TenthBips32 managementFeeRate, std::int32_t minimumScale)
Number computeFullPaymentInterest(Number const &theoreticalPrincipalOutstanding, Number const &periodicRate, NetClock::time_point parentCloseTime, std::uint32_t paymentInterval, std::uint32_t prevPaymentDate, std::uint32_t startDate, TenthBips32 closeInterestRate)