rippled
Loading...
Searching...
No Matches
MPTokenIssuanceCreate.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/tx/detail/MPTokenIssuanceCreate.h>
21
22#include <xrpl/ledger/View.h>
23#include <xrpl/protocol/Feature.h>
24#include <xrpl/protocol/TxFlags.h>
25
26namespace ripple {
27
28bool
30{
31 if (ctx.tx.isFieldPresent(sfDomainID) &&
32 !(ctx.rules.enabled(featurePermissionedDomains) &&
33 ctx.rules.enabled(featureSingleAssetVault)))
34 return false;
35
36 if (ctx.tx.isFieldPresent(sfMutableFlags) &&
37 !ctx.rules.enabled(featureDynamicMPT))
38 return false;
39
40 return true;
41}
42
45{
46 // This mask is only compared against sfFlags
48}
49
52{
53 // If the mutable flags field is included, at least one flag must be
54 // specified.
55 if (auto const mutableFlags = ctx.tx[~sfMutableFlags]; mutableFlags &&
56 (!*mutableFlags || *mutableFlags & tmfMPTokenIssuanceCreateMutableMask))
57 return temINVALID_FLAG;
58
59 if (auto const fee = ctx.tx[~sfTransferFee])
60 {
61 if (fee > maxTransferFee)
63
64 // If a non-zero TransferFee is set then the tfTransferable flag
65 // must also be set.
66 if (fee > 0u && !ctx.tx.isFlag(tfMPTCanTransfer))
67 return temMALFORMED;
68 }
69
70 if (auto const domain = ctx.tx[~sfDomainID])
71 {
72 if (*domain == beast::zero)
73 return temMALFORMED;
74
75 // Domain present implies that MPTokenIssuance is not public
76 if ((ctx.tx.getFlags() & tfMPTRequireAuth) == 0)
77 return temMALFORMED;
78 }
79
80 if (auto const metadata = ctx.tx[~sfMPTokenMetadata])
81 {
82 if (metadata->length() == 0 ||
83 metadata->length() > maxMPTokenMetadataLength)
84 return temMALFORMED;
85 }
86
87 // Check if maximumAmount is within unsigned 63 bit range
88 if (auto const maxAmt = ctx.tx[~sfMaximumAmount])
89 {
90 if (maxAmt == 0)
91 return temMALFORMED;
92
93 if (maxAmt > maxMPTokenAmount)
94 return temMALFORMED;
95 }
96 return tesSUCCESS;
97}
98
101 ApplyView& view,
102 beast::Journal journal,
103 MPTCreateArgs const& args)
104{
105 auto const acct = view.peek(keylet::account(args.account));
106 if (!acct)
107 return Unexpected(tecINTERNAL); // LCOV_EXCL_LINE
108
109 if (args.priorBalance &&
110 *(args.priorBalance) <
111 view.fees().accountReserve((*acct)[sfOwnerCount] + 1))
113
114 auto const mptId = makeMptID(args.sequence, args.account);
115 auto const mptIssuanceKeylet = keylet::mptIssuance(mptId);
116
117 // create the MPTokenIssuance
118 {
119 auto const ownerNode = view.dirInsert(
121 mptIssuanceKeylet,
123
124 if (!ownerNode)
125 return Unexpected(tecDIR_FULL); // LCOV_EXCL_LINE
126
127 auto mptIssuance = std::make_shared<SLE>(mptIssuanceKeylet);
128 (*mptIssuance)[sfFlags] = args.flags & ~tfUniversal;
129 (*mptIssuance)[sfIssuer] = args.account;
130 (*mptIssuance)[sfOutstandingAmount] = 0;
131 (*mptIssuance)[sfOwnerNode] = *ownerNode;
132 (*mptIssuance)[sfSequence] = args.sequence;
133
134 if (args.maxAmount)
135 (*mptIssuance)[sfMaximumAmount] = *args.maxAmount;
136
137 if (args.assetScale)
138 (*mptIssuance)[sfAssetScale] = *args.assetScale;
139
140 if (args.transferFee)
141 (*mptIssuance)[sfTransferFee] = *args.transferFee;
142
143 if (args.metadata)
144 (*mptIssuance)[sfMPTokenMetadata] = *args.metadata;
145
146 if (args.domainId)
147 (*mptIssuance)[sfDomainID] = *args.domainId;
148
149 if (args.mutableFlags)
150 (*mptIssuance)[sfMutableFlags] = *args.mutableFlags;
151
152 view.insert(mptIssuance);
153 }
154
155 // Update owner count.
156 adjustOwnerCount(view, acct, 1, journal);
157
158 return mptId;
159}
160
161TER
163{
164 auto const& tx = ctx_.tx;
165 auto const result = create(
166 view(),
167 j_,
168 {
169 .priorBalance = mPriorBalance,
170 .account = account_,
171 .sequence = tx.getSeqValue(),
172 .flags = tx.getFlags(),
173 .maxAmount = tx[~sfMaximumAmount],
174 .assetScale = tx[~sfAssetScale],
175 .transferFee = tx[~sfTransferFee],
176 .metadata = tx[~sfMPTokenMetadata],
177 .domainId = tx[~sfDomainID],
178 .mutableFlags = tx[~sfMutableFlags],
179 });
180 return result ? tesSUCCESS : result.error();
181}
182
183} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:60
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:143
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Insert an entry to a directory.
Definition ApplyView.h:319
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static bool checkExtraFeatures(PreflightContext const &ctx)
static Expected< MPTID, TER > create(ApplyView &view, beast::Journal journal, MPTCreateArgs const &args)
static NotTEC preflight(PreflightContext const &ctx)
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:130
bool isFlag(std::uint32_t) const
Definition STObject.cpp:531
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:484
std::uint32_t getFlags() const
Definition STObject.cpp:537
AccountID const account_
Definition Transactor.h:147
ApplyView & view()
Definition Transactor.h:163
beast::Journal const j_
Definition Transactor.h:145
XRPAmount mPriorBalance
Definition Transactor.h:148
ApplyContext & ctx_
Definition Transactor.h:143
T is_same_v
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:526
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:184
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:374
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
constexpr std::uint32_t const tfMPTCanTransfer
Definition TxFlags.h:152
std::uint64_t constexpr maxMPTokenAmount
The maximum amount of MPTokenIssuance.
Definition Protocol.h:116
constexpr std::uint32_t const tfMPTokenIssuanceCreateMask
Definition TxFlags.h:154
std::uint16_t constexpr maxTransferFee
The maximum token transfer fee allowed.
Definition Protocol.h:82
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
Definition View.cpp:1032
constexpr std::uint32_t const tmfMPTokenIssuanceCreateMutableMask
Definition TxFlags.h:167
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition View.cpp:1050
std::size_t constexpr maxMPTokenMetadataLength
The maximum length of MPTokenMetadata.
Definition Protocol.h:113
@ tecDIR_FULL
Definition TER.h:287
@ tecINTERNAL
Definition TER.h:310
@ tecINSUFFICIENT_RESERVE
Definition TER.h:307
@ tesSUCCESS
Definition TER.h:244
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)
Definition Indexes.cpp:170
constexpr std::uint32_t const tfMPTRequireAuth
Definition TxFlags.h:149
@ temBAD_TRANSFER_FEE
Definition TER.h:142
@ temMALFORMED
Definition TER.h:87
@ temINVALID_FLAG
Definition TER.h:111
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
std::optional< std::uint16_t > transferFee
std::optional< XRPAmount > priorBalance
std::optional< std::uint32_t > mutableFlags
std::optional< std::uint8_t > assetScale
std::optional< uint256 > domainId
std::optional< std::uint64_t > maxAmount
std::optional< Slice > const & metadata
State information when preflighting a tx.
Definition Transactor.h:35