54 bool const lpV11Enabled = view.
rules().
enabled(featureLendingProtocolV1_1);
88 JLOG(j.
fatal()) <<
"Invariant failed: closed-ended loan final payment "
89 "must precede RedemptionDate by at least "
90 "kLoanRedemptionBuffer";
99 if (
after->at(sfPaymentRemaining) == 0 &&
104 JLOG(j.
fatal()) <<
"Invariant failed: Loan with zero payments "
105 "remaining has not been paid off";
110 if (
after->at(sfPaymentRemaining) != 0 &&
115 JLOG(j.
fatal()) <<
"Invariant failed: Fully paid off Loan still has payments remaining";
121 if (!lpV11Enabled && before &&
122 (before->isFlag(lsfLoanOverpayment) !=
after->isFlag(lsfLoanOverpayment)))
124 JLOG(j.
fatal()) <<
"Invariant failed: Loan Overpayment flag changed";
128 for (
auto const field : {
132 &sfPrincipalOutstanding,
133 &sfTotalValueOutstanding,
134 &sfManagementFeeOutstanding,
137 if (
after->at(*field) < 0)
139 JLOG(j.
fatal()) <<
"Invariant failed: " << field->getName() <<
" is negative ";
144 for (
auto const field : {
148 if (
after->at(*field) <= 0)
150 JLOG(j.
fatal()) <<
"Invariant failed: " << field->getName()
151 <<
" is zero or negative ";
158 if (!before && txType != ttLOAN_SET)
160 JLOG(j.
fatal()) <<
"Invariant failed: Loan created by a transaction "
161 "other than LoanSet";
165 if (
after->at(sfPaymentRemaining) == 0 &&
166 after->at(~sfNextPaymentDueDate).value_or(0) != 0)
168 JLOG(j.
fatal()) <<
"Invariant failed: Loan with zero payments must have zero next "
175 bool const wasImpaired = before->isFlag(lsfLoanImpaired);
176 bool const isImpaired =
after->isFlag(lsfLoanImpaired);
177 bool const wasDefaulted = before->isFlag(lsfLoanDefault);
178 bool const isDefaulted =
after->isFlag(lsfLoanDefault);
180 if (wasImpaired != isImpaired && txType != ttLOAN_MANAGE && txType != ttLOAN_PAY)
182 JLOG(j.
fatal()) <<
"Invariant failed: lsfLoanImpaired changed "
183 "outside LoanManage or LoanPay";
186 if (wasDefaulted != isDefaulted && txType != ttLOAN_MANAGE)
188 JLOG(j.
fatal()) <<
"Invariant failed: lsfLoanDefault changed "
189 "outside LoanManage";
200 JLOG(j.
fatal()) <<
"Invariant failed: Loan broker does not exist";
206 JLOG(j.
fatal()) <<
"Invariant failed: Loan broker vault does not exist";
216 auto const interestDue =
after->at(sfTotalValueOutstanding) -
217 after->at(sfPrincipalOutstanding) -
after->at(sfManagementFeeOutstanding);
221 bool const integral =
Asset{vaultSle->at(sfAsset)}.
integral();
224 if (interestDue < tolerance)
226 JLOG(j.
fatal()) <<
"Invariant failed: Loan interest due is negative";
244 if (before &&
after->at(sfPaymentRemaining) != 0)
246 if (
after->at(sfPrincipalOutstanding) > before->at(sfPrincipalOutstanding))
248 JLOG(j.
fatal()) <<
"Invariant failed: loan pay must not increase "
249 "PrincipalOutstanding on a non-full-repayment";
252 if (
after->at(sfTotalValueOutstanding) > before->at(sfTotalValueOutstanding))
254 JLOG(j.
fatal()) <<
"Invariant failed: loan pay must not increase "
255 "TotalValueOutstanding on a non-full-repayment";
258 if (
after->at(sfPrincipalOutstanding) == before->at(sfPrincipalOutstanding) &&
259 after->at(sfTotalValueOutstanding) == before->at(sfTotalValueOutstanding))
261 JLOG(j.
fatal()) <<
"Invariant failed: loan pay must decrease "
262 "PrincipalOutstanding or TotalValueOutstanding "
263 "on a non-full-repayment";
266 if (
after->at(sfPaymentRemaining) >= before->at(sfPaymentRemaining))
268 JLOG(j.
fatal()) <<
"Invariant failed: loan pay must decrease "
269 "PaymentRemaining on a non-full-repayment";
273 std::uint32_t const beforeDue = before->at(~sfNextPaymentDueDate).value_or(0);
276 if (afterDue <= beforeDue || interval == 0 ||
277 (afterDue - beforeDue) % interval != 0)
279 JLOG(j.
fatal()) <<
"Invariant failed: loan pay must advance "
280 "NextPaymentDueDate by a positive multiple of "
281 "PaymentInterval on a non-full-repayment";
290 if (lpV11Enabled && txType != ttLOAN_DELETE && !
deletedLoans_.empty())
292 JLOG(j.
fatal()) <<
"Invariant failed: Loan deleted by a transaction "
293 "other than LoanDelete";
std::shared_ptr< STLedgerEntry const > const & ConstRef