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
30{
31 if (!ctx.rules.enabled(featureMPTokensV1))
32 return temDISABLED;
33
34 if (ctx.tx.isFieldPresent(sfDomainID) &&
35 !(ctx.rules.enabled(featurePermissionedDomains) &&
36 ctx.rules.enabled(featureSingleAssetVault)))
37 return temDISABLED;
38
39 if (ctx.tx.isFieldPresent(sfMutableFlags) &&
40 !ctx.rules.enabled(featureDynamicMPT))
41 return temDISABLED;
42
43 if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
44 return ret;
45
46 if (auto const mutableFlags = ctx.tx[~sfMutableFlags]; mutableFlags &&
47 (!*mutableFlags || *mutableFlags & tmfMPTokenIssuanceCreateMutableMask))
48 return temINVALID_FLAG;
49
51 return temINVALID_FLAG;
52
53 if (auto const fee = ctx.tx[~sfTransferFee])
54 {
55 if (fee > maxTransferFee)
57
58 // If a non-zero TransferFee is set then the tfTransferable flag
59 // must also be set.
60 if (fee > 0u && !ctx.tx.isFlag(tfMPTCanTransfer))
61 return temMALFORMED;
62 }
63
64 if (auto const domain = ctx.tx[~sfDomainID])
65 {
66 if (*domain == beast::zero)
67 return temMALFORMED;
68
69 // Domain present implies that MPTokenIssuance is not public
70 if ((ctx.tx.getFlags() & tfMPTRequireAuth) == 0)
71 return temMALFORMED;
72 }
73
74 if (auto const metadata = ctx.tx[~sfMPTokenMetadata])
75 {
76 if (metadata->length() == 0 ||
77 metadata->length() > maxMPTokenMetadataLength)
78 return temMALFORMED;
79 }
80
81 // Check if maximumAmount is within unsigned 63 bit range
82 if (auto const maxAmt = ctx.tx[~sfMaximumAmount])
83 {
84 if (maxAmt == 0)
85 return temMALFORMED;
86
87 if (maxAmt > maxMPTokenAmount)
88 return temMALFORMED;
89 }
90 return preflight2(ctx);
91}
92
95 ApplyView& view,
96 beast::Journal journal,
97 MPTCreateArgs const& args)
98{
99 auto const acct = view.peek(keylet::account(args.account));
100 if (!acct)
101 return Unexpected(tecINTERNAL); // LCOV_EXCL_LINE
102
103 if (args.priorBalance &&
104 *(args.priorBalance) <
105 view.fees().accountReserve((*acct)[sfOwnerCount] + 1))
107
108 auto const mptId = makeMptID(args.sequence, args.account);
109 auto const mptIssuanceKeylet = keylet::mptIssuance(mptId);
110
111 // create the MPTokenIssuance
112 {
113 auto const ownerNode = view.dirInsert(
115 mptIssuanceKeylet,
117
118 if (!ownerNode)
119 return Unexpected(tecDIR_FULL); // LCOV_EXCL_LINE
120
121 auto mptIssuance = std::make_shared<SLE>(mptIssuanceKeylet);
122 (*mptIssuance)[sfFlags] = args.flags & ~tfUniversal;
123 (*mptIssuance)[sfIssuer] = args.account;
124 (*mptIssuance)[sfOutstandingAmount] = 0;
125 (*mptIssuance)[sfOwnerNode] = *ownerNode;
126 (*mptIssuance)[sfSequence] = args.sequence;
127
128 if (args.maxAmount)
129 (*mptIssuance)[sfMaximumAmount] = *args.maxAmount;
130
131 if (args.assetScale)
132 (*mptIssuance)[sfAssetScale] = *args.assetScale;
133
134 if (args.transferFee)
135 (*mptIssuance)[sfTransferFee] = *args.transferFee;
136
137 if (args.metadata)
138 (*mptIssuance)[sfMPTokenMetadata] = *args.metadata;
139
140 if (args.domainId)
141 (*mptIssuance)[sfDomainID] = *args.domainId;
142
143 if (args.mutableFlags)
144 (*mptIssuance)[sfMutableFlags] = *args.mutableFlags;
145
146 view.insert(mptIssuance);
147 }
148
149 // Update owner count.
150 adjustOwnerCount(view, acct, 1, journal);
151
152 return mptId;
153}
154
155TER
157{
158 auto const& tx = ctx_.tx;
159 auto const result = create(
160 view(),
161 j_,
162 {
163 .priorBalance = mPriorBalance,
164 .account = account_,
165 .sequence = tx.getSeqValue(),
166 .flags = tx.getFlags(),
167 .maxAmount = tx[~sfMaximumAmount],
168 .assetScale = tx[~sfAssetScale],
169 .transferFee = tx[~sfTransferFee],
170 .metadata = tx[~sfMPTokenMetadata],
171 .domainId = tx[~sfDomainID],
172 .mutableFlags = tx[~sfMutableFlags],
173 });
174 return result ? tesSUCCESS : result.error();
175}
176
177} // 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:317
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static Expected< MPTID, TER > create(ApplyView &view, beast::Journal journal, MPTCreateArgs const &args)
static NotTEC preflight(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:143
ApplyView & view()
Definition Transactor.h:159
beast::Journal const j_
Definition Transactor.h:141
XRPAmount mPriorBalance
Definition Transactor.h:144
ApplyContext & ctx_
Definition Transactor.h:140
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:149
std::uint64_t constexpr maxMPTokenAmount
The maximum amount of MPTokenIssuance.
Definition Protocol.h:117
constexpr std::uint32_t const tfMPTokenIssuanceCreateMask
Definition TxFlags.h:151
std::uint16_t constexpr maxTransferFee
The maximum token transfer fee allowed.
Definition Protocol.h:83
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:1029
constexpr std::uint32_t const tmfMPTokenIssuanceCreateMutableMask
Definition TxFlags.h:164
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition View.cpp:1047
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
std::size_t constexpr maxMPTokenMetadataLength
The maximum length of MPTokenMetadata.
Definition Protocol.h:114
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
@ tecDIR_FULL
Definition TER.h:287
@ tecINTERNAL
Definition TER.h:310
@ tecINSUFFICIENT_RESERVE
Definition TER.h:307
@ tesSUCCESS
Definition TER.h:244
bool isTesSuccess(TER x) noexcept
Definition TER.h:674
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)
Definition Indexes.cpp:170
constexpr std::uint32_t const tfMPTRequireAuth
Definition TxFlags.h:146
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:605
@ temBAD_TRANSFER_FEE
Definition TER.h:142
@ temMALFORMED
Definition TER.h:87
@ temINVALID_FLAG
Definition TER.h:111
@ temDISABLED
Definition TER.h:114
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:34