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/TER.h>
10#include <xrpl/protocol/TxFlags.h>
17 if (ctx.
tx[sfVaultID] == beast::zero)
19 JLOG(ctx.
j.
debug()) <<
"VaultWithdraw: zero/empty vault ID.";
23 if (ctx.
tx[sfAmount] <= beast::zero)
26 if (
auto const destination = ctx.
tx[~sfDestination])
28 if (*destination == beast::zero)
44 auto const assets = ctx.
tx[sfAmount];
45 auto const vaultAsset = vault->at(sfAsset);
46 auto const vaultShare = vault->at(sfShareMPTID);
47 if (assets.asset() != vaultAsset && assets.asset() != vaultShare)
50 auto const& vaultAccount = vault->at(sfAccount);
51 auto const& account = ctx.
tx[sfAccount];
52 auto const& dstAcct = ctx.
tx[~sfDestination].value_or(account);
53 if (
auto ter =
canTransfer(ctx.
view, vaultAsset, vaultAccount, dstAcct);
57 <<
"VaultWithdraw: vault assets are non-transferable.";
65 JLOG(ctx.
j.
error()) <<
"VaultWithdraw: invalid withdrawal policy.";
78 if (
auto const ter =
requireAuth(ctx.
view, vaultAsset, dstAcct, authType);
101 auto const mptIssuanceID = *((*vault)[sfShareMPTID]);
106 JLOG(
j_.
error()) <<
"VaultWithdraw: missing issuance of vault shares.";
116 auto const amount =
ctx_.
tx[sfAmount];
117 Asset const vaultAsset = vault->at(sfAsset);
118 MPTIssue const share{mptIssuanceID};
123 if (amount.asset() == vaultAsset)
127 auto const maybeShares =
131 sharesRedeemed = *maybeShares;
134 if (sharesRedeemed == beast::zero)
136 auto const maybeAssets =
140 assetsWithdrawn = *maybeAssets;
142 else if (amount.asset() == share)
145 sharesRedeemed = amount;
146 auto const maybeAssets =
150 assetsWithdrawn = *maybeAssets;
160 <<
"VaultWithdraw: overflow error with"
161 <<
" scale=" << (int)vault->at(sfScale).value()
162 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
163 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
164 <<
", amount=" << amount.value();
174 j_) < sharesRedeemed)
176 JLOG(
j_.
debug()) <<
"VaultWithdraw: account doesn't hold enough shares";
180 auto assetsAvailable = vault->at(sfAssetsAvailable);
181 auto assetsTotal = vault->at(sfAssetsTotal);
182 [[maybe_unused]]
auto const lossUnrealized = vault->at(sfLossUnrealized);
184 lossUnrealized <= (assetsTotal - assetsAvailable),
185 "xrpl::VaultWithdraw::doApply : loss and assets do balance");
190 if (*assetsAvailable < assetsWithdrawn)
192 JLOG(
j_.
debug()) <<
"VaultWithdraw: vault doesn't hold enough assets";
196 assetsTotal -= assetsWithdrawn;
197 assetsAvailable -= assetsWithdrawn;
200 auto const& vaultAccount = vault->at(sfAccount);
222 <<
"VaultWithdraw: removed empty MPToken for vault shares"
230 <<
"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.
std::string transToken(TER code)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
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.
TER canWithdraw(AccountID const &from, ReadView const &view, AccountID const &to, SLE::const_ref toSle, bool hasDestinationTag)
Checks that can withdraw funds from an object to itself or a destination.
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)
TERSubset< CanCvtToNotTEC > NotTEC
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.