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#include <xrpl/protocol/Feature.h>
23#include <xrpl/protocol/TxFlags.h>
24
25namespace ripple {
26
29{
30 if (!ctx.rules.enabled(featureMPTokensV1))
31 return temDISABLED;
32
33 if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
34 return ret;
35
37 return temINVALID_FLAG;
38
39 if (auto const fee = ctx.tx[~sfTransferFee])
40 {
41 if (fee > maxTransferFee)
43
44 // If a non-zero TransferFee is set then the tfTransferable flag
45 // must also be set.
46 if (fee > 0u && !ctx.tx.isFlag(tfMPTCanTransfer))
47 return temMALFORMED;
48 }
49
50 if (auto const metadata = ctx.tx[~sfMPTokenMetadata])
51 {
52 if (metadata->length() == 0 ||
53 metadata->length() > maxMPTokenMetadataLength)
54 return temMALFORMED;
55 }
56
57 // Check if maximumAmount is within unsigned 63 bit range
58 if (auto const maxAmt = ctx.tx[~sfMaximumAmount])
59 {
60 if (maxAmt == 0)
61 return temMALFORMED;
62
63 if (maxAmt > maxMPTokenAmount)
64 return temMALFORMED;
65 }
66 return preflight2(ctx);
67}
68
69TER
71 ApplyView& view,
72 beast::Journal journal,
73 MPTCreateArgs const& args)
74{
75 auto const acct = view.peek(keylet::account(args.account));
76 if (!acct)
77 return tecINTERNAL;
78
79 if (args.priorBalance <
80 view.fees().accountReserve((*acct)[sfOwnerCount] + 1))
82
83 auto const mptIssuanceKeylet =
85
86 // create the MPTokenIssuance
87 {
88 auto const ownerNode = view.dirInsert(
90 mptIssuanceKeylet,
92
93 if (!ownerNode)
94 return tecDIR_FULL;
95
96 auto mptIssuance = std::make_shared<SLE>(mptIssuanceKeylet);
97 (*mptIssuance)[sfFlags] = args.flags & ~tfUniversal;
98 (*mptIssuance)[sfIssuer] = args.account;
99 (*mptIssuance)[sfOutstandingAmount] = 0;
100 (*mptIssuance)[sfOwnerNode] = *ownerNode;
101 (*mptIssuance)[sfSequence] = args.sequence;
102
103 if (args.maxAmount)
104 (*mptIssuance)[sfMaximumAmount] = *args.maxAmount;
105
106 if (args.assetScale)
107 (*mptIssuance)[sfAssetScale] = *args.assetScale;
108
109 if (args.transferFee)
110 (*mptIssuance)[sfTransferFee] = *args.transferFee;
111
112 if (args.metadata)
113 (*mptIssuance)[sfMPTokenMetadata] = *args.metadata;
114
115 view.insert(mptIssuance);
116 }
117
118 // Update owner count.
119 adjustOwnerCount(view, acct, 1, journal);
120
121 return tesSUCCESS;
122}
123
124TER
126{
127 auto const& tx = ctx_.tx;
128 return create(
129 ctx_.view(),
131 {.priorBalance = mPriorBalance,
132 .account = account_,
133 .sequence = tx.getSeqProxy().value(),
134 .flags = tx.getFlags(),
135 .maxAmount = tx[~sfMaximumAmount],
136 .assetScale = tx[~sfAssetScale],
137 .transferFee = tx[~sfTransferFee],
138 .metadata = tx[~sfMPTokenMetadata]});
139}
140
141} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:60
ApplyView & view()
Definition: ApplyContext.h:54
beast::Journal const journal
Definition: ApplyContext.h:51
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:140
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:314
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static NotTEC preflight(PreflightContext const &ctx)
static TER create(ApplyView &view, beast::Journal journal, MPTCreateArgs const &args)
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
ApplyView & view()
Definition: Transactor.h:107
ApplyContext & ctx_
Definition: Transactor.h:88
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition: Indexes.cpp:509
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:175
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:365
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:145
std::uint64_t constexpr maxMPTokenAmount
The maximum amount of MPTokenIssuance.
Definition: Protocol.h:117
constexpr std::uint32_t const tfMPTokenIssuanceCreateMask
Definition: TxFlags.h:147
std::uint16_t constexpr maxTransferFee
The maximum token transfer fee allowed.
Definition: Protocol.h:83
bool isTesSuccess(TER x)
Definition: TER.h:656
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:924
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:81
std::size_t constexpr maxMPTokenMetadataLength
The maximum length of MPTokenMetadata.
Definition: Protocol.h:114
static bool adjustOwnerCount(ApplyContext &ctx, int count)
Definition: SetOracle.cpp:185
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:133
@ tecDIR_FULL
Definition: TER.h:274
@ tecINTERNAL
Definition: TER.h:297
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:294
@ tesSUCCESS
Definition: TER.h:242
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:587
@ temBAD_TRANSFER_FEE
Definition: TER.h:143
@ 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< std::uint8_t > assetScale
XRPAmount const & priorBalance
std::optional< std::uint64_t > maxAmount
std::optional< Slice > const & metadata
State information when preflighting a tx.
Definition: Transactor.h:32