1#include <xrpl/ledger/View.h>
2#include <xrpl/ledger/helpers/CredentialHelpers.h>
3#include <xrpl/ledger/helpers/TokenHelpers.h>
4#include <xrpl/ledger/helpers/VaultHelpers.h>
5#include <xrpl/protocol/AccountID.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/SField.h>
8#include <xrpl/protocol/STNumber.h>
9#include <xrpl/protocol/STTakesAsset.h>
10#include <xrpl/protocol/TER.h>
11#include <xrpl/protocol/TxFlags.h>
12#include <xrpl/tx/transactors/vault/VaultWithdraw.h>
19 if (ctx.
tx[sfVaultID] == beast::zero)
21 JLOG(ctx.
j.
debug()) <<
"VaultWithdraw: zero/empty vault ID.";
25 if (ctx.
tx[sfAmount] <= beast::zero)
28 if (
auto const destination = ctx.
tx[~sfDestination])
30 if (*destination == beast::zero)
46 auto const amount = ctx.
tx[sfAmount];
47 auto const vaultAsset = vault->at(sfAsset);
48 auto const vaultShare = vault->at(sfShareMPTID);
49 if (amount.asset() != vaultAsset && amount.asset() != vaultShare)
52 auto const& vaultAccount = vault->at(sfAccount);
53 auto const& account = ctx.
tx[sfAccount];
54 auto const& dstAcct = ctx.
tx[~sfDestination].value_or(account);
57 JLOG(ctx.
j.
debug()) <<
"VaultWithdraw: vault assets are non-transferable.";
65 JLOG(ctx.
j.
error()) <<
"VaultWithdraw: invalid withdrawal policy.";
70 if (ctx.
view.
rules().
enabled(fixSecurity3_1_3) && amount.asset() == vaultShare)
80 JLOG(ctx.
j.
error()) <<
"VaultWithdraw: missing issuance of vault shares.";
104 <<
"VaultWithdraw: overflow error with"
105 <<
" scale=" << (int)vault->at(sfScale)
106 <<
", assetsTotal=" << vault->at(sfAssetsTotal)
107 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
108 <<
", amount=" << amount.value();
144 auto const mptIssuanceID = *((*vault)[sfShareMPTID]);
149 JLOG(
j_.
error()) <<
"VaultWithdraw: missing issuance of vault shares.";
159 auto const amount =
ctx_.
tx[sfAmount];
160 Asset const vaultAsset = vault->at(sfAsset);
162 MPTIssue const share{mptIssuanceID};
167 if (amount.asset() == vaultAsset)
174 sharesRedeemed = *maybeShares;
177 if (sharesRedeemed == beast::zero)
182 assetsWithdrawn = *maybeAssets;
184 else if (amount.asset() == share)
187 sharesRedeemed = amount;
191 assetsWithdrawn = *maybeAssets;
203 <<
"VaultWithdraw: overflow error with"
204 <<
" scale=" << (int)vault->at(sfScale).value()
205 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
206 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
207 <<
", amount=" << amount.value();
217 j_) < sharesRedeemed)
219 JLOG(
j_.
debug()) <<
"VaultWithdraw: account doesn't hold enough shares";
223 auto assetsAvailable = vault->at(sfAssetsAvailable);
224 auto assetsTotal = vault->at(sfAssetsTotal);
225 [[maybe_unused]]
auto const lossUnrealized = vault->at(sfLossUnrealized);
227 lossUnrealized <= (assetsTotal - assetsAvailable),
228 "xrpl::VaultWithdraw::doApply : loss and assets do balance");
233 if (*assetsAvailable < assetsWithdrawn)
235 JLOG(
j_.
debug()) <<
"VaultWithdraw: vault doesn't hold enough assets";
239 assetsTotal -= assetsWithdrawn;
240 assetsAvailable -= assetsWithdrawn;
243 auto const& vaultAccount = vault->at(sfAccount);
259 <<
"VaultWithdraw: removed empty MPToken for vault shares"
267 <<
"VaultWithdraw: failed to remove MPToken for vault shares"
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.
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual std::shared_ptr< SLE const > 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.
Asset const & asset() const
bool isFieldPresent(SField const &field) const
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
Keylet mptIssuance(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::uint8_t constexpr vaultStrategyFirstComeFirstServe
Vault withdrawal policies.
std::string to_string(base_uint< Bits, Tag > const &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.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=shSIMPLE_BALANCE)
std::string transToken(TER code)
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Calls static accountSendIOU if saAmount represents Issue.
std::optional< STAmount > assetsToSharesWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &assets, TruncateShares truncate=TruncateShares::no)
From the perspective of a vault, return the number of shares to demand from the depositor when they a...
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to)
Check if the destination account is allowed to receive MPT.
bool isTesSuccess(TER x) noexcept
std::optional< STAmount > sharesToAssetsWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &shares)
From the perspective of a vault, return the number of assets to give the depositor when they redeem a...
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)
TERSubset< CanCvtToNotTEC > NotTEC
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.
State information when preflighting a tx.