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 auto const& vaultAccount = vault->at(sfAccount);
46 if (
auto ter =
canTransfer(ctx.
view, vaultAsset, account, vaultAccount);
50 <<
"VaultDeposit: vault assets are non-transferable.";
54 auto const mptIssuanceID = vault->at(sfShareMPTID);
55 auto const vaultShare =
MPTIssue(mptIssuanceID);
56 if (vaultShare == assets.asset())
60 <<
"VaultDeposit: vault shares and assets cannot be same.";
70 <<
"VaultDeposit: missing issuance of vault shares.";
79 <<
"VaultDeposit: issuance of vault shares is locked.";
94 auto const maybeDomainID = sleIssuance->at(~sfDomainID);
120 if ((vaultAsset.native() || vaultAsset.getIssuer() != account) &&
140 auto const amount =
ctx_.
tx[sfAmount];
142 auto const mptIssuanceID = (*vault)[sfShareMPTID];
147 JLOG(
j_.
error()) <<
"VaultDeposit: missing issuance of vault shares.";
152 auto const& vaultAccount = vault->at(sfAccount);
169 mptIssuanceID->
value(),
182 "xrpl::VaultDeposit::doApply : account is owner");
186 mptIssuanceID->
value(),
187 sleIssuance->at(sfIssuer),
197 STAmount sharesCreated = {vault->at(sfShareMPTID)}, assetsDeposited;
202 auto const maybeShares =
206 sharesCreated = *maybeShares;
208 if (sharesCreated == beast::zero)
211 auto const maybeAssets =
215 else if (*maybeAssets > amount)
218 JLOG(
j_.
error()) <<
"VaultDeposit: would take more than offered.";
222 assetsDeposited = *maybeAssets;
229 <<
"VaultDeposit: overflow error with"
230 <<
" scale=" << (int)vault->at(sfScale).value()
231 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
232 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
233 <<
", amount=" << amount;
238 sharesCreated.
asset() != assetsDeposited.asset(),
239 "xrpl::VaultDeposit::doApply : assets are not shares");
241 vault->at(sfAssetsTotal) += assetsDeposited;
242 vault->at(sfAssetsAvailable) += assetsDeposited;
246 auto const maximum = *vault->at(sfAssetsMaximum);
247 if (maximum != 0 && *vault->at(sfAssetsTotal) > maximum)
265 assetsDeposited.asset(),
271 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 bool exists(Keylet const &k) const =0
Determine if a state item exists.
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 mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
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)
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)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
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.
std::optional< STAmount > assetsToSharesDeposit(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &assets)
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to)
Check if the destination account is allowed to receive MPT.
bool isFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
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.
TERSubset< CanCvtToNotTEC > NotTEC
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.