1#include <xrpl/tx/transactors/vault/VaultDeposit.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/ledger/helpers/MPTokenHelpers.h>
10#include <xrpl/ledger/helpers/TokenHelpers.h>
11#include <xrpl/ledger/helpers/VaultHelpers.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/Feature.h>
14#include <xrpl/protocol/Indexes.h>
15#include <xrpl/protocol/Issue.h>
16#include <xrpl/protocol/LedgerFormats.h>
17#include <xrpl/protocol/MPTIssue.h>
18#include <xrpl/protocol/Protocol.h>
19#include <xrpl/protocol/SField.h>
20#include <xrpl/protocol/STAmount.h>
21#include <xrpl/protocol/STLedgerEntry.h>
22#include <xrpl/protocol/STNumber.h>
23#include <xrpl/protocol/STTakesAsset.h>
24#include <xrpl/protocol/STTx.h>
25#include <xrpl/protocol/TER.h>
26#include <xrpl/protocol/XRPAmount.h>
27#include <xrpl/tx/Transactor.h>
38 XRPL_ASSERT(vault && vault->getType() == ltVAULT,
"xrpl::roundToVaultScale : valid vault sle");
40 amount.
asset() == vault->at(sfAsset),
"xrpl::roundToVaultScale : valid vault asset");
45 int const postScale = [&]() {
47 return scale(vault->at(sfAssetsTotal) + amount, vault->at(sfAsset));
77 if (balance - assets != balance)
81 <<
" leaves the depositor's balance " << balance.getFullText() <<
" unchanged";
90 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: zero/empty vault ID.";
115 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: vault deposit is not allowed in the investment "
116 "or redemption phase.";
121 auto const& account = ctx.
tx[sfAccount];
122 auto const amount = ctx.
tx[sfAmount];
123 auto const vaultAsset = vault->at(sfAsset);
124 if (amount.asset() != vaultAsset)
127 auto const& vaultAccount = vault->at(sfAccount);
130 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: vault assets are non-transferable.";
134 auto const mptIssuanceID = vault->at(sfShareMPTID);
135 auto const vaultShare =
MPTIssue(mptIssuanceID);
136 if (vaultShare == amount.asset())
139 JLOG(ctx.
j.
error()) <<
"VaultDeposit: vault shares and assets cannot be same.";
148 JLOG(ctx.
j.
error()) <<
"VaultDeposit: missing issuance of vault shares.";
153 if (sleIssuance->isFlag(lsfMPTLocked))
156 JLOG(ctx.
j.
error()) <<
"VaultDeposit: issuance of vault shares is locked.";
179 if (vault->isFlag(lsfVaultPrivate) && account != vault->at(sfOwner))
190 auto const roundedAmount = fix320Enabled ?
roundToVaultScale(amount, vault) : amount;
194 JLOG(ctx.
j.
warn()) <<
"VaultDeposit: deposit amount: " << ctx.
tx[sfAmount]
195 <<
" is zero at vault scale";
208 if (accountBalance < roundedAmount)
212 if (fix320Enabled && !roundedAmount.integral())
217 if (account != amount.getIssuer() &&
218 amount.isZeroAtScale(
scale(accountBalance, vaultAsset)))
220 JLOG(ctx.
j.
warn()) <<
"VaultDeposit: amount " << amount.getFullText()
221 <<
" rounds to zero at counterparty trust-line scale";
235 auto applyViewContext =
ctx_.getApplyViewContext();
238 auto const vaultAsset = vault->at(sfAsset);
249 JLOG(
j_.error()) <<
"VaultDeposit: deposit amount: " <<
ctx_.tx[sfAmount] <<
" is zero";
255 auto const mptIssuanceID = (*vault)[sfShareMPTID];
260 JLOG(
j_.error()) <<
"VaultDeposit: missing issuance of vault shares.";
265 auto const& vaultAccount = vault->at(sfAccount);
267 if (vault->isFlag(lsfVaultPrivate) &&
accountID_ != vault->at(sfOwner))
282 mptIssuanceID->value(),
290 if (vault->isFlag(lsfVaultPrivate))
294 accountID_ == vault->at(sfOwner),
"xrpl::VaultDeposit::doApply : account is owner");
298 mptIssuanceID->value(),
299 sleIssuance->at(sfIssuer),
309 STAmount sharesCreated = {vault->at(sfShareMPTID)}, assetsDeposited;
319 sharesCreated = *maybeShares;
335 if (*maybeAssets > amount)
338 JLOG(
j_.error()) <<
"VaultDeposit: would take more than offered.";
342 assetsDeposited = *maybeAssets;
355 return maybeClamped.error();
356 assetsDeposited = *maybeClamped;
370 <<
"VaultDeposit: overflow error with"
371 <<
" scale=" << (int)vault->at(sfScale).value()
372 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
373 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount) <<
", amount=" << amount;
378 sharesCreated.
asset() != assetsDeposited.asset(),
379 "xrpl::VaultDeposit::doApply : assets are not shares");
381 vault->at(sfAssetsTotal) += assetsDeposited;
382 vault->at(sfAssetsAvailable) += assetsDeposited;
386 auto const maximum = *vault->at(sfAssetsMaximum);
387 if (maximum != 0 && *vault->at(sfAssetsTotal) > maximum)
408 assetsDeposited.asset(),
413 JLOG(
j_.error()) <<
"VaultDeposit: negative balance of account assets.";
A generic endpoint for log messages.
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void update(SLE::Ref sle)=0
Indicate changes to a peeked SLE.
A currency issued by an account.
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
std::string getFullText() const override
bool integral() const noexcept
Asset const & asset() const
std::shared_ptr< STLedgerEntry const > const & ConstRef
AccountID const accountID_
static TER preclaim(PreclaimContext const &ctx)
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
static NotTEC preflight(PreflightContext const &ctx)
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
TER enforceMPTokenAuthorization(ApplyViewContext ctx, MPTID const &mptIssuanceID, AccountID const &account, XRPAmount const &priorBalance, beast::Journal j)
Enforce account has MPToken to match its authorization.
TER checkVaultDomain(ReadView const &view, SLE::ConstRef issuance, AccountID const &subject, SuppressExpired suppressExpired)
Checks that subject belongs to the permissioned domain governing a vault's shares.
TER checkDepositFreeze(ReadView const &view, AccountID const &srcAcct, AccountID const &pseudoAcct, Asset const &asset)
Checks freeze compliance for depositing an asset into a pseudo-account (e.g.
std::optional< STAmount > sharesToAssetsDeposit(SLE::ConstRef vault, SLE::ConstRef issuance, STAmount const &shares)
From the perspective of a vault, return the number of assets to take from depositor when they receive...
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
static bool roundsToZeroForDepositor(ReadView const &view, AccountID const &account, STAmount const &assets, beast::Journal j)
std::expected< STAmount, TER > clampToAssetsTotalScale(SLE::ConstRef vault, STAmount const &delta)
Adjusts a requested asset change (delta) to match the decimal scale of the updated total vault assets...
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to, WaiveMPTCanTransfer waive=WaiveMPTCanTransfer::No, std::uint8_t depth=0)
Check whether to may receive the given MPT from from.
STAmount roundToScale(STAmount const &value, std::int32_t scale, Number::RoundingMode rounding=Number::getround())
Round an arbitrary precision Amount to the precision of an STAmount that has a given exponent.
TERSubset< CanCvtToNotTEC > NotTEC
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, SLE::Ref sponsorSle={}, WaiveTransferFee waiveFee=WaiveTransferFee::No, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
std::optional< STAmount > assetsToSharesDeposit(SLE::ConstRef vault, SLE::ConstRef issuance, STAmount const &assets)
From the perspective of a vault, return the number of shares to give depositor when they offer a fixe...
VaultPhase getVaultPhase(ReadView const &view, SLE::ConstRef vault)
Returns the current lifecycle phase of a vault.
bool isFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptIssue, std::uint8_t depth=0)
Returns true if account cannot send or receive tokens of mptIssue because a freeze applies.
static STAmount roundToVaultScale(STAmount const &amount, SLE::ConstRef vault)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
TER authorizeMPToken(ApplyViewContext ctx, XRPAmount const &priorBalance, MPTID const &mptIssuanceID, AccountID const &account, beast::Journal journal, std::uint32_t flags=0, std::optional< AccountID > holderID=std::nullopt)
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, std::uint8_t depth=0)
Check if the account lacks required authorization for MPT.
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.