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#include <xrpld/ledger/View.h>
22
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 (auto const ret = preflight1(ctx); !isTesSuccess(ret))
40 return ret;
41
43 return temINVALID_FLAG;
44
45 if (auto const fee = ctx.tx[~sfTransferFee])
46 {
47 if (fee > maxTransferFee)
49
50 // If a non-zero TransferFee is set then the tfTransferable flag
51 // must also be set.
52 if (fee > 0u && !ctx.tx.isFlag(tfMPTCanTransfer))
53 return temMALFORMED;
54 }
55
56 if (auto const domain = ctx.tx[~sfDomainID])
57 {
58 if (*domain == beast::zero)
59 return temMALFORMED;
60
61 // Domain present implies that MPTokenIssuance is not public
62 if ((ctx.tx.getFlags() & tfMPTRequireAuth) == 0)
63 return temMALFORMED;
64 }
65
66 if (auto const metadata = ctx.tx[~sfMPTokenMetadata])
67 {
68 if (metadata->length() == 0 ||
69 metadata->length() > maxMPTokenMetadataLength)
70 return temMALFORMED;
71 }
72
73 // Check if maximumAmount is within unsigned 63 bit range
74 if (auto const maxAmt = ctx.tx[~sfMaximumAmount])
75 {
76 if (maxAmt == 0)
77 return temMALFORMED;
78
79 if (maxAmt > maxMPTokenAmount)
80 return temMALFORMED;
81 }
82 return preflight2(ctx);
83}
84
87 ApplyView& view,
88 beast::Journal journal,
89 MPTCreateArgs const& args)
90{
91 auto const acct = view.peek(keylet::account(args.account));
92 if (!acct)
93 return Unexpected(tecINTERNAL); // LCOV_EXCL_LINE
94
95 if (args.priorBalance &&
96 *(args.priorBalance) <
97 view.fees().accountReserve((*acct)[sfOwnerCount] + 1))
99
100 auto const mptId = makeMptID(args.sequence, args.account);
101 auto const mptIssuanceKeylet = keylet::mptIssuance(mptId);
102
103 // create the MPTokenIssuance
104 {
105 auto const ownerNode = view.dirInsert(
107 mptIssuanceKeylet,
109
110 if (!ownerNode)
111 return Unexpected(tecDIR_FULL); // LCOV_EXCL_LINE
112
113 auto mptIssuance = std::make_shared<SLE>(mptIssuanceKeylet);
114 (*mptIssuance)[sfFlags] = args.flags & ~tfUniversal;
115 (*mptIssuance)[sfIssuer] = args.account;
116 (*mptIssuance)[sfOutstandingAmount] = 0;
117 (*mptIssuance)[sfOwnerNode] = *ownerNode;
118 (*mptIssuance)[sfSequence] = args.sequence;
119
120 if (args.maxAmount)
121 (*mptIssuance)[sfMaximumAmount] = *args.maxAmount;
122
123 if (args.assetScale)
124 (*mptIssuance)[sfAssetScale] = *args.assetScale;
125
126 if (args.transferFee)
127 (*mptIssuance)[sfTransferFee] = *args.transferFee;
128
129 if (args.metadata)
130 (*mptIssuance)[sfMPTokenMetadata] = *args.metadata;
131
132 if (args.domainId)
133 (*mptIssuance)[sfDomainID] = *args.domainId;
134
135 view.insert(mptIssuance);
136 }
137
138 // Update owner count.
139 adjustOwnerCount(view, acct, 1, journal);
140
141 return mptId;
142}
143
144TER
146{
147 auto const& tx = ctx_.tx;
148 auto const result = create(
149 view(),
150 j_,
151 {
152 .priorBalance = mPriorBalance,
153 .account = account_,
154 .sequence = tx.getSeqValue(),
155 .flags = tx.getFlags(),
156 .maxAmount = tx[~sfMaximumAmount],
157 .assetScale = tx[~sfAssetScale],
158 .transferFee = tx[~sfTransferFee],
159 .metadata = tx[~sfMPTokenMetadata],
160 .domainId = tx[~sfDomainID],
161 });
162 return result ? tesSUCCESS : result.error();
163}
164
165} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:60
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:144
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:318
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
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition View.cpp:1048
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
static bool adjustOwnerCount(ApplyContext &ctx, int count)
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::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