xrpld
Loading...
Searching...
No Matches
LoanBrokerCoverWithdraw.cpp
1#include <xrpl/tx/transactors/lending/LoanBrokerCoverWithdraw.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/View.h>
7#include <xrpl/ledger/helpers/AccountRootHelpers.h>
8#include <xrpl/ledger/helpers/LendingHelpers.h>
9#include <xrpl/ledger/helpers/TokenHelpers.h>
10#include <xrpl/protocol/Feature.h>
11#include <xrpl/protocol/Indexes.h>
12#include <xrpl/protocol/Protocol.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/STAmount.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STTakesAsset.h>
17#include <xrpl/protocol/STTx.h>
18#include <xrpl/protocol/TER.h>
19#include <xrpl/protocol/Units.h>
20#include <xrpl/protocol/XRPAmount.h>
21#include <xrpl/tx/Transactor.h>
22
23namespace xrpl {
24
25bool
30
33{
34 if (ctx.tx[sfLoanBrokerID] == beast::kZero)
35 return temINVALID;
36
37 auto const dstAmount = ctx.tx[sfAmount];
38 if (dstAmount <= beast::kZero)
39 return temBAD_AMOUNT;
40
41 if (!isLegalNet(dstAmount))
42 return temBAD_AMOUNT;
43
44 if (auto const destination = ctx.tx[~sfDestination])
45 {
46 if (*destination == beast::kZero)
47 {
48 return temMALFORMED;
49 }
50 }
51
52 return tesSUCCESS;
53}
54
55TER
57{
58 auto const fix320Enabled = ctx.view.rules().enabled(fixCleanup3_2_0);
59 auto const fix330Enabled = ctx.view.rules().enabled(fixCleanup3_3_0);
60 auto const& tx = ctx.tx;
61
62 auto const account = tx[sfAccount];
63 auto const brokerID = tx[sfLoanBrokerID];
64 auto const amount = tx[sfAmount];
65
66 auto const dstAcct = tx[~sfDestination].value_or(account);
67
68 if (isPseudoAccount(ctx.view, dstAcct))
69 {
70 JLOG(ctx.j.warn()) << "Trying to withdraw into a pseudo-account.";
71 return tecPSEUDO_ACCOUNT;
72 }
73 auto const sleBroker = ctx.view.read(keylet::loanBroker(brokerID));
74 if (!sleBroker)
75 {
76 JLOG(ctx.j.warn()) << "LoanBroker does not exist.";
77 return tecNO_ENTRY;
78 }
79 if (account != sleBroker->at(sfOwner))
80 {
81 JLOG(ctx.j.warn()) << "Account is not the owner of the LoanBroker.";
82 return tecNO_PERMISSION;
83 }
84 auto const vault = ctx.view.read(keylet::vault(sleBroker->at(sfVaultID)));
85 if (!vault)
86 {
87 // LCOV_EXCL_START
88 JLOG(ctx.j.fatal()) << "Vault is missing for Broker " << brokerID;
89 return tefBAD_LEDGER;
90 // LCOV_EXCL_STOP
91 }
92
93 auto const vaultAsset = vault->at(sfAsset);
94 if (amount.asset() != vaultAsset)
95 return tecWRONG_ASSET;
96
97 // Helper handles both IOU and MPT correctly without explicit branching.
98 if (auto const ret = canApplyToBrokerCover(
99 ctx.view, sleBroker, vaultAsset, amount, ctx.j, "LoanBrokerCoverWithdraw"))
100 return ret;
101
102 // The broker's pseudo-account is the source of funds.
103 auto const pseudoAccountID = sleBroker->at(sfAccount);
104 // Post-fixCleanup3_2_0: cover withdraw is a recovery path that bypasses
105 // the lsfMPTCanTransfer flag check, so an issuer cannot trap a broker's
106 // first-loss capital. Other transferability checks (IOU NoRipple, freeze,
107 // requireAuth) still apply.
108 auto const waive = fix320Enabled ? WaiveMPTCanTransfer::Yes : WaiveMPTCanTransfer::No;
109 if (auto const ret = canTransfer(ctx.view, vaultAsset, pseudoAccountID, dstAcct, waive))
110 return ret;
111
112 // Withdrawal to a 3rd party destination account is essentially a transfer.
113 // Enforce all the usual asset transfer checks.
114 AuthType authType = AuthType::WeakAuth;
115 if (account != dstAcct)
116 {
117 if (auto const ret = canWithdraw(ctx.view, tx))
118 return ret;
119
120 // The destination account must have consented to receive the asset by
121 // creating a RippleState or MPToken
122 authType = AuthType::StrongAuth;
123 }
124
125 // Destination MPToken must exist (if asset is an MPT)
126 if (auto const ter = requireAuth(ctx.view, vaultAsset, dstAcct, authType))
127 return ter;
128
129 if (fix330Enabled)
130 {
131 if (auto const ret =
132 checkWithdrawFreeze(ctx.view, pseudoAccountID, account, dstAcct, vaultAsset))
133 return ret;
134 }
135 else
136 { // Check for freezes, unless sending directly to the issuer
137 if (dstAcct != vaultAsset.getIssuer())
138 {
139 // Cannot send a frozen Asset
140 if (auto const ret = checkFrozen(ctx.view, pseudoAccountID, vaultAsset))
141 return ret;
142 // Destination account cannot receive if asset is deep frozen
143 if (auto const ret = checkDeepFrozen(ctx.view, dstAcct, vaultAsset))
144 return ret;
145 }
146 }
147
148 auto const coverAvail = sleBroker->at(sfCoverAvailable);
149 // Cover Rate is in 1/10 bips units
150 auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
151 auto const minimumCover = [&]() {
152 if (fix320Enabled)
153 {
154 return minimumBrokerCover(
155 currentDebtTotal, TenthBips32{sleBroker->at(sfCoverRateMinimum)}, vault);
156 }
157
158 // Always round the minimum required up.
159 // Applies to `tenthBipsOfValue` as well as `roundToAsset`.
161 return roundToAsset(
162 vaultAsset,
163 tenthBipsOfValue(currentDebtTotal, TenthBips32(sleBroker->at(sfCoverRateMinimum))),
164 scale(currentDebtTotal, vaultAsset));
165 }();
166 if (coverAvail < amount)
168 if ((coverAvail - amount) < minimumCover)
170
171 auto const freezeHandling = fix330Enabled && dstAcct == vaultAsset.getIssuer()
174
175 if (accountHolds(
176 ctx.view,
177 pseudoAccountID,
178 vaultAsset,
179 freezeHandling,
181 ctx.j) < amount)
183
184 return tesSUCCESS;
185}
186
187TER
189{
190 auto const& tx = ctx_.tx;
191
192 auto const brokerID = tx[sfLoanBrokerID];
193 auto const amount = tx[sfAmount];
194 auto const dstAcct = tx[~sfDestination].value_or(accountID_);
195
196 auto broker = view().peek(keylet::loanBroker(brokerID));
197 if (!broker)
198 return tecINTERNAL; // LCOV_EXCL_LINE
199
200 auto const vault = view().read(keylet::vault(broker->at(sfVaultID)));
201 if (!vault)
202 return tecINTERNAL; // LCOV_EXCL_LINE
203
204 auto const vaultAsset = vault->at(sfAsset);
205
206 auto const brokerPseudoID = *broker->at(sfAccount);
207
208 // Decrease the LoanBroker's CoverAvailable by Amount
209 broker->at(sfCoverAvailable) -= amount;
210 view().update(broker);
211
212 associateAsset(*broker, vaultAsset);
213
214 return doWithdraw(view(), tx, accountID_, dstAcct, brokerPseudoID, preFeeBalance_, amount, j_);
215}
216
217void
219{
220 // No transaction-specific invariants yet (future work).
221}
222
223bool
225 STTx const&,
226 TER,
227 XRPAmount,
228 ReadView const&,
229 beast::Journal const&)
230{
231 // No transaction-specific invariants yet (future work).
232 return true;
233}
234
235//------------------------------------------------------------------------------
236
237} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Stream fatal() const
Definition Journal.h:321
Stream warn() const
Definition Journal.h:309
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void update(SLE::ref sle)=0
Indicate changes to a peeked SLE.
static bool checkExtraFeatures(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
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)
static TER preclaim(PreclaimContext const &ctx)
A view into a ledger.
Definition ReadView.h:31
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:171
std::shared_ptr< STLedgerEntry const > const & const_ref
beast::Journal const j_
Definition Transactor.h:118
ApplyView & view()
Definition Transactor.h:136
AccountID const accountID_
Definition Transactor.h:120
XRPAmount preFeeBalance_
Definition Transactor.h:121
ApplyContext & ctx_
Definition Transactor.h:116
Keylet loanBroker(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:557
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:551
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)
TER canApplyToBrokerCover(ReadView const &view, SLE::const_ref sleBroker, Asset const &vaultAsset, STAmount const &amount, beast::Journal j, std::string_view logPrefix)
Broker cover preclaim precision guard (fixCleanup3_2_0).
TER doWithdraw(ApplyView &view, STTx const &tx, AccountID const &senderAcct, AccountID const &dstAcct, AccountID const &sourceAcct, XRPAmount priorBalance, STAmount const &amount, beast::Journal j)
Definition View.cpp:433
constexpr T tenthBipsOfValue(T value, TenthBips< TBips > bips)
Definition Protocol.h:111
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:779
@ tefBAD_LEDGER
Definition TER.h:160
Number minimumBrokerCover(Number const &debtTotal, TenthBips32 coverRateMinimum, SLE::const_ref vaultSle)
bool isLegalNet(STAmount const &value)
Definition STAmount.h:598
TenthBips< std::uint32_t > TenthBips32
Definition Units.h:439
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to, WaiveMPTCanTransfer waive=WaiveMPTCanTransfer::No, std::uint8_t depth=0)
Check whether to may receive the given MPT from from.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
TER checkWithdrawFreeze(ReadView const &view, AccountID const &srcAcct, AccountID const &submitterAcct, AccountID const &dstAcct, Asset const &asset)
Checks freeze compliance for withdrawing an asset from a pseudo-account (e.g.
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
Definition STAmount.h:722
@ temINVALID
Definition TER.h:96
@ temMALFORMED
Definition TER.h:73
@ temBAD_AMOUNT
Definition TER.h:75
TERSubset< CanCvtToTER > TER
Definition TER.h:634
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, std::uint8_t depth=0)
Check if the account lacks required authorization for MPT.
@ tecWRONG_ASSET
Definition TER.h:358
@ tecPSEUDO_ACCOUNT
Definition TER.h:360
@ tecNO_ENTRY
Definition TER.h:304
@ tecINTERNAL
Definition TER.h:308
@ tecINSUFFICIENT_FUNDS
Definition TER.h:323
@ tecNO_PERMISSION
Definition TER.h:303
TER canWithdraw(ReadView const &view, AccountID const &from, AccountID const &to, SLE::const_ref toSle, STAmount const &amount, bool hasDestinationTag)
Checks that can withdraw funds from an object to itself or a destination.
Definition View.cpp:387
bool isPseudoAccount(SLE::const_pointer sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
Returns true if and only if sleAcct is a pseudo-account or specific pseudo-accounts in pseudoFieldFil...
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
@ tesSUCCESS
Definition TER.h:240
bool checkLendingProtocolDependencies(Rules const &rules, STTx const &tx)
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:18