rippled
Loading...
Searching...
No Matches
LoanBrokerCoverWithdraw.cpp
1#include <xrpl/tx/transactors/lending/LoanBrokerCoverWithdraw.h>
2//
3#include <xrpl/ledger/helpers/AccountRootHelpers.h>
4#include <xrpl/ledger/helpers/CredentialHelpers.h>
5#include <xrpl/ledger/helpers/TokenHelpers.h>
6#include <xrpl/protocol/STTakesAsset.h>
7#include <xrpl/tx/transactors/lending/LendingHelpers.h>
8#include <xrpl/tx/transactors/payment/Payment.h>
9
10namespace xrpl {
11
12bool
17
20{
21 if (ctx.tx[sfLoanBrokerID] == beast::zero)
22 return temINVALID;
23
24 auto const dstAmount = ctx.tx[sfAmount];
25 if (dstAmount <= beast::zero)
26 return temBAD_AMOUNT;
27
28 if (!isLegalNet(dstAmount))
29 return temBAD_AMOUNT;
30
31 if (auto const destination = ctx.tx[~sfDestination])
32 {
33 if (*destination == beast::zero)
34 {
35 return temMALFORMED;
36 }
37 }
38
39 return tesSUCCESS;
40}
41
42TER
44{
45 auto const& tx = ctx.tx;
46
47 auto const account = tx[sfAccount];
48 auto const brokerID = tx[sfLoanBrokerID];
49 auto const amount = tx[sfAmount];
50
51 auto const dstAcct = tx[~sfDestination].value_or(account);
52
53 if (isPseudoAccount(ctx.view, dstAcct))
54 {
55 JLOG(ctx.j.warn()) << "Trying to withdraw into a pseudo-account.";
56 return tecPSEUDO_ACCOUNT;
57 }
58 auto const sleBroker = ctx.view.read(keylet::loanbroker(brokerID));
59 if (!sleBroker)
60 {
61 JLOG(ctx.j.warn()) << "LoanBroker does not exist.";
62 return tecNO_ENTRY;
63 }
64 if (account != sleBroker->at(sfOwner))
65 {
66 JLOG(ctx.j.warn()) << "Account is not the owner of the LoanBroker.";
67 return tecNO_PERMISSION;
68 }
69 auto const vault = ctx.view.read(keylet::vault(sleBroker->at(sfVaultID)));
70 if (!vault)
71 {
72 // LCOV_EXCL_START
73 JLOG(ctx.j.fatal()) << "Vault is missing for Broker " << brokerID;
74 return tefBAD_LEDGER;
75 // LCOV_EXCL_STOP
76 }
77
78 auto const vaultAsset = vault->at(sfAsset);
79 if (amount.asset() != vaultAsset)
80 return tecWRONG_ASSET;
81
82 // The broker's pseudo-account is the source of funds.
83 auto const pseudoAccountID = sleBroker->at(sfAccount);
84 // Cannot transfer a non-transferable Asset
85 if (auto const ret = canTransfer(ctx.view, vaultAsset, pseudoAccountID, dstAcct))
86 return ret;
87
88 // Withdrawal to a 3rd party destination account is essentially a transfer.
89 // Enforce all the usual asset transfer checks.
91 if (account != dstAcct)
92 {
93 if (auto const ret = canWithdraw(ctx.view, tx))
94 return ret;
95
96 // The destination account must have consented to receive the asset by
97 // creating a RippleState or MPToken
98 authType = AuthType::StrongAuth;
99 }
100
101 // Destination MPToken must exist (if asset is an MPT)
102 if (auto const ter = requireAuth(ctx.view, vaultAsset, dstAcct, authType))
103 return ter;
104
105 // Check for freezes, unless sending directly to the issuer
106 if (dstAcct != vaultAsset.getIssuer())
107 {
108 // Cannot send a frozen Asset
109 if (auto const ret = checkFrozen(ctx.view, pseudoAccountID, vaultAsset))
110 return ret;
111 // Destination account cannot receive if asset is deep frozen
112 if (auto const ret = checkDeepFrozen(ctx.view, dstAcct, vaultAsset))
113 return ret;
114 }
115
116 auto const coverAvail = sleBroker->at(sfCoverAvailable);
117 // Cover Rate is in 1/10 bips units
118 auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
119 auto const minimumCover = [&]() {
120 // Always round the minimum required up.
121 // Applies to `tenthBipsOfValue` as well as `roundToAsset`.
123 return roundToAsset(
124 vaultAsset,
125 tenthBipsOfValue(currentDebtTotal, TenthBips32(sleBroker->at(sfCoverRateMinimum))),
126 currentDebtTotal.exponent());
127 }();
128 if (coverAvail < amount)
130 if ((coverAvail - amount) < minimumCover)
132
133 if (accountHolds(
134 ctx.view,
135 pseudoAccountID,
136 vaultAsset,
139 ctx.j) < amount)
141
142 return tesSUCCESS;
143}
144
145TER
147{
148 auto const& tx = ctx_.tx;
149
150 auto const brokerID = tx[sfLoanBrokerID];
151 auto const amount = tx[sfAmount];
152 auto const dstAcct = tx[~sfDestination].value_or(account_);
153
154 auto broker = view().peek(keylet::loanbroker(brokerID));
155 if (!broker)
156 return tecINTERNAL; // LCOV_EXCL_LINE
157
158 auto const vault = view().read(keylet::vault(broker->at(sfVaultID)));
159 if (!vault)
160 return tecINTERNAL; // LCOV_EXCL_LINE
161
162 auto const vaultAsset = vault->at(sfAsset);
163
164 auto const brokerPseudoID = *broker->at(sfAccount);
165
166 // Decrease the LoanBroker's CoverAvailable by Amount
167 broker->at(sfCoverAvailable) -= amount;
168 view().update(broker);
169
170 associateAsset(*broker, vaultAsset);
171
172 return doWithdraw(view(), tx, account_, dstAcct, brokerPseudoID, preFeeBalance_, amount, j_);
173}
174
175//------------------------------------------------------------------------------
176
177} // namespace xrpl
Stream fatal() const
Definition Journal.h:325
Stream warn() const
Definition Journal.h:313
STTx const & tx
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static bool checkExtraFeatures(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext 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:116
beast::Journal const j_
Definition Transactor.h:114
ApplyView & view()
Definition Transactor.h:132
XRPAmount preFeeBalance_
Definition Transactor.h:117
ApplyContext & ctx_
Definition Transactor.h:112
Keylet loanbroker(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:510
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:504
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)
@ fhZERO_IF_FROZEN
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:402
constexpr T tenthBipsOfValue(T value, TenthBips< TBips > bips)
Definition Protocol.h:107
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
@ tefBAD_LEDGER
Definition TER.h:150
bool isLegalNet(STAmount const &value)
Definition STAmount.h:584
bool checkLendingProtocolDependencies(PreflightContext const &ctx)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=shSIMPLE_BALANCE)
TenthBips< std::uint32_t > TenthBips32
Definition Units.h:437
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
Returns true if and only if sleAcct is a pseudo-account or specific pseudo-accounts in pseudoFieldFil...
@ ahZERO_IF_UNAUTHORIZED
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to)
Check if the destination account is allowed to receive MPT.
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
Definition STAmount.h:695
@ temINVALID
Definition TER.h:90
@ temMALFORMED
Definition TER.h:67
@ temBAD_AMOUNT
Definition TER.h:69
@ tecWRONG_ASSET
Definition TER.h:341
@ tecPSEUDO_ACCOUNT
Definition TER.h:343
@ tecNO_ENTRY
Definition TER.h:287
@ tecINTERNAL
Definition TER.h:291
@ tecINSUFFICIENT_FUNDS
Definition TER.h:306
@ tecNO_PERMISSION
Definition TER.h:286
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:356
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
@ tesSUCCESS
Definition TER.h:225
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, int depth=0)
Check if the account lacks required authorization for MPT.
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:57
ReadView const & view
Definition Transactor.h:60
beast::Journal const j
Definition Transactor.h:65
State information when preflighting a tx.
Definition Transactor.h:14