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/ledger/helpers/SponsorHelpers.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/LedgerFormats.h>
14#include <xrpl/protocol/Protocol.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STLedgerEntry.h>
17#include <xrpl/protocol/STTx.h>
18#include <xrpl/protocol/TER.h>
19#include <xrpl/protocol/TxFlags.h>
20#include <xrpl/protocol/UintTypes.h>
21#include <xrpl/protocol/XRPAmount.h>
22#include <xrpl/tx/Transactor.h>
23
24#include <cstdint>
25#include <expected>
26#include <memory>
27
28namespace xrpl {
29
30bool
32{
33 if (ctx.tx.isFieldPresent(sfDomainID) &&
34 !(ctx.rules.enabled(featurePermissionedDomains) &&
35 ctx.rules.enabled(featureSingleAssetVault)))
36 return false;
37
38 if (ctx.tx.isFieldPresent(sfImmutableFlags) && !ctx.rules.enabled(featureDynamicMPT))
39 return false;
40
41 if (ctx.tx.isFlag(tfMPTCanHoldConfidentialBalance) &&
42 !ctx.rules.enabled(featureConfidentialTransfer))
43 return false;
44
45 // can not set tifMPTCanHoldConfidentialBalance without featureConfidentialTransfer
46 auto const immutableFlags = ctx.tx[~sfImmutableFlags];
47 // NOLINTBEGIN(readability-simplify-boolean-expr)
48 if (immutableFlags && ((*immutableFlags & tifMPTCanHoldConfidentialBalance) != 0u) &&
49 !ctx.rules.enabled(featureConfidentialTransfer))
50 {
51 return false;
52 }
53 // NOLINTEND(readability-simplify-boolean-expr)
54
55 return true;
56}
57
60{
61 // This mask is only compared against sfFlags
62 return tfMPTokenIssuanceCreateMask;
63}
64
67{
68 // sfReferenceHolding is set only internally by VaultCreate. Reject
69 // any user-submitted MPTokenIssuanceCreate that attempts to carry it.
70 if (ctx.rules.enabled(fixCleanup3_2_0) && ctx.tx.isFieldPresent(sfReferenceHolding))
71 return temMALFORMED;
72
73 // If the immutable flags field is included, at least one flag must be
74 // specified, and undefined flags must not be specified.
75 if (auto const immutableFlags = ctx.tx[~sfImmutableFlags]; immutableFlags &&
76 ((*immutableFlags == 0u) || ((*immutableFlags & tifMPTokenIssuanceImmutableMask) != 0u)))
77 return temINVALID_FLAG;
78
79 if (auto const fee = ctx.tx[~sfTransferFee])
80 {
81 if (fee > kMaxTransferFee)
83
84 // If a non-zero TransferFee is set then the tfTransferable flag
85 // must also be set.
86 if (fee > 0u && !ctx.tx.isFlag(tfMPTCanTransfer))
87 return temMALFORMED;
88
89 // Confidential amounts are encrypted so transfer rate is disallowed.
90 if (fee > 0u && ctx.tx.isFlag(tfMPTCanHoldConfidentialBalance))
92 }
93
94 if (auto const domain = ctx.tx[~sfDomainID])
95 {
96 if (*domain == beast::kZero)
97 return temMALFORMED;
98
99 // Domain present implies that MPTokenIssuance is not public
100 if (!ctx.tx.isFlag(tfMPTRequireAuth))
101 return temMALFORMED;
102 }
103
104 if (auto const metadata = ctx.tx[~sfMPTokenMetadata])
105 {
106 if (metadata->empty() || metadata->length() > kMaxMpTokenMetadataLength)
107 return temMALFORMED;
108 }
109
110 // Check if maximumAmount is within unsigned 63 bit range
111 if (auto const maxAmt = ctx.tx[~sfMaximumAmount])
112 {
113 if (maxAmt == 0)
114 return temMALFORMED;
115
116 if (maxAmt > kMaxMpTokenAmount)
117 return temMALFORMED;
118 }
119 return tesSUCCESS;
120}
121
122std::expected<MPTID, TER>
125 beast::Journal journal,
126 MPTCreateArgs const& args)
127{
128 auto const acct = ctx.view.peek(keylet::account(args.account));
129 if (!acct)
130 return std::unexpected(tecINTERNAL); // LCOV_EXCL_LINE
131
132 // A reserve sponsor only covers tx.Account's own objects.
133 auto const sponsorExp = getEffectiveTxReserveSponsor(ctx, acct);
134 if (!sponsorExp)
135 return std::unexpected(sponsorExp.error()); // LCOV_EXCL_LINE
136 auto const sponsorSle = *sponsorExp;
137
138 if (args.priorBalance)
139 {
140 if (auto const ret = checkReserve(
141 ctx, acct, *(args.priorBalance), sponsorSle, {.ownerCountDelta = 1}, journal);
142 !isTesSuccess(ret))
143 return std::unexpected(ret);
144 }
145
146 auto const mptId = makeMptID(args.sequence, args.account);
147 auto const mptIssuanceKeylet = keylet::mptokenIssuance(mptId);
148
149 // create the MPTokenIssuance
150 {
151 auto const ownerNode = ctx.view.dirInsert(
152 keylet::ownerDir(args.account), mptIssuanceKeylet, describeOwnerDir(args.account));
153
154 if (!ownerNode)
155 return std::unexpected(tecDIR_FULL); // LCOV_EXCL_LINE
156
157 auto mptIssuance = std::make_shared<SLE>(mptIssuanceKeylet);
158 (*mptIssuance)[sfFlags] = args.flags & ~tfUniversal;
159 (*mptIssuance)[sfIssuer] = args.account;
160 (*mptIssuance)[sfOutstandingAmount] = 0;
161 (*mptIssuance)[sfOwnerNode] = *ownerNode;
162 (*mptIssuance)[sfSequence] = args.sequence;
163
164 if (args.maxAmount)
165 (*mptIssuance)[sfMaximumAmount] = *args.maxAmount;
166
167 if (args.assetScale)
168 (*mptIssuance)[sfAssetScale] = *args.assetScale;
169
170 if (args.transferFee)
171 (*mptIssuance)[sfTransferFee] = *args.transferFee;
172
173 if (args.metadata)
174 (*mptIssuance)[sfMPTokenMetadata] = *args.metadata;
175
176 if (args.domainId)
177 (*mptIssuance)[sfDomainID] = *args.domainId;
178
179 if (args.immutableFlags)
180 (*mptIssuance)[sfImmutableFlags] = *args.immutableFlags;
181
182 if (args.referenceHolding)
183 {
184 // Defensive: the holding must already exist and be of an
185 // expected type. Callers (currently only VaultCreate)
186 // populate this after the pseudo-account's MPToken /
187 // RippleState has been installed. A missing holding here
188 // would dangle the pointer and is a programmer error.
189 auto const sleHolding = ctx.view.read(keylet::unchecked(*args.referenceHolding));
190 if (!sleHolding)
191 return std::unexpected(tecINTERNAL); // LCOV_EXCL_LINE
192 auto const type = sleHolding->getType();
193 if (type != ltMPTOKEN && type != ltRIPPLE_STATE)
194 return std::unexpected(tecINTERNAL); // LCOV_EXCL_LINE
195 (*mptIssuance)[sfReferenceHolding] = *args.referenceHolding;
196 }
197
198 addSponsorToLedgerEntry(mptIssuance, sponsorSle);
199
200 ctx.view.insert(mptIssuance);
201 }
202
203 // Update owner count.
204 increaseOwnerCount(ctx.view, acct, sponsorSle, 1, journal);
205
206 return mptId;
207}
208
209TER
211{
212 auto const& tx = ctx_.tx;
213 auto const result = create(
214 ctx_.getApplyViewContext(),
215 j_,
216 {
217 .priorBalance = preFeeBalance_,
218 .account = accountID_,
219 .sequence = tx.getSeqProxy().value(),
220 .flags = tx.getFlags(),
221 .maxAmount = tx[~sfMaximumAmount],
222 .assetScale = tx[~sfAssetScale],
223 .transferFee = tx[~sfTransferFee],
224 .metadata = tx[~sfMPTokenMetadata],
225 .domainId = tx[~sfDomainID],
226 .immutableFlags = tx[~sfImmutableFlags],
227 });
228 return result ? tesSUCCESS : result.error();
229}
230
231void
233{
234 // No transaction-specific invariants yet (future work).
235}
236
237bool
239 STTx const&,
240 TER,
241 XRPAmount,
242 ReadView const&,
243 beast::Journal const&)
244{
245 // No transaction-specific invariants yet (future work).
246 return true;
247}
248
249} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
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.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(SLE::ref)> const &describe)
Insert an entry to a directory.
Definition ApplyView.h:366
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(ApplyViewContext ctx, beast::Journal journal, MPTCreateArgs const &args)
A view into a ledger.
Definition ReadView.h:41
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:180
std::shared_ptr< STLedgerEntry const > const & const_ref
bool isFlag(std::uint32_t) const
Definition STObject.cpp:511
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
beast::Journal const j_
Definition Transactor.h:155
ApplyContext & ctx_
Definition Transactor.h:153
T make_shared(T... args)
constexpr Zero kZero
Definition Zero.h:30
Keylet unchecked(uint256 const &key) noexcept
Any ledger entry.
Definition Indexes.cpp:367
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:373
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:198
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:537
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void increaseOwnerCount(ApplyView &view, SLE::ref accountSle, SLE::ref sponsorSle, std::uint32_t count, beast::Journal j)
Increase owner-count fields when the caller supplies the sponsor.
constexpr FlagValue tfUniversal
Definition TxFlags.h:45
std::expected< SLE::pointer, TER > getEffectiveTxReserveSponsor(ApplyViewContext ctx, SLE::const_ref accountSle)
The transaction's reserve sponsor for the given account, if applicable.
void addSponsorToLedgerEntry(SLE::ref sle, SLE::const_ref sponsorSle, SF_ACCOUNT const &field=sfSponsor)
Stamp a reserve sponsor onto a ledger entry using an explicit sponsor SLE.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:607
constexpr std::size_t kMaxMpTokenMetadataLength
The maximum length of MPTokenMetadata.
Definition Protocol.h:291
constexpr FlagValue tifMPTokenIssuanceImmutableMask
Definition TxFlags.h:377
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:184
constexpr std::uint16_t kMaxTransferFee
The maximum token transfer fee allowed.
Definition Protocol.h:96
TER checkReserve(ApplyViewContext ctx, SLE::const_ref accSle, XRPAmount accBalance, SLE::const_ref sponsorSle, Adjustment adj, beast::Journal j, TER insufReserveCode=tecINSUFFICIENT_RESERVE)
Check if an account has sufficient reserve.
constexpr FlagValue tifMPTCanHoldConfidentialBalance
Definition TxFlags.h:376
@ temINVALID_FLAG
Definition TER.h:99
@ temMALFORMED
Definition TER.h:75
@ temBAD_TRANSFER_FEE
Definition TER.h:130
bool isTesSuccess(TER x) noexcept
Definition TER.h:676
TERSubset< CanCvtToTER > TER
Definition TER.h:647
@ tecDIR_FULL
Definition TER.h:290
@ tecINTERNAL
Definition TER.h:313
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
Definition Protocol.h:296
@ tesSUCCESS
Definition TER.h:245
Bundles the mutable ledger view and the transaction being applied.
Definition ApplyView.h:444
std::optional< std::uint16_t > transferFee
std::optional< std::uint32_t > immutableFlags
State information when preflighting a tx.
Definition Transactor.h:38
T unexpected(T... args)