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>
45 if (ctx.
tx[sfVaultID] == beast::zero)
47 JLOG(ctx.
j.
debug()) <<
"VaultWithdraw: zero/empty vault ID.";
51 if (ctx.
tx[sfAmount] <= beast::zero)
54 if (
auto const destination = ctx.
tx[~sfDestination];
55 destination.has_value())
57 if (*destination == beast::zero)
60 <<
"VaultWithdraw: zero/empty destination account.";
66 JLOG(ctx.
j.
debug()) <<
"VaultWithdraw: sfDestinationTag is set but "
67 "sfDestination is not";
81 auto const assets = ctx.
tx[sfAmount];
82 auto const vaultAsset = vault->at(sfAsset);
83 auto const vaultShare = vault->at(sfShareMPTID);
84 if (assets.asset() != vaultAsset && assets.asset() != vaultShare)
87 if (vaultAsset.native())
89 else if (vaultAsset.holds<
MPTIssue>())
91 auto mptID = vaultAsset.get<
MPTIssue>().getMptID();
99 <<
"VaultWithdraw: vault assets are non-transferable.";
104 else if (vaultAsset.holds<
Issue>())
112 <<
"VaultWithdraw: missing issuer of vault assets.";
122 JLOG(ctx.
j.
error()) <<
"VaultWithdraw: invalid withdrawal policy.";
127 auto const account = ctx.
tx[sfAccount];
128 auto const dstAcct = [&]() ->
AccountID {
137 if (account != dstAcct)
140 if (sleDst ==
nullptr)
159 if (
auto const ter =
requireAuth(ctx.
view, vaultAsset, dstAcct, authType);
180 auto const mptIssuanceID = *((*vault)[sfShareMPTID]);
185 JLOG(
j_.
error()) <<
"VaultWithdraw: missing issuance of vault shares.";
195 auto const amount =
ctx_.
tx[sfAmount];
196 Asset const vaultAsset = vault->at(sfAsset);
197 MPTIssue const share{mptIssuanceID};
202 if (amount.asset() == vaultAsset)
206 auto const maybeShares =
210 sharesRedeemed = *maybeShares;
213 if (sharesRedeemed == beast::zero)
215 auto const maybeAssets =
219 assetsWithdrawn = *maybeAssets;
221 else if (amount.asset() == share)
224 sharesRedeemed = amount;
225 auto const maybeAssets =
229 assetsWithdrawn = *maybeAssets;
239 <<
"VaultWithdraw: overflow error with"
240 <<
" scale=" << (int)vault->at(sfScale).value()
241 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
242 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
243 <<
", amount=" << amount.value();
253 j_) < sharesRedeemed)
255 JLOG(
j_.
debug()) <<
"VaultWithdraw: account doesn't hold enough shares";
259 auto assetsAvailable = vault->at(sfAssetsAvailable);
260 auto assetsTotal = vault->at(sfAssetsTotal);
261 [[maybe_unused]]
auto const lossUnrealized = vault->at(sfLossUnrealized);
263 lossUnrealized <= (assetsTotal - assetsAvailable),
264 "ripple::VaultWithdraw::doApply : loss and assets do balance");
269 if (*assetsAvailable < assetsWithdrawn)
271 JLOG(
j_.
debug()) <<
"VaultWithdraw: vault doesn't hold enough assets";
275 assetsTotal -= assetsWithdrawn;
276 assetsAvailable -= assetsWithdrawn;
279 auto const& vaultAccount = vault->at(sfAccount);
301 <<
"VaultWithdraw: removed empty MPToken for vault shares"
309 <<
"VaultWithdraw: failed to remove MPToken for vault shares"
319 auto const dstAcct = [&]() ->
AccountID {
340 assetsWithdrawn.
asset(),
346 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.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Asset const & asset() const
AccountID getAccountID(SField const &field) const
bool isFieldPresent(SField const &field) const
std::uint32_t getFlags() 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)
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
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)
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
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)
constexpr std::uint32_t tfUniversalMask
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.