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
70TER
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 tecINTERNAL;
79
80 if (args.priorBalance <
81 view.fees().accountReserve((*acct)[sfOwnerCount] + 1))
83
84 auto const mptIssuanceKeylet =
86
87 // create the MPTokenIssuance
88 {
89 auto const ownerNode = view.dirInsert(
91 mptIssuanceKeylet,
93
94 if (!ownerNode)
95 return tecDIR_FULL;
96
97 auto mptIssuance = std::make_shared<SLE>(mptIssuanceKeylet);
98 (*mptIssuance)[sfFlags] = args.flags & ~tfUniversal;
99 (*mptIssuance)[sfIssuer] = args.account;
100 (*mptIssuance)[sfOutstandingAmount] = 0;
101 (*mptIssuance)[sfOwnerNode] = *ownerNode;
102 (*mptIssuance)[sfSequence] = args.sequence;
103
104 if (args.maxAmount)
105 (*mptIssuance)[sfMaximumAmount] = *args.maxAmount;
106
107 if (args.assetScale)
108 (*mptIssuance)[sfAssetScale] = *args.assetScale;
109
110 if (args.transferFee)
111 (*mptIssuance)[sfTransferFee] = *args.transferFee;
112
113 if (args.metadata)
114 (*mptIssuance)[sfMPTokenMetadata] = *args.metadata;
115
116 view.insert(mptIssuance);
117 }
118
119 // Update owner count.
120 adjustOwnerCount(view, acct, 1, journal);
121
122 return tesSUCCESS;
123}
124
125TER
127{
128 auto const& tx = ctx_.tx;
129 return create(
130 ctx_.view(),
132 {.priorBalance = mPriorBalance,
133 .account = account_,
134 .sequence = tx.getSeqProxy().value(),
135 .flags = tx.getFlags(),
136 .maxAmount = tx[~sfMaximumAmount],
137 .assetScale = tx[~sfAssetScale],
138 .transferFee = tx[~sfTransferFee],
139 .metadata = tx[~sfMPTokenMetadata]});
140}
141
142} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:60
ApplyView & view()
Definition: ApplyContext.h:55
beast::Journal const journal
Definition: ApplyContext.h:52
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:141
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:315
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:109
ApplyContext & ctx_
Definition: Transactor.h:90
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition: Indexes.cpp:518
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:176
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:366
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:152
std::uint64_t constexpr maxMPTokenAmount
The maximum amount of MPTokenIssuance.
Definition: Protocol.h:117
constexpr std::uint32_t const tfMPTokenIssuanceCreateMask
Definition: TxFlags.h:154
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:925
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:83
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:144
@ 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:34