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/core/ServiceRegistry.h>
7#include <xrpl/ledger/ApplyView.h>
8#include <xrpl/ledger/View.h>
9#include <xrpl/ledger/helpers/LendingHelpers.h>
10#include <xrpl/ledger/helpers/TokenHelpers.h>
11#include <xrpl/protocol/Asset.h>
12#include <xrpl/protocol/Feature.h>
13#include <xrpl/protocol/Indexes.h>
14#include <xrpl/protocol/LedgerFormats.h>
15#include <xrpl/protocol/Protocol.h>
16#include <xrpl/protocol/SField.h>
17#include <xrpl/protocol/STAmount.h>
18#include <xrpl/protocol/STLedgerEntry.h>
19#include <xrpl/protocol/STTakesAsset.h>
20#include <xrpl/protocol/STTx.h>
21#include <xrpl/protocol/TER.h>
22#include <xrpl/protocol/TxFlags.h>
23#include <xrpl/protocol/Units.h>
24#include <xrpl/protocol/XRPAmount.h>
25#include <xrpl/tx/Transactor.h>
26
27#include <algorithm>
28#include <cstdint>
29namespace xrpl {
30
31bool
36
39{
40 return tfLoanManageMask;
41}
42
45{
46 if (ctx.tx[sfLoanID] == beast::kZero)
47 return temINVALID;
48
49 // Flags are mutually exclusive
50 if (auto const flagField = ctx.tx[~sfFlags]; flagField && (*flagField != 0u))
51 {
52 auto const flags = *flagField & tfUniversalMask;
53 if ((flags & (flags - 1)) != 0)
54 {
55 JLOG(ctx.j.warn()) << "LoanManage: Only one of tfLoanDefault, tfLoanImpair, or "
56 "tfLoanUnimpair can be set.";
57 return temINVALID_FLAG;
58 }
59 }
60
61 return tesSUCCESS;
62}
63
64TER
66{
67 auto const& tx = ctx.tx;
68
69 auto const account = tx[sfAccount];
70 auto const loanID = tx[sfLoanID];
71
72 auto const loanSle = ctx.view.read(keylet::loan(loanID));
73 if (!loanSle)
74 {
75 JLOG(ctx.j.warn()) << "Loan does not exist.";
76 return tecNO_ENTRY;
77 }
78 // Impairment only allows certain transitions.
79 // 1. Once it's in default, it can't be changed.
80 // 2. It can get worse: unimpaired -> impaired -> default
81 // or unimpaired -> default
82 // 3. It can get better: impaired -> unimpaired
83 // 4. If it's in a state, it can't be put in that state again.
84 if (loanSle->isFlag(lsfLoanDefault))
85 {
86 JLOG(ctx.j.warn()) << "Loan is in default. A defaulted loan can not be modified.";
87 return tecNO_PERMISSION;
88 }
89 if (loanSle->isFlag(lsfLoanImpaired) && tx.isFlag(tfLoanImpair))
90 {
91 JLOG(ctx.j.warn()) << "Loan is impaired. A loan can not be impaired twice.";
92 return tecNO_PERMISSION;
93 }
94 if (!(loanSle->isFlag(lsfLoanImpaired) || loanSle->isFlag(lsfLoanDefault)) &&
95 (tx.isFlag(tfLoanUnimpair)))
96 {
97 JLOG(ctx.j.warn()) << "Loan is unimpaired. Can not be unimpaired again.";
98 return tecNO_PERMISSION;
99 }
100 if (loanSle->at(sfPaymentRemaining) == 0)
101 {
102 JLOG(ctx.j.warn()) << "Loan is fully paid. A loan can not be modified "
103 "after it is fully paid.";
104 return tecNO_PERMISSION;
105 }
106 if (tx.isFlag(tfLoanDefault) &&
107 !hasExpired(ctx.view, loanSle->at(sfNextPaymentDueDate) + loanSle->at(sfGracePeriod)))
108 {
109 JLOG(ctx.j.warn()) << "A loan can not be defaulted before the next payment due date.";
110 return tecTOO_SOON;
111 }
112
113 auto const loanBrokerID = loanSle->at(sfLoanBrokerID);
114 auto const loanBrokerSle = ctx.view.read(keylet::loanBroker(loanBrokerID));
115 if (!loanBrokerSle)
116 {
117 // should be impossible
118 return tecINTERNAL; // LCOV_EXCL_LINE
119 }
120 if (loanBrokerSle->at(sfOwner) != account)
121 {
122 JLOG(ctx.j.warn()) << "LoanBroker for Loan does not belong to the account. LoanManage "
123 "can only be submitted by the Loan Broker.";
124 return tecNO_PERMISSION;
125 }
126
127 return tesSUCCESS;
128}
129
130static Number
132{
133 // Spec section 3.2.3.2, defines the default amount as
134 //
135 // DefaultAmount = (Loan.PrincipalOutstanding + Loan.InterestOutstanding)
136 //
137 // Loan.InterestOutstanding is not stored directly on ledger.
138 // It is computed as
139 //
140 // Loan.TotalValueOutstanding - Loan.PrincipalOutstanding -
141 // Loan.ManagementFeeOutstanding
142 //
143 // Add that to the original formula, and you get this:
144 return loanSle->at(sfTotalValueOutstanding) - loanSle->at(sfManagementFeeOutstanding);
145}
146
147TER
150 SLE::ref loanSle,
151 SLE::ref brokerSle,
152 SLE::ref vaultSle,
153 Asset const& vaultAsset,
155{
156 // Calculate the amount of the Default that First-Loss Capital covers:
157
158 std::int32_t const loanScale = loanSle->at(sfLoanScale);
159 auto brokerDebtTotalProxy = brokerSle->at(sfDebtTotal);
160
161 Number const totalDefaultAmount = owedToVault(loanSle);
162
163 // Apply the First-Loss Capital to the Default Amount
164 TenthBips32 const coverRateMinimum{brokerSle->at(sfCoverRateMinimum)};
165 TenthBips32 const coverRateLiquidation{brokerSle->at(sfCoverRateLiquidation)};
166 auto const defaultCovered = [&]() {
167 // Always round the minimum required up.
169 auto const minimumCover = tenthBipsOfValue(brokerDebtTotalProxy.value(), coverRateMinimum);
170 // Round the liquidation amount up, too
171 auto const covered = roundToAsset(
172 vaultAsset,
173 /*
174 * This formula is from the XLS-66 spec, section 3.2.3.2 (State
175 * Changes), specifically "if the `tfLoanDefault` flag is set" /
176 * "Apply the First-Loss Capital to the Default Amount"
177 */
178 std::min(tenthBipsOfValue(minimumCover, coverRateLiquidation), totalDefaultAmount),
179 loanScale);
180 auto const coverAvailable = *brokerSle->at(sfCoverAvailable);
181
182 return std::min(covered, coverAvailable);
183 }();
184
185 auto const vaultDefaultAmount = totalDefaultAmount - defaultCovered;
186
187 // Update the Vault object:
188
189 // The vault may be at a different scale than the loan. Reduce rounding
190 // errors during the accounting by rounding some of the values to that
191 // scale.
192 auto const vaultScale = getAssetsTotalScale(vaultSle);
193
194 {
195 // Decrease the Total Value of the Vault:
196 auto vaultTotalProxy = vaultSle->at(sfAssetsTotal);
197 auto vaultAvailableProxy = vaultSle->at(sfAssetsAvailable);
198
199 if (vaultTotalProxy < vaultDefaultAmount)
200 {
201 // LCOV_EXCL_START
202 JLOG(j.warn()) << "Vault total assets is less than the vault default amount";
203 return tefBAD_LEDGER;
204 // LCOV_EXCL_STOP
205 }
206
207 auto const vaultDefaultRounded = roundToAsset(
208 vaultAsset, vaultDefaultAmount, vaultScale, Number::RoundingMode::Downward);
209 vaultTotalProxy -= vaultDefaultRounded;
210 // Increase the Asset Available of the Vault by liquidated First-Loss
211 // Capital and any unclaimed funds amount:
212 vaultAvailableProxy += defaultCovered;
213 if (*vaultAvailableProxy > *vaultTotalProxy && !vaultAsset.integral())
214 {
215 auto const difference = vaultAvailableProxy - vaultTotalProxy;
216 JLOG(j.debug()) << "Vault assets available: " << *vaultAvailableProxy << "("
217 << vaultAvailableProxy.value().exponent()
218 << "), Total: " << *vaultTotalProxy << "("
219 << vaultTotalProxy.value().exponent() << "), Difference: " << difference
220 << "(" << difference.exponent() << ")";
221 if (vaultAvailableProxy.value().exponent() - difference.exponent() > 13)
222 {
223 // If the difference is dust, bring the total up to match
224 // the available
225 JLOG(j.debug()) << "Difference between vault assets available and total is "
226 "dust. Set both to the larger value.";
227 vaultTotalProxy = vaultAvailableProxy;
228 }
229 }
230 if (*vaultAvailableProxy > *vaultTotalProxy)
231 {
232 // LCOV_EXCL_START
233 JLOG(j.fatal()) << "Vault assets available must not be greater "
234 "than assets outstanding. Available: "
235 << *vaultAvailableProxy << ", Total: " << *vaultTotalProxy;
236 return tecINTERNAL;
237 // LCOV_EXCL_STOP
238 }
239
240 // The loss has been realized
241 if (loanSle->isFlag(lsfLoanImpaired))
242 {
243 auto vaultLossUnrealizedProxy = vaultSle->at(sfLossUnrealized);
244 if (vaultLossUnrealizedProxy < totalDefaultAmount)
245 {
246 // LCOV_EXCL_START
247 JLOG(j.warn()) << "Vault unrealized loss is less than the default amount";
248 return tefBAD_LEDGER;
249 // LCOV_EXCL_STOP
250 }
252 vaultLossUnrealizedProxy, -totalDefaultAmount, vaultAsset, vaultScale);
253 }
254 view.update(vaultSle);
255 }
256
257 // Update the LoanBroker object:
258
259 {
260 // Decrease the Debt of the LoanBroker:
261 adjustImpreciseNumber(brokerDebtTotalProxy, -totalDefaultAmount, vaultAsset, vaultScale);
262 // Decrease the First-Loss Capital Cover Available:
263 auto coverAvailableProxy = brokerSle->at(sfCoverAvailable);
264 if (coverAvailableProxy < defaultCovered)
265 {
266 // LCOV_EXCL_START
267 JLOG(j.warn()) << "LoanBroker cover available is less than amount covered";
268 return tefBAD_LEDGER;
269 // LCOV_EXCL_STOP
270 }
271 coverAvailableProxy -= defaultCovered;
272 view.update(brokerSle);
273 }
274
275 // Update the Loan object:
276 loanSle->setFlag(lsfLoanDefault);
277
278 loanSle->at(sfTotalValueOutstanding) = 0;
279 loanSle->at(sfPaymentRemaining) = 0;
280 loanSle->at(sfPrincipalOutstanding) = 0;
281 loanSle->at(sfManagementFeeOutstanding) = 0;
282 // Zero out the next due date. Since it's default, it'll be removed from
283 // the object.
284 loanSle->at(sfNextPaymentDueDate) = 0;
285 view.update(loanSle);
286
287 // Return funds from the LoanBroker pseudo-account to the
288 // Vault pseudo-account:
289 return accountSend(
290 view,
291 brokerSle->at(sfAccount),
292 vaultSle->at(sfAccount),
293 STAmount{vaultAsset, defaultCovered},
294 j,
296}
297
298TER
301 SLE::ref loanSle,
302 SLE::ref vaultSle,
303 Asset const& vaultAsset,
305{
306 Number const lossUnrealized = owedToVault(loanSle);
307
308 // The vault may be at a different scale than the loan. Reduce rounding
309 // errors during the accounting by rounding some of the values to that
310 // scale.
311 auto const vaultScale = getAssetsTotalScale(vaultSle);
312
313 // Update the Vault object(set "paper loss")
314 auto vaultLossUnrealizedProxy = vaultSle->at(sfLossUnrealized);
315 adjustImpreciseNumber(vaultLossUnrealizedProxy, lossUnrealized, vaultAsset, vaultScale);
316 if (vaultLossUnrealizedProxy > vaultSle->at(sfAssetsTotal) - vaultSle->at(sfAssetsAvailable))
317 {
318 // Having a loss greater than the vault's unavailable assets
319 // will leave the vault in an invalid / inconsistent state.
320 JLOG(j.warn()) << "Vault unrealized loss is too large, and will "
321 "corrupt the vault.";
322 return tecLIMIT_EXCEEDED;
323 }
324 view.update(vaultSle);
325
326 // Update the Loan object
327 loanSle->setFlag(lsfLoanImpaired);
328 auto loanNextDueProxy = loanSle->at(sfNextPaymentDueDate);
329 if (!hasExpired(view, loanNextDueProxy))
330 {
331 // loan payment is not yet late -
332 // move the next payment due date to now
333 loanNextDueProxy = view.parentCloseTime().time_since_epoch().count();
334 }
335 view.update(loanSle);
336
337 return tesSUCCESS;
338}
339
340[[nodiscard]] TER
343 SLE::ref loanSle,
344 SLE::ref vaultSle,
345 Asset const& vaultAsset,
347{
348 // The vault may be at a different scale than the loan. Reduce rounding
349 // errors during the accounting by rounding some of the values to that
350 // scale.
351 auto const vaultScale = getAssetsTotalScale(vaultSle);
352
353 // Update the Vault object(clear "paper loss")
354 auto vaultLossUnrealizedProxy = vaultSle->at(sfLossUnrealized);
355 Number const lossReversed = owedToVault(loanSle);
356 if (vaultLossUnrealizedProxy < lossReversed)
357 {
358 // LCOV_EXCL_START
359 JLOG(j.warn()) << "Vault unrealized loss is less than the amount to be cleared";
360 return tefBAD_LEDGER;
361 // LCOV_EXCL_STOP
362 }
363 // Reverse the "paper loss"
364 adjustImpreciseNumber(vaultLossUnrealizedProxy, -lossReversed, vaultAsset, vaultScale);
365
366 view.update(vaultSle);
367
368 // Update the Loan object
369 loanSle->clearFlag(lsfLoanImpaired);
370 auto const paymentInterval = loanSle->at(sfPaymentInterval);
371 auto const normalPaymentDueDate =
372 std::max(loanSle->at(sfPreviousPaymentDueDate), loanSle->at(sfStartDate)) + paymentInterval;
373 if (!hasExpired(view, normalPaymentDueDate))
374 {
375 // loan was unimpaired within the payment interval
376 loanSle->at(sfNextPaymentDueDate) = normalPaymentDueDate;
377 }
378 else
379 {
380 // loan was unimpaired after the original payment due date
381 loanSle->at(sfNextPaymentDueDate) =
382 view.parentCloseTime().time_since_epoch().count() + paymentInterval;
383 }
384 view.update(loanSle);
385
386 return tesSUCCESS;
387}
388
389TER
391{
392 auto const& tx = ctx_.tx;
393 auto& view = ctx_.view();
394
395 auto const loanID = tx[sfLoanID];
396 auto const loanSle = view.peek(keylet::loan(loanID));
397 if (!loanSle)
398 return tefBAD_LEDGER; // LCOV_EXCL_LINE
399
400 auto const brokerID = loanSle->at(sfLoanBrokerID);
401 auto const brokerSle = view.peek(keylet::loanBroker(brokerID));
402 if (!brokerSle)
403 return tefBAD_LEDGER; // LCOV_EXCL_LINE
404
405 auto const vaultSle = view.peek(keylet::vault(brokerSle->at(sfVaultID)));
406 if (!vaultSle)
407 return tefBAD_LEDGER; // LCOV_EXCL_LINE
408 auto const vaultAsset = vaultSle->at(sfAsset);
409
410 auto const result = [&]() -> TER {
411 // Valid flag combinations are checked in preflight. No flags is valid -
412 // just a noop.
413 if (tx.isFlag(tfLoanDefault))
414 return defaultLoan(view, loanSle, brokerSle, vaultSle, vaultAsset, j_);
415 if (tx.isFlag(tfLoanImpair))
416 return impairLoan(view, loanSle, vaultSle, vaultAsset, j_);
417 if (tx.isFlag(tfLoanUnimpair))
418 return unimpairLoan(view, loanSle, vaultSle, vaultAsset, j_);
419 // NoOp, as described above.
420 return tesSUCCESS;
421 }();
422
423 // Pre-amendment, associateAsset was only called on the noop (no flags)
424 // path. Post-amendment, we call associateAsset on all successful paths.
425 if (view.rules().enabled(fixCleanup3_1_3) && isTesSuccess(result))
426 {
427 associateAsset(*loanSle, vaultAsset);
428 associateAsset(*brokerSle, vaultAsset);
429 associateAsset(*vaultSle, vaultAsset);
430 }
431
432 return result;
433}
434
435void
437{
438 // No transaction-specific invariants yet (future work).
439}
440
441bool
443{
444 // No transaction-specific invariants yet (future work).
445 return true;
446}
447
448//------------------------------------------------------------------------------
449
450} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Stream fatal() const
Definition Journal.h:321
Stream debug() const
Definition Journal.h:297
Stream warn() const
Definition Journal.h:309
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:118
bool integral() const
Definition Asset.h:123
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref 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, 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 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.
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.
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:306
A view into a ledger.
Definition ReadView.h:31
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
std::shared_ptr< STLedgerEntry > const & ref
std::shared_ptr< STLedgerEntry const > const & const_ref
beast::Journal const j_
Definition Transactor.h:118
ApplyView & view()
Definition Transactor.h:136
ApplyContext & ctx_
Definition Transactor.h:116
T max(T... args)
T min(T... args)
Keylet loanBroker(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:557
Keylet loan(uint256 const &loanBrokerID, std::uint32_t loanSeq) noexcept
Definition Indexes.cpp:563
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:551
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr T tenthBipsOfValue(T value, TenthBips< TBips > bips)
Definition Protocol.h:111
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
Definition View.cpp:47
int getAssetsTotalScale(SLE::const_ref vaultSle)
void adjustImpreciseNumber(NumberProxy value, Number const &adjustment, Asset const &asset, int vaultScale)
@ tefBAD_LEDGER
Definition TER.h:160
TenthBips< std::uint32_t > TenthBips32
Definition Units.h:439
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
static Number owedToVault(SLE::ref loanSle)
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
Definition STAmount.h:722
@ temINVALID
Definition TER.h:96
@ temINVALID_FLAG
Definition TER.h:97
bool isTesSuccess(TER x) noexcept
Definition TER.h:663
TERSubset< CanCvtToTER > TER
Definition TER.h:634
@ tecNO_ENTRY
Definition TER.h:304
@ tecINTERNAL
Definition TER.h:308
@ tecTOO_SOON
Definition TER.h:316
@ tecLIMIT_EXCEEDED
Definition TER.h:359
@ tecNO_PERMISSION
Definition TER.h:303
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
constexpr FlagValue tfUniversalMask
Definition TxFlags.h:45
@ tesSUCCESS
Definition TER.h:240
bool checkLendingProtocolDependencies(Rules const &rules, STTx const &tx)
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
beast::Journal const j
Definition Transactor.h:69
State information when preflighting a tx.
Definition Transactor.h:18
beast::Journal const j
Definition Transactor.h:25