20#include <xrpld/app/tx/detail/VaultWithdraw.h>
22#include <xrpl/ledger/CredentialHelpers.h>
23#include <xrpl/ledger/View.h>
24#include <xrpl/protocol/AccountID.h>
25#include <xrpl/protocol/Feature.h>
26#include <xrpl/protocol/SField.h>
27#include <xrpl/protocol/STNumber.h>
28#include <xrpl/protocol/TER.h>
29#include <xrpl/protocol/TxFlags.h>
36 if (ctx.
tx[sfVaultID] == beast::zero)
38 JLOG(ctx.
j.
debug()) <<
"VaultWithdraw: zero/empty vault ID.";
42 if (ctx.
tx[sfAmount] <= beast::zero)
45 if (
auto const destination = ctx.
tx[~sfDestination];
46 destination.has_value())
48 if (*destination == beast::zero)
51 <<
"VaultWithdraw: zero/empty destination account.";
57 JLOG(ctx.
j.
debug()) <<
"VaultWithdraw: sfDestinationTag is set but "
58 "sfDestination is not";
72 auto const assets = ctx.
tx[sfAmount];
73 auto const vaultAsset = vault->at(sfAsset);
74 auto const vaultShare = vault->at(sfShareMPTID);
75 if (assets.asset() != vaultAsset && assets.asset() != vaultShare)
78 if (vaultAsset.native())
80 else if (vaultAsset.holds<
MPTIssue>())
82 auto mptID = vaultAsset.get<
MPTIssue>().getMptID();
90 <<
"VaultWithdraw: vault assets are non-transferable.";
95 else if (vaultAsset.holds<
Issue>())
103 <<
"VaultWithdraw: missing issuer of vault assets.";
113 JLOG(ctx.
j.
error()) <<
"VaultWithdraw: invalid withdrawal policy.";
118 auto const account = ctx.
tx[sfAccount];
119 auto const dstAcct = [&]() ->
AccountID {
128 if (account != dstAcct)
131 if (sleDst ==
nullptr)
150 if (
auto const ter =
requireAuth(ctx.
view, vaultAsset, dstAcct, authType);
171 auto const mptIssuanceID = *((*vault)[sfShareMPTID]);
176 JLOG(
j_.
error()) <<
"VaultWithdraw: missing issuance of vault shares.";
186 auto const amount =
ctx_.
tx[sfAmount];
187 Asset const vaultAsset = vault->at(sfAsset);
188 MPTIssue const share{mptIssuanceID};
193 if (amount.asset() == vaultAsset)
197 auto const maybeShares =
201 sharesRedeemed = *maybeShares;
204 if (sharesRedeemed == beast::zero)
206 auto const maybeAssets =
210 assetsWithdrawn = *maybeAssets;
212 else if (amount.asset() == share)
215 sharesRedeemed = amount;
216 auto const maybeAssets =
220 assetsWithdrawn = *maybeAssets;
230 <<
"VaultWithdraw: overflow error with"
231 <<
" scale=" << (int)vault->at(sfScale).value()
232 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
233 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
234 <<
", amount=" << amount.value();
244 j_) < sharesRedeemed)
246 JLOG(
j_.
debug()) <<
"VaultWithdraw: account doesn't hold enough shares";
250 auto assetsAvailable = vault->at(sfAssetsAvailable);
251 auto assetsTotal = vault->at(sfAssetsTotal);
252 [[maybe_unused]]
auto const lossUnrealized = vault->at(sfLossUnrealized);
254 lossUnrealized <= (assetsTotal - assetsAvailable),
255 "ripple::VaultWithdraw::doApply : loss and assets do balance");
260 if (*assetsAvailable < assetsWithdrawn)
262 JLOG(
j_.
debug()) <<
"VaultWithdraw: vault doesn't hold enough assets";
266 assetsTotal -= assetsWithdrawn;
267 assetsAvailable -= assetsWithdrawn;
270 auto const& vaultAccount = vault->at(sfAccount);
292 <<
"VaultWithdraw: removed empty MPToken for vault shares"
300 <<
"VaultWithdraw: failed to remove MPToken for vault shares"
310 auto const dstAcct = [&]() ->
AccountID {
331 assetsWithdrawn.
asset(),
337 JLOG(
j_.
error()) <<
"VaultWithdraw: negative balance of vault assets.";
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.
A currency issued by an account.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
Asset const & asset() const
AccountID getAccountID(SField const &field) 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
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &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 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.
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
std::optional< STAmount > sharesToAssetsWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &shares)
std::string transToken(TER code)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
bool isTesSuccess(TER x) noexcept
std::string to_string(base_uint< Bits, Tag > const &a)
std::uint8_t constexpr vaultStrategyFirstComeFirstServe
Vault withdrawal policies.
TER removeEmptyHolding(ApplyView &view, AccountID const &accountID, Issue const &issue, beast::Journal journal)
TERSubset< CanCvtToNotTEC > NotTEC
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.