1#include <xrpl/tx/transactors/vault/VaultWithdraw.h>
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>
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>
34 issuance && issuance->getType() == ltMPTOKEN_ISSUANCE,
35 "xrpl::shouldWaiveWithdrawal : valid issuance sle");
45 if (ctx.
tx[sfVaultID] == beast::kZero)
47 JLOG(ctx.
j.
debug()) <<
"VaultWithdraw: zero/empty vault ID.";
51 if (ctx.
tx[sfAmount] <= beast::kZero)
54 if (
auto const destination = ctx.
tx[~sfDestination])
56 if (*destination == beast::kZero)
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)
82 auto const& vaultAccount = vault->at(sfAccount);
83 auto const& account = ctx.
tx[sfAccount];
84 auto const& dstAcct = ctx.
tx[~sfDestination].value_or(account);
90 if (
auto ter =
canTransfer(ctx.
view, vaultAsset, vaultAccount, dstAcct, waive);
93 JLOG(ctx.
j.
debug()) <<
"VaultWithdraw: vault assets are non-transferable.";
101 JLOG(ctx.
j.
error()) <<
"VaultWithdraw: invalid withdrawal policy.";
106 if (fix313Enabled && amount.asset() == vaultShare)
116 JLOG(ctx.
j.
error()) <<
"VaultWithdraw: missing issuance of vault shares.";
127 auto const maybeAssets =
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();
200 auto const mptIssuanceID = *((*vault)[sfShareMPTID]);
205 JLOG(
j_.error()) <<
"VaultWithdraw: missing issuance of vault shares.";
215 auto const amount =
ctx_.tx[sfAmount];
216 Asset const vaultAsset = vault->at(sfAsset);
218 MPTIssue const share{mptIssuanceID};
228 if (amount.asset() == vaultAsset)
236 sharesRedeemed = *maybeShares;
239 if (sharesRedeemed == beast::kZero)
241 auto const maybeAssets =
245 assetsWithdrawn = *maybeAssets;
247 else if (amount.asset() == share)
250 sharesRedeemed = amount;
251 auto const maybeAssets =
255 assetsWithdrawn = *maybeAssets;
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();
285 JLOG(
j_.debug()) <<
"VaultWithdraw: account doesn't hold enough shares";
289 auto assetsAvailable = vault->at(sfAssetsAvailable);
290 auto assetsTotal = vault->at(sfAssetsTotal);
291 auto const lossUnrealized = vault->at(sfLossUnrealized);
293 lossUnrealized <= (assetsTotal - assetsAvailable),
294 "xrpl::VaultWithdraw::doApply : loss and assets do balance");
297 if (*assetsAvailable < assetsWithdrawn)
299 JLOG(
j_.debug()) <<
"VaultWithdraw: vault doesn't hold enough assets";
311 bool const isFinalWithdrawal =
312 sharesRedeemed ==
STAmount{share, sleIssuance->at(sfOutstandingAmount)};
313 if (
view().rules().enabled(fixCleanup3_2_0) && isFinalWithdrawal)
318 if (*lossUnrealized != beast::kZero)
322 "xrpl::VaultWithdraw::doApply : final withdrawal with non-zero unrealized loss");
325 "Cannot burn all outstanding shares while unrealized loss is non-zero";
330 STAmount const allAvailable{vaultAsset, *assetsAvailable};
331 if (assetsWithdrawn != allAvailable)
334 <<
"VaultWithdraw: final withdrawal share-value mismatch;"
335 <<
" computed=" << assetsWithdrawn.
getText()
336 <<
" assetsAvailable=" << allAvailable.
getText();
338 assetsWithdrawn = allAvailable;
346 assetsTotal -= assetsWithdrawn;
347 assetsAvailable -= assetsWithdrawn;
351 auto const& vaultAccount = vault->at(sfAccount);
367 <<
"VaultWithdraw: removed empty MPToken for vault shares"
375 <<
"VaultWithdraw: failed to remove MPToken for vault shares"
387 auto const dstAcct =
ctx_.tx[~sfDestination].value_or(
accountID_);
A generic endpoint for log messages.
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.
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.
std::string getText() const override
Asset const & asset() const
std::shared_ptr< STLedgerEntry const > const & const_ref
bool isFieldPresent(SField const &field) const
AccountID const accountID_
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 TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
Keylet mptokenIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
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)
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
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)
std::string to_string(BaseUInt< Bits, Tag > const &a)
TERSubset< CanCvtToNotTEC > NotTEC
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.
bool isTesSuccess(TER x) noexcept
constexpr std::uint8_t kVaultStrategyFirstComeFirstServe
Vault withdrawal policies.
TERSubset< CanCvtToTER > TER
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.
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.
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 ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
WaiveUnrealizedLoss
Controls whether the withdraw conversion helpers (assetsToSharesWithdraw and sharesToAssetsWithdraw) ...
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.