rippled
Loading...
Searching...
No Matches
LoanBrokerDelete.cpp
1#include <xrpld/app/tx/detail/LoanBrokerDelete.h>
2//
3#include <xrpld/app/misc/LendingHelpers.h>
4
5namespace xrpl {
6
7bool
12
15{
16 if (ctx.tx[sfLoanBrokerID] == beast::zero)
17 return temINVALID;
18
19 return tesSUCCESS;
20}
21
22TER
24{
25 auto const& tx = ctx.tx;
26
27 auto const account = tx[sfAccount];
28 auto const brokerID = tx[sfLoanBrokerID];
29
30 auto const sleBroker = ctx.view.read(keylet::loanbroker(brokerID));
31 if (!sleBroker)
32 {
33 JLOG(ctx.j.warn()) << "LoanBroker does not exist.";
34 return tecNO_ENTRY;
35 }
36
37 auto const brokerOwner = sleBroker->at(sfOwner);
38
39 if (account != brokerOwner)
40 {
41 JLOG(ctx.j.warn()) << "Account is not the owner of the LoanBroker.";
42 return tecNO_PERMISSION;
43 }
44 if (auto const ownerCount = sleBroker->at(sfOwnerCount); ownerCount != 0)
45 {
46 JLOG(ctx.j.warn()) << "LoanBrokerDelete: Owner count is " << ownerCount;
47 return tecHAS_OBLIGATIONS;
48 }
49 if (auto const debtTotal = sleBroker->at(sfDebtTotal);
50 debtTotal != beast::zero)
51 {
52 // Any remaining debt should have been wiped out by the last Loan
53 // Delete. This check is purely defensive.
54 auto const vault =
55 ctx.view.read(keylet::vault(sleBroker->at(sfVaultID)));
56 if (!vault)
57 return tefINTERNAL; // LCOV_EXCL_LINE
58 auto const asset = vault->at(sfAsset);
59 auto const scale = getVaultScale(vault);
60
61 auto const rounded =
62 roundToAsset(asset, debtTotal, scale, Number::towards_zero);
63
64 if (rounded != beast::zero)
65 {
66 // LCOV_EXCL_START
67 JLOG(ctx.j.warn()) << "LoanBrokerDelete: Debt total is "
68 << debtTotal << ", which rounds to " << rounded;
69 return tecHAS_OBLIGATIONS;
70 // LCOV_EXCL_START
71 }
72 }
73
74 auto const vault = ctx.view.read(keylet::vault(sleBroker->at(sfVaultID)));
75 if (!vault)
76 {
77 // LCOV_EXCL_START
78 JLOG(ctx.j.fatal()) << "Vault is missing for Broker " << brokerID;
79 return tefBAD_LEDGER;
80 // LCOV_EXCL_STOP
81 }
82
83 Asset const asset = vault->at(sfAsset);
84
85 auto const coverAvailable =
86 STAmount{asset, sleBroker->at(sfCoverAvailable)};
87 // If there are assets in the cover, broker will receive them on deletion.
88 // So we need to check if the broker owner is deep frozen for that asset.
89 if (coverAvailable > beast::zero)
90 {
91 if (auto const ret = checkDeepFrozen(ctx.view, brokerOwner, asset))
92 {
93 JLOG(ctx.j.warn()) << "Broker owner account is frozen.";
94 return ret;
95 }
96 }
97
98 return tesSUCCESS;
99}
100
101TER
103{
104 auto const& tx = ctx_.tx;
105
106 auto const brokerID = tx[sfLoanBrokerID];
107
108 // Delete the loan broker
109 auto broker = view().peek(keylet::loanbroker(brokerID));
110 if (!broker)
111 return tefBAD_LEDGER; // LCOV_EXCL_LINE
112 auto const vaultID = broker->at(sfVaultID);
113 auto const sleVault = view().read(keylet::vault(vaultID));
114 if (!sleVault)
115 return tefBAD_LEDGER; // LCOV_EXCL_LINE
116 auto const vaultPseudoID = sleVault->at(sfAccount);
117 auto const vaultAsset = sleVault->at(sfAsset);
118
119 auto const brokerPseudoID = broker->at(sfAccount);
120
121 if (!view().dirRemove(
123 broker->at(sfOwnerNode),
124 broker->key(),
125 false))
126 {
127 return tefBAD_LEDGER; // LCOV_EXCL_LINE
128 }
129 if (!view().dirRemove(
130 keylet::ownerDir(vaultPseudoID),
131 broker->at(sfVaultNode),
132 broker->key(),
133 false))
134 {
135 return tefBAD_LEDGER; // LCOV_EXCL_LINE
136 }
137
138 {
139 auto const coverAvailable =
140 STAmount{vaultAsset, broker->at(sfCoverAvailable)};
141 if (auto const ter = accountSend(
142 view(),
143 brokerPseudoID,
144 account_,
145 coverAvailable,
146 j_,
148 return ter;
149 }
150
151 if (auto ter = removeEmptyHolding(view(), brokerPseudoID, vaultAsset, j_))
152 return ter;
153
154 auto brokerPseudoSLE = view().peek(keylet::account(brokerPseudoID));
155 if (!brokerPseudoSLE)
156 return tefBAD_LEDGER; // LCOV_EXCL_LINE
157
158 // Making the payment and removing the empty holding should have deleted any
159 // obligations associated with the broker or broker pseudo-account.
160 if (*brokerPseudoSLE->at(sfBalance))
161 {
162 JLOG(j_.warn()) << "LoanBrokerDelete: Pseudo-account has a balance";
163 return tecHAS_OBLIGATIONS; // LCOV_EXCL_LINE
164 }
165 if (brokerPseudoSLE->at(sfOwnerCount) != 0)
166 {
167 JLOG(j_.warn())
168 << "LoanBrokerDelete: Pseudo-account still owns objects";
169 return tecHAS_OBLIGATIONS; // LCOV_EXCL_LINE
170 }
171 if (auto const directory = keylet::ownerDir(brokerPseudoID);
172 view().read(directory))
173 {
174 JLOG(j_.warn()) << "LoanBrokerDelete: Pseudo-account has a directory";
175 return tecHAS_OBLIGATIONS; // LCOV_EXCL_LINE
176 }
177
178 view().erase(brokerPseudoSLE);
179
180 view().erase(broker);
181
182 {
183 auto owner = view().peek(keylet::account(account_));
184 if (!owner)
185 return tefBAD_LEDGER; // LCOV_EXCL_LINE
186
187 // Decreases the owner count by two: one for the LoanBroker object, and
188 // one for the pseudo-account.
189 adjustOwnerCount(view(), owner, -2, j_);
190 }
191
192 return tesSUCCESS;
193}
194
195//------------------------------------------------------------------------------
196
197} // namespace xrpl
Stream fatal() const
Definition Journal.h:333
Stream warn() const
Definition Journal.h:321
STTx const & tx
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 NotTEC preflight(PreflightContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
AccountID const account_
Definition Transactor.h:128
beast::Journal const j_
Definition Transactor.h:126
ApplyView & view()
Definition Transactor.h:144
ApplyContext & ctx_
Definition Transactor.h:124
Keylet loanbroker(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:552
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:356
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:546
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:166
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
TER checkDeepFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
Definition View.h:269
TER removeEmptyHolding(ApplyView &view, AccountID const &accountID, Issue const &issue, beast::Journal journal)
Definition View.cpp:1762
Number roundToAsset(A const &asset, Number const &value, std::int32_t scale, Number::rounding_mode rounding=Number::getround())
Round an arbitrary precision Number to the precision of a given Asset.
Definition STAmount.h:722
@ tefBAD_LEDGER
Definition TER.h:151
@ tefINTERNAL
Definition TER.h:154
bool checkLendingProtocolDependencies(PreflightContext const &ctx)
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Calls static accountSendIOU if saAmount represents Issue.
Definition View.cpp:2777
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:1134
int getVaultScale(SLE::const_ref vaultSle)
@ temINVALID
Definition TER.h:91
@ tecNO_ENTRY
Definition TER.h:288
@ tecNO_PERMISSION
Definition TER.h:287
@ tecHAS_OBLIGATIONS
Definition TER.h:299
@ tesSUCCESS
Definition TER.h:226
uint256 key
Definition Keylet.h:21
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:16