xrpld
Loading...
Searching...
No Matches
LoanDelete.cpp
1#include <xrpl/tx/transactors/lending/LoanDelete.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h> // IWYU pragma: keep
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/ledger/helpers/AccountRootHelpers.h>
8#include <xrpl/ledger/helpers/LendingHelpers.h>
9#include <xrpl/ledger/helpers/VaultHelpers.h>
10#include <xrpl/protocol/Indexes.h>
11#include <xrpl/protocol/SField.h>
12#include <xrpl/protocol/STAmount.h> // IWYU pragma: keep
13#include <xrpl/protocol/STLedgerEntry.h>
14#include <xrpl/protocol/STTakesAsset.h>
15#include <xrpl/protocol/STTx.h>
16#include <xrpl/protocol/TER.h>
17#include <xrpl/protocol/XRPAmount.h>
18#include <xrpl/tx/Transactor.h>
19
20namespace xrpl {
21
22bool
27
30{
31 if (ctx.tx[sfLoanID] == beast::kZero)
32 return temINVALID;
33
34 return tesSUCCESS;
35}
36
37TER
39{
40 auto const& tx = ctx.tx;
41
42 auto const account = tx[sfAccount];
43 auto const loanID = tx[sfLoanID];
44
45 auto const loanSle = ctx.view.read(keylet::loan(loanID));
46 if (!loanSle)
47 {
48 JLOG(ctx.j.warn()) << "Loan does not exist.";
49 return tecNO_ENTRY;
50 }
51 if (loanSle->at(sfPaymentRemaining) > 0)
52 {
53 JLOG(ctx.j.warn()) << "Active loan can not be deleted.";
54 return tecHAS_OBLIGATIONS;
55 }
56
57 auto const loanBrokerID = loanSle->at(sfLoanBrokerID);
58 auto const loanBrokerSle = ctx.view.read(keylet::loanBroker(loanBrokerID));
59 if (!loanBrokerSle)
60 {
61 // should be impossible
62 return tecINTERNAL; // LCOV_EXCL_LINE
63 }
64 if (loanBrokerSle->at(sfOwner) != account && loanSle->at(sfBorrower) != account)
65 {
66 JLOG(ctx.j.warn()) << "Account is not Loan Broker Owner or Loan Borrower.";
67 return tecNO_PERMISSION;
68 }
69
70 return tesSUCCESS;
71}
72
73TER
75{
76 auto const& tx = ctx_.tx;
77 auto& view = ctx_.view();
78
79 auto const loanID = tx[sfLoanID];
80 auto const loanSle = view.peek(keylet::loan(loanID));
81 if (!loanSle)
82 return tefBAD_LEDGER; // LCOV_EXCL_LINE
83 auto const borrower = loanSle->at(sfBorrower);
84 auto const borrowerSle = view.peek(keylet::account(borrower));
85 if (!borrowerSle)
86 return tefBAD_LEDGER; // LCOV_EXCL_LINE
87
88 auto const brokerID = loanSle->at(sfLoanBrokerID);
89 auto const brokerSle = view.peek(keylet::loanBroker(brokerID));
90 if (!brokerSle)
91 return tefBAD_LEDGER; // LCOV_EXCL_LINE
92 auto const brokerPseudoAccount = brokerSle->at(sfAccount);
93
94 auto const vaultSle = view.peek(keylet::vault(brokerSle->at(sfVaultID)));
95 if (!vaultSle)
96 return tefBAD_LEDGER; // LCOV_EXCL_LINE
97 auto const vaultAsset = vaultSle->at(sfAsset);
98
99 // Remove LoanID from Directory of the LoanBroker pseudo-account.
100 if (!view.dirRemove(
101 keylet::ownerDir(brokerPseudoAccount), loanSle->at(sfLoanBrokerNode), loanID, false))
102 return tefBAD_LEDGER; // LCOV_EXCL_LINE
103 // Remove LoanID from Directory of the Borrower.
104 if (!view.dirRemove(keylet::ownerDir(borrower), loanSle->at(sfOwnerNode), loanID, false))
105 return tefBAD_LEDGER; // LCOV_EXCL_LINE
106
107 // Delete the Loan object
108 view.erase(loanSle);
109
110 // Decrement the LoanBroker's owner count.
111 adjustLoanBrokerOwnerCount(view, brokerSle, -1, j_);
112
113 // If there are no loans left, then any remaining debt must be forgiven,
114 // because there is no other way to pay it back.
115 if (brokerSle->at(sfOwnerCount) == 0)
116 {
117 auto debtTotalProxy = brokerSle->at(sfDebtTotal);
118 if (*debtTotalProxy != beast::kZero)
119 {
120 XRPL_ASSERT_PARTS(
122 vaultSle->at(sfAsset),
123 debtTotalProxy,
124 getVaultScale(vaultSle),
126 "xrpl::LoanDelete::doApply",
127 "last loan, remaining debt rounds to zero");
128 debtTotalProxy = 0;
129 }
130 }
131 // Decrement the borrower's owner count
132 decreaseOwnerCountForObject(view, borrowerSle, loanSle, 1, j_);
133
134 associateAsset(*vaultSle, vaultAsset);
135
136 return tesSUCCESS;
137}
138
139void
141{
142 // No transaction-specific invariants yet (future work).
143}
144
145bool
147{
148 // No transaction-specific invariants yet (future work).
149 return true;
150}
151
152//------------------------------------------------------------------------------
153
154} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream warn() const
Definition Journal.h:356
static TER preclaim(PreclaimContext const &ctx)
static bool checkExtraFeatures(PreflightContext 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 NotTEC preflight(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
TER doApply() override
A view into a ledger.
Definition ReadView.h:41
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
std::shared_ptr< STLedgerEntry const > const & ConstRef
beast::Journal const j_
Definition Transactor.h:164
ApplyView & view()
Definition Transactor.h:184
ApplyContext & ctx_
Definition Transactor.h:162
constexpr Zero kZero
Definition Zero.h:30
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:415
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 account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
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
@ tefBAD_LEDGER
Definition TER.h:164
int getVaultScale(SLE::ConstRef vault)
Return the Vault's current live exponent.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
void decreaseOwnerCountForObject(ApplyView &view, SLE::Ref accountSle, SLE::Ref objectSle, std::uint32_t count, beast::Journal j)
Decrease owner-count fields for an existing ledger object.
void adjustLoanBrokerOwnerCount(ApplyView &view, SLE::Ref brokerSle, std::int32_t delta, beast::Journal j)
Adjust a LoanBroker's owner count.
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
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecNO_ENTRY
Definition TER.h:317
@ tecINTERNAL
Definition TER.h:321
@ tecNO_PERMISSION
Definition TER.h:316
@ tecHAS_OBLIGATIONS
Definition TER.h:328
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
@ 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