1#include <xrpld/app/tx/detail/MPTokenAuthorize.h>
2#include <xrpld/app/tx/detail/VaultDeposit.h>
4#include <xrpl/ledger/CredentialHelpers.h>
5#include <xrpl/ledger/View.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/Indexes.h>
8#include <xrpl/protocol/LedgerFormats.h>
9#include <xrpl/protocol/MPTIssue.h>
10#include <xrpl/protocol/SField.h>
11#include <xrpl/protocol/STNumber.h>
12#include <xrpl/protocol/TER.h>
13#include <xrpl/protocol/TxFlags.h>
20 if (ctx.
tx[sfVaultID] == beast::zero)
22 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: zero/empty vault ID.";
26 if (ctx.
tx[sfAmount] <= beast::zero)
39 auto const account = ctx.
tx[sfAccount];
40 auto const assets = ctx.
tx[sfAmount];
41 auto const vaultAsset = vault->at(sfAsset);
42 if (assets.asset() != vaultAsset)
45 if (vaultAsset.native())
47 else if (vaultAsset.holds<
MPTIssue>())
49 auto mptID = vaultAsset.get<
MPTIssue>().getMptID();
57 <<
"VaultDeposit: vault assets are non-transferable.";
62 else if (vaultAsset.holds<
Issue>())
70 <<
"VaultDeposit: missing issuer of vault assets.";
76 auto const mptIssuanceID = vault->at(sfShareMPTID);
77 auto const vaultShare =
MPTIssue(mptIssuanceID);
78 if (vaultShare == assets.asset())
82 <<
"VaultDeposit: vault shares and assets cannot be same.";
92 <<
"VaultDeposit: missing issuance of vault shares.";
101 <<
"VaultDeposit: issuance of vault shares is locked.";
116 auto const maybeDomainID = sleIssuance->at(~sfDomainID);
142 if ((vaultAsset.native() || vaultAsset.getIssuer() != account) &&
162 auto const amount =
ctx_.
tx[sfAmount];
164 auto const mptIssuanceID = (*vault)[sfShareMPTID];
169 JLOG(
j_.
error()) <<
"VaultDeposit: missing issuance of vault shares.";
174 auto const& vaultAccount = vault->at(sfAccount);
191 mptIssuanceID->
value(),
204 "ripple::VaultDeposit::doApply : account is owner");
208 mptIssuanceID->
value(),
209 sleIssuance->at(sfIssuer),
219 STAmount sharesCreated = {vault->at(sfShareMPTID)}, assetsDeposited;
224 auto const maybeShares =
228 sharesCreated = *maybeShares;
230 if (sharesCreated == beast::zero)
233 auto const maybeAssets =
237 else if (*maybeAssets > amount)
240 JLOG(
j_.
error()) <<
"VaultDeposit: would take more than offered.";
244 assetsDeposited = *maybeAssets;
251 <<
"VaultDeposit: overflow error with"
252 <<
" scale=" << (int)vault->at(sfScale).value()
253 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
254 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
255 <<
", amount=" << amount;
260 sharesCreated.
asset() != assetsDeposited.asset(),
261 "ripple::VaultDeposit::doApply : assets are not shares");
263 vault->at(sfAssetsTotal) += assetsDeposited;
264 vault->at(sfAssetsAvailable) += assetsDeposited;
268 auto const maximum = *vault->at(sfAssetsMaximum);
269 if (maximum != 0 && *vault->at(sfAssetsTotal) > maximum)
287 assetsDeposited.asset(),
293 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.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
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.