xrpld
Loading...
Searching...
No Matches
LoanBrokerCoverDeposit.cpp
1#include <xrpl/tx/transactors/lending/LoanBrokerCoverDeposit.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/LendingHelpers.h>
7#include <xrpl/ledger/helpers/TokenHelpers.h>
8#include <xrpl/ledger/helpers/VaultHelpers.h>
9#include <xrpl/protocol/Feature.h>
10#include <xrpl/protocol/Indexes.h>
11#include <xrpl/protocol/Protocol.h>
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/STAmount.h>
14#include <xrpl/protocol/STLedgerEntry.h>
15#include <xrpl/protocol/STTakesAsset.h>
16#include <xrpl/protocol/STTx.h>
17#include <xrpl/protocol/TER.h>
18#include <xrpl/protocol/XRPAmount.h>
19#include <xrpl/tx/Transactor.h>
20
21namespace xrpl {
22
23bool
28
31{
32 if (ctx.tx[sfLoanBrokerID] == beast::kZero)
33 return temINVALID;
34
35 auto const dstAmount = ctx.tx[sfAmount];
36 if (dstAmount <= beast::kZero)
37 return temBAD_AMOUNT;
38
39 if (!isLegalNet(dstAmount))
40 return temBAD_AMOUNT;
41
42 return tesSUCCESS;
43}
44
45TER
47{
48 auto const fix320Enabled = ctx.view.rules().enabled(fixCleanup3_2_0);
49 auto const fix330Enabled = ctx.view.rules().enabled(fixCleanup3_3_0);
50 auto const& tx = ctx.tx;
51
52 auto const account = tx[sfAccount];
53 auto const brokerID = tx[sfLoanBrokerID];
54 auto const amount = tx[sfAmount];
55
56 auto const sleBroker = ctx.view.read(keylet::loanBroker(brokerID));
57 if (!sleBroker)
58 {
59 JLOG(ctx.j.warn()) << "LoanBroker does not exist.";
60 return tecNO_ENTRY;
61 }
62 if (account != sleBroker->at(sfOwner))
63 {
64 JLOG(ctx.j.warn()) << "Account is not the owner of the LoanBroker.";
65 return tecNO_PERMISSION;
66 }
67 auto const vault = ctx.view.read(keylet::vault(sleBroker->at(sfVaultID)));
68 if (!vault)
69 {
70 // LCOV_EXCL_START
71 JLOG(ctx.j.fatal()) << "Vault is missing for Broker " << brokerID;
72 return tefBAD_LEDGER;
73 // LCOV_EXCL_STOP
74 }
75
76 auto const vaultAsset = vault->at(sfAsset);
77 if (amount.asset() != vaultAsset)
78 return tecWRONG_ASSET;
79
80 auto const pseudoAccountID = sleBroker->at(sfAccount);
81 // Cannot transfer a non-transferable Asset
82 if (auto const ret = canTransfer(ctx.view, vaultAsset, account, pseudoAccountID))
83 return ret;
84
85 if (fix330Enabled)
86 {
87 if (auto const ret = checkDepositFreeze(ctx.view, account, pseudoAccountID, vaultAsset))
88 return ret;
89 }
90 else
91 {
92 if (auto const ret = checkFrozen(ctx.view, account, vaultAsset))
93 return ret;
94
95 if (auto const ret = checkDeepFrozen(ctx.view, pseudoAccountID, vaultAsset))
96 return ret;
97 }
98
99 // Cannot transfer unauthorized asset
100 if (auto const ret = requireAuth(ctx.view, vaultAsset, account, AuthType::StrongAuth))
101 return ret;
102
103 // Deposit must round the amount Downward to cover scale and then reuse that rounded
104 // value for the actual transfer in doApply — otherwise implicit round-to-nearest during
105 // `sfCoverAvailable +=` could credit the broker more than the depositor paid Computing it
106 // here in preclaim lets us reject sub-cover-scale dust early with tecPRECISION_LOSS instead of
107 // failing only in doApply.
108 auto const roundedAmount = [&] -> STAmount {
110 {
111 // Cover deposit is an inflow to CoverAvailable, so it must floor
112 // the sum, not just the delta.
114 vault, sleBroker, amount, Number::RoundingMode::Downward);
115 }
116 if (!fix320Enabled)
117 return STAmount{amount};
118
119 return roundToScale(
120 amount,
121 scale(sleBroker->at(sfCoverAvailable), vaultAsset),
123 }();
124
125 // fixCleanup3_2_0 is enabled on the network, so this also covers the
126 // FixedPrecision zero-credit case.
127 if (fix320Enabled && roundedAmount <= beast::kZero)
128 {
129 JLOG(ctx.j.warn()) << "LoanBrokerCoverDeposit: deposit amount: " << amount
130 << " is zero or negative at loan broker scale";
131 return tecPRECISION_LOSS;
132 }
133
134 if (auto const ter = checkOptionalBrokerCoverInflow(vault, sleBroker, roundedAmount);
135 !isTesSuccess(ter))
136 return ter;
137
138 if (accountHolds(
139 ctx.view,
140 account,
141 vaultAsset,
144 ctx.j,
145 SpendableHandling::FullBalance) < roundedAmount)
147
148 return tesSUCCESS;
149}
150
151TER
153{
154 auto const& tx = ctx_.tx;
155
156 auto const brokerID = tx[sfLoanBrokerID];
157 auto broker = view().peek(keylet::loanBroker(brokerID));
158 if (!broker)
159 return tecINTERNAL; // LCOV_EXCL_LINE
160
161 auto const vault = view().read(keylet::vault(broker->at(sfVaultID)));
162 if (!vault)
163 return tecINTERNAL; // LCOV_EXCL_LINE
164
165 auto const vaultAsset = vault->at(sfAsset);
166 auto const brokerPseudoID = broker->at(sfAccount);
167
168 // Re-round here (matches preclaim) so the same cover-scale-quantized
169 // value drives both the trustline transfer and the cover increment;
170 // see the rationale comment in preclaim.
171 bool const fix320Enabled = view().rules().enabled(fixCleanup3_2_0);
172 auto const amount = [&] -> STAmount {
174 {
175 // Cover deposit is an inflow to CoverAvailable, so it must floor
176 // the sum, not just the delta.
178 vault, broker, tx[sfAmount], Number::RoundingMode::Downward);
179 }
180 if (!fix320Enabled)
181 return tx[sfAmount];
182
183 return roundToScale(
184 tx[sfAmount],
185 scale(broker->at(sfCoverAvailable), vaultAsset),
187 }();
188
189 // We validated zero-or-negative amount in preclaim, if we ended up with one now, fail hard.
190 if (amount <= beast::kZero)
191 {
192 // LCOV_EXCL_START
193 JLOG(j_.error()) << "LoanBrokerCoverDeposit: deposit amount: " << tx[sfAmount]
194 << " is zero or negative";
195 return tecINTERNAL;
196 // LCOV_EXCL_STOP
197 }
198
199 // Transfer assets from depositor to pseudo-account.
200 if (auto ter =
201 accountSend(view(), accountID_, brokerPseudoID, amount, j_, {}, WaiveTransferFee::Yes))
202 return ter;
203
204 // Increase the LoanBroker's CoverAvailable by Amount
205 broker->at(sfCoverAvailable) += amount;
206 view().update(broker);
207
208 associateAsset(*broker, vaultAsset);
209
210 return tesSUCCESS;
211}
212
213void
215{
216 // No transaction-specific invariants yet (future work).
217}
218
219bool
221 STTx const&,
222 TER,
223 XRPAmount,
224 ReadView const&,
225 beast::Journal const&)
226{
227 // No transaction-specific invariants yet (future work).
228 return true;
229}
230
231//------------------------------------------------------------------------------
232
233} // 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 update(SLE::Ref sle)=0
Indicate changes to a peeked SLE.
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef 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 bool checkExtraFeatures(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(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:182
std::shared_ptr< STLedgerEntry const > const & ConstRef
beast::Journal const j_
Definition Transactor.h:164
ApplyView & view()
Definition Transactor.h:184
AccountID const accountID_
Definition Transactor.h:166
ApplyContext & ctx_
Definition Transactor.h:162
constexpr Zero kZero
Definition Zero.h:30
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
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 checkDepositFreeze(ReadView const &view, AccountID const &srcAcct, AccountID const &pseudoAcct, Asset const &asset)
Checks freeze compliance for depositing an asset into a pseudo-account (e.g.
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:819
@ tefBAD_LEDGER
Definition TER.h:164
bool isLegalNet(STAmount const &value)
Definition STAmount.h:616
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.
STAmount roundToScale(STAmount const &value, std::int32_t scale, Number::RoundingMode rounding=Number::getround())
Round an arbitrary precision Amount to the precision of an STAmount that has a given exponent.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
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 checkOptionalBrokerCoverInflow(SLE::ConstRef vault, SLE::ConstRef broker, STAmount const &amount)
Check whether amount is an admissible optional cover inflow.
@ temINVALID
Definition TER.h:98
@ temBAD_AMOUNT
Definition TER.h:77
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
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:371
@ tecNO_ENTRY
Definition TER.h:317
@ tecINTERNAL
Definition TER.h:321
@ tecINSUFFICIENT_FUNDS
Definition TER.h:336
@ tecPRECISION_LOSS
Definition TER.h:374
@ tecNO_PERMISSION
Definition TER.h:316
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
VaultVersion getVaultVersion(SLE::ConstRef vault)
Resolves a Vault's LEVersion.
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:253
bool checkLendingProtocolDependencies(Rules const &rules, STTx const &tx)
STAmount creditToPosteriorBrokerCoverScale(SLE::ConstRef vault, SLE::ConstRef broker, Number const &raw, Number::RoundingMode roundingMode)
Round a cover inflow (cover deposit, LoanPay's fee redirect into cover) into CoverAvailable by floori...
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