20#include <xrpld/app/misc/CredentialHelpers.h>
21#include <xrpld/app/tx/detail/VaultWithdraw.h>
22#include <xrpld/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 amount =
ctx_.
tx[sfAmount];
196 auto const asset = vault->at(sfAsset);
197 auto const share =
MPTIssue(mptIssuanceID);
199 if (amount.asset() == asset)
205 else if (amount.asset() == share)
222 JLOG(
j_.
debug()) <<
"VaultWithdraw: account doesn't hold enough shares";
229 if (*vault->at(sfAssetsAvailable) < assets)
231 JLOG(
j_.
debug()) <<
"VaultWithdraw: vault doesn't hold enough assets";
235 vault->at(sfAssetsTotal) -= assets;
236 vault->at(sfAssetsAvailable) -= assets;
239 auto const& vaultAccount = vault->at(sfAccount);
245 auto const dstAcct = [&]() ->
AccountID {
266 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.
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType)
Check if the account lacks required authorization.
STAmount sharesToAssetsWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &shares)
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
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
STAmount assetsToSharesWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &assets)
constexpr std::uint32_t tfUniversalMask
std::uint8_t constexpr vaultStrategyFirstComeFirstServe
Vault withdrawal policies.
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee)
Calls static accountSendIOU if saAmount represents Issue.
TERSubset< CanCvtToNotTEC > NotTEC
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.