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>
59 if (balance - assets != balance)
63 <<
" leaves the depositor's balance " << balance.getFullText() <<
" unchanged";
72 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: zero/empty vault ID.";
97 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: vault deposit is not allowed in the investment "
98 "or redemption phase.";
103 auto const& account = ctx.
tx[sfAccount];
104 auto const amount = ctx.
tx[sfAmount];
105 auto const vaultAsset = vault->at(sfAsset);
106 if (amount.asset() != vaultAsset)
109 auto const& vaultAccount = vault->at(sfAccount);
112 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: vault assets are non-transferable.";
116 auto const mptIssuanceID = vault->at(sfShareMPTID);
117 auto const vaultShare =
MPTIssue(mptIssuanceID);
118 if (vaultShare == amount.asset())
121 JLOG(ctx.
j.
error()) <<
"VaultDeposit: vault shares and assets cannot be same.";
130 JLOG(ctx.
j.
error()) <<
"VaultDeposit: missing issuance of vault shares.";
135 if (sleIssuance->isFlag(lsfMPTLocked))
138 JLOG(ctx.
j.
error()) <<
"VaultDeposit: issuance of vault shares is locked.";
161 if (vault->isFlag(lsfVaultPrivate) && account != vault->at(sfOwner))
172 auto const roundedAmount = fix320Enabled
178 JLOG(ctx.
j.
warn()) <<
"VaultDeposit: deposit amount: " << ctx.
tx[sfAmount]
179 <<
" is zero at vault scale";
192 if (accountBalance < roundedAmount)
196 if (fix320Enabled && !roundedAmount.integral())
201 if (account != amount.getIssuer() &&
202 amount.isZeroAtScale(
scale(accountBalance, vaultAsset)))
204 JLOG(ctx.
j.
warn()) <<
"VaultDeposit: amount " << amount.getFullText()
205 <<
" rounds to zero at counterparty trust-line scale";
219 auto const applyViewContext =
ctx_.getApplyViewContext();
222 auto const vaultAsset = vault->at(sfAsset);
226 auto const amount = fix320Enabled
234 JLOG(
j_.error()) <<
"VaultDeposit: deposit amount: " <<
ctx_.tx[sfAmount] <<
" is zero";
240 auto const mptIssuanceID = (*vault)[sfShareMPTID];
245 JLOG(
j_.error()) <<
"VaultDeposit: missing issuance of vault shares.";
250 auto const& vaultAccount = vault->at(sfAccount);
252 if (vault->isFlag(lsfVaultPrivate) &&
accountID_ != vault->at(sfOwner))
267 mptIssuanceID->value(),
275 if (vault->isFlag(lsfVaultPrivate))
279 accountID_ == vault->at(sfOwner),
"xrpl::VaultDeposit::doApply : account is owner");
283 mptIssuanceID->value(),
284 sleIssuance->at(sfIssuer),
294 STAmount sharesCreated = {vault->at(sfShareMPTID)}, assetsDeposited;
304 sharesCreated = *maybeShares;
320 if (*maybeAssets > amount)
323 JLOG(
j_.error()) <<
"VaultDeposit: would take more than offered.";
327 assetsDeposited = *maybeAssets;
340 return maybeClamped.error();
341 assetsDeposited = *maybeClamped;
355 <<
"VaultDeposit: overflow error with"
356 <<
" scale=" << (int)vault->at(sfScale).value()
357 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
358 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount) <<
", amount=" << amount;
366 sharesCreated.
asset() != assetsDeposited.asset(),
367 "xrpl::VaultDeposit::doApply : assets are not shares");
374 auto const maximum = *vault->at(sfAssetsMaximum);
396 assetsDeposited.asset(),
401 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.
TER adjustVaultCash(SLE::Ref vault, STAmount const &delta, beast::Journal j)
Apply a signed cash movement to a Vault of any version.
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...
STAmount roundToPosteriorVaultScale(SLE::ConstRef vault, STAmount const &amount, Number::RoundingMode roundingMode)
Round an amount at the Vault's posterior live exponent.
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.
TER checkOptionalVaultInflow(SLE::ConstRef vault, STAmount const &amount)
Check whether amount is an admissible optional inflow.
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.
Number getAssetsTotal(SLE::ConstRef vault)
Return the Vault's current assets total.
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.