1#include <xrpl/ledger/View.h>
2#include <xrpl/ledger/helpers/CredentialHelpers.h>
3#include <xrpl/ledger/helpers/MPTokenHelpers.h>
4#include <xrpl/ledger/helpers/TokenHelpers.h>
5#include <xrpl/ledger/helpers/VaultHelpers.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/STTakesAsset.h>
13#include <xrpl/protocol/TER.h>
14#include <xrpl/protocol/TxFlags.h>
15#include <xrpl/tx/transactors/token/MPTokenAuthorize.h>
16#include <xrpl/tx/transactors/vault/VaultDeposit.h>
23 if (ctx.
tx[sfVaultID] == beast::zero)
25 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: zero/empty vault ID.";
29 if (ctx.
tx[sfAmount] <= beast::zero)
42 auto const& account = ctx.
tx[sfAccount];
43 auto const assets = ctx.
tx[sfAmount];
44 auto const vaultAsset = vault->at(sfAsset);
45 if (assets.asset() != vaultAsset)
48 auto const& vaultAccount = vault->at(sfAccount);
51 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: vault assets are non-transferable.";
55 auto const mptIssuanceID = vault->at(sfShareMPTID);
56 auto const vaultShare =
MPTIssue(mptIssuanceID);
57 if (vaultShare == assets.asset())
60 JLOG(ctx.
j.
error()) <<
"VaultDeposit: vault shares and assets cannot be same.";
69 JLOG(ctx.
j.
error()) <<
"VaultDeposit: missing issuance of vault shares.";
74 if (sleIssuance->isFlag(lsfMPTLocked))
77 JLOG(ctx.
j.
error()) <<
"VaultDeposit: issuance of vault shares is locked.";
90 if (vault->isFlag(lsfVaultPrivate) && account != vault->at(sfOwner))
92 auto const maybeDomainID = sleIssuance->at(~sfDomainID);
135 auto const vaultAsset = vault->at(sfAsset);
137 auto const amount =
ctx_.
tx[sfAmount];
139 auto const mptIssuanceID = (*vault)[sfShareMPTID];
144 JLOG(
j_.
error()) <<
"VaultDeposit: missing issuance of vault shares.";
149 auto const& vaultAccount = vault->at(sfAccount);
151 if (vault->isFlag(lsfVaultPrivate) &&
account_ != vault->at(sfOwner))
170 if (vault->isFlag(lsfVaultPrivate))
174 account_ == vault->at(sfOwner),
"xrpl::VaultDeposit::doApply : account is owner");
178 mptIssuanceID->
value(),
179 sleIssuance->at(sfIssuer),
189 STAmount sharesCreated = {vault->at(sfShareMPTID)}, assetsDeposited;
197 sharesCreated = *maybeShares;
199 if (sharesCreated == beast::zero)
207 if (*maybeAssets > amount)
210 JLOG(
j_.
error()) <<
"VaultDeposit: would take more than offered.";
214 assetsDeposited = *maybeAssets;
221 <<
"VaultDeposit: overflow error with"
222 <<
" scale=" << (int)vault->at(sfScale).value()
223 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
224 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount) <<
", amount=" << amount;
229 sharesCreated.
asset() != assetsDeposited.asset(),
230 "xrpl::VaultDeposit::doApply : assets are not shares");
232 vault->at(sfAssetsTotal) += assetsDeposited;
233 vault->at(sfAssetsAvailable) += assetsDeposited;
237 auto const maximum = *vault->at(sfAssetsMaximum);
238 if (maximum != 0 && *vault->at(sfAssetsTotal) > maximum)
251 assetsDeposited.asset(),
257 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)
From the perspective of a vault, return the number of assets to take from depositor when they receive...
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=shSIMPLE_BALANCE)
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)
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)
From the perspective of a vault, return the number of shares to give depositor when they offer a fixe...
bool isFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptIssue, int depth=0)
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 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.
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
TERSubset< CanCvtToNotTEC > NotTEC
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, int depth=0)
Check if the account lacks required authorization for MPT.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.