1#include <xrpl/tx/transactors/vault/VaultCreate.h>
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/core/ServiceRegistry.h>
7#include <xrpl/ledger/View.h>
8#include <xrpl/ledger/helpers/AccountRootHelpers.h>
9#include <xrpl/ledger/helpers/MPTokenHelpers.h>
10#include <xrpl/ledger/helpers/TokenHelpers.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/Asset.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/STLedgerEntry.h>
21#include <xrpl/protocol/STNumber.h>
22#include <xrpl/protocol/STTakesAsset.h>
23#include <xrpl/protocol/STTx.h>
24#include <xrpl/protocol/TER.h>
25#include <xrpl/protocol/TxFlags.h>
26#include <xrpl/protocol/XRPAmount.h>
27#include <xrpl/tx/Transactor.h>
28#include <xrpl/tx/transactors/token/MPTokenIssuanceCreate.h>
51 return tfVaultCreateMask;
60 if (
auto const withdrawalPolicy = ctx.
tx[~sfWithdrawalPolicy])
67 if (
auto const domain = ctx.
tx[~sfDomainID])
69 if (*domain == beast::kZero)
79 if (
auto const assetMax = ctx.
tx[~sfAssetsMaximum])
81 if (*assetMax < beast::kZero)
85 if (
auto const metadata = ctx.
tx[~sfMPTokenMetadata])
91 if (
auto const scale = ctx.
tx[~sfScale])
93 auto const vaultAsset = ctx.
tx[sfAsset];
107 auto const vaultAsset = ctx.
tx[sfAsset];
108 auto const account = ctx.
tx[sfAccount];
116 if (!vaultAsset.native())
126 if (
auto const domain = ctx.
tx[~sfDomainID])
135 accountId == beast::kZero)
148 auto const& tx =
ctx_.tx;
149 auto const sequence = tx.getSeqValue();
151 if (owner ==
nullptr)
160 auto const ownerCount = owner->at(sfOwnerCount);
166 return maybePseudo.error();
167 auto const& pseudo = *maybePseudo;
168 AccountID const pseudoId = pseudo->at(sfAccount);
169 auto const asset = tx[sfAsset];
179 if (!tx.isFlag(tfVaultShareNonTransferable))
180 mptFlags |= (lsfMPTCanEscrow | lsfMPTCanTrade | lsfMPTCanTransfer);
181 if (tx.isFlag(tfVaultPrivate))
182 mptFlags |= lsfMPTRequireAuth;
193 if (!
view().rules().enabled(fixCleanup3_2_0) || asset.native())
203 .priorBalance = std::nullopt,
208 .transferFee = std::nullopt,
209 .metadata = tx[~sfMPTokenMetadata],
210 .domainId = tx[~sfDomainID],
211 .mutableFlags = std::nullopt,
212 .referenceHolding = referenceHolding,
215 return maybeShare.error();
216 auto const& mptIssuanceID = *maybeShare;
218 vault->setFieldIssue(sfAsset,
STIssue{sfAsset, asset});
219 vault->at(sfFlags) = tx.getFlags() & tfVaultPrivate;
220 vault->at(sfSequence) = sequence;
222 vault->at(sfAccount) = pseudoId;
223 vault->at(sfAssetsTotal) =
Number(0);
224 vault->at(sfAssetsAvailable) =
Number(0);
225 vault->at(sfLossUnrealized) =
Number(0);
227 if (
auto value = tx[~sfAssetsMaximum])
228 vault->at(sfAssetsMaximum) = *value;
229 vault->at(sfShareMPTID) = mptIssuanceID;
230 if (
auto value = tx[~sfData])
231 vault->at(sfData) = *value;
233 if (
auto value = tx[~sfWithdrawalPolicy])
235 vault->at(sfWithdrawalPolicy) = *value;
242 vault->at(sfScale) =
scale;
252 if (tx.isFlag(tfVaultPrivate))
A generic endpoint for log messages.
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void insert(SLE::ref sle)=0
Insert a new state SLE.
A currency issued by an account.
constexpr MPTID const & getMptID() const
static std::expected< MPTID, TER > create(ApplyView &view, beast::Journal journal, MPTCreateArgs const &args)
Number is a floating point type that can represent a wide range of values.
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::shared_ptr< STLedgerEntry const > const & const_ref
bool isFlag(std::uint32_t) const
bool isFieldPresent(SField const &field) const
std::uint32_t getSeqValue() const
Returns the first non-zero value of (Sequence, TicketSequence).
AccountID const accountID_
static bool validDataLength(std::optional< Slice > const &slice, std::size_t maxLength)
static std::uint32_t getFlagsMask(PreflightContext 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.
static bool checkExtraFeatures(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet permissionedDomain(AccountID const &account, std::uint32_t seq) noexcept
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
AccountID pseudoAccountAddress(ReadView const &view, uint256 const &pseudoOwnerKey)
Generate a pseudo-account address from a pseudo owner key.
TER addEmptyHolding(ApplyView &view, AccountID const &accountID, XRPAmount priorBalance, MPTIssue const &mptIssue, beast::Journal journal)
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
constexpr std::uint8_t kVaultMaximumIouScale
Maximum scale factor for a Vault.
std::expected< SLE::pointer, TER > createPseudoAccount(ApplyView &view, uint256 const &pseudoOwnerKey, SField const &ownerField)
Create pseudo-account, storing pseudoOwnerKey into ownerField.
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)
constexpr std::size_t kMaxDataPayloadLength
The maximum length of Data payload.
TER canAddHolding(ReadView const &view, MPTIssue const &mptIssue)
TERSubset< CanCvtToNotTEC > NotTEC
constexpr std::uint8_t kVaultDefaultIouScale
Default IOU scale factor for a Vault.
TER dirLink(ApplyView &view, AccountID const &owner, SLE::pointer &object, SF_UINT64 const &node=sfOwnerNode)
constexpr std::size_t kMaxMpTokenMetadataLength
The maximum length of MPTokenMetadata.
void adjustOwnerCount(ApplyView &view, SLE::ref sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
bool isFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptIssue, std::uint8_t depth=0)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool isTesSuccess(TER x) noexcept
constexpr std::uint8_t kVaultStrategyFirstComeFirstServe
Vault withdrawal policies.
TERSubset< CanCvtToTER > TER
@ tecINSUFFICIENT_RESERVE
bool isPseudoAccount(SLE::const_pointer sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
Returns true if and only if sleAcct is a pseudo-account or specific pseudo-accounts in pseudoFieldFil...
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.