xrpld
Loading...
Searching...
No Matches
MPTokenIssuanceCreate.cpp
1#include <xrpl/tx/transactors/token/MPTokenIssuanceCreate.h>
2
3#include <xrpl/beast/utility/Journal.h>
4#include <xrpl/beast/utility/Zero.h>
5#include <xrpl/core/ServiceRegistry.h>
6#include <xrpl/ledger/ApplyView.h>
7#include <xrpl/ledger/ReadView.h>
8#include <xrpl/ledger/helpers/AccountRootHelpers.h>
9#include <xrpl/ledger/helpers/DirectoryHelpers.h>
10#include <xrpl/protocol/Feature.h>
11#include <xrpl/protocol/Indexes.h>
12#include <xrpl/protocol/LedgerFormats.h>
13#include <xrpl/protocol/Protocol.h>
14#include <xrpl/protocol/SField.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STTx.h>
17#include <xrpl/protocol/TER.h>
18#include <xrpl/protocol/TxFlags.h>
19#include <xrpl/protocol/UintTypes.h>
20#include <xrpl/protocol/XRPAmount.h>
21#include <xrpl/tx/Transactor.h>
22
23#include <cstdint>
24#include <expected>
25#include <memory>
26
27namespace xrpl {
28
29bool
31{
32 if (ctx.tx.isFieldPresent(sfDomainID) &&
33 !(ctx.rules.enabled(featurePermissionedDomains) &&
34 ctx.rules.enabled(featureSingleAssetVault)))
35 return false;
36
37 if (ctx.tx.isFieldPresent(sfMutableFlags) && !ctx.rules.enabled(featureDynamicMPT))
38 return false;
39
40 if (ctx.tx.isFlag(tfMPTCanHoldConfidentialBalance) &&
41 !ctx.rules.enabled(featureConfidentialTransfer))
42 return false;
43
44 // can not set tmfMPTCannotEnableCanHoldConfidentialBalance without featureConfidentialTransfer
45 auto const mutableFlags = ctx.tx[~sfMutableFlags];
46 return !mutableFlags ||
47 ((*mutableFlags & tmfMPTCannotEnableCanHoldConfidentialBalance) == 0u) ||
48 ctx.rules.enabled(featureConfidentialTransfer);
49}
50
53{
54 // This mask is only compared against sfFlags
55 return tfMPTokenIssuanceCreateMask;
56}
57
60{
61 // sfReferenceHolding is set only internally by VaultCreate. Reject
62 // any user-submitted MPTokenIssuanceCreate that attempts to carry it.
63 if (ctx.rules.enabled(fixCleanup3_2_0) && ctx.tx.isFieldPresent(sfReferenceHolding))
64 return temMALFORMED;
65
66 // If the mutable flags field is included, at least one flag must be
67 // specified.
68 if (auto const mutableFlags = ctx.tx[~sfMutableFlags]; mutableFlags &&
69 ((*mutableFlags == 0u) || ((*mutableFlags & tmfMPTokenIssuanceCreateMutableMask) != 0u)))
70 return temINVALID_FLAG;
71
72 if (auto const fee = ctx.tx[~sfTransferFee])
73 {
74 if (fee > kMaxTransferFee)
76
77 // If a non-zero TransferFee is set then the tfTransferable flag
78 // must also be set.
79 if (fee > 0u && !ctx.tx.isFlag(tfMPTCanTransfer))
80 return temMALFORMED;
81
82 // Confidential amounts are encrypted so transfer rate is disallowed.
83 if (fee > 0u && ctx.tx.isFlag(tfMPTCanHoldConfidentialBalance))
85 }
86
87 if (auto const domain = ctx.tx[~sfDomainID])
88 {
89 if (*domain == beast::kZero)
90 return temMALFORMED;
91
92 // Domain present implies that MPTokenIssuance is not public
93 if (!ctx.tx.isFlag(tfMPTRequireAuth))
94 return temMALFORMED;
95 }
96
97 if (auto const metadata = ctx.tx[~sfMPTokenMetadata])
98 {
99 if (metadata->empty() || metadata->length() > kMaxMpTokenMetadataLength)
100 return temMALFORMED;
101 }
102
103 // Check if maximumAmount is within unsigned 63 bit range
104 if (auto const maxAmt = ctx.tx[~sfMaximumAmount])
105 {
106 if (maxAmt == 0)
107 return temMALFORMED;
108
109 if (maxAmt > kMaxMpTokenAmount)
110 return temMALFORMED;
111 }
112 return tesSUCCESS;
113}
114
115std::expected<MPTID, TER>
117{
118 auto const acct = view.peek(keylet::account(args.account));
119 if (!acct)
120 return std::unexpected(tecINTERNAL); // LCOV_EXCL_LINE
121
122 if (args.priorBalance &&
123 *(args.priorBalance) < view.fees().accountReserve((*acct)[sfOwnerCount] + 1))
125
126 auto const mptId = makeMptID(args.sequence, args.account);
127 auto const mptIssuanceKeylet = keylet::mptokenIssuance(mptId);
128
129 // create the MPTokenIssuance
130 {
131 auto const ownerNode = view.dirInsert(
132 keylet::ownerDir(args.account), mptIssuanceKeylet, describeOwnerDir(args.account));
133
134 if (!ownerNode)
135 return std::unexpected(tecDIR_FULL); // LCOV_EXCL_LINE
136
137 auto mptIssuance = std::make_shared<SLE>(mptIssuanceKeylet);
138 (*mptIssuance)[sfFlags] = args.flags & ~tfUniversal;
139 (*mptIssuance)[sfIssuer] = args.account;
140 (*mptIssuance)[sfOutstandingAmount] = 0;
141 (*mptIssuance)[sfOwnerNode] = *ownerNode;
142 (*mptIssuance)[sfSequence] = args.sequence;
143
144 if (args.maxAmount)
145 (*mptIssuance)[sfMaximumAmount] = *args.maxAmount;
146
147 if (args.assetScale)
148 (*mptIssuance)[sfAssetScale] = *args.assetScale;
149
150 if (args.transferFee)
151 (*mptIssuance)[sfTransferFee] = *args.transferFee;
152
153 if (args.metadata)
154 (*mptIssuance)[sfMPTokenMetadata] = *args.metadata;
155
156 if (args.domainId)
157 (*mptIssuance)[sfDomainID] = *args.domainId;
158
159 if (args.mutableFlags)
160 (*mptIssuance)[sfMutableFlags] = *args.mutableFlags;
161
162 if (args.referenceHolding)
163 {
164 // Defensive: the holding must already exist and be of an
165 // expected type. Callers (currently only VaultCreate)
166 // populate this after the pseudo-account's MPToken /
167 // RippleState has been installed. A missing holding here
168 // would dangle the pointer and is a programmer error.
169 auto const sleHolding = view.read(keylet::unchecked(*args.referenceHolding));
170 if (!sleHolding)
171 return std::unexpected(tecINTERNAL); // LCOV_EXCL_LINE
172 auto const type = sleHolding->getType();
173 if (type != ltMPTOKEN && type != ltRIPPLE_STATE)
174 return std::unexpected(tecINTERNAL); // LCOV_EXCL_LINE
175 (*mptIssuance)[sfReferenceHolding] = *args.referenceHolding;
176 }
177
178 view.insert(mptIssuance);
179 }
180
181 // Update owner count.
182 adjustOwnerCount(view, acct, 1, journal);
183
184 return mptId;
185}
186
187TER
189{
190 auto const& tx = ctx_.tx;
191 auto const result = create(
192 view(),
193 j_,
194 {
195 .priorBalance = preFeeBalance_,
196 .account = accountID_,
197 .sequence = tx.getSeqValue(),
198 .flags = tx.getFlags(),
199 .maxAmount = tx[~sfMaximumAmount],
200 .assetScale = tx[~sfAssetScale],
201 .transferFee = tx[~sfTransferFee],
202 .metadata = tx[~sfMPTokenMetadata],
203 .domainId = tx[~sfDomainID],
204 .mutableFlags = tx[~sfMutableFlags],
205 });
206 return result ? tesSUCCESS : result.error();
207}
208
209void
211{
212 // No transaction-specific invariants yet (future work).
213}
214
215bool
217 STTx const&,
218 TER,
219 XRPAmount,
220 ReadView const&,
221 beast::Journal const&)
222{
223 // No transaction-specific invariants yet (future work).
224 return true;
225}
226
227} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:118
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::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static bool checkExtraFeatures(PreflightContext const &ctx)
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static std::expected< MPTID, TER > create(ApplyView &view, beast::Journal journal, MPTCreateArgs const &args)
A view into a ledger.
Definition ReadView.h:31
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
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
ApplyContext & ctx_
Definition Transactor.h:116
T make_shared(T... args)
Keylet unchecked(uint256 const &key) noexcept
Any ledger entry.
Definition Indexes.cpp:351
Keylet mptokenIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:521
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:357
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr FlagValue tmfMPTCannotEnableCanHoldConfidentialBalance
Definition TxFlags.h:353
constexpr FlagValue tmfMPTokenIssuanceCreateMutableMask
Definition TxFlags.h:355
constexpr FlagValue tfUniversal
Definition TxFlags.h:44
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
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.
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
constexpr std::uint16_t kMaxTransferFee
The maximum token transfer fee allowed.
Definition Protocol.h:70
@ temINVALID_FLAG
Definition TER.h:97
@ temMALFORMED
Definition TER.h:73
@ temBAD_TRANSFER_FEE
Definition TER.h:128
TERSubset< CanCvtToTER > TER
Definition TER.h:634
@ tecDIR_FULL
Definition TER.h:285
@ tecINTERNAL
Definition TER.h:308
@ tecINSUFFICIENT_RESERVE
Definition TER.h:305
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
Definition Protocol.h:238
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)
Definition Indexes.cpp:172
@ tesSUCCESS
Definition TER.h:240
std::optional< std::uint32_t > mutableFlags
std::optional< std::uint16_t > transferFee
State information when preflighting a tx.
Definition Transactor.h:18
T unexpected(T... args)