xrpld
Loading...
Searching...
No Matches
VaultCreate.cpp
1#include <xrpl/tx/transactors/vault/VaultCreate.h>
2
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> // IWYU pragma: keep
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>
29
30#include <cstdint>
31#include <memory>
32#include <optional>
33
34namespace xrpl {
35
36bool
38{
39 if (!ctx.rules.enabled(featureMPTokensV1))
40 return false;
41
42 if (ctx.tx.isFieldPresent(sfDomainID) && !ctx.rules.enabled(featurePermissionedDomains))
43 return false;
44
45 return true;
46}
47
50{
51 return tfVaultCreateMask;
52}
53
56{
57 if (!validDataLength(ctx.tx[~sfData], kMaxDataPayloadLength))
58 return temMALFORMED;
59
60 if (auto const withdrawalPolicy = ctx.tx[~sfWithdrawalPolicy])
61 {
62 // Enforce valid withdrawal policy
63 if (*withdrawalPolicy != kVaultStrategyFirstComeFirstServe)
64 return temMALFORMED;
65 }
66
67 if (auto const domain = ctx.tx[~sfDomainID])
68 {
69 if (*domain == beast::kZero)
70 {
71 return temMALFORMED;
72 }
73 if (!ctx.tx.isFlag(tfVaultPrivate))
74 {
75 return temMALFORMED; // DomainID only allowed on private vaults
76 }
77 }
78
79 if (auto const assetMax = ctx.tx[~sfAssetsMaximum])
80 {
81 if (*assetMax < beast::kZero)
82 return temMALFORMED;
83 }
84
85 if (auto const metadata = ctx.tx[~sfMPTokenMetadata])
86 {
87 if (metadata->empty() || metadata->length() > kMaxMpTokenMetadataLength)
88 return temMALFORMED;
89 }
90
91 if (auto const scale = ctx.tx[~sfScale])
92 {
93 auto const vaultAsset = ctx.tx[sfAsset];
94 if (vaultAsset.holds<MPTIssue>() || vaultAsset.native())
95 return temMALFORMED;
96
98 return temMALFORMED;
99 }
100
101 return tesSUCCESS;
102}
103
104TER
106{
107 auto const vaultAsset = ctx.tx[sfAsset];
108 auto const account = ctx.tx[sfAccount];
109
110 if (auto const ter = canAddHolding(ctx.view, vaultAsset))
111 return ter;
112
113 // Check for pseudo-account issuers - we do not want a vault to hold such
114 // assets (e.g. MPT shares to other vaults or AMM LPTokens) as they would be
115 // impossible to clawback (should the need arise)
116 if (!vaultAsset.native())
117 {
118 if (isPseudoAccount(ctx.view, vaultAsset.getIssuer()))
119 return tecWRONG_ASSET;
120 }
121
122 // Cannot create Vault for an Asset frozen for the vault owner
123 if (isFrozen(ctx.view, account, vaultAsset))
124 return vaultAsset.holds<Issue>() ? tecFROZEN : tecLOCKED;
125
126 if (auto const domain = ctx.tx[~sfDomainID])
127 {
128 auto const sleDomain = ctx.view.read(keylet::permissionedDomain(*domain));
129 if (!sleDomain)
130 return tecOBJECT_NOT_FOUND;
131 }
132
133 auto const sequence = ctx.tx.getSeqValue();
134 if (auto const accountId = pseudoAccountAddress(ctx.view, keylet::vault(account, sequence).key);
135 accountId == beast::kZero)
137
138 return tesSUCCESS;
139}
140
141TER
143{
144 // All return codes in `doApply` must be `tec`, `ter`, or `tes`.
145 // As we move checks into `preflight` and `preclaim`,
146 // we can consider downgrading them to `tef` or `tem`.
147
148 auto const& tx = ctx_.tx;
149 auto const sequence = tx.getSeqValue();
150 auto const owner = view().peek(keylet::account(accountID_));
151 if (owner == nullptr)
152 return tefINTERNAL; // LCOV_EXCL_LINE
153
154 auto vault = std::make_shared<SLE>(keylet::vault(accountID_, sequence));
155
156 if (auto ter = dirLink(view(), accountID_, vault))
157 return ter;
158 // We will create Vault and PseudoAccount, hence increase OwnerCount by 2
159 adjustOwnerCount(view(), owner, 2, j_);
160 auto const ownerCount = owner->at(sfOwnerCount);
161 if (preFeeBalance_ < view().fees().accountReserve(ownerCount))
163
164 auto maybePseudo = createPseudoAccount(view(), vault->key(), sfVaultID);
165 if (!maybePseudo)
166 return maybePseudo.error(); // LCOV_EXCL_LINE
167 auto const& pseudo = *maybePseudo;
168 AccountID const pseudoId = pseudo->at(sfAccount);
169 auto const asset = tx[sfAsset];
170
171 if (auto ter = addEmptyHolding(view(), pseudoId, preFeeBalance_, asset, j_); !isTesSuccess(ter))
172 return ter;
173
174 std::uint8_t const scale = (asset.holds<MPTIssue>() || asset.native())
175 ? 0
176 : ctx_.tx[~sfScale].value_or(kVaultDefaultIouScale);
177
178 std::uint32_t mptFlags = 0;
179 if (!tx.isFlag(tfVaultShareNonTransferable))
180 mptFlags |= (lsfMPTCanEscrow | lsfMPTCanTrade | lsfMPTCanTransfer);
181 if (tx.isFlag(tfVaultPrivate))
182 mptFlags |= lsfMPTRequireAuth;
183
184 // Note, here we are **not** creating an MPToken for the assets held in
185 // the vault. That MPToken or TrustLine/RippleState is created above, in
186 // addEmptyHolding. Here we are creating MPTokenIssuance for the shares
187 // in the vault.
188 //
189 // Post-fixCleanup3_2_0: surface the vault pseudo's holding (MPToken
190 // for MPT, RippleState for IOU) on the share via sfReferenceHolding.
191 // XRP underlyings leave it unset.
192 auto const referenceHolding = [&]() -> std::optional<uint256> {
193 if (!view().rules().enabled(fixCleanup3_2_0) || asset.native())
194 return std::nullopt;
195 return asset.holds<MPTIssue>()
196 ? keylet::mptoken(asset.get<MPTIssue>().getMptID(), pseudoId).key
197 : keylet::trustLine(pseudoId, asset.get<Issue>()).key;
198 }();
199 auto const maybeShare = MPTokenIssuanceCreate::create(
200 view(),
201 j_,
202 {
203 .priorBalance = std::nullopt,
204 .account = pseudoId,
205 .sequence = 1,
206 .flags = mptFlags,
207 .assetScale = scale,
208 .transferFee = std::nullopt,
209 .metadata = tx[~sfMPTokenMetadata],
210 .domainId = tx[~sfDomainID],
211 .mutableFlags = std::nullopt,
212 .referenceHolding = referenceHolding,
213 });
214 if (!maybeShare)
215 return maybeShare.error(); // LCOV_EXCL_LINE
216 auto const& mptIssuanceID = *maybeShare;
217
218 vault->setFieldIssue(sfAsset, STIssue{sfAsset, asset});
219 vault->at(sfFlags) = tx.getFlags() & tfVaultPrivate;
220 vault->at(sfSequence) = sequence;
221 vault->at(sfOwner) = accountID_;
222 vault->at(sfAccount) = pseudoId;
223 vault->at(sfAssetsTotal) = Number(0);
224 vault->at(sfAssetsAvailable) = Number(0);
225 vault->at(sfLossUnrealized) = Number(0);
226 // Leave default values for AssetTotal and AssetAvailable, both zero.
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;
232 // Required field, default to vaultStrategyFirstComeFirstServe
233 if (auto value = tx[~sfWithdrawalPolicy])
234 {
235 vault->at(sfWithdrawalPolicy) = *value;
236 }
237 else
238 {
239 vault->at(sfWithdrawalPolicy) = kVaultStrategyFirstComeFirstServe;
240 }
241 if (scale != 0u)
242 vault->at(sfScale) = scale;
243 view().insert(vault);
244
245 // Explicitly create MPToken for the vault owner
246 if (auto const err =
247 authorizeMPToken(view(), preFeeBalance_, mptIssuanceID, accountID_, ctx_.journal);
248 !isTesSuccess(err))
249 return err;
250
251 // If the vault is private, set the authorized flag for the vault owner
252 if (tx.isFlag(tfVaultPrivate))
253 {
254 if (auto const err = authorizeMPToken(
255 view(), preFeeBalance_, mptIssuanceID, pseudoId, ctx_.journal, {}, accountID_);
256 !isTesSuccess(err))
257 return err;
258 }
259
260 associateAsset(*vault, asset);
261
262 return tesSUCCESS;
263}
264
265void
267{
268 // No transaction-specific invariants yet (future work).
269}
270
271bool
273{
274 // No transaction-specific invariants yet (future work).
275 return true;
276}
277
278} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
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.
Definition Issue.h:13
static bool native()
Definition MPTIssue.h:51
constexpr MPTID const & getMptID() const
Definition MPTIssue.h:33
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.
Definition Number.h:306
A view into a ledger.
Definition ReadView.h:31
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.
Definition Rules.cpp:171
std::shared_ptr< STLedgerEntry const > const & const_ref
bool isFlag(std::uint32_t) const
Definition STObject.cpp:501
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:454
std::uint32_t getSeqValue() const
Returns the first non-zero value of (Sequence, TicketSequence).
Definition STTx.cpp:210
beast::Journal const j_
Definition Transactor.h:118
ApplyView & view()
Definition Transactor.h:136
AccountID const accountID_
Definition Transactor.h:120
XRPAmount preFeeBalance_
Definition Transactor.h:121
static bool validDataLength(std::optional< Slice > const &slice, std::size_t maxLength)
ApplyContext & ctx_
Definition Transactor.h:116
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
TER doApply() override
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.
T make_shared(T... args)
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:551
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:533
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Keylet permissionedDomain(AccountID const &account, std::uint32_t seq) noexcept
Definition Indexes.cpp:569
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:241
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ terADDRESS_COLLISION
Definition TER.h:220
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.
Definition STAmount.h:779
@ tefINTERNAL
Definition TER.h:163
constexpr std::uint8_t kVaultMaximumIouScale
Maximum scale factor for a Vault.
Definition Protocol.h:252
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.
Definition Protocol.h:242
TER canAddHolding(ReadView const &view, MPTIssue const &mptIssue)
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
constexpr std::uint8_t kVaultDefaultIouScale
Default IOU scale factor for a Vault.
Definition Protocol.h:248
TER dirLink(ApplyView &view, AccountID const &owner, SLE::pointer &object, SF_UINT64 const &node=sfOwnerNode)
Definition View.cpp:334
constexpr std::size_t kMaxMpTokenMetadataLength
The maximum length of MPTokenMetadata.
Definition Protocol.h:235
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.
Definition AccountID.h:28
@ temMALFORMED
Definition TER.h:73
bool isTesSuccess(TER x) noexcept
Definition TER.h:663
constexpr std::uint8_t kVaultStrategyFirstComeFirstServe
Vault withdrawal policies.
Definition Protocol.h:245
TERSubset< CanCvtToTER > TER
Definition TER.h:634
@ tecWRONG_ASSET
Definition TER.h:358
@ tecLOCKED
Definition TER.h:356
@ tecOBJECT_NOT_FOUND
Definition TER.h:324
@ tecFROZEN
Definition TER.h:301
@ tecINSUFFICIENT_RESERVE
Definition TER.h:305
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.
@ tesSUCCESS
Definition TER.h:240
uint256 key
Definition Keylet.h:20
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
State information when preflighting a tx.
Definition Transactor.h:18