xrpld
Loading...
Searching...
No Matches
VaultDeposit.cpp
1#include <xrpl/tx/transactors/vault/VaultDeposit.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/ledger/helpers/CredentialHelpers.h>
8#include <xrpl/ledger/helpers/MPTokenHelpers.h>
9#include <xrpl/ledger/helpers/TokenHelpers.h>
10#include <xrpl/ledger/helpers/VaultHelpers.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/Issue.h>
14#include <xrpl/protocol/LedgerFormats.h>
15#include <xrpl/protocol/MPTIssue.h>
16#include <xrpl/protocol/SField.h>
17#include <xrpl/protocol/STAmount.h>
18#include <xrpl/protocol/STLedgerEntry.h>
19#include <xrpl/protocol/STNumber.h> // IWYU pragma: keep
20#include <xrpl/protocol/STTakesAsset.h>
21#include <xrpl/protocol/STTx.h>
22#include <xrpl/protocol/TER.h>
23#include <xrpl/protocol/XRPAmount.h>
24#include <xrpl/tx/Transactor.h>
25
26#include <stdexcept>
27
28namespace xrpl {
29
30[[nodiscard]]
31static STAmount
33{
34 XRPL_ASSERT(vault && vault->getType() == ltVAULT, "xrpl::roundToVaultScale : valid vault sle");
35 XRPL_ASSERT(
36 amount.asset() == vault->at(sfAsset), "xrpl::roundToVaultScale : valid vault asset");
37
38 if (amount.integral())
39 return amount;
40
41 int const postScale = [&]() {
43 return scale(vault->at(sfAssetsTotal) + amount, vault->at(sfAsset));
44 }();
45 return roundToScale(amount, postScale, Number::RoundingMode::Downward);
46}
47
50{
51 if (ctx.tx[sfVaultID] == beast::kZero)
52 {
53 JLOG(ctx.j.debug()) << "VaultDeposit: zero/empty vault ID.";
54 return temMALFORMED;
55 }
56
57 if (ctx.tx[sfAmount] <= beast::kZero)
58 return temBAD_AMOUNT;
59
60 return tesSUCCESS;
61}
62
63TER
65{
66 auto const fix320Enabled = ctx.view.rules().enabled(fixCleanup3_2_0);
67 auto const fix330Enabled = ctx.view.rules().enabled(fixCleanup3_3_0);
68
69 auto const vault = ctx.view.read(keylet::vault(ctx.tx[sfVaultID]));
70 if (!vault)
71 return tecNO_ENTRY;
72
73 auto const& account = ctx.tx[sfAccount];
74 auto const amount = ctx.tx[sfAmount];
75 auto const vaultAsset = vault->at(sfAsset);
76 if (amount.asset() != vaultAsset)
77 return tecWRONG_ASSET;
78
79 auto const& vaultAccount = vault->at(sfAccount);
80 if (auto ter = canTransfer(ctx.view, vaultAsset, account, vaultAccount); !isTesSuccess(ter))
81 {
82 JLOG(ctx.j.debug()) << "VaultDeposit: vault assets are non-transferable.";
83 return ter;
84 }
85
86 auto const mptIssuanceID = vault->at(sfShareMPTID);
87 auto const vaultShare = MPTIssue(mptIssuanceID);
88 if (vaultShare == amount.asset())
89 {
90 // LCOV_EXCL_START
91 JLOG(ctx.j.error()) << "VaultDeposit: vault shares and assets cannot be same.";
92 return tefINTERNAL;
93 // LCOV_EXCL_STOP
94 }
95
96 auto const sleIssuance = ctx.view.read(keylet::mptokenIssuance(mptIssuanceID));
97 if (!sleIssuance)
98 {
99 // LCOV_EXCL_START
100 JLOG(ctx.j.error()) << "VaultDeposit: missing issuance of vault shares.";
101 return tefINTERNAL;
102 // LCOV_EXCL_STOP
103 }
104
105 if (sleIssuance->isFlag(lsfMPTLocked))
106 {
107 // LCOV_EXCL_START
108 JLOG(ctx.j.error()) << "VaultDeposit: issuance of vault shares is locked.";
109 return tefINTERNAL;
110 // LCOV_EXCL_STOP
111 }
112
113 if (fix330Enabled)
114 {
115 if (auto const ret = checkDepositFreeze(ctx.view, account, vaultAccount, vaultAsset))
116 return ret;
117 }
118 else
119 {
120 // Cannot deposit inside Vault an Asset frozen for the depositor
121 if (isFrozen(ctx.view, account, vaultAsset))
122 return vaultAsset.holds<Issue>() ? tecFROZEN : tecLOCKED;
123
124 // Cannot deposit if the shares of the vault are frozen
125 if (isFrozen(ctx.view, account, vaultShare))
126 return tecLOCKED;
127 }
128
129 if (vault->isFlag(lsfVaultPrivate) && account != vault->at(sfOwner))
130 {
131 auto const maybeDomainID = sleIssuance->at(~sfDomainID);
132 // Since this is a private vault and the account is not its owner, we
133 // perform authorization check based on DomainID read from sleIssuance.
134 // Had the vault shares been a regular MPToken, we would allow
135 // authorization granted by the Issuer explicitly, but Vault uses Issuer
136 // pseudo-account, which cannot grant an authorization.
137 if (maybeDomainID)
138 {
139 // As per validDomain documentation, we suppress tecEXPIRED error
140 // here, so we can delete any expired credentials inside doApply.
141 if (auto const err = credentials::validDomain(ctx.view, *maybeDomainID, account);
142 !isTesSuccess(err) && err != tecEXPIRED)
143 return err;
144 }
145 else
146 {
147 return tecNO_AUTH;
148 }
149 }
150
151 // Source MPToken must exist (if asset is an MPT)
152 if (auto const ter = requireAuth(ctx.view, vaultAsset, account); !isTesSuccess(ter))
153 return ter;
154
155 auto const roundedAmount = fix320Enabled ? roundToVaultScale(amount, vault) : amount;
156
157 if (fix320Enabled && roundedAmount == beast::kZero)
158 {
159 JLOG(ctx.j.warn()) << "VaultDeposit: deposit amount: " << ctx.tx[sfAmount]
160 << " is zero at vault scale";
161 return tecPRECISION_LOSS;
162 }
163
164 auto const accountBalance = accountHolds(
165 ctx.view,
166 account,
167 vaultAsset,
170 ctx.j,
172
173 if (accountBalance < roundedAmount)
175
176 // IOU precision checks
177 if (fix320Enabled && !roundedAmount.integral())
178 {
179 // reject deposits that would canonicalize to a no-op at the depositor's trustline scale.
180 // Skipped for issuer-as-depositor: accountHolds returns (kMaxValue @ kMaxOffset) which
181 // would always trip the predicate.
182 if (account != amount.getIssuer() &&
183 amount.isZeroAtScale(scale(accountBalance, vaultAsset)))
184 {
185 JLOG(ctx.j.warn()) << "VaultDeposit: amount " << amount.getFullText()
186 << " rounds to zero at counterparty trust-line scale";
187 return tecPRECISION_LOSS;
188 }
189 }
190
191 return tesSUCCESS;
192}
193
194TER
196{
197 bool const fix320Enabled = view().rules().enabled(fixCleanup3_2_0);
198 auto const vault = view().peek(keylet::vault(ctx_.tx[sfVaultID]));
199 if (!vault)
200 return tefINTERNAL; // LCOV_EXCL_LINE
201 auto const vaultAsset = vault->at(sfAsset);
202
203 // Post-amendment IOU only: round Downward to the AssetsTotal precision so
204 // a sub-ULP tail can't be silently absorbed by one rail and not the other.
205 auto const amount =
206 fix320Enabled ? roundToVaultScale(ctx_.tx[sfAmount], vault) : ctx_.tx[sfAmount];
207
208 // We validated zero-amount in preclaim, if we ended up with zero now, fail hard.
209 if (amount == beast::kZero)
210 {
211 // LCOV_EXCL_START
212 JLOG(j_.error()) << "VaultDeposit: deposit amount: " << ctx_.tx[sfAmount] << " is zero";
213 return tecINTERNAL;
214 // LCOV_EXCL_STOP
215 }
216
217 // Make sure the depositor can hold shares.
218 auto const mptIssuanceID = (*vault)[sfShareMPTID];
219 auto const sleIssuance = view().read(keylet::mptokenIssuance(mptIssuanceID));
220 if (!sleIssuance)
221 {
222 // LCOV_EXCL_START
223 JLOG(j_.error()) << "VaultDeposit: missing issuance of vault shares.";
224 return tefINTERNAL;
225 // LCOV_EXCL_STOP
226 }
227
228 auto const& vaultAccount = vault->at(sfAccount);
229 // Note, vault owner is always authorized
230 if (vault->isFlag(lsfVaultPrivate) && accountID_ != vault->at(sfOwner))
231 {
232 if (auto const err = enforceMPTokenAuthorization(
233 ctx_.view(), mptIssuanceID, accountID_, preFeeBalance_, j_);
234 !isTesSuccess(err))
235 return err;
236 }
237 else // !vault->isFlag(lsfVaultPrivate) || accountID_ == vault->at(sfOwner)
238 {
239 // No authorization needed, but must ensure there is MPToken
240 if (!view().exists(keylet::mptoken(mptIssuanceID, accountID_)))
241 {
242 if (auto const err = authorizeMPToken(
243 view(), preFeeBalance_, mptIssuanceID->value(), accountID_, ctx_.journal);
244 !isTesSuccess(err))
245 return err;
246 }
247
248 // If the vault is private, set the authorized flag for the vault owner
249 if (vault->isFlag(lsfVaultPrivate))
250 {
251 // This follows from the reverse of the outer enclosing if condition
252 XRPL_ASSERT(
253 accountID_ == vault->at(sfOwner), "xrpl::VaultDeposit::doApply : account is owner");
254 if (auto const err = authorizeMPToken(
255 view(),
256 preFeeBalance_, // priorBalance
257 mptIssuanceID->value(), // mptIssuanceID
258 sleIssuance->at(sfIssuer), // account
259 ctx_.journal,
260 {}, // flags
261 accountID_ // holderID
262 );
263 !isTesSuccess(err))
264 return err;
265 }
266 }
267
268 STAmount sharesCreated = {vault->at(sfShareMPTID)}, assetsDeposited;
269 try
270 {
271 // Compute exchange before transferring any amounts.
272 {
273 auto const maybeShares = assetsToSharesDeposit(vault, sleIssuance, amount);
274 if (!maybeShares)
275 return tecINTERNAL; // LCOV_EXCL_LINE
276 sharesCreated = *maybeShares;
277 }
278 if (sharesCreated == beast::kZero)
279 return tecPRECISION_LOSS;
280
281 auto const maybeAssets = sharesToAssetsDeposit(vault, sleIssuance, sharesCreated);
282 if (!maybeAssets)
283 {
284 return tecINTERNAL; // LCOV_EXCL_LINE
285 }
286 if (*maybeAssets > amount)
287 {
288 // LCOV_EXCL_START
289 JLOG(j_.error()) << "VaultDeposit: would take more than offered.";
290 return tecINTERNAL;
291 // LCOV_EXCL_STOP
292 }
293 assetsDeposited = *maybeAssets;
294 }
295 catch (std::overflow_error const&)
296 {
297 // It's easy to hit this exception from Number with large enough Scale
298 // so we avoid spamming the log and only use debug here.
299 JLOG(j_.debug()) //
300 << "VaultDeposit: overflow error with"
301 << " scale=" << (int)vault->at(sfScale).value() //
302 << ", assetsTotal=" << vault->at(sfAssetsTotal).value()
303 << ", sharesTotal=" << sleIssuance->at(sfOutstandingAmount) << ", amount=" << amount;
304 return tecPATH_DRY;
305 }
306
307 XRPL_ASSERT(
308 sharesCreated.asset() != assetsDeposited.asset(),
309 "xrpl::VaultDeposit::doApply : assets are not shares");
310
311 vault->at(sfAssetsTotal) += assetsDeposited;
312 vault->at(sfAssetsAvailable) += assetsDeposited;
313 view().update(vault);
314
315 // A deposit must not push the vault over its limit.
316 auto const maximum = *vault->at(sfAssetsMaximum);
317 if (maximum != 0 && *vault->at(sfAssetsTotal) > maximum)
318 return tecLIMIT_EXCEEDED;
319
320 // Transfer assets from depositor to vault.
321 if (auto const ter = accountSend(
322 view(), accountID_, vaultAccount, assetsDeposited, j_, WaiveTransferFee::Yes);
323 !isTesSuccess(ter))
324 return ter;
325
326 // This check is wrong. Disable it with fixCleanup3_2_0.
327 // For XRP and MPT the predicate is structurally unsatisfiable: xrpLiquid clamps at zero, and
328 // MPT balances are unsigned. For IOUs it only fires when the deposit drove the depositor's
329 // trust line into debt the exact case preclaim authorizes via SpendableHandling::FullBalance.
330 // The check thus converts a preclaim- authorized deposit into tefINTERNAL after the asset
331 // transfer.
332 if (!fix320Enabled)
333 {
334 // Sanity check
335 if (accountHolds(
336 view(),
338 assetsDeposited.asset(),
341 j_) < beast::kZero)
342 {
343 JLOG(j_.error()) << "VaultDeposit: negative balance of account assets.";
344 return tefINTERNAL;
345 }
346 }
347
348 // Transfer shares from vault to depositor.
349 if (auto const ter =
350 accountSend(view(), vaultAccount, accountID_, sharesCreated, j_, WaiveTransferFee::Yes);
351 !isTesSuccess(ter))
352 return ter;
353
354 associateAsset(*vault, vaultAsset);
355
356 return tesSUCCESS;
357}
358
359void
361{
362 // No transaction-specific invariants yet (future work).
363}
364
365bool
367 STTx const&,
368 TER,
369 XRPAmount,
370 ReadView const&,
371 beast::Journal const&)
372{
373 // No transaction-specific invariants yet (future work).
374 return true;
375}
376
377} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Stream error() const
Definition Journal.h:315
Stream debug() const
Definition Journal.h:297
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.
A currency issued by an account.
Definition Issue.h:13
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
bool integral() const noexcept
Definition STAmount.h:447
Asset const & asset() const
Definition STAmount.h:478
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
TER doApply() override
static TER preclaim(PreclaimContext 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)
TER validDomain(ReadView const &view, uint256 domainID, AccountID const &subject)
Keylet mptokenIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:521
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:551
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:533
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
Definition STAmount.h:779
@ tefINTERNAL
Definition TER.h:163
TER authorizeMPToken(ApplyView &view, XRPAmount const &priorBalance, MPTID const &mptIssuanceID, AccountID const &account, beast::Journal journal, std::uint32_t flags=0, std::optional< AccountID > holderID=std::nullopt)
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:594
std::optional< STAmount > sharesToAssetsDeposit(SLE::const_ref vault, SLE::const_ref issuance, STAmount const &shares)
From the perspective of a vault, return the number of assets to take from depositor when they receive...
bool isFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptIssue, std::uint8_t depth=0)
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
TER checkDepositFreeze(ReadView const &view, AccountID const &srcAcct, AccountID const &dstAcct, Asset const &asset)
Checks freeze compliance for depositing an asset into a pseudo-account (e.g.
@ temMALFORMED
Definition TER.h:73
@ temBAD_AMOUNT
Definition TER.h:75
bool isTesSuccess(TER x) noexcept
Definition TER.h:663
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
@ tecLOCKED
Definition TER.h:356
@ tecNO_ENTRY
Definition TER.h:304
@ tecPATH_DRY
Definition TER.h:292
@ tecNO_AUTH
Definition TER.h:298
@ tecINTERNAL
Definition TER.h:308
@ tecFROZEN
Definition TER.h:301
@ tecINSUFFICIENT_FUNDS
Definition TER.h:323
@ tecEXPIRED
Definition TER.h:312
@ tecPRECISION_LOSS
Definition TER.h:361
@ tecLIMIT_EXCEEDED
Definition TER.h:359
TER enforceMPTokenAuthorization(ApplyView &view, MPTID const &mptIssuanceID, AccountID const &account, XRPAmount const &priorBalance, beast::Journal j)
Enforce account has MPToken to match its authorization.
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
std::optional< STAmount > assetsToSharesDeposit(SLE::const_ref vault, SLE::const_ref issuance, STAmount const &assets)
From the perspective of a vault, return the number of shares to give depositor when they offer a fixe...
static STAmount roundToVaultScale(STAmount const &amount, SLE::const_ref vault)
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
beast::Journal const j
Definition Transactor.h:25