20#include <xrpld/app/tx/detail/MPTokenAuthorize.h>
21#include <xrpld/app/tx/detail/VaultDeposit.h>
23#include <xrpl/ledger/CredentialHelpers.h>
24#include <xrpl/ledger/View.h>
25#include <xrpl/protocol/Feature.h>
26#include <xrpl/protocol/Indexes.h>
27#include <xrpl/protocol/LedgerFormats.h>
28#include <xrpl/protocol/MPTIssue.h>
29#include <xrpl/protocol/SField.h>
30#include <xrpl/protocol/STNumber.h>
31#include <xrpl/protocol/TER.h>
32#include <xrpl/protocol/TxFlags.h>
39 if (ctx.
tx[sfVaultID] == beast::zero)
41 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: zero/empty vault ID.";
45 if (ctx.
tx[sfAmount] <= beast::zero)
58 auto const account = ctx.
tx[sfAccount];
59 auto const assets = ctx.
tx[sfAmount];
60 auto const vaultAsset = vault->at(sfAsset);
61 if (assets.asset() != vaultAsset)
64 if (vaultAsset.native())
66 else if (vaultAsset.holds<
MPTIssue>())
68 auto mptID = vaultAsset.get<
MPTIssue>().getMptID();
76 <<
"VaultDeposit: vault assets are non-transferable.";
81 else if (vaultAsset.holds<
Issue>())
89 <<
"VaultDeposit: missing issuer of vault assets.";
95 auto const mptIssuanceID = vault->at(sfShareMPTID);
96 auto const vaultShare =
MPTIssue(mptIssuanceID);
97 if (vaultShare == assets.asset())
101 <<
"VaultDeposit: vault shares and assets cannot be same.";
111 <<
"VaultDeposit: missing issuance of vault shares.";
120 <<
"VaultDeposit: issuance of vault shares is locked.";
135 auto const maybeDomainID = sleIssuance->at(~sfDomainID);
178 auto const amount =
ctx_.
tx[sfAmount];
180 auto const mptIssuanceID = (*vault)[sfShareMPTID];
185 JLOG(
j_.
error()) <<
"VaultDeposit: missing issuance of vault shares.";
190 auto const& vaultAccount = vault->at(sfAccount);
208 mptIssuanceID->
value(),
221 "ripple::VaultDeposit::doApply : account is owner");
225 mptIssuanceID->
value(),
226 sleIssuance->at(sfIssuer),
236 STAmount sharesCreated = {vault->at(sfShareMPTID)}, assetsDeposited;
241 auto const maybeShares =
245 sharesCreated = *maybeShares;
247 if (sharesCreated == beast::zero)
250 auto const maybeAssets =
254 else if (*maybeAssets > amount)
257 JLOG(
j_.
error()) <<
"VaultDeposit: would take more than offered.";
261 assetsDeposited = *maybeAssets;
268 <<
"VaultDeposit: overflow error with"
269 <<
" scale=" << (int)vault->at(sfScale).value()
270 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
271 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
272 <<
", amount=" << amount;
277 sharesCreated.
asset() != assetsDeposited.asset(),
278 "ripple::VaultDeposit::doApply : assets are not shares");
280 vault->at(sfAssetsTotal) += assetsDeposited;
281 vault->at(sfAssetsAvailable) += assetsDeposited;
285 auto const maximum = *vault->at(sfAssetsMaximum);
286 if (maximum != 0 && *vault->at(sfAssetsTotal) > maximum)
304 assetsDeposited.asset(),
310 JLOG(
j_.
error()) <<
"VaultDeposit: negative balance of account assets.";
beast::Journal const journal
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.
Asset const & asset() const
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
constexpr value_type value() const
Returns the underlying value.
TER validDomain(ReadView const &view, uint256 domainID, AccountID const &subject)
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::optional< STAmount > sharesToAssetsDeposit(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &shares)
std::optional< STAmount > assetsToSharesDeposit(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &assets)
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.
bool isFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
bool isTesSuccess(TER x) noexcept
TER enforceMPTokenAuthorization(ApplyView &view, MPTID const &mptIssuanceID, AccountID const &account, XRPAmount const &priorBalance, beast::Journal j)
Enforce account has MPToken to match its authorization.
TER authorizeMPToken(ApplyView &view, XRPAmount const &priorBalance, MPTID const &mptIssuanceID, AccountID const &account, beast::Journal journal, std::uint32_t flags=0, std::optional< AccountID > holderID=std::nullopt)
TERSubset< CanCvtToNotTEC > NotTEC
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.