rippled
Loading...
Searching...
No Matches
LoanDelete.cpp
1#include <xrpl/tx/transactors/lending/LoanDelete.h>
2//
3#include <xrpl/ledger/helpers/AccountRootHelpers.h>
4#include <xrpl/protocol/STTakesAsset.h>
5#include <xrpl/tx/transactors/lending/LendingHelpers.h>
6
7namespace xrpl {
8
9bool
14
17{
18 if (ctx.tx[sfLoanID] == beast::zero)
19 return temINVALID;
20
21 return tesSUCCESS;
22}
23
24TER
26{
27 auto const& tx = ctx.tx;
28
29 auto const account = tx[sfAccount];
30 auto const loanID = tx[sfLoanID];
31
32 auto const loanSle = ctx.view.read(keylet::loan(loanID));
33 if (!loanSle)
34 {
35 JLOG(ctx.j.warn()) << "Loan does not exist.";
36 return tecNO_ENTRY;
37 }
38 if (loanSle->at(sfPaymentRemaining) > 0)
39 {
40 JLOG(ctx.j.warn()) << "Active loan can not be deleted.";
41 return tecHAS_OBLIGATIONS;
42 }
43
44 auto const loanBrokerID = loanSle->at(sfLoanBrokerID);
45 auto const loanBrokerSle = ctx.view.read(keylet::loanbroker(loanBrokerID));
46 if (!loanBrokerSle)
47 {
48 // should be impossible
49 return tecINTERNAL; // LCOV_EXCL_LINE
50 }
51 if (loanBrokerSle->at(sfOwner) != account && loanSle->at(sfBorrower) != account)
52 {
53 JLOG(ctx.j.warn()) << "Account is not Loan Broker Owner or Loan Borrower.";
54 return tecNO_PERMISSION;
55 }
56
57 return tesSUCCESS;
58}
59
60TER
62{
63 auto const& tx = ctx_.tx;
64 auto& view = ctx_.view();
65
66 auto const loanID = tx[sfLoanID];
67 auto const loanSle = view.peek(keylet::loan(loanID));
68 if (!loanSle)
69 return tefBAD_LEDGER; // LCOV_EXCL_LINE
70 auto const borrower = loanSle->at(sfBorrower);
71 auto const borrowerSle = view.peek(keylet::account(borrower));
72 if (!borrowerSle)
73 return tefBAD_LEDGER; // LCOV_EXCL_LINE
74
75 auto const brokerID = loanSle->at(sfLoanBrokerID);
76 auto const brokerSle = view.peek(keylet::loanbroker(brokerID));
77 if (!brokerSle)
78 return tefBAD_LEDGER; // LCOV_EXCL_LINE
79 auto const brokerPseudoAccount = brokerSle->at(sfAccount);
80
81 auto const vaultSle = view.peek(keylet::vault(brokerSle->at(sfVaultID)));
82 if (!vaultSle)
83 return tefBAD_LEDGER; // LCOV_EXCL_LINE
84 auto const vaultAsset = vaultSle->at(sfAsset);
85
86 // Remove LoanID from Directory of the LoanBroker pseudo-account.
87 if (!view.dirRemove(
88 keylet::ownerDir(brokerPseudoAccount), loanSle->at(sfLoanBrokerNode), loanID, false))
89 return tefBAD_LEDGER; // LCOV_EXCL_LINE
90 // Remove LoanID from Directory of the Borrower.
91 if (!view.dirRemove(keylet::ownerDir(borrower), loanSle->at(sfOwnerNode), loanID, false))
92 return tefBAD_LEDGER; // LCOV_EXCL_LINE
93
94 // Delete the Loan object
95 view.erase(loanSle);
96
97 // Decrement the LoanBroker's owner count.
98 // The broker's owner count is solely for the number of outstanding loans,
99 // and is distinct from the broker's pseudo-account's owner count
100 adjustOwnerCount(view, brokerSle, -1, j_);
101 // If there are no loans left, then any remaining debt must be forgiven,
102 // because there is no other way to pay it back.
103 if (brokerSle->at(sfOwnerCount) == 0)
104 {
105 auto debtTotalProxy = brokerSle->at(sfDebtTotal);
106 if (*debtTotalProxy != beast::zero)
107 {
108 XRPL_ASSERT_PARTS(
110 vaultSle->at(sfAsset),
111 debtTotalProxy,
112 getAssetsTotalScale(vaultSle),
113 Number::towards_zero) == beast::zero,
114 "xrpl::LoanDelete::doApply",
115 "last loan, remaining debt rounds to zero");
116 debtTotalProxy = 0;
117 }
118 }
119 // Decrement the borrower's owner count
120 adjustOwnerCount(view, borrowerSle, -1, j_);
121
122 // These associations shouldn't do anything, but do them just to be safe
123 associateAsset(*loanSle, vaultAsset);
124 associateAsset(*brokerSle, vaultAsset);
125 associateAsset(*vaultSle, vaultAsset);
126
127 return tesSUCCESS;
128}
129
130//------------------------------------------------------------------------------
131
132} // namespace xrpl
Stream warn() const
Definition Journal.h:313
STTx const & tx
ApplyView & view()
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static TER preclaim(PreclaimContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
TER doApply() override
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
beast::Journal const j_
Definition Transactor.h:114
ApplyView & view()
Definition Transactor.h:132
ApplyContext & ctx_
Definition Transactor.h:112
Keylet loanbroker(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:510
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:336
Keylet loan(uint256 const &loanBrokerID, std::uint32_t loanSeq) noexcept
Definition Indexes.cpp:516
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:504
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
int getAssetsTotalScale(SLE::const_ref vaultSle)
@ tefBAD_LEDGER
Definition TER.h:150
bool checkLendingProtocolDependencies(PreflightContext const &ctx)
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
Definition STAmount.h:695
@ temINVALID
Definition TER.h:90
@ tecNO_ENTRY
Definition TER.h:287
@ tecINTERNAL
Definition TER.h:291
@ tecNO_PERMISSION
Definition TER.h:286
@ tecHAS_OBLIGATIONS
Definition TER.h:298
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
@ tesSUCCESS
Definition TER.h:225
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:57
ReadView const & view
Definition Transactor.h:60
beast::Journal const j
Definition Transactor.h:65
State information when preflighting a tx.
Definition Transactor.h:14