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/SField.h>
17#include <xrpl/protocol/STAmount.h>
18#include <xrpl/protocol/STLedgerEntry.h>
19#include <xrpl/protocol/STNumber.h>
20#include <xrpl/protocol/STTakesAsset.h>
21#include <xrpl/protocol/STTx.h>
22#include <xrpl/protocol/TER.h>
23#include <xrpl/protocol/XRPAmount.h>
24#include <xrpl/tx/Transactor.h>
34 XRPL_ASSERT(vault && vault->getType() == ltVAULT,
"xrpl::roundToVaultScale : valid vault sle");
36 amount.
asset() == vault->at(sfAsset),
"xrpl::roundToVaultScale : valid vault asset");
41 int const postScale = [&]() {
43 return scale(vault->at(sfAssetsTotal) + amount, vault->at(sfAsset));
51 if (ctx.
tx[sfVaultID] == beast::kZero)
53 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: zero/empty vault ID.";
57 if (ctx.
tx[sfAmount] <= beast::kZero)
73 auto const& account = ctx.
tx[sfAccount];
74 auto const amount = ctx.
tx[sfAmount];
75 auto const vaultAsset = vault->at(sfAsset);
76 if (amount.asset() != vaultAsset)
79 auto const& vaultAccount = vault->at(sfAccount);
82 JLOG(ctx.
j.
debug()) <<
"VaultDeposit: vault assets are non-transferable.";
86 auto const mptIssuanceID = vault->at(sfShareMPTID);
87 auto const vaultShare =
MPTIssue(mptIssuanceID);
88 if (vaultShare == amount.asset())
91 JLOG(ctx.
j.
error()) <<
"VaultDeposit: vault shares and assets cannot be same.";
100 JLOG(ctx.
j.
error()) <<
"VaultDeposit: missing issuance of vault shares.";
105 if (sleIssuance->isFlag(lsfMPTLocked))
108 JLOG(ctx.
j.
error()) <<
"VaultDeposit: issuance of vault shares is locked.";
129 if (vault->isFlag(lsfVaultPrivate) && account != vault->at(sfOwner))
131 auto const maybeDomainID = sleIssuance->at(~sfDomainID);
155 auto const roundedAmount = fix320Enabled ?
roundToVaultScale(amount, vault) : amount;
157 if (fix320Enabled && roundedAmount == beast::kZero)
159 JLOG(ctx.
j.
warn()) <<
"VaultDeposit: deposit amount: " << ctx.
tx[sfAmount]
160 <<
" is zero at vault scale";
173 if (accountBalance < roundedAmount)
177 if (fix320Enabled && !roundedAmount.integral())
182 if (account != amount.getIssuer() &&
183 amount.isZeroAtScale(
scale(accountBalance, vaultAsset)))
185 JLOG(ctx.
j.
warn()) <<
"VaultDeposit: amount " << amount.getFullText()
186 <<
" rounds to zero at counterparty trust-line scale";
201 auto const vaultAsset = vault->at(sfAsset);
209 if (amount == beast::kZero)
212 JLOG(
j_.error()) <<
"VaultDeposit: deposit amount: " <<
ctx_.tx[sfAmount] <<
" is zero";
218 auto const mptIssuanceID = (*vault)[sfShareMPTID];
223 JLOG(
j_.error()) <<
"VaultDeposit: missing issuance of vault shares.";
228 auto const& vaultAccount = vault->at(sfAccount);
230 if (vault->isFlag(lsfVaultPrivate) &&
accountID_ != vault->at(sfOwner))
249 if (vault->isFlag(lsfVaultPrivate))
253 accountID_ == vault->at(sfOwner),
"xrpl::VaultDeposit::doApply : account is owner");
257 mptIssuanceID->value(),
258 sleIssuance->at(sfIssuer),
268 STAmount sharesCreated = {vault->at(sfShareMPTID)}, assetsDeposited;
276 sharesCreated = *maybeShares;
278 if (sharesCreated == beast::kZero)
286 if (*maybeAssets > amount)
289 JLOG(
j_.error()) <<
"VaultDeposit: would take more than offered.";
293 assetsDeposited = *maybeAssets;
300 <<
"VaultDeposit: overflow error with"
301 <<
" scale=" << (int)vault->at(sfScale).value()
302 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
303 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount) <<
", amount=" << amount;
308 sharesCreated.
asset() != assetsDeposited.asset(),
309 "xrpl::VaultDeposit::doApply : assets are not shares");
311 vault->at(sfAssetsTotal) += assetsDeposited;
312 vault->at(sfAssetsAvailable) += assetsDeposited;
316 auto const maximum = *vault->at(sfAssetsMaximum);
317 if (maximum != 0 && *vault->at(sfAssetsTotal) > maximum)
338 assetsDeposited.asset(),
343 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 mptokenIssuance(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.
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
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 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)
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, 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 &dstAcct, Asset const &asset)
Checks freeze compliance for depositing an asset into a pseudo-account (e.g.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
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.
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.
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.