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/Zero.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/ledger/helpers/CredentialHelpers.h>
8#include <xrpl/ledger/helpers/MPTokenHelpers.h>
9#include <xrpl/ledger/helpers/TokenHelpers.h>
10#include <xrpl/ledger/helpers/VaultHelpers.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/Issue.h>
14#include <xrpl/protocol/LedgerFormats.h>
15#include <xrpl/protocol/MPTIssue.h>
16#include <xrpl/protocol/Protocol.h>
17#include <xrpl/protocol/SField.h>
18#include <xrpl/protocol/STAmount.h>
19#include <xrpl/protocol/STLedgerEntry.h>
20#include <xrpl/protocol/STNumber.h>
21#include <xrpl/protocol/STTakesAsset.h>
22#include <xrpl/protocol/STTx.h>
23#include <xrpl/protocol/TER.h>
24#include <xrpl/protocol/XRPAmount.h>
25#include <xrpl/tx/Transactor.h>
36 XRPL_ASSERT(vault && vault->getType() == ltVAULT,
"xrpl::roundToVaultScale : valid vault sle");
38 amount.
asset() == vault->at(sfAsset),
"xrpl::roundToVaultScale : valid vault asset");
43 int const postScale = [&]() {
45 return scale(vault->at(sfAssetsTotal) + amount, vault->at(sfAsset));
55 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: zero/empty vault ID.";
80 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: vault deposit is not allowed in the investment "
81 "or redemption phase.";
86 auto const& account = ctx.
tx[sfAccount];
87 auto const amount = ctx.
tx[sfAmount];
88 auto const vaultAsset = vault->at(sfAsset);
89 if (amount.asset() != vaultAsset)
92 auto const& vaultAccount = vault->at(sfAccount);
95 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: vault assets are non-transferable.";
99 auto const mptIssuanceID = vault->at(sfShareMPTID);
100 auto const vaultShare =
MPTIssue(mptIssuanceID);
101 if (vaultShare == amount.asset())
104 JLOG(ctx.
j.
error()) <<
"VaultDeposit: vault shares and assets cannot be same.";
113 JLOG(ctx.
j.
error()) <<
"VaultDeposit: missing issuance of vault shares.";
118 if (sleIssuance->isFlag(lsfMPTLocked))
121 JLOG(ctx.
j.
error()) <<
"VaultDeposit: issuance of vault shares is locked.";
142 if (vault->isFlag(lsfVaultPrivate) && account != vault->at(sfOwner))
144 auto const maybeDomainID = sleIssuance->at(~sfDomainID);
168 auto const roundedAmount = fix320Enabled ?
roundToVaultScale(amount, vault) : amount;
172 JLOG(ctx.
j.
warn()) <<
"VaultDeposit: deposit amount: " << ctx.
tx[sfAmount]
173 <<
" is zero at vault scale";
186 if (accountBalance < roundedAmount)
190 if (fix320Enabled && !roundedAmount.integral())
195 if (account != amount.getIssuer() &&
196 amount.isZeroAtScale(
scale(accountBalance, vaultAsset)))
198 JLOG(ctx.
j.
warn()) <<
"VaultDeposit: amount " << amount.getFullText()
199 <<
" rounds to zero at counterparty trust-line scale";
212 auto applyViewContext =
ctx_.getApplyViewContext();
215 auto const vaultAsset = vault->at(sfAsset);
226 JLOG(
j_.error()) <<
"VaultDeposit: deposit amount: " <<
ctx_.tx[sfAmount] <<
" is zero";
232 auto const mptIssuanceID = (*vault)[sfShareMPTID];
237 JLOG(
j_.error()) <<
"VaultDeposit: missing issuance of vault shares.";
242 auto const& vaultAccount = vault->at(sfAccount);
244 if (vault->isFlag(lsfVaultPrivate) &&
accountID_ != vault->at(sfOwner))
259 mptIssuanceID->value(),
267 if (vault->isFlag(lsfVaultPrivate))
271 accountID_ == vault->at(sfOwner),
"xrpl::VaultDeposit::doApply : account is owner");
275 mptIssuanceID->value(),
276 sleIssuance->at(sfIssuer),
286 STAmount sharesCreated = {vault->at(sfShareMPTID)}, assetsDeposited;
294 sharesCreated = *maybeShares;
304 if (*maybeAssets > amount)
307 JLOG(
j_.error()) <<
"VaultDeposit: would take more than offered.";
311 assetsDeposited = *maybeAssets;
318 <<
"VaultDeposit: overflow error with"
319 <<
" scale=" << (int)vault->at(sfScale).value()
320 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
321 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount) <<
", amount=" << amount;
326 sharesCreated.
asset() != assetsDeposited.asset(),
327 "xrpl::VaultDeposit::doApply : assets are not shares");
329 vault->at(sfAssetsTotal) += assetsDeposited;
330 vault->at(sfAssetsAvailable) += assetsDeposited;
334 auto const maximum = *vault->at(sfAssetsMaximum);
335 if (maximum != 0 && *vault->at(sfAssetsTotal) > maximum)
356 assetsDeposited.asset(),
361 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.
bool integral() const noexcept
Asset const & asset() const
std::shared_ptr< STLedgerEntry const > const & const_ref
AccountID const accountID_
static TER preclaim(PreclaimContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
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.
static NotTEC preflight(PreflightContext const &ctx)
TER validDomain(ReadView const &view, uint256 domainID, AccountID const &subject)
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 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.
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.
VaultPhase getVaultPhase(ReadView const &view, SLE::const_ref vault)
Returns the current lifecycle phase of a vault.
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
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
std::optional< STAmount > sharesToAssetsDeposit(SLE::const_ref vault, SLE::const_ref issuance, STAmount const &shares)
From the perspective of a vault, return the number of assets to take from depositor when they receive...
bool isFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptIssue, std::uint8_t depth=0)
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)
std::optional< STAmount > assetsToSharesDeposit(SLE::const_ref vault, SLE::const_ref issuance, STAmount const &assets)
From the perspective of a vault, return the number of shares to give depositor when they offer a fixe...
static STAmount roundToVaultScale(STAmount const &amount, SLE::const_ref vault)
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.