xrpld
Loading...
Searching...
No Matches
LendingHelpers.h
1#pragma once
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/chrono.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/ledger/ApplyView.h>
9#include <xrpl/ledger/ReadView.h>
10#include <xrpl/ledger/helpers/VaultHelpers.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/Asset.h>
13#include <xrpl/protocol/LedgerFormats.h> // IWYU pragma: keep
14#include <xrpl/protocol/Rules.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STAmount.h>
17#include <xrpl/protocol/STLedgerEntry.h>
18#include <xrpl/protocol/STNumber.h> // IWYU pragma: keep
19#include <xrpl/protocol/STTx.h>
20#include <xrpl/protocol/TER.h>
21#include <xrpl/protocol/Units.h>
22
23#include <cstdint>
24#include <expected>
25#include <optional>
26#include <string_view>
27#include <utility>
28
29namespace xrpl {
30
50[[nodiscard]] TER
52 ReadView const& view,
53 SLE::ConstRef sleBroker,
54 Asset const& vaultAsset,
55 STAmount const& amount,
56 beast::Journal j,
57 std::string_view logPrefix);
58
59namespace detail {
60
65[[nodiscard]] int
66getPosteriorBrokerCoverScale(SLE::ConstRef vault, SLE::ConstRef broker, Number const& delta);
67
76[[nodiscard]] STAmount
78 SLE::ConstRef vault,
79 SLE::ConstRef broker,
80 STAmount const& delta,
81 Number::RoundingMode roundingMode);
82
83} // namespace detail
84
96[[nodiscard]] STAmount
98 SLE::ConstRef vault,
99 SLE::ConstRef broker,
100 STAmount const& amount,
101 Number::RoundingMode roundingMode);
102
111[[nodiscard]] STAmount
113 SLE::ConstRef vault,
114 SLE::ConstRef broker,
115 Number const& raw,
116 Number::RoundingMode roundingMode);
117
132[[nodiscard]] TER
134
135// Lending protocol has dependencies, so capture them here.
136bool
137checkLendingProtocolDependencies(Rules const& rules, STTx const& tx);
138
159
173getLoanDefaultFreezeExemptAccounts(ReadView const& view, STTx const& tx);
174
175static constexpr std::uint32_t kSecondsInYear = 365 * 24 * 60 * 60;
176
177Number
178loanPeriodicRate(TenthBips32 interestRate, std::uint32_t paymentInterval);
179
183inline Number
184roundPeriodicPayment(Asset const& asset, Number const& periodicPayment, std::int32_t scale)
185{
186 return roundToAsset(asset, periodicPayment, scale, Number::RoundingMode::Upward);
187}
188
189/* Represents the breakdown of amounts to be paid and changes applied to the
190 * Loan object while processing a loan payment.
191 *
192 * This structure is returned after processing a loan payment transaction and
193 * captures the amounts that need to be paid. The actual ledger entry changes
194 * are made in LoanPay based on this structure values.
195 *
196 * The sum of principalPaid, interestPaid, and feePaid represents the total
197 * amount to be deducted from the borrower's account. The valueChange field
198 * tracks whether the loan's total value increased or decreased beyond normal
199 * amortization.
200 *
201 * This structure is explained in the XLS-66 spec, section 3.2.4.2 (Payment
202 * Processing).
203 */
205{
206 // The amount of principal paid that reduces the loan balance.
207 // This amount is subtracted from sfPrincipalOutstanding in the Loan object
208 // and paid to the Vault
210
211 // The total amount of interest paid to the Vault.
212 // This includes:
213 // - Tracked interest from the amortization schedule
214 // - Untracked interest (e.g., late payment penalty interest)
215 // This value is always non-negative.
217
218 // The change in the loan's total value outstanding.
219 // - If valueChange < 0: Loan value decreased
220 // - If valueChange > 0: Loan value increased
221 // - If valueChange = 0: No value adjustment
222 //
223 // For regular on-time payments, this is always 0. Non-zero values occur
224 // when:
225 // - Overpayments reduce the loan balance beyond the scheduled amount
226 // - Late payments add penalty interest to the loan value
227 // - Early full payment may increase or decrease the loan value based on
228 // terms
230
231 /* The total amount of fees paid to the Broker.
232 * This includes:
233 * - Tracked management fees from the amortization schedule
234 * - Untracked fees (e.g., late payment fees, service fees, origination
235 * fees) This value is always non-negative.
236 */
238
240 operator+=(LoanPaymentParts const& other);
241
242 bool
243 operator==(LoanPaymentParts const& other) const;
244};
245
259{
260 // Total value still due to be paid by the borrower.
262 // Principal still due to be paid by the borrower.
264 // Interest still due to be paid to the Vault.
265 // This is a portion of interestOutstanding
267 // Management fee still due to be paid to the broker.
268 // This is a portion of interestOutstanding
270
271 // Interest still due to be paid by the borrower.
272 [[nodiscard]] Number
274 {
275 XRPL_ASSERT_PARTS(
277 "xrpl::LoanState::interestOutstanding",
278 "other values add up correctly");
280 }
281};
282
283/* Describes the initial computed properties of a loan.
284 *
285 * This structure contains the fundamental calculated values that define a
286 * loan's payment structure and amortization schedule. These properties are
287 * computed:
288 * - At loan creation (LoanSet transaction)
289 * - When loan terms change (e.g., after an overpayment that reduces the loan
290 * balance)
291 */
293{
294 // The unrounded amount to be paid at each regular payment period.
295 // Calculated using the standard amortization formula based on principal,
296 // interest rate, and number of payments.
297 // The actual amount paid in the LoanPay transaction must be rounded up to
298 // the precision of the asset and loan.
300
301 // The loan's current state, with all values rounded to the loan's scale.
303
304 // The scale (decimal places) used for rounding all loan amounts.
305 // This is the maximum of:
306 // - The asset's native scale
307 // - A minimum scale required to represent the periodic payment accurately
308 // All loan state values (principal, interest, fees) are rounded to this
309 // scale.
311
312 // The principal portion of the first payment.
314};
315
316// Some values get re-rounded to the vault scale any time they are adjusted. In
317// addition, they are prevented from ever going below zero. This helps avoid
318// accumulated rounding errors and leftover dust amounts.
319template <class NumberProxy>
320void
322 NumberProxy value,
323 Number const& adjustment,
324 Asset const& asset,
325 int vaultScale)
326{
327 value = roundToAsset(asset, value + adjustment, vaultScale);
328
329 if (*value < beast::kZero)
330 value = 0;
331}
332
348void
350 SLE::Ref brokerSle,
351 SLE::ConstRef vaultSle,
352 Number const& delta,
353 int vaultScale);
354
363Number
364minimumBrokerCover(Number const& debtTotal, TenthBips32 coverRateMinimum, SLE::ConstRef vaultSle);
365
366TER
368 Asset const& vaultAsset,
369 Number const& principalRequested,
370 bool expectInterest,
371 std::uint32_t paymentTotal,
372 LoanProperties const& properties,
374
375LoanState
377 Rules const& rules,
378 Number const& periodicPayment,
379 Number const& periodicRate,
380 std::uint32_t const paymentRemaining,
381 TenthBips32 const managementFeeRate);
382
383// Constructs a valid LoanState object from arbitrary inputs
384LoanState
386 Number const& totalValueOutstanding,
387 Number const& principalOutstanding,
388 Number const& managementFeeOutstanding);
389
390// Overload of constructLoanState() that reads the three tracked fields
391// directly from a Loan ledger object, which always holds rounded values,
392// rather than taking them as separate Number arguments.
393LoanState
395
396Number
398 Asset const& asset,
399 Number const& interest,
400 TenthBips32 managementFeeRate,
402
403Number
405 Number const& theoreticalPrincipalOutstanding,
406 Number const& periodicRate,
407 NetClock::time_point parentCloseTime,
408 std::uint32_t paymentInterval,
409 std::uint32_t prevPaymentDate,
410 std::uint32_t startDate,
411 TenthBips32 closeInterestRate);
412
413// Returns true if the loan's next payment is late per protocol rules. The
414// boundary is amendment-gated: with fixCleanup3_4_0 the due date must be
415// strictly in the past, otherwise the exact due-date instant counts as late.
416[[nodiscard]] bool
417isPaymentLate(ReadView const& view, SLE::ConstRef loanSle);
418
419// Deltas applied to Vault.AssetsTotal and LoanBroker.DebtTotal at a single
420// accounting touch point (origination, payment, impair/unimpair/default).
426
427// Instant interest recognition (pre-LendingProtocolV1_1): interest is
428// recognized into AssetsTotal/DebtTotal immediately, at origination.
430
431// LoanSet origination: what's added to Vault.AssetsTotal and LoanBroker.DebtTotal
433loanOriginationDeltas(Number const& principalRequested, Number const& interestDue);
434
435// LoanSet origination: would recognizing this loan's interest push
436// Vault.AssetsTotal past Vault.AssetsMaximum?
437bool
439 Number const& vaultMaximum,
440 Number const& vaultTotal,
441 Number const& interestDue);
442
443// LoanManage impair/unimpair/default: the vault's exposure to this loan
444Number
446
447// LoanPay: what's added to Vault.AssetsTotal and subtracted from LoanBroker.DebtTotal for a payment
450
451} // namespace instant_recognition
452
453// Cash-basis (LendingProtocolV1_1) recognition model: AssetsTotal/DebtTotal
454// are principal-only, interest is recognized only as it's actually paid.
455namespace cash_basis {
456
458loanOriginationDeltas(Number const& principalRequested);
459
460Number
462
465
466} // namespace cash_basis
467
468// Public dispatchers: pick cash_basis:: if the Vault's LEVersion
469// (VaultHelpers::getVaultVersion) is CashBasis or later (CashBasis or
470// FixedPrecision), else instant_recognition::. These are the only entry
471// points transactors call.
474 SLE::ConstRef vaultSle,
475 Number const& principalRequested,
476 Number const& interestDue);
477
478bool
480 SLE::ConstRef vaultSle,
481 Number const& vaultTotal,
482 Number const& interestDue);
483
484Number
486
488loanPaymentDeltas(SLE::ConstRef vaultSle, LoanPaymentParts const& parts);
489
490namespace detail {
491// These classes and functions should only be accessed by LendingHelper
492// functions and unit tests
493
495
496/* Represents a single loan payment component parts.
497
498* This structure captures the "delta" (change) values that will be applied to
499* the tracked fields in the Loan ledger object when a payment is processed.
500*
501* These are called "deltas" because they represent the amount by which each
502* corresponding field in the Loan object will be reduced.
503* They are "tracked" as they change tracked loan values.
504*/
506{
507 // The change in total value outstanding for this payment.
508 // This amount will be subtracted from sfTotalValueOutstanding in the Loan
509 // object. Equal to the sum of trackedPrincipalDelta,
510 // trackedInterestPart(), and trackedManagementFeeDelta.
512
513 // The change in principal outstanding for this payment.
514 // This amount will be subtracted from sfPrincipalOutstanding in the Loan
515 // object, representing the portion of the payment that reduces the
516 // original loan amount.
518
519 // The change in management fee outstanding for this payment.
520 // This amount will be subtracted from sfManagementFeeOutstanding in the
521 // Loan object. This represents only the tracked management fees from the
522 // amortization schedule and does not include additional untracked fees
523 // (such as late payment fees) that go directly to the broker.
525
526 // Indicates if this payment has special handling requirements.
527 // - none: Regular scheduled payment
528 // - final: The last payment that closes out the loan
529 // - extra: An additional payment beyond the regular schedule (overpayment)
531
540 [[nodiscard]] Number
541 trackedInterestPart() const;
542};
543
544/* Extends PaymentComponents with untracked payment amounts.
545 *
546 * This structure adds untracked fees and interest to the base
547 * PaymentComponents, representing amounts that don't affect the Loan object's
548 * tracked state but are still part of the total payment due from the borrower.
549 *
550 * Untracked amounts include:
551 * - Late payment fees that go directly to the Broker
552 * - Late payment penalty interest that goes directly to the Vault
553 * - Service fees
554 *
555 * The key distinction is that tracked amounts reduce the Loan object's state
556 * (sfTotalValueOutstanding, sfPrincipalOutstanding,
557 * sfManagementFeeOutstanding), while untracked amounts are paid directly to the
558 * recipient without affecting the loan's amortization schedule.
559 */
561{
562 // Additional management fees that go directly to the Broker.
563 // This includes fees not part of the standard amortization schedule
564 // (e.g., late fees, service fees, origination fees).
565 // This value may be negative, though the final value returned in
566 // LoanPaymentParts.feePaid will never be negative.
568
569 // Additional interest that goes directly to the Vault.
570 // This includes interest not part of the standard amortization schedule
571 // (e.g., late payment penalty interest).
572 // This value may be negative, though the final value returned in
573 // LoanPaymentParts.interestPaid will never be negative.
575
576 // The complete amount due from the borrower for this payment.
577 // Calculated as: trackedValueDelta + untrackedInterest +
578 // untrackedManagementFee
579 //
580 // This value is used to validate that the payment amount provided by the
581 // borrower is sufficient to cover all components of the payment.
583
591};
592
593/* Represents the differences between two loan states.
594 *
595 * This structure is used to capture the change in each component of a loan's
596 * state, typically when computing the difference between two LoanState objects
597 * (e.g., before and after a payment). It is a convenient way to capture changes
598 * in each component. How that difference is used depends on the context.
599 */
601{
602 // The difference in principal outstanding between two loan states.
604
605 // The difference in interest due between two loan states.
607
608 // The difference in management fee outstanding between two loan states.
610
615 [[nodiscard]] Number
616 total() const
617 {
619 }
620
621 // Ensures all delta values are non-negative.
622 void
623 nonNegative();
624};
625
626std::expected<std::pair<LoanPaymentParts, LoanProperties>, TER>
628 Rules const& rules,
629 Asset const& asset,
630 std::int32_t loanScale,
631 ExtendedPaymentComponents const& overpaymentComponents,
632 LoanState const& roundedLoanState,
633 Number const& periodicPayment,
634 Number const& periodicRate,
635 std::uint32_t paymentRemaining,
636 TenthBips16 const managementFeeRate,
638
639[[nodiscard]] Number
640computePowerMinusOne(Number const& periodicRate, std::uint32_t paymentsRemaining);
641
642[[nodiscard]] Number
643computePowerMinusOneHybrid(Number const& periodicRate, std::uint32_t paymentsRemaining);
644
645[[nodiscard]] Number
647 Rules const& rules,
648 Number const& periodicRate,
649 std::uint32_t paymentsRemaining);
650
653 Asset const& asset,
654 Number const& interest,
655 TenthBips16 managementFeeRate,
656 std::int32_t loanScale);
657
658Number
660 Rules const& rules,
661 Number const& principalOutstanding,
662 Number const& periodicRate,
663 std::uint32_t paymentsRemaining);
664
665Number
667 Rules const& rules,
668 Number const& periodicPayment,
669 Number const& periodicRate,
670 std::uint32_t paymentsRemaining);
671
672Number
674 Number const& principalOutstanding,
675 TenthBips32 lateInterestRate,
676 NetClock::time_point parentCloseTime,
677 std::uint32_t nextPaymentDueDate);
678
679Number
681 Number const& principalOutstanding,
682 Number const& periodicRate,
683 NetClock::time_point parentCloseTime,
684 std::uint32_t startDate,
685 std::uint32_t prevPaymentDate,
686 std::uint32_t paymentInterval);
687
688ExtendedPaymentComponents
690 Rules const& rules,
691 Asset const& asset,
692 int32_t const loanScale,
693 Number const& overpayment,
694 TenthBips32 const overpaymentInterestRate,
695 TenthBips32 const overpaymentFeeRate,
696 TenthBips16 const managementFeeRate);
697
698PaymentComponents
700 Rules const& rules,
701 Asset const& asset,
703 Number const& totalValueOutstanding,
704 Number const& principalOutstanding,
705 Number const& managementFeeOutstanding,
706 Number const& periodicPayment,
707 Number const& periodicRate,
708 std::uint32_t paymentRemaining,
709 TenthBips16 managementFeeRate);
710
711} // namespace detail
712
713detail::LoanStateDeltas
714operator-(LoanState const& lhs, LoanState const& rhs);
715
716LoanState
717operator-(LoanState const& lhs, detail::LoanStateDeltas const& rhs);
718
719LoanState
720operator+(LoanState const& lhs, detail::LoanStateDeltas const& rhs);
721
722LoanProperties
724 Rules const& rules,
725 Asset const& asset,
726 Number const& principalOutstanding,
727 TenthBips32 interestRate,
728 std::uint32_t paymentInterval,
729 std::uint32_t paymentsRemaining,
730 TenthBips32 managementFeeRate,
731 std::int32_t minimumScale);
732
733LoanProperties
735 Rules const& rules,
736 Asset const& asset,
737 Number const& principalOutstanding,
738 Number const& periodicRate,
739 std::uint32_t paymentsRemaining,
740 TenthBips32 managementFeeRate,
741 std::int32_t minimumScale);
742
743bool
744isRounded(Asset const& asset, Number const& value, std::int32_t scale);
745
746// Indicates what type of payment is being made.
747// regular, late, and full are mutually exclusive.
748// overpayment is an "add on" to a regular payment, and follows that path with
749// potential extra work at the end.
751
752std::expected<LoanPaymentParts, TER>
754 Asset const& asset,
755 ApplyView& view,
756 SLE::Ref loan,
757 SLE::ConstRef brokerSle,
758 STAmount const& amount,
759 LoanPaymentType const paymentType,
761
762} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
A view into a ledger.
Definition ReadView.h:41
Rules controlling protocol behavior.
Definition Rules.h:40
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::shared_ptr< STLedgerEntry > const & Ref
constexpr Zero kZero
Definition Zero.h:30
AccountingDeltas loanPaymentDeltas(LoanPaymentParts const &parts)
AccountingDeltas loanOriginationDeltas(Number const &principalRequested)
Number loanVaultExposure(SLE::ConstRef loanSle)
Number computePaymentFactor(Rules const &rules, Number const &periodicRate, std::uint32_t paymentsRemaining)
Number loanPrincipalFromPeriodicPayment(Rules const &rules, Number const &periodicPayment, Number const &periodicRate, std::uint32_t paymentsRemaining)
Number computePowerMinusOneHybrid(Number const &periodicRate, std::uint32_t paymentsRemaining)
Number loanPeriodicPayment(Rules const &rules, Number const &principalOutstanding, Number const &periodicRate, std::uint32_t paymentsRemaining)
Number loanAccruedInterest(Number const &principalOutstanding, Number const &periodicRate, NetClock::time_point parentCloseTime, std::uint32_t startDate, std::uint32_t prevPaymentDate, std::uint32_t paymentInterval)
STAmount roundToPosteriorBrokerCoverScale(SLE::ConstRef vault, SLE::ConstRef broker, STAmount const &delta, Number::RoundingMode roundingMode)
Round a cover outflow delta (cover withdraw, cover clawback, the broker-side decrease of a default's ...
int getPosteriorBrokerCoverScale(SLE::ConstRef vault, SLE::ConstRef broker, Number const &delta)
Return a LoanBroker's posterior live cover exponent after applying an unrounded delta.
std::pair< Number, Number > computeInterestAndFeeParts(Asset const &asset, Number const &interest, TenthBips16 managementFeeRate, std::int32_t loanScale)
Number loanLatePaymentInterest(Number const &principalOutstanding, TenthBips32 lateInterestRate, NetClock::time_point parentCloseTime, std::uint32_t nextPaymentDueDate)
std::expected< std::pair< LoanPaymentParts, LoanProperties >, TER > tryOverpayment(Rules const &rules, Asset const &asset, std::int32_t loanScale, ExtendedPaymentComponents const &overpaymentComponents, LoanState const &roundedLoanState, Number const &periodicPayment, Number const &periodicRate, std::uint32_t paymentRemaining, TenthBips16 const managementFeeRate, beast::Journal j)
ExtendedPaymentComponents computeOverpaymentComponents(Rules const &rules, Asset const &asset, int32_t const loanScale, Number const &overpayment, TenthBips32 const overpaymentInterestRate, TenthBips32 const overpaymentFeeRate, TenthBips16 const managementFeeRate)
Number computePowerMinusOne(Number const &periodicRate, std::uint32_t paymentsRemaining)
PaymentComponents computePaymentComponents(Rules const &rules, Asset const &asset, std::int32_t scale, Number const &totalValueOutstanding, Number const &principalOutstanding, Number const &managementFeeOutstanding, Number const &periodicPayment, Number const &periodicRate, std::uint32_t paymentRemaining, TenthBips16 managementFeeRate)
AccountingDeltas loanPaymentDeltas(LoanPaymentParts const &parts)
Number loanVaultExposure(SLE::ConstRef loanSle)
AccountingDeltas loanOriginationDeltas(Number const &principalRequested, Number const &interestDue)
bool loanOriginationExceedsVaultMaximum(Number const &vaultMaximum, Number const &vaultTotal, Number const &interestDue)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
static constexpr Number kNumZero
Definition Number.h:667
constexpr BaseUInt< Bits, Tag > operator+(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
Definition base_uint.h:650
void adjustBrokerDebtTotal(SLE::Ref brokerSle, SLE::ConstRef vaultSle, Number const &delta, int vaultScale)
Apply a signed delta to a LoanBroker's DebtTotal: on FixedPrecision Vaults, add the exact delta (Debt...
Number loanPeriodicRate(TenthBips32 interestRate, std::uint32_t paymentInterval)
bool loanOriginationExceedsVaultMaximum(SLE::ConstRef vaultSle, Number const &vaultTotal, Number const &interestDue)
STAmount debitToPosteriorBrokerCoverScale(SLE::ConstRef vault, SLE::ConstRef broker, STAmount const &amount, Number::RoundingMode roundingMode)
Round a cover debit (cover withdraw, cover clawback, the broker-side decrease of a default's cover cr...
Number operator-(Number const &x, Number const &y)
Definition Number.h:793
void adjustImpreciseNumber(NumberProxy value, Number const &adjustment, Asset const &asset, int vaultScale)
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
Definition STAmount.h:819
std::expected< LoanPaymentParts, TER > loanMakePayment(Asset const &asset, ApplyView &view, SLE::Ref loan, SLE::ConstRef brokerSle, STAmount const &amount, LoanPaymentType const paymentType, beast::Journal j)
TenthBips< std::uint32_t > TenthBips32
Definition Units.h:454
TenthBips< std::uint16_t > TenthBips16
Definition Units.h:453
TER checkLoanGuards(Asset const &vaultAsset, Number const &principalRequested, bool expectInterest, std::uint32_t paymentTotal, LoanProperties const &properties, beast::Journal j)
Number minimumBrokerCover(Number const &debtTotal, TenthBips32 coverRateMinimum, SLE::ConstRef vaultSle)
Minimum required broker cover, rounded up.
bool isPaymentLate(ReadView const &view, SLE::ConstRef loanSle)
std::optional< LoanDefaultFreezeExemptAccounts > getLoanDefaultFreezeExemptAccounts(ReadView const &view, STTx const &tx)
Resolves the accounts and asset a LoanManage default transaction is exempt from freeze/lock for.
LoanState computeTheoreticalLoanState(Rules const &rules, Number const &periodicPayment, Number const &periodicRate, std::uint32_t const paymentRemaining, TenthBips32 const managementFeeRate)
AccountingDeltas loanPaymentDeltas(SLE::ConstRef vaultSle, LoanPaymentParts const &parts)
TER checkOptionalBrokerCoverInflow(SLE::ConstRef vault, SLE::ConstRef broker, STAmount const &amount)
Check whether amount is an admissible optional cover inflow.
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
Definition STAmount.h:745
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
Number roundPeriodicPayment(Asset const &asset, Number const &periodicPayment, std::int32_t scale)
Ensure the periodic payment is always rounded consistently.
Number computeManagementFee(Asset const &asset, Number const &interest, TenthBips32 managementFeeRate, std::int32_t scale)
TERSubset< CanCvtToTER > TER
Definition TER.h:657
Number loanVaultExposure(SLE::ConstRef vaultSle, SLE::ConstRef loanSle)
static constexpr std::uint32_t kSecondsInYear
LoanProperties computeLoanProperties(Rules const &rules, Asset const &asset, Number const &principalOutstanding, TenthBips32 interestRate, std::uint32_t paymentInterval, std::uint32_t paymentsRemaining, TenthBips32 managementFeeRate, std::int32_t minimumScale)
AccountingDeltas loanOriginationDeltas(SLE::ConstRef vaultSle, Number const &principalRequested, Number const &interestDue)
LoanState constructLoanState(Number const &totalValueOutstanding, Number const &principalOutstanding, Number const &managementFeeOutstanding)
TER canApplyToBrokerCover(ReadView const &view, SLE::ConstRef sleBroker, Asset const &vaultAsset, STAmount const &amount, beast::Journal j, std::string_view logPrefix)
Broker cover preclaim precision guard (fixCleanup3_2_0).
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)
bool checkLendingProtocolDependencies(Rules const &rules, STTx const &tx)
STAmount creditToPosteriorBrokerCoverScale(SLE::ConstRef vault, SLE::ConstRef broker, Number const &raw, Number::RoundingMode roundingMode)
Round a cover inflow (cover deposit, LoanPay's fee redirect into cover) into CoverAvailable by floori...
bool isRounded(Asset const &asset, Number const &value, std::int32_t scale)
The accounts and asset that LoanManage::defaultLoan's fixCleanup3_4_0 freeze/lock exemption applies t...
bool operator==(LoanPaymentParts const &other) const
LoanPaymentParts & operator+=(LoanPaymentParts const &other)
This structure captures the parts of a loan state.
Number principalOutstanding
Number interestOutstanding() const
ExtendedPaymentComponents(PaymentComponents const &p, Number fee, Number interest=kNumZero)
Number total() const
Calculates the total change across all components.
Number trackedInterestPart() const
Calculates the tracked interest portion of this payment.