xrpld
Loading...
Searching...
No Matches
VaultWithdraw.cpp
1#include <xrpl/tx/transactors/vault/VaultWithdraw.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/ledger/ReadView.h>
8#include <xrpl/ledger/View.h>
9#include <xrpl/ledger/helpers/TokenHelpers.h>
10#include <xrpl/ledger/helpers/VaultHelpers.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/Feature.h>
13#include <xrpl/protocol/Indexes.h>
14#include <xrpl/protocol/LedgerFormats.h>
15#include <xrpl/protocol/MPTIssue.h>
16#include <xrpl/protocol/Protocol.h>
17#include <xrpl/protocol/SField.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
31shouldWaiveWithdrawal(ReadView const& view, AccountID const& account, SLE::const_ref issuance)
32{
33 XRPL_ASSERT(
34 issuance && issuance->getType() == ltMPTOKEN_ISSUANCE,
35 "xrpl::shouldWaiveWithdrawal : valid issuance sle");
36
37 return view.rules().enabled(fixCleanup3_2_0) && isSoleShareholder(view, account, issuance)
40}
41
44{
45 if (ctx.tx[sfVaultID] == beast::kZero)
46 {
47 JLOG(ctx.j.debug()) << "VaultWithdraw: zero/empty vault ID.";
48 return temMALFORMED;
49 }
50
51 if (ctx.tx[sfAmount] <= beast::kZero)
52 return temBAD_AMOUNT;
53
54 if (auto const destination = ctx.tx[~sfDestination])
55 {
56 if (*destination == beast::kZero)
57 {
58 return temMALFORMED;
59 }
60 }
61
62 return tesSUCCESS;
63}
64
65TER
67{
68 auto const fix313Enabled = ctx.view.rules().enabled(fixCleanup3_1_3);
69 auto const fix320Enabled = ctx.view.rules().enabled(fixCleanup3_2_0);
70 auto const fix330Enabled = ctx.view.rules().enabled(fixCleanup3_3_0);
71
72 auto const vault = ctx.view.read(keylet::vault(ctx.tx[sfVaultID]));
73 if (!vault)
74 return tecNO_ENTRY;
75
76 auto const amount = ctx.tx[sfAmount];
77 auto const vaultAsset = vault->at(sfAsset);
78 auto const vaultShare = vault->at(sfShareMPTID);
79 if (amount.asset() != vaultAsset && amount.asset() != vaultShare)
80 return tecWRONG_ASSET;
81
82 auto const& vaultAccount = vault->at(sfAccount);
83 auto const& account = ctx.tx[sfAccount];
84 auto const& dstAcct = ctx.tx[~sfDestination].value_or(account);
85 // Post-fixCleanup3_2_0: withdraw is a recovery path that bypasses the
86 // lsfMPTCanTransfer flag check, so an issuer cannot trap depositor funds.
87 // Other transferability checks (IOU NoRipple, freeze, requireAuth) still
88 // apply.
89 auto const waive = fix320Enabled ? WaiveMPTCanTransfer::Yes : WaiveMPTCanTransfer::No;
90 if (auto ter = canTransfer(ctx.view, vaultAsset, vaultAccount, dstAcct, waive);
91 !isTesSuccess(ter))
92 {
93 JLOG(ctx.j.debug()) << "VaultWithdraw: vault assets are non-transferable.";
94 return ter;
95 }
96
97 // Enforce valid withdrawal policy
98 if (vault->at(sfWithdrawalPolicy) != kVaultStrategyFirstComeFirstServe)
99 {
100 // LCOV_EXCL_START
101 JLOG(ctx.j.error()) << "VaultWithdraw: invalid withdrawal policy.";
102 return tefINTERNAL;
103 // LCOV_EXCL_STOP
104 }
105
106 if (fix313Enabled && amount.asset() == vaultShare)
107 {
108 // Post-fixCleanup3_1_3: if the user specified shares, convert
109 // to the equivalent asset amount before checking withdrawal
110 // limits. Pre-amendment the limit check was skipped for
111 // share-denominated withdrawals.
112 auto const sleIssuance = ctx.view.read(keylet::mptokenIssuance(vaultShare));
113 if (!sleIssuance)
114 {
115 // LCOV_EXCL_START
116 JLOG(ctx.j.error()) << "VaultWithdraw: missing issuance of vault shares.";
117 return tefINTERNAL;
118 // LCOV_EXCL_STOP
119 }
120
121 // When the user is the sole shareholder they own both the available and future value.
122 // We waive the unrealized-loss subtraction in this case to avoid user withdrawing all of
123 // their shares but keeping future value in the vault.
124 auto const waiveUnrealizedLoss = shouldWaiveWithdrawal(ctx.view, account, sleIssuance);
125 try
126 {
127 auto const maybeAssets =
128 sharesToAssetsWithdraw(vault, sleIssuance, amount, waiveUnrealizedLoss);
129 if (!maybeAssets)
130 return tefINTERNAL; // LCOV_EXCL_LINE
131
132 if (auto const ret = canWithdraw(
133 ctx.view,
134 account,
135 dstAcct,
136 *maybeAssets,
137 ctx.tx.isFieldPresent(sfDestinationTag)))
138 return ret;
139 }
140 catch (std::overflow_error const&)
141 {
142 // It's easy to hit this exception from Number with large enough Scale
143 // so we avoid spamming the log and only use debug here.
144 JLOG(ctx.j.debug()) //
145 << "VaultWithdraw: overflow error with"
146 << " scale=" << (int)vault->at(sfScale) //
147 << ", assetsTotal=" << vault->at(sfAssetsTotal)
148 << ", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
149 << ", amount=" << amount.value();
150 return tecPATH_DRY;
151 }
152 }
153 else
154 {
155 if (auto const ret = canWithdraw(ctx.view, ctx.tx))
156 return ret;
157 }
158
159 // If sending to Account (i.e. not a transfer), we will also create (only
160 // if authorized) a trust line or MPToken as needed, in doApply().
161 // Destination MPToken or trust line must exist if _not_ sending to Account.
162 AuthType const authType = account == dstAcct ? AuthType::WeakAuth : AuthType::StrongAuth;
163 if (auto const ter = requireAuth(ctx.view, vaultAsset, dstAcct, authType); !isTesSuccess(ter))
164 return ter;
165
166 if (fix330Enabled)
167 {
168 // checkWithdrawFreeze checks the underlying asset on the source
169 // (vault pseudo-account), the submitter, and the destination.
170 // A separate share-level freeze check is unnecessary: vault shares
171 // are issued by the vault pseudo-account, which cannot submit
172 // MPTokenIssuanceSet to individually lock a holder's MPToken.
173 // The only way shares become locked is transitively via the
174 // underlying asset, which checkWithdrawFreeze covers.
175 if (auto const ret =
176 checkWithdrawFreeze(ctx.view, vaultAccount, account, dstAcct, vaultAsset))
177 return ret;
178 }
179 else
180 {
181 // Cannot withdraw from a Vault an Asset frozen for the destination account
182 if (auto const ret = checkFrozen(ctx.view, dstAcct, vaultAsset))
183 return ret;
184
185 // Cannot return shares to the vault, if the underlying asset was frozen for
186 // the submitter
187 if (auto const ret = checkFrozen(ctx.view, account, Asset{vaultShare}))
188 return ret;
189 }
190 return tesSUCCESS;
191}
192
193TER
195{
196 auto const vault = view().peek(keylet::vault(ctx_.tx[sfVaultID]));
197 if (!vault)
198 return tefINTERNAL; // LCOV_EXCL_LINE
199
200 auto const mptIssuanceID = *((*vault)[sfShareMPTID]);
201 auto const sleIssuance = view().read(keylet::mptokenIssuance(mptIssuanceID));
202 if (!sleIssuance)
203 {
204 // LCOV_EXCL_START
205 JLOG(j_.error()) << "VaultWithdraw: missing issuance of vault shares.";
206 return tefINTERNAL;
207 // LCOV_EXCL_STOP
208 }
209
210 // Note, we intentionally do not check lsfVaultPrivate flag on the Vault. If
211 // you have a share in the vault, it means you were at some point authorized
212 // to deposit into it, and this means you are also indefinitely authorized
213 // to withdraw from it.
214
215 auto const amount = ctx_.tx[sfAmount];
216 Asset const vaultAsset = vault->at(sfAsset);
217
218 MPTIssue const share{mptIssuanceID};
219 STAmount sharesRedeemed = {share};
220 STAmount assetsWithdrawn;
221
222 // When the user is the sole shareholder they own both the available and future value.
223 // We waive the unrealized-loss subtraction in this case to avoid user withdrawing all of their
224 // shares but keeping future value in the vault.
225 auto const waiveUnrealizedLoss = shouldWaiveWithdrawal(view(), accountID_, sleIssuance);
226 try
227 {
228 if (amount.asset() == vaultAsset)
229 {
230 // Fixed assets, variable shares.
231 {
232 auto const maybeShares = assetsToSharesWithdraw(
233 vault, sleIssuance, amount, TruncateShares::No, waiveUnrealizedLoss);
234 if (!maybeShares)
235 return tecINTERNAL; // LCOV_EXCL_LINE
236 sharesRedeemed = *maybeShares;
237 }
238
239 if (sharesRedeemed == beast::kZero)
240 return tecPRECISION_LOSS;
241 auto const maybeAssets =
242 sharesToAssetsWithdraw(vault, sleIssuance, sharesRedeemed, waiveUnrealizedLoss);
243 if (!maybeAssets)
244 return tecINTERNAL; // LCOV_EXCL_LINE
245 assetsWithdrawn = *maybeAssets;
246 }
247 else if (amount.asset() == share)
248 {
249 // Fixed shares, variable assets.
250 sharesRedeemed = amount;
251 auto const maybeAssets =
252 sharesToAssetsWithdraw(vault, sleIssuance, sharesRedeemed, waiveUnrealizedLoss);
253 if (!maybeAssets)
254 return tecINTERNAL; // LCOV_EXCL_LINE
255 assetsWithdrawn = *maybeAssets;
256 }
257 else
258 {
259 return tefINTERNAL; // LCOV_EXCL_LINE
260 }
261 }
262 catch (std::overflow_error const&)
263 {
264 // It's easy to hit this exception from Number with large enough Scale
265 // so we avoid spamming the log and only use debug here.
266 JLOG(j_.debug()) //
267 << "VaultWithdraw: overflow error with"
268 << " scale=" << (int)vault->at(sfScale).value() //
269 << ", assetsTotal=" << vault->at(sfAssetsTotal).value()
270 << ", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
271 << ", amount=" << amount.value();
272 return tecPATH_DRY;
273 }
274
275 // Post-fixCleanup3_3_0: preclaim already validated all freeze conditions
276 // (checkWithdrawFreeze), so IgnoreFreeze avoids a redundant check that
277 // would incorrectly return zero for vault pseudo-accounts whose shares
278 // are frozen via a transitively frozen underlying asset.
279 auto const freezeHandling = view().rules().enabled(fixCleanup3_3_0)
282 if (accountHolds(view(), accountID_, share, freezeHandling, AuthHandling::IgnoreAuth, j_) <
283 sharesRedeemed)
284 {
285 JLOG(j_.debug()) << "VaultWithdraw: account doesn't hold enough shares";
287 }
288
289 auto assetsAvailable = vault->at(sfAssetsAvailable);
290 auto assetsTotal = vault->at(sfAssetsTotal);
291 auto const lossUnrealized = vault->at(sfLossUnrealized);
292 XRPL_ASSERT(
293 lossUnrealized <= (assetsTotal - assetsAvailable),
294 "xrpl::VaultWithdraw::doApply : loss and assets do balance");
295
296 // The vault must have enough assets on hand.
297 if (*assetsAvailable < assetsWithdrawn)
298 {
299 JLOG(j_.debug()) << "VaultWithdraw: vault doesn't hold enough assets";
301 }
302
303 // Post-fixCleanup3_2_0 "final withdrawal" rule:
304 // a transaction that would burn every outstanding share is only permitted when the vault is in
305 // a clean state — no outstanding receivables and no unrealized loss. Otherwise the resulting
306 // (shares == 0, assetsTotal > 0) state would violate the zero-sized-vault invariant.
307 //
308 // When the rule applies, the payout is the remaining sfAssetsAvailable; in a clean vault
309 // the helper result should already equal that value, and any mismatch is a rounding artifact
310 // worth logging.
311 bool const isFinalWithdrawal =
312 sharesRedeemed == STAmount{share, sleIssuance->at(sfOutstandingAmount)};
313 if (view().rules().enabled(fixCleanup3_2_0) && isFinalWithdrawal)
314 {
315 // Unreachable: a final withdrawal with lossUnrealized > 0 has
316 // assetsWithdrawn == assetsTotal > assetsAvailable, which the
317 // insufficient-funds guard above already rejected.
318 if (*lossUnrealized != beast::kZero)
319 {
320 // LCOV_EXCL_START
321 UNREACHABLE(
322 "xrpl::VaultWithdraw::doApply : final withdrawal with non-zero unrealized loss");
323 JLOG(j_.fatal())
324 << "VaultWithdraw: " //
325 "Cannot burn all outstanding shares while unrealized loss is non-zero";
326 return tefINTERNAL;
327 // LCOV_EXCL_STOP
328 }
329
330 STAmount const allAvailable{vaultAsset, *assetsAvailable};
331 if (assetsWithdrawn != allAvailable)
332 {
333 JLOG(j_.error()) //
334 << "VaultWithdraw: final withdrawal share-value mismatch;"
335 << " computed=" << assetsWithdrawn.getText()
336 << " assetsAvailable=" << allAvailable.getText();
337 }
338 assetsWithdrawn = allAvailable;
339
340 // Do not let dust accumulate in the Vault.
341 assetsTotal = 0;
342 assetsAvailable = 0;
343 }
344 else
345 {
346 assetsTotal -= assetsWithdrawn;
347 assetsAvailable -= assetsWithdrawn;
348 }
349 view().update(vault);
350
351 auto const& vaultAccount = vault->at(sfAccount);
352 // Transfer shares from depositor to vault.
353 if (auto const ter = accountSend(
354 view(), accountID_, vaultAccount, sharesRedeemed, j_, WaiveTransferFee::Yes);
355 !isTesSuccess(ter))
356 return ter;
357
358 // Try to remove MPToken for shares, if the account balance is zero. Vault
359 // pseudo-account will never set lsfMPTAuthorized, so we ignore flags.
360 // Keep MPToken if holder is the vault owner.
361 if (accountID_ != vault->at(sfOwner))
362 {
363 if (auto const ter = removeEmptyHolding(view(), accountID_, sharesRedeemed.asset(), j_);
364 isTesSuccess(ter))
365 {
366 JLOG(j_.debug()) //
367 << "VaultWithdraw: removed empty MPToken for vault shares"
368 << " MPTID=" << to_string(mptIssuanceID) //
369 << " account=" << toBase58(accountID_);
370 }
371 else if (ter != tecHAS_OBLIGATIONS)
372 {
373 // LCOV_EXCL_START
374 JLOG(j_.error()) //
375 << "VaultWithdraw: failed to remove MPToken for vault shares"
376 << " MPTID=" << to_string(mptIssuanceID) //
377 << " account=" << toBase58(accountID_) //
378 << " with result: " << transToken(ter);
379 return ter;
380 // LCOV_EXCL_STOP
381 }
382 // else quietly ignore, account balance is not zero
383 }
384
385 associateAsset(*vault, vaultAsset);
386
387 auto const dstAcct = ctx_.tx[~sfDestination].value_or(accountID_);
388 return doWithdraw(
389 view(), ctx_.tx, accountID_, dstAcct, vaultAccount, preFeeBalance_, assetsWithdrawn, j_);
390}
391
392void
394{
395 // No transaction-specific invariants yet (future work).
396}
397
398bool
400 STTx const&,
401 TER,
402 XRPAmount,
403 ReadView const&,
404 beast::Journal const&)
405{
406 // No transaction-specific invariants yet (future work).
407 return true;
408}
409
410} // 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
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 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::string getText() const override
Definition STAmount.cpp:646
Asset const & asset() const
Definition STAmount.h:478
std::shared_ptr< STLedgerEntry const > const & const_ref
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:454
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
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.
TER doApply() override
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
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
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::optional< STAmount > assetsToSharesWithdraw(SLE::const_ref vault, SLE::const_ref issuance, STAmount const &assets, TruncateShares truncate=TruncateShares::No, WaiveUnrealizedLoss waive=WaiveUnrealizedLoss::No)
From the perspective of a vault, return the number of shares to demand from the depositor when they a...
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
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
@ tefINTERNAL
Definition TER.h:163
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:93
std::optional< STAmount > sharesToAssetsWithdraw(SLE::const_ref vault, SLE::const_ref issuance, STAmount const &shares, WaiveUnrealizedLoss waive=WaiveUnrealizedLoss::No)
From the perspective of a vault, return the number of assets to give the depositor when they redeem a...
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.
std::string transToken(TER code)
Definition TER.cpp:247
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
static WaiveUnrealizedLoss shouldWaiveWithdrawal(ReadView const &view, AccountID const &account, SLE::const_ref issuance)
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.
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.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
@ temMALFORMED
Definition TER.h:73
@ temBAD_AMOUNT
Definition TER.h:75
bool isTesSuccess(TER x) noexcept
Definition TER.h:663
constexpr std::uint8_t kVaultStrategyFirstComeFirstServe
Vault withdrawal policies.
Definition Protocol.h:245
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
@ tecNO_ENTRY
Definition TER.h:304
@ tecPATH_DRY
Definition TER.h:292
@ tecINTERNAL
Definition TER.h:308
@ tecINSUFFICIENT_FUNDS
Definition TER.h:323
@ tecPRECISION_LOSS
Definition TER.h:361
@ tecHAS_OBLIGATIONS
Definition TER.h:315
bool isSoleShareholder(ReadView const &view, AccountID const &account, SLE::const_ref issuance)
Returns true iff account holds all of the vault's outstanding shares — i.e.
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
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
TER removeEmptyHolding(ApplyView &view, AccountID const &accountID, MPTIssue const &mptIssue, beast::Journal journal)
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
WaiveUnrealizedLoss
Controls whether the withdraw conversion helpers (assetsToSharesWithdraw and sharesToAssetsWithdraw) ...
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