xrpld
Loading...
Searching...
No Matches
LoanManage.cpp
1#include <xrpl/tx/transactors/lending/LoanManage.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/core/ServiceRegistry.h>
8#include <xrpl/ledger/ApplyView.h>
9#include <xrpl/ledger/View.h>
10#include <xrpl/ledger/helpers/LendingHelpers.h>
11#include <xrpl/ledger/helpers/TokenHelpers.h>
12#include <xrpl/ledger/helpers/VaultHelpers.h>
13#include <xrpl/protocol/Asset.h>
14#include <xrpl/protocol/Feature.h>
15#include <xrpl/protocol/Indexes.h>
16#include <xrpl/protocol/LedgerFormats.h>
17#include <xrpl/protocol/Protocol.h>
18#include <xrpl/protocol/SField.h>
19#include <xrpl/protocol/STAmount.h>
20#include <xrpl/protocol/STLedgerEntry.h>
21#include <xrpl/protocol/STTakesAsset.h>
22#include <xrpl/protocol/STTx.h>
23#include <xrpl/protocol/TER.h>
24#include <xrpl/protocol/TxFlags.h>
25#include <xrpl/protocol/Units.h>
26#include <xrpl/protocol/XRPAmount.h>
27#include <xrpl/tx/Transactor.h>
28
29#include <algorithm>
30#include <cstdint>
31namespace xrpl {
32
33bool
38
41{
42 return tfLoanManageMask;
43}
44
47{
48 if (ctx.tx[sfLoanID] == beast::kZero)
49 return temINVALID;
50
51 // Flags are mutually exclusive
52 if (auto const flagField = ctx.tx[~sfFlags]; flagField && (*flagField != 0u))
53 {
54 auto const flags = *flagField & tfUniversalMask;
55 if ((flags & (flags - 1)) != 0)
56 {
57 JLOG(ctx.j.warn()) << "LoanManage: Only one of tfLoanDefault, tfLoanImpair, or "
58 "tfLoanUnimpair can be set.";
59 return temINVALID_FLAG;
60 }
61 }
62
63 return tesSUCCESS;
64}
65
66TER
68{
69 auto const& tx = ctx.tx;
70
71 auto const account = tx[sfAccount];
72 auto const loanID = tx[sfLoanID];
73
74 auto const loanSle = ctx.view.read(keylet::loan(loanID));
75 if (!loanSle)
76 {
77 JLOG(ctx.j.warn()) << "Loan does not exist.";
78 return tecNO_ENTRY;
79 }
80 // Impairment only allows certain transitions.
81 // 1. Once it's in default, it can't be changed.
82 // 2. It can get worse: unimpaired -> impaired -> default
83 // or unimpaired -> default
84 // 3. It can get better: impaired -> unimpaired
85 // 4. If it's in a state, it can't be put in that state again.
86 if (loanSle->isFlag(lsfLoanDefault))
87 {
88 JLOG(ctx.j.warn()) << "Loan is in default. A defaulted loan can not be modified.";
89 return tecNO_PERMISSION;
90 }
91 if (loanSle->isFlag(lsfLoanImpaired) && tx.isFlag(tfLoanImpair))
92 {
93 JLOG(ctx.j.warn()) << "Loan is impaired. A loan can not be impaired twice.";
94 return tecNO_PERMISSION;
95 }
96 if (!(loanSle->isFlag(lsfLoanImpaired) || loanSle->isFlag(lsfLoanDefault)) &&
97 tx.isFlag(tfLoanUnimpair))
98 {
99 JLOG(ctx.j.warn()) << "Loan is unimpaired. Can not be unimpaired again.";
100 return tecNO_PERMISSION;
101 }
102 if (loanSle->at(sfPaymentRemaining) == 0)
103 {
104 JLOG(ctx.j.warn()) << "Loan is fully paid. A loan can not be modified "
105 "after it is fully paid.";
106 return tecNO_PERMISSION;
107 }
108 if (tx.isFlag(tfLoanDefault) &&
109 !hasExpired(
110 ctx.view,
111 loanSle->at(sfNextPaymentDueDate) + loanSle->at(sfGracePeriod),
112 ctx.view.rules().enabled(fixCleanup3_4_0) ? ExpiryComparison::Exclusive
114 {
115 JLOG(ctx.j.warn()) << "A loan can not be defaulted before the next payment due date.";
116 return tecTOO_SOON;
117 }
118
119 auto const loanBrokerID = loanSle->at(sfLoanBrokerID);
120 auto const loanBrokerSle = ctx.view.read(keylet::loanBroker(loanBrokerID));
121 if (!loanBrokerSle)
122 {
123 // should be impossible
124 return tecINTERNAL; // LCOV_EXCL_LINE
125 }
126 if (loanBrokerSle->at(sfOwner) != account)
127 {
128 JLOG(ctx.j.warn()) << "LoanBroker for Loan does not belong to the account. LoanManage "
129 "can only be submitted by the Loan Broker.";
130 return tecNO_PERMISSION;
131 }
132
133 return tesSUCCESS;
134}
135
138{
139 auto const vaultAsset = vaultSle->at(sfAsset);
140
141 Number const principalOutstanding = loanSle->at(sfPrincipalOutstanding);
142
143 TenthBips32 const coverRateMinimum{brokerSle->at(sfCoverRateMinimum)};
144 TenthBips32 const coverRateLiquidation{brokerSle->at(sfCoverRateLiquidation)};
145
146 Number const rawCover = [&] {
148 // Compute minimum cover
149 Number const minimumCover =
150 tenthBipsOfValue(brokerSle->at(sfDebtTotal).value(), coverRateMinimum);
151
152 // Apply coverRateLiquidation
153 return std::min(tenthBipsOfValue(minimumCover, coverRateLiquidation), principalOutstanding);
154 }();
155
156 // rawCover is a product of multiplication and can carry more digits than STAmount's. Keep the
157 // cap as an exact Number: rounding it to 16 digits first could drop digits that change
158 // floor16(AssetsAvailable + cover).
159 Number const cappedCover = std::min(rawCover, *brokerSle->at(sfCoverAvailable));
160
161 if (Asset{vaultAsset}.integral())
162 {
163 // Fix the mode so the STAmount conversion truncates the fractional part.
165 return STAmount{vaultAsset, cappedCover};
166 }
167
168 // Floor AssetsAvailable + cover at its posterior grid so the Vault side is exact, unless
169 // AssetsAvailable has digits finer than the 16-digit cover can carry. The LoanBroker absorbs
170 // the rounding: CoverAvailable - cover may need finer digits than its posterior grid and
171 // rounds to nearest, as the broker's trust line does. Downward also keeps the 19-digit
172 // AssetsAvailable + cover sum from rounding up past cappedCover.
175}
176
177namespace {
178
179// Defaults a Loan on a FixedPrecision Vault. Cover is the raw XLS-66
180// First-Loss Capital amount, capped at the LoanBroker's CoverAvailable and
181// floored so AssetsAvailable + cover lands exactly on its posterior grid (a
182// no-op for integral assets). AssetsAvailable rises by cover through the cash writer;
183// AssetsDeployed drops by the Loan's full PrincipalOutstanding exactly, and
184// AssetsTotal is re-derived from AssetsAvailable + AssetsDeployed by the
185// writer's sync -- this function never touches sfAssetsTotal directly.
186[[nodiscard]] TER
187defaultLoanFixedPrecision(
188 ApplyView& view,
189 SLE::Ref loanSle,
190 SLE::Ref brokerSle,
191 SLE::Ref vaultSle,
193{
194 Number const principalOutstanding = loanSle->at(sfPrincipalOutstanding);
195 Number const scheduledInterest = loanSle->at(sfTotalValueOutstanding) - principalOutstanding -
196 loanSle->at(sfManagementFeeOutstanding);
197
198 STAmount const coverAmount = LoanManage::calculateDefaultCover(loanSle, brokerSle, vaultSle);
199
200 // The broker's CoverAvailable decrease and the broker-to-vault transfer
201 // both use this amount. AssetsAvailable + cover is exact unless
202 // AssetsAvailable has digits finer than the 16-digit cover can carry; then
203 // adjustVaultBalances rounds the sum. CoverAvailable - cover may need finer
204 // digits than the broker's posterior grid; it rounds to nearest, as the
205 // broker's trust line does.
206
207 Number const loss = loanSle->isFlag(lsfLoanImpaired) ? -principalOutstanding : 0;
208 if (auto const ter = adjustVaultBalances(
209 vaultSle,
210 {
211 .cash = coverAmount,
212 .deployed = -principalOutstanding,
213 .yield = -scheduledInterest,
214 .loss = loss,
215 },
216 j);
217 !isTesSuccess(ter))
218 return ter; // LCOV_EXCL_LINE
219 view.update(vaultSle);
220
221 brokerSle->at(sfDebtTotal) -= principalOutstanding;
222 brokerSle->at(sfCoverAvailable) -= coverAmount;
223 view.update(brokerSle);
224
225 loanSle->setFlag(lsfLoanDefault);
226 loanSle->at(sfTotalValueOutstanding) = 0;
227 loanSle->at(sfPaymentRemaining) = 0;
228 loanSle->at(sfPrincipalOutstanding) = 0;
229 loanSle->at(sfManagementFeeOutstanding) = 0;
230 loanSle->at(sfNextPaymentDueDate) = 0;
231 view.update(loanSle);
232
233 if (coverAmount == beast::kZero)
234 return tesSUCCESS;
235
236 return accountSend(
237 view,
238 brokerSle->at(sfAccount),
239 vaultSle->at(sfAccount),
240 coverAmount,
241 j,
242 {},
244}
245
246} // namespace
247
248TER
251 SLE::Ref loanSle,
252 SLE::Ref brokerSle,
253 SLE::Ref vaultSle,
255{
257 return defaultLoanFixedPrecision(view, loanSle, brokerSle, vaultSle, j);
258
259 auto const vaultAsset = *vaultSle->at(sfAsset);
260 // Calculate the amount of the Default that First-Loss Capital covers:
261
262 std::int32_t const loanScale = loanSle->at(sfLoanScale);
263 auto brokerDebtTotalProxy = brokerSle->at(sfDebtTotal);
264
265 Number const totalDefaultAmount = loanVaultExposure(vaultSle, loanSle);
266
267 // Apply the First-Loss Capital to the Default Amount
268 TenthBips32 const coverRateMinimum{brokerSle->at(sfCoverRateMinimum)};
269 TenthBips32 const coverRateLiquidation{brokerSle->at(sfCoverRateLiquidation)};
270 auto const defaultCovered = [&] {
271 // Always round the minimum required up.
273 auto const minimumCover = tenthBipsOfValue(brokerDebtTotalProxy.value(), coverRateMinimum);
274 // Round the liquidation amount up, too
275 auto const covered = roundToAsset(
276 vaultAsset,
277 /*
278 * This formula is from the XLS-66 spec, section 3.2.3.2 (State
279 * Changes), specifically "if the `tfLoanDefault` flag is set" /
280 * "Apply the First-Loss Capital to the Default Amount"
281 */
282 std::min(tenthBipsOfValue(minimumCover, coverRateLiquidation), totalDefaultAmount),
283 loanScale);
284 auto const coverAvailable = *brokerSle->at(sfCoverAvailable);
285
286 return std::min(covered, coverAvailable);
287 }();
288
289 auto const vaultDefaultAmount = totalDefaultAmount - defaultCovered;
290
291 // Update the Vault object:
292
293 // The vault may be at a different scale than the loan. Reduce rounding
294 // errors during the accounting by rounding some of the values to that
295 // scale.
296 auto const vaultScale = getVaultScale(vaultSle);
297
298 {
299 // Decrease the Total Value of the Vault:
300 auto vaultTotalProxy = vaultSle->at(sfAssetsTotal);
301 auto vaultAvailableProxy = vaultSle->at(sfAssetsAvailable);
302
303 if (vaultTotalProxy < vaultDefaultAmount)
304 {
305 // LCOV_EXCL_START
306 JLOG(j.warn()) << "Vault total assets is less than the vault default amount";
307 return tefBAD_LEDGER;
308 // LCOV_EXCL_STOP
309 }
310
311 auto const vaultDefaultRounded = roundToAsset(
312 vaultAsset, vaultDefaultAmount, vaultScale, Number::RoundingMode::Downward);
313 vaultTotalProxy -= vaultDefaultRounded;
314 // Increase the Asset Available of the Vault by liquidated First-Loss
315 // Capital and any unclaimed funds amount:
316 vaultAvailableProxy += defaultCovered;
317 if (*vaultAvailableProxy > *vaultTotalProxy && !vaultAsset.integral())
318 {
319 auto const difference = vaultAvailableProxy - vaultTotalProxy;
320 JLOG(j.debug()) << "Vault assets available: " << *vaultAvailableProxy << "("
321 << vaultAvailableProxy.value().exponent()
322 << "), Total: " << *vaultTotalProxy << "("
323 << vaultTotalProxy.value().exponent() << "), Difference: " << difference
324 << "(" << difference.exponent() << ")";
325 if (vaultAvailableProxy.value().exponent() - difference.exponent() > 13)
326 {
327 // If the difference is dust, bring the total up to match
328 // the available
329 JLOG(j.debug()) << "Difference between vault assets available and total is "
330 "dust. Set both to the larger value.";
331 vaultTotalProxy = vaultAvailableProxy;
332 }
333 }
334 if (*vaultAvailableProxy > *vaultTotalProxy)
335 {
336 // LCOV_EXCL_START
337 JLOG(j.fatal()) << "Vault assets available must not be greater "
338 "than assets outstanding. Available: "
339 << *vaultAvailableProxy << ", Total: " << *vaultTotalProxy;
340 return tecINTERNAL;
341 // LCOV_EXCL_STOP
342 }
343
344 // The loss has been realized
345 if (loanSle->isFlag(lsfLoanImpaired))
346 {
347 auto const vaultLossUnrealizedProxy = vaultSle->at(sfLossUnrealized);
348 if (vaultLossUnrealizedProxy < totalDefaultAmount)
349 {
350 // LCOV_EXCL_START
351 JLOG(j.warn()) << "Vault unrealized loss is less than the default amount";
352 return tefBAD_LEDGER;
353 // LCOV_EXCL_STOP
354 }
356 vaultLossUnrealizedProxy, -totalDefaultAmount, vaultAsset, vaultScale);
357 }
358 view.update(vaultSle);
359 }
360
361 // Update the LoanBroker object:
362
363 {
364 // Decrease the Debt of the LoanBroker:
365 adjustImpreciseNumber(brokerDebtTotalProxy, -totalDefaultAmount, vaultAsset, vaultScale);
366 // Decrease the First-Loss Capital Cover Available:
367 auto coverAvailableProxy = brokerSle->at(sfCoverAvailable);
368 if (coverAvailableProxy < defaultCovered)
369 {
370 // LCOV_EXCL_START
371 JLOG(j.warn()) << "LoanBroker cover available is less than amount covered";
372 return tefBAD_LEDGER;
373 // LCOV_EXCL_STOP
374 }
375 coverAvailableProxy -= defaultCovered;
376 view.update(brokerSle);
377 }
378
379 // Update the Loan object:
380 loanSle->setFlag(lsfLoanDefault);
381
382 loanSle->at(sfTotalValueOutstanding) = 0;
383 loanSle->at(sfPaymentRemaining) = 0;
384 loanSle->at(sfPrincipalOutstanding) = 0;
385 loanSle->at(sfManagementFeeOutstanding) = 0;
386 // Zero out the next due date. Since it's default, it'll be removed from
387 // the object.
388 loanSle->at(sfNextPaymentDueDate) = 0;
389 view.update(loanSle);
390
391 // Return funds from the LoanBroker pseudo-account to the
392 // Vault pseudo-account:
393 return accountSend(
394 view,
395 brokerSle->at(sfAccount),
396 vaultSle->at(sfAccount),
397 STAmount{vaultAsset, defaultCovered},
398 j,
399 {},
401}
402
403TER
406 SLE::Ref loanSle,
407 SLE::Ref vaultSle,
408 Asset const& vaultAsset,
410{
411 bool const fixEnabled340 = view.rules().enabled(fixCleanup3_4_0);
412
413 if (fixEnabled340 && !isPaymentLate(view, loanSle))
414 {
415 JLOG(j.warn()) << "Cannot impair a loan that is not late";
416 return tecTOO_SOON;
417 }
418
419 bool const fixedPrecision = getVaultVersion(vaultSle) == VaultVersion::FixedPrecision;
420 Number const lossUnrealized = loanVaultExposure(vaultSle, loanSle);
421
422 // Update the Vault object (set "paper loss")
423 if (fixedPrecision)
424 {
425 // adjustVaultBalances's own LU' > AD' check covers the comparison
426 // against AssetsDeployed.
427 if (auto const ter = adjustVaultBalances(vaultSle, {.loss = lossUnrealized}, j);
428 !isTesSuccess(ter))
429 {
430 // LCOV_EXCL_START
431 JLOG(j.warn()) << "Vault unrealized loss is too large, and will corrupt the vault.";
432 return ter;
433 // LCOV_EXCL_STOP
434 }
435 }
436 else
437 {
438 auto const vaultLossUnrealizedProxy = vaultSle->at(sfLossUnrealized);
439 // The vault may be at a different scale than the loan. Reduce rounding
440 // errors during the accounting by rounding some of the values to that
441 // scale.
442 auto const vaultScale = getVaultScale(vaultSle);
443 adjustImpreciseNumber(vaultLossUnrealizedProxy, lossUnrealized, vaultAsset, vaultScale);
444 if (vaultLossUnrealizedProxy >
445 vaultSle->at(sfAssetsTotal) - vaultSle->at(sfAssetsAvailable))
446 {
447 // Having a loss greater than the vault's unavailable assets
448 // will leave the vault in an invalid / inconsistent state.
449 JLOG(j.warn()) << "Vault unrealized loss is too large, and will corrupt the vault.";
450 return tecLIMIT_EXCEEDED; // LCOV_EXCL_LINE
451 }
452 }
453 view.update(vaultSle);
454
455 // Update the Loan object
456 loanSle->setFlag(lsfLoanImpaired);
457
458 if (!fixEnabled340)
459 {
460 auto loanNextDueProxy = loanSle->at(sfNextPaymentDueDate);
461 if (!isPaymentLate(view, loanSle))
462 {
463 // loan payment is not yet late move the next payment due date to now
464 loanNextDueProxy = view.parentCloseTime().time_since_epoch().count();
465 }
466 }
467 view.update(loanSle);
468
469 return tesSUCCESS;
470}
471
472[[nodiscard]] TER
475 SLE::Ref loanSle,
476 SLE::Ref vaultSle,
477 Asset const& vaultAsset,
479{
480 // Update the Vault object (clear "paper loss")
481 bool const fixedPrecision = getVaultVersion(vaultSle) == VaultVersion::FixedPrecision;
482 Number const lossReversed = loanVaultExposure(vaultSle, loanSle);
483 if (fixedPrecision)
484 {
485 // adjustVaultBalances's own LU' < 0 check covers the comparison.
486 if (auto const ter = adjustVaultBalances(vaultSle, {.loss = -lossReversed}, j);
487 !isTesSuccess(ter))
488 return ter; // LCOV_EXCL_LINE
489 }
490 else
491 {
492 auto const vaultLossUnrealizedProxy = vaultSle->at(sfLossUnrealized);
493 if (vaultLossUnrealizedProxy < lossReversed)
494 {
495 // LCOV_EXCL_START
496 JLOG(j.warn()) << "Vault unrealized loss is less than the amount to be cleared";
497 return tefBAD_LEDGER;
498 // LCOV_EXCL_STOP
499 }
500 // The vault may be at a different scale than the loan. Reduce rounding
501 // errors during the accounting by rounding some of the values to that
502 // scale.
503 auto const vaultScale = getVaultScale(vaultSle);
504 adjustImpreciseNumber(vaultLossUnrealizedProxy, -lossReversed, vaultAsset, vaultScale);
505 }
506
507 view.update(vaultSle);
508
509 // Update the Loan object
510 loanSle->clearFlag(lsfLoanImpaired);
511 if (!view.rules().enabled(fixCleanup3_4_0))
512 {
513 auto const paymentInterval = loanSle->at(sfPaymentInterval);
514 auto const normalPaymentDueDate =
515 std::max(loanSle->at(sfPreviousPaymentDueDate), loanSle->at(sfStartDate)) +
516 paymentInterval;
517
518 if (!hasExpired(view, normalPaymentDueDate))
519 {
520 // loan was unimpaired within the payment interval
521 loanSle->at(sfNextPaymentDueDate) = normalPaymentDueDate;
522 }
523 else
524 {
525 // loan was unimpaired after the original payment due date
526 loanSle->at(sfNextPaymentDueDate) =
527 view.parentCloseTime().time_since_epoch().count() + paymentInterval;
528 }
529 }
530 view.update(loanSle);
531
532 return tesSUCCESS;
533}
534
535TER
537{
538 auto const& tx = ctx_.tx;
539 auto& view = ctx_.view();
540
541 auto const loanID = tx[sfLoanID];
542 auto const loanSle = view.peek(keylet::loan(loanID));
543 if (!loanSle)
544 return tefBAD_LEDGER; // LCOV_EXCL_LINE
545
546 auto const brokerID = loanSle->at(sfLoanBrokerID);
547 auto const brokerSle = view.peek(keylet::loanBroker(brokerID));
548 if (!brokerSle)
549 return tefBAD_LEDGER; // LCOV_EXCL_LINE
550
551 auto const vaultSle = view.peek(keylet::vault(brokerSle->at(sfVaultID)));
552 if (!vaultSle)
553 return tefBAD_LEDGER; // LCOV_EXCL_LINE
554 auto const vaultAsset = vaultSle->at(sfAsset);
555
556 auto const result = [&] -> TER {
557 // Valid flag combinations are checked in preflight. No flags is valid -
558 // just a noop.
559 if (tx.isFlag(tfLoanDefault))
560 return defaultLoan(view, loanSle, brokerSle, vaultSle, j_);
561 if (tx.isFlag(tfLoanImpair))
562 return impairLoan(view, loanSle, vaultSle, vaultAsset, j_);
563 if (tx.isFlag(tfLoanUnimpair))
564 return unimpairLoan(view, loanSle, vaultSle, vaultAsset, j_);
565 // NoOp, as described above.
566 return tesSUCCESS;
567 }();
568
569 // Pre-amendment, associateAsset was only called on the noop (no flags)
570 // path. Post-amendment, we call associateAsset on all successful paths.
571 if (view.rules().enabled(fixCleanup3_1_3) && isTesSuccess(result))
572 {
573 bool const fixedPrecision = getVaultVersion(vaultSle) == VaultVersion::FixedPrecision;
574 [[maybe_unused]] bool const assetsEqualBeforeStore =
575 fixedPrecision && vaultSle->at(sfAssetsAvailable) == vaultSle->at(sfAssetsTotal);
576
577 associateAsset(*loanSle, vaultAsset);
578 associateAsset(*brokerSle, vaultAsset);
579 associateAsset(*vaultSle, vaultAsset);
580
581 if (fixedPrecision)
582 {
583 [[maybe_unused]] Number const assetsAvailableAfter = *vaultSle->at(sfAssetsAvailable);
584 [[maybe_unused]] Number const assetsTotalAfter = *vaultSle->at(sfAssetsTotal);
585 XRPL_ASSERT_PARTS(
586 assetsAvailableAfter <= assetsTotalAfter,
587 "xrpl::LoanManage::doApply",
588 "assets available must not be greater than assets outstanding");
589 XRPL_ASSERT_PARTS(
590 !assetsEqualBeforeStore || assetsAvailableAfter == assetsTotalAfter,
591 "xrpl::LoanManage::doApply",
592 "equal assets remain equal after storage");
593 }
594 }
595
596 return result;
597}
598
599void
601{
602 // No transaction-specific invariants yet (future work).
603}
604
605bool
607{
608 // No transaction-specific invariants yet (future work).
609 return true;
610}
611
612//------------------------------------------------------------------------------
613
614} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream fatal() const
Definition Journal.h:368
Stream debug() const
Definition Journal.h:344
Stream warn() const
Definition Journal.h:356
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:141
virtual void update(SLE::Ref sle)=0
Indicate changes to a peeked SLE.
bool integral() const
Definition Asset.h:133
static TER impairLoan(ApplyView &view, SLE::Ref loanSle, SLE::Ref vaultSle, Asset const &vaultAsset, beast::Journal j)
Helper function that might be needed by other transactors.
static TER preclaim(PreclaimContext const &ctx)
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static STAmount calculateDefaultCover(SLE::Ref loanSle, SLE::Ref brokerSle, SLE::Ref vaultSle)
Compute the First-Loss Capital cover amount for a defaulted Loan on a FixedPrecision Vault,...
static TER unimpairLoan(ApplyView &view, SLE::Ref loanSle, SLE::Ref vaultSle, Asset const &vaultAsset, beast::Journal j)
Helper function that might be needed by other transactors.
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
static TER defaultLoan(ApplyView &view, SLE::Ref loanSle, SLE::Ref brokerSle, SLE::Ref vaultSle, beast::Journal j)
Helper function that might be needed by other transactors.
TER doApply() override
static NotTEC preflight(PreflightContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
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
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::shared_ptr< STLedgerEntry > const & Ref
beast::Journal const j_
Definition Transactor.h:164
ApplyView & view()
Definition Transactor.h:184
ApplyContext & ctx_
Definition Transactor.h:162
T max(T... args)
T min(T... args)
constexpr Zero kZero
Definition Zero.h:30
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:565
Keylet loanBroker(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:571
Keylet loan(UInt256 const &loanBrokerID, SeqProxy const &loanSeq) noexcept
Definition Indexes.cpp:577
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp, ExpiryComparison comparison=ExpiryComparison::Inclusive)
Determines whether the given expiration time has passed.
Definition View.cpp:52
constexpr T tenthBipsOfValue(T value, TenthBips< TBips > bips)
Definition Protocol.h:139
void adjustImpreciseNumber(NumberProxy value, Number const &adjustment, Asset const &asset, int vaultScale)
@ tefBAD_LEDGER
Definition TER.h:164
int getVaultScale(SLE::ConstRef vault)
Return the Vault's current live exponent.
TenthBips< std::uint32_t > TenthBips32
Definition Units.h:454
TER adjustVaultBalances(SLE::Ref vault, VaultBalanceChange const &change, beast::Journal j)
Apply a balance change to a FixedPrecision Vault.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, SLE::Ref sponsorSle={}, WaiveTransferFee waiveFee=WaiveTransferFee::No, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
bool isPaymentLate(ReadView const &view, SLE::ConstRef loanSle)
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
@ temINVALID
Definition TER.h:98
@ temINVALID_FLAG
Definition TER.h:99
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
Number loanVaultExposure(SLE::ConstRef vaultSle, SLE::ConstRef loanSle)
@ tecNO_ENTRY
Definition TER.h:317
@ tecINTERNAL
Definition TER.h:321
@ tecTOO_SOON
Definition TER.h:329
@ tecLIMIT_EXCEEDED
Definition TER.h:372
@ tecNO_PERMISSION
Definition TER.h:316
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
VaultVersion getVaultVersion(SLE::ConstRef vault)
Resolves a Vault's LEVersion.
STAmount creditToPosteriorAvailableScale(SLE::ConstRef vault, Number const &raw, Number::RoundingMode roundingMode)
Round a FixedPrecision cash inflow (LoanPay's credit, LoanManage's default cover credit) into AssetsA...
constexpr FlagValue tfUniversalMask
Definition TxFlags.h:46
@ tesSUCCESS
Definition TER.h:253
bool checkLendingProtocolDependencies(Rules const &rules, STTx const &tx)
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:92
ReadView const & view
Definition Transactor.h:95
beast::Journal const j
Definition Transactor.h:100
State information when preflighting a tx.
Definition Transactor.h:39
beast::Journal const j
Definition Transactor.h:46