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 (auto const ret = preflight1(ctx); !isTesSuccess(ret))
35 return ret;
36
38 return temINVALID_FLAG;
39
40 if (auto const fee = ctx.tx[~sfTransferFee])
41 {
42 if (fee > maxTransferFee)
44
45 // If a non-zero TransferFee is set then the tfTransferable flag
46 // must also be set.
47 if (fee > 0u && !ctx.tx.isFlag(tfMPTCanTransfer))
48 return temMALFORMED;
49 }
50
51 if (auto const metadata = ctx.tx[~sfMPTokenMetadata])
52 {
53 if (metadata->length() == 0 ||
54 metadata->length() > maxMPTokenMetadataLength)
55 return temMALFORMED;
56 }
57
58 // Check if maximumAmount is within unsigned 63 bit range
59 if (auto const maxAmt = ctx.tx[~sfMaximumAmount])
60 {
61 if (maxAmt == 0)
62 return temMALFORMED;
63
64 if (maxAmt > maxMPTokenAmount)
65 return temMALFORMED;
66 }
67 return preflight2(ctx);
68}
69
72 ApplyView& view,
73 beast::Journal journal,
74 MPTCreateArgs const& args)
75{
76 auto const acct = view.peek(keylet::account(args.account));
77 if (!acct)
78 return Unexpected(tecINTERNAL); // LCOV_EXCL_LINE
79
80 if (args.priorBalance &&
81 *(args.priorBalance) <
82 view.fees().accountReserve((*acct)[sfOwnerCount] + 1))
84
85 auto const mptId = makeMptID(args.sequence, args.account);
86 auto const mptIssuanceKeylet = keylet::mptIssuance(mptId);
87
88 // create the MPTokenIssuance
89 {
90 auto const ownerNode = view.dirInsert(
92 mptIssuanceKeylet,
94
95 if (!ownerNode)
96 return Unexpected(tecDIR_FULL); // LCOV_EXCL_LINE
97
98 auto mptIssuance = std::make_shared<SLE>(mptIssuanceKeylet);
99 (*mptIssuance)[sfFlags] = args.flags & ~tfUniversal;
100 (*mptIssuance)[sfIssuer] = args.account;
101 (*mptIssuance)[sfOutstandingAmount] = 0;
102 (*mptIssuance)[sfOwnerNode] = *ownerNode;
103 (*mptIssuance)[sfSequence] = args.sequence;
104
105 if (args.maxAmount)
106 (*mptIssuance)[sfMaximumAmount] = *args.maxAmount;
107
108 if (args.assetScale)
109 (*mptIssuance)[sfAssetScale] = *args.assetScale;
110
111 if (args.transferFee)
112 (*mptIssuance)[sfTransferFee] = *args.transferFee;
113
114 if (args.metadata)
115 (*mptIssuance)[sfMPTokenMetadata] = *args.metadata;
116
117 if (args.domainId)
118 (*mptIssuance)[sfDomainID] = *args.domainId;
119
120 view.insert(mptIssuance);
121 }
122
123 // Update owner count.
124 adjustOwnerCount(view, acct, 1, journal);
125
126 return mptId;
127}
128
129TER
131{
132 auto const& tx = ctx_.tx;
133 auto const result = create(
134 view(),
135 j_,
136 {
137 .priorBalance = mPriorBalance,
138 .account = account_,
139 .sequence = tx.getSeqValue(),
140 .flags = tx.getFlags(),
141 .maxAmount = tx[~sfMaximumAmount],
142 .assetScale = tx[~sfAssetScale],
143 .transferFee = tx[~sfTransferFee],
144 .metadata = tx[~sfMPTokenMetadata],
145 });
146 return result ? tesSUCCESS : result.error();
147}
148
149} // 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
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
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:26
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:1049
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:91
std::size_t constexpr maxMPTokenMetadataLength
The maximum length of MPTokenMetadata.
Definition: Protocol.h:114
static bool adjustOwnerCount(ApplyContext &ctx, int count)
Definition: SetOracle.cpp:186
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:160
@ 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
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.
Definition: protocol/Fees.h:49
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