xrpld
Loading...
Searching...
No Matches
LoanBrokerDelete.cpp
1#include <xrpl/tx/transactors/lending/LoanBrokerDelete.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/ledger/helpers/AccountRootHelpers.h>
7#include <xrpl/ledger/helpers/LendingHelpers.h>
8#include <xrpl/ledger/helpers/TokenHelpers.h>
9#include <xrpl/protocol/Asset.h>
10#include <xrpl/protocol/Feature.h>
11#include <xrpl/protocol/Indexes.h>
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/STAmount.h>
14#include <xrpl/protocol/STLedgerEntry.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[sfLoanBrokerID] == 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 brokerID = tx[sfLoanBrokerID];
44
45 auto const sleBroker = ctx.view.read(keylet::loanBroker(brokerID));
46 if (!sleBroker)
47 {
48 JLOG(ctx.j.warn()) << "LoanBroker does not exist.";
49 return tecNO_ENTRY;
50 }
51
52 auto const brokerOwner = sleBroker->at(sfOwner);
53
54 if (account != brokerOwner)
55 {
56 JLOG(ctx.j.warn()) << "Account is not the owner of the LoanBroker.";
57 return tecNO_PERMISSION;
58 }
59 if (auto const ownerCount = sleBroker->at(sfOwnerCount); ownerCount != 0)
60 {
61 JLOG(ctx.j.warn()) << "LoanBrokerDelete: Owner count is " << ownerCount;
62 return tecHAS_OBLIGATIONS;
63 }
64
65 auto const vault = ctx.view.read(keylet::vault(sleBroker->at(sfVaultID)));
66 if (!vault)
67 {
68 // LCOV_EXCL_START
69 JLOG(ctx.j.fatal()) << "Vault is missing for Broker " << brokerID;
70 return tefBAD_LEDGER;
71 // LCOV_EXCL_STOP
72 }
73
74 Asset const asset = vault->at(sfAsset);
75
76 if (auto const debtTotal = sleBroker->at(sfDebtTotal); debtTotal != beast::kZero)
77 {
78 // Any remaining debt should have been wiped out by the last Loan
79 // Delete. This check is purely defensive.
80 auto const scale = getAssetsTotalScale(vault);
81
82 auto const rounded =
84
85 if (rounded != beast::kZero)
86 {
87 // LCOV_EXCL_START
88 JLOG(ctx.j.warn()) << "LoanBrokerDelete: Debt total is " << debtTotal
89 << ", which rounds to " << rounded;
90 return tecHAS_OBLIGATIONS;
91 // LCOV_EXCL_STOP
92 }
93 }
94
95 auto const coverAvailable = STAmount{asset, sleBroker->at(sfCoverAvailable)};
96 // If there are assets in the cover, broker will receive them on deletion.
97 // So we need to check if the broker owner is deep frozen for that asset.
98 if (coverAvailable > beast::kZero)
99 {
100 if (auto const ret = checkDeepFrozen(ctx.view, brokerOwner, asset))
101 {
102 JLOG(ctx.j.warn()) << "Broker owner account is frozen.";
103 return ret;
104 }
105 }
106
107 if (ctx.view.rules().enabled(fixCleanup3_2_0))
108 {
109 if (coverAvailable > beast::kZero)
110 {
111 auto const brokerPseudo = sleBroker->at(sfAccount);
112 if (auto const ret = checkFrozen(ctx.view, brokerPseudo, asset))
113 {
114 JLOG(ctx.j.warn()) << "Broker pseudo-account is frozen/locked.";
115 return ret;
116 }
117 }
118 }
119
120 return tesSUCCESS;
121}
122
123TER
125{
126 auto const& tx = ctx_.tx;
127
128 auto const brokerID = tx[sfLoanBrokerID];
129
130 // Delete the loan broker
131 auto broker = view().peek(keylet::loanBroker(brokerID));
132 if (!broker)
133 return tefBAD_LEDGER; // LCOV_EXCL_LINE
134 auto const vaultID = broker->at(sfVaultID);
135 auto const sleVault = view().read(keylet::vault(vaultID));
136 if (!sleVault)
137 return tefBAD_LEDGER; // LCOV_EXCL_LINE
138 auto const vaultPseudoID = sleVault->at(sfAccount);
139 auto const vaultAsset = sleVault->at(sfAsset);
140
141 auto const brokerPseudoID = broker->at(sfAccount);
142
143 if (!view().dirRemove(
144 keylet::ownerDir(accountID_), broker->at(sfOwnerNode), broker->key(), false))
145 {
146 return tefBAD_LEDGER; // LCOV_EXCL_LINE
147 }
148 if (!view().dirRemove(
149 keylet::ownerDir(vaultPseudoID), broker->at(sfVaultNode), broker->key(), false))
150 {
151 return tefBAD_LEDGER; // LCOV_EXCL_LINE
152 }
153
154 {
155 auto const coverAvailable = STAmount{vaultAsset, broker->at(sfCoverAvailable)};
156 if (auto const ter = accountSend(
157 view(), brokerPseudoID, accountID_, coverAvailable, j_, {}, WaiveTransferFee::Yes))
158 return ter;
159 }
160
161 if (auto ter = removeEmptyHolding(ctx_.getApplyViewContext(), brokerPseudoID, vaultAsset, j_))
162 return ter;
163
164 auto brokerPseudoSLE = view().peek(keylet::account(brokerPseudoID));
165 if (!brokerPseudoSLE)
166 return tefBAD_LEDGER; // LCOV_EXCL_LINE
167
168 // Making the payment and removing the empty holding should have deleted any
169 // obligations associated with the broker or broker pseudo-account.
170 if (*brokerPseudoSLE->at(sfBalance))
171 {
172 JLOG(j_.warn()) << "LoanBrokerDelete: Pseudo-account has a balance";
173 return tecHAS_OBLIGATIONS; // LCOV_EXCL_LINE
174 }
175 if (brokerPseudoSLE->at(sfOwnerCount) != 0)
176 {
177 JLOG(j_.warn()) << "LoanBrokerDelete: Pseudo-account still owns objects";
178 return tecHAS_OBLIGATIONS; // LCOV_EXCL_LINE
179 }
180 if (auto const directory = keylet::ownerDir(brokerPseudoID); view().read(directory))
181 {
182 JLOG(j_.warn()) << "LoanBrokerDelete: Pseudo-account has a directory";
183 return tecHAS_OBLIGATIONS; // LCOV_EXCL_LINE
184 }
185
186 view().erase(brokerPseudoSLE);
187
188 {
189 auto owner = view().peek(keylet::account(accountID_));
190 if (!owner)
191 return tefBAD_LEDGER; // LCOV_EXCL_LINE
192
193 // Decreases the owner count by two: one for the LoanBroker object, and
194 // one for the pseudo-account.
195 decreaseOwnerCountForObject(view(), owner, broker, 2, j_);
196 }
197
198 view().erase(broker);
199
200 return tesSUCCESS;
201}
202
203void
205{
206 // No transaction-specific invariants yet (future work).
207}
208
209bool
211 STTx const&,
212 TER,
213 XRPAmount,
214 ReadView const&,
215 beast::Journal const&)
216{
217 // No transaction-specific invariants yet (future work).
218 return true;
219}
220
221//------------------------------------------------------------------------------
222
223} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream fatal() const
Definition Journal.h:368
Stream warn() const
Definition Journal.h:356
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void erase(SLE::ref sle)=0
Remove a peeked SLE.
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.
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static NotTEC preflight(PreflightContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
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:180
std::shared_ptr< STLedgerEntry const > const & const_ref
beast::Journal const j_
Definition Transactor.h:155
ApplyView & view()
Definition Transactor.h:175
AccountID const accountID_
Definition Transactor.h:157
ApplyContext & ctx_
Definition Transactor.h:153
constexpr Zero kZero
Definition Zero.h:30
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:373
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:561
Keylet loanBroker(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:567
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:198
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TER checkDeepFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
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.
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.
TER removeEmptyHolding(ApplyViewContext ctx, AccountID const &accountID, MPTIssue const &mptIssue, beast::Journal journal)
int getAssetsTotalScale(SLE::const_ref vaultSle)
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
Definition STAmount.h:794
@ tefBAD_LEDGER
Definition TER.h:162
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:607
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
Definition STAmount.h:735
@ temINVALID
Definition TER.h:98
TERSubset< CanCvtToTER > TER
Definition TER.h:647
@ tecNO_ENTRY
Definition TER.h:309
@ tecNO_PERMISSION
Definition TER.h:308
@ tecHAS_OBLIGATIONS
Definition TER.h:320
std::uint32_t ownerCount(SLE::const_ref sle, beast::Journal j, std::int32_t ownerCountAdj=0)
Return number of the objects which reserve is covered by the account(sle) (so called "ownercount").
@ tesSUCCESS
Definition TER.h:245
bool checkLendingProtocolDependencies(Rules const &rules, STTx const &tx)
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:83
ReadView const & view
Definition Transactor.h:86
beast::Journal const j
Definition Transactor.h:91
State information when preflighting a tx.
Definition Transactor.h:38