1#include <xrpld/app/tx/detail/VaultWithdraw.h>
3#include <xrpl/ledger/CredentialHelpers.h>
4#include <xrpl/ledger/View.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>
18 if (ctx.
tx[sfVaultID] == beast::zero)
20 JLOG(ctx.
j.
debug()) <<
"VaultWithdraw: zero/empty vault ID.";
24 if (ctx.
tx[sfAmount] <= beast::zero)
27 if (
auto const destination = ctx.
tx[~sfDestination])
29 if (*destination == beast::zero)
45 auto const assets = ctx.
tx[sfAmount];
46 auto const vaultAsset = vault->at(sfAsset);
47 auto const vaultShare = vault->at(sfShareMPTID);
48 if (assets.asset() != vaultAsset && assets.asset() != vaultShare)
51 auto const& vaultAccount = vault->at(sfAccount);
52 auto const& account = ctx.
tx[sfAccount];
53 auto const& dstAcct = ctx.
tx[~sfDestination].value_or(account);
56 JLOG(ctx.
j.
debug()) <<
"VaultWithdraw: vault assets are non-transferable.";
64 JLOG(ctx.
j.
error()) <<
"VaultWithdraw: invalid withdrawal policy.";
98 auto const mptIssuanceID = *((*vault)[sfShareMPTID]);
103 JLOG(
j_.
error()) <<
"VaultWithdraw: missing issuance of vault shares.";
113 auto const amount =
ctx_.
tx[sfAmount];
114 Asset const vaultAsset = vault->at(sfAsset);
116 MPTIssue const share{mptIssuanceID};
121 if (amount.asset() == vaultAsset)
128 sharesRedeemed = *maybeShares;
131 if (sharesRedeemed == beast::zero)
136 assetsWithdrawn = *maybeAssets;
138 else if (amount.asset() == share)
141 sharesRedeemed = amount;
145 assetsWithdrawn = *maybeAssets;
155 <<
"VaultWithdraw: overflow error with"
156 <<
" scale=" << (int)vault->at(sfScale).value()
157 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
158 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount) <<
", amount=" << amount.value();
165 JLOG(
j_.
debug()) <<
"VaultWithdraw: account doesn't hold enough shares";
169 auto assetsAvailable = vault->at(sfAssetsAvailable);
170 auto assetsTotal = vault->at(sfAssetsTotal);
171 [[maybe_unused]]
auto const lossUnrealized = vault->at(sfLossUnrealized);
173 lossUnrealized <= (assetsTotal - assetsAvailable),
"xrpl::VaultWithdraw::doApply : loss and assets do balance");
178 if (*assetsAvailable < assetsWithdrawn)
180 JLOG(
j_.
debug()) <<
"VaultWithdraw: vault doesn't hold enough assets";
184 assetsTotal -= assetsWithdrawn;
185 assetsAvailable -= assetsWithdrawn;
188 auto const& vaultAccount = vault->at(sfAccount);
202 <<
"VaultWithdraw: removed empty MPToken for vault shares"
210 <<
"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 std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
Asset const & asset() 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.
TER removeEmptyHolding(ApplyView &view, AccountID const &accountID, Issue const &issue, beast::Journal journal)
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)
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
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)
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.
TERSubset< CanCvtToNotTEC > NotTEC
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.