rippled
Loading...
Searching...
No Matches
LoanDelete.cpp
1#include <xrpld/app/tx/detail/LoanDelete.h>
2//
3#include <xrpld/app/misc/LendingHelpers.h>
4
5#include <xrpl/protocol/STTakesAsset.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(keylet::ownerDir(brokerPseudoAccount), loanSle->at(sfLoanBrokerNode), loanID, false))
88 return tefBAD_LEDGER; // LCOV_EXCL_LINE
89 // Remove LoanID from Directory of the Borrower.
90 if (!view.dirRemove(keylet::ownerDir(borrower), loanSle->at(sfOwnerNode), loanID, false))
91 return tefBAD_LEDGER; // LCOV_EXCL_LINE
92
93 // Delete the Loan object
94 view.erase(loanSle);
95
96 // Decrement the LoanBroker's owner count.
97 // The broker's owner count is solely for the number of outstanding loans,
98 // and is distinct from the broker's pseudo-account's owner count
99 adjustOwnerCount(view, brokerSle, -1, j_);
100 // If there are no loans left, then any remaining debt must be forgiven,
101 // because there is no other way to pay it back.
102 if (brokerSle->at(sfOwnerCount) == 0)
103 {
104 auto debtTotalProxy = brokerSle->at(sfDebtTotal);
105 if (*debtTotalProxy != beast::zero)
106 {
107 XRPL_ASSERT_PARTS(
109 vaultSle->at(sfAsset), debtTotalProxy, getAssetsTotalScale(vaultSle), Number::towards_zero) ==
110 beast::zero,
111 "xrpl::LoanDelete::doApply",
112 "last loan, remaining debt rounds to zero");
113 debtTotalProxy = 0;
114 }
115 }
116 // Decrement the borrower's owner count
117 adjustOwnerCount(view, borrowerSle, -1, j_);
118
119 // These associations shouldn't do anything, but do them just to be safe
120 associateAsset(*loanSle, vaultAsset);
121 associateAsset(*brokerSle, vaultAsset);
122 associateAsset(*vaultSle, vaultAsset);
123
124 return tesSUCCESS;
125}
126
127//------------------------------------------------------------------------------
128
129} // namespace xrpl
Stream warn() const
Definition Journal.h:312
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:110
ApplyView & view()
Definition Transactor.h:128
ApplyContext & ctx_
Definition Transactor.h:108
Keylet loanbroker(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:498
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:325
Keylet loan(uint256 const &loanBrokerID, std::uint32_t loanSeq) noexcept
Definition Indexes.cpp:504
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:492
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:160
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.
Definition View.cpp:941
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
Definition STAmount.h:674
@ 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:53
ReadView const & view
Definition Transactor.h:56
beast::Journal const j
Definition Transactor.h:61
State information when preflighting a tx.
Definition Transactor.h:15