xrpld
Loading...
Searching...
No Matches
MPTokenAuthorize.cpp
1#include <xrpl/tx/transactors/token/MPTokenAuthorize.h>
2
3#include <xrpl/core/ServiceRegistry.h>
4#include <xrpl/ledger/helpers/AccountRootHelpers.h>
5#include <xrpl/ledger/helpers/MPTokenHelpers.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/Indexes.h>
8#include <xrpl/protocol/LedgerFormats.h>
9#include <xrpl/protocol/SField.h>
10#include <xrpl/protocol/STLedgerEntry.h>
11#include <xrpl/protocol/STTx.h>
12#include <xrpl/protocol/TER.h>
13#include <xrpl/protocol/TxFlags.h>
14#include <xrpl/protocol/XRPAmount.h>
15#include <xrpl/tx/Transactor.h>
16
17#include <cstdint>
18namespace xrpl {
19
20std::uint32_t
22{
23 return tfMPTokenAuthorizeMask;
24}
25
28{
29 if (ctx.tx[sfAccount] == ctx.tx[~sfHolder])
30 return temMALFORMED;
31
32 return tesSUCCESS;
33}
34
35TER
37{
38 auto const accountID = ctx.tx[sfAccount];
39 auto const holderID = ctx.tx[~sfHolder];
40
41 // if non-issuer account submits this tx, then they are trying either:
42 // 1. Unauthorize/delete MPToken
43 // 2. Use/create MPToken
44 //
45 // Note: `accountID` is holder's account
46 // `holderID` is NOT used
47 if (!holderID)
48 {
49 SLE::const_pointer const sleMpt =
50 ctx.view.read(keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], accountID));
51
52 // There is an edge case where all holders have zero balance, issuance
53 // is legally destroyed, then outstanding MPT(s) are deleted afterwards.
54 // Thus, there is no need to check for the existence of the issuance if
55 // the MPT is being deleted with a zero balance. Check for unauthorize
56 // before fetching the MPTIssuance object.
57
58 // if holder wants to delete/unauthorize a mpt
59 if (ctx.tx.isFlag(tfMPTUnauthorize))
60 {
61 if (!sleMpt)
63
64 if ((*sleMpt)[sfMPTAmount] != 0)
65 {
66 auto const sleMptIssuance =
67 ctx.view.read(keylet::mptokenIssuance(ctx.tx[sfMPTokenIssuanceID]));
68 if (!sleMptIssuance)
69 return tefINTERNAL; // LCOV_EXCL_LINE
70
71 return tecHAS_OBLIGATIONS;
72 }
73
74 if ((*sleMpt)[~sfLockedAmount].value_or(0) != 0)
75 {
76 auto const sleMptIssuance =
77 ctx.view.read(keylet::mptokenIssuance(ctx.tx[sfMPTokenIssuanceID]));
78 if (!sleMptIssuance)
79 return tefINTERNAL; // LCOV_EXCL_LINE
80
81 return tecHAS_OBLIGATIONS;
82 }
83 if (ctx.view.rules().enabled(featureSingleAssetVault) && sleMpt->isFlag(lsfMPTLocked))
84 return tecNO_PERMISSION;
85
86 if (ctx.view.rules().enabled(featureConfidentialTransfer))
87 {
88 auto const sleMptIssuance =
89 ctx.view.read(keylet::mptokenIssuance(ctx.tx[sfMPTokenIssuanceID]));
90
91 // if there still existing encrypted balances of MPT in
92 // circulation
93 if (sleMptIssuance &&
94 (*sleMptIssuance)[~sfConfidentialOutstandingAmount].value_or(0) != 0)
95 {
96 // this MPT still has encrypted balance, since we don't know
97 // if it's non-zero or not, we won't allow deletion of
98 // MPToken
99 if (sleMpt->isFieldPresent(sfConfidentialBalanceInbox) ||
100 sleMpt->isFieldPresent(sfConfidentialBalanceSpending))
101 return tecHAS_OBLIGATIONS;
102 }
103 }
104
105 return tesSUCCESS;
106 }
107
108 // Now test when the holder wants to hold/create/authorize a new MPT
109 auto const sleMptIssuance =
110 ctx.view.read(keylet::mptokenIssuance(ctx.tx[sfMPTokenIssuanceID]));
111
112 if (!sleMptIssuance)
113 return tecOBJECT_NOT_FOUND;
114
115 if (accountID == (*sleMptIssuance)[sfIssuer])
116 return tecNO_PERMISSION;
117
118 // if holder wants to use and create a mpt
119 if (sleMpt)
120 return tecDUPLICATE;
121
122 return tesSUCCESS;
123 }
124
125 auto const sleHolder = ctx.view.read(keylet::account(*holderID));
126 if (!sleHolder)
127 return tecNO_DST;
128
129 auto const sleMptIssuance = ctx.view.read(keylet::mptokenIssuance(ctx.tx[sfMPTokenIssuanceID]));
130 if (!sleMptIssuance)
131 return tecOBJECT_NOT_FOUND;
132
133 // If tx is submitted by issuer, they would either try to do the following
134 // for allowlisting:
135 // 1. authorize an account
136 // 2. unauthorize an account
137 //
138 // Note: `accountID` is issuer's account
139 // `holderID` is holder's account
140 if (accountID != (*sleMptIssuance)[sfIssuer])
141 return tecNO_PERMISSION;
142
143 // If tx is submitted by issuer, it only applies for MPT with
144 // lsfMPTRequireAuth set
145 if (!sleMptIssuance->isFlag(lsfMPTRequireAuth))
146 return tecNO_AUTH;
147
148 // The holder must create the MPT before the issuer can authorize it.
149 if (!ctx.view.exists(keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], *holderID)))
150 return tecOBJECT_NOT_FOUND;
151
152 // Can't unauthorize the pseudo-accounts because they are implicitly
153 // always authorized. No need to amendment gate since Vault and LoanBroker
154 // can only be created if the Vault amendment is enabled; AMM with MPToken asset
155 // can only be created if MPTokensV2 is enabled.
156 if (isPseudoAccount(ctx.view, *holderID, {&sfVaultID, &sfLoanBrokerID, &sfAMMID}))
157 return tecNO_PERMISSION;
158
159 return tesSUCCESS;
160}
161
162TER
164{
165 auto const& tx = ctx_.tx;
166 return authorizeMPToken(
167 ctx_.view(),
169 tx[sfMPTokenIssuanceID],
171 ctx_.journal,
172 tx.getFlags(),
173 tx[~sfHolder]);
174}
175
176void
178{
179 // No transaction-specific invariants yet (future work).
180}
181
182bool
184 STTx const&,
185 TER,
186 XRPAmount,
187 ReadView const&,
188 beast::Journal const&)
189{
190 // No transaction-specific invariants yet (future work).
191 return true;
192}
193
194} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
A view into a ledger.
Definition ReadView.h:31
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:171
std::shared_ptr< STLedgerEntry const > const & const_ref
std::shared_ptr< STLedgerEntry const > const_pointer
bool isFlag(std::uint32_t) const
Definition STObject.cpp:501
AccountID const accountID_
Definition Transactor.h:120
XRPAmount preFeeBalance_
Definition Transactor.h:121
ApplyContext & ctx_
Definition Transactor.h:116
Keylet mptokenIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:521
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:533
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ tefINTERNAL
Definition TER.h:163
TER authorizeMPToken(ApplyView &view, XRPAmount const &priorBalance, MPTID const &mptIssuanceID, AccountID const &account, beast::Journal journal, std::uint32_t flags=0, std::optional< AccountID > holderID=std::nullopt)
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
@ temMALFORMED
Definition TER.h:73
TERSubset< CanCvtToTER > TER
Definition TER.h:634
@ tecOBJECT_NOT_FOUND
Definition TER.h:324
@ tecNO_AUTH
Definition TER.h:298
@ tecNO_PERMISSION
Definition TER.h:303
@ tecDUPLICATE
Definition TER.h:313
@ tecHAS_OBLIGATIONS
Definition TER.h:315
@ tecNO_DST
Definition TER.h:288
bool isPseudoAccount(SLE::const_pointer sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
Returns true if and only if sleAcct is a pseudo-account or specific pseudo-accounts in pseudoFieldFil...
@ tesSUCCESS
Definition TER.h:240
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
State information when preflighting a tx.
Definition Transactor.h:18