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.";
66 auto const assets = ctx.
tx[sfAmount];
67 auto const vaultAsset = vault->at(sfAsset);
68 auto const vaultShare = vault->at(sfShareMPTID);
69 if (assets.asset() != vaultAsset && assets.asset() != vaultShare)
72 if (vaultAsset.native())
74 else if (vaultAsset.holds<
MPTIssue>())
76 auto mptID = vaultAsset.get<
MPTIssue>().getMptID();
84 <<
"VaultWithdraw: vault assets are non-transferable.";
89 else if (vaultAsset.holds<
Issue>())
97 <<
"VaultWithdraw: missing issuer of vault assets.";
107 JLOG(ctx.
j.
error()) <<
"VaultWithdraw: invalid withdrawal policy.";
112 auto const account = ctx.
tx[sfAccount];
113 auto const dstAcct = ctx.
tx[~sfDestination].value_or(account);
115 if (sleDst ==
nullptr)
135 if (
auto const ter =
requireAuth(ctx.
view, vaultAsset, dstAcct, authType);
156 auto const mptIssuanceID = *((*vault)[sfShareMPTID]);
161 JLOG(
j_.
error()) <<
"VaultWithdraw: missing issuance of vault shares.";
171 auto const amount =
ctx_.
tx[sfAmount];
172 Asset const vaultAsset = vault->at(sfAsset);
173 MPTIssue const share{mptIssuanceID};
178 if (amount.asset() == vaultAsset)
182 auto const maybeShares =
186 sharesRedeemed = *maybeShares;
189 if (sharesRedeemed == beast::zero)
191 auto const maybeAssets =
195 assetsWithdrawn = *maybeAssets;
197 else if (amount.asset() == share)
200 sharesRedeemed = amount;
201 auto const maybeAssets =
205 assetsWithdrawn = *maybeAssets;
215 <<
"VaultWithdraw: overflow error with"
216 <<
" scale=" << (int)vault->at(sfScale).value()
217 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
218 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
219 <<
", amount=" << amount.value();
229 j_) < sharesRedeemed)
231 JLOG(
j_.
debug()) <<
"VaultWithdraw: account doesn't hold enough shares";
235 auto assetsAvailable = vault->at(sfAssetsAvailable);
236 auto assetsTotal = vault->at(sfAssetsTotal);
237 [[maybe_unused]]
auto const lossUnrealized = vault->at(sfLossUnrealized);
239 lossUnrealized <= (assetsTotal - assetsAvailable),
240 "ripple::VaultWithdraw::doApply : loss and assets do balance");
245 if (*assetsAvailable < assetsWithdrawn)
247 JLOG(
j_.
debug()) <<
"VaultWithdraw: vault doesn't hold enough assets";
251 assetsTotal -= assetsWithdrawn;
252 assetsAvailable -= assetsWithdrawn;
255 auto const& vaultAccount = vault->at(sfAccount);
277 <<
"VaultWithdraw: removed empty MPToken for vault shares"
285 <<
"VaultWithdraw: failed to remove MPToken for vault shares"
296 if (!vaultAsset.
native() &&
321 assetsWithdrawn.
asset(),
327 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.
AccountID const & getIssuer() const
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
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)
TER addEmptyHolding(ApplyView &view, AccountID const &accountID, XRPAmount priorBalance, Issue const &issue, beast::Journal journal)
Any transactors that call addEmptyHolding() in doApply must call canAddHolding() in preflight with th...
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.