xrpld
Loading...
Searching...
No Matches
NFTokenBurn.cpp
1#include <xrpl/tx/transactors/nft/NFTokenBurn.h>
2
3#include <xrpl/beast/utility/Journal.h>
4#include <xrpl/ledger/helpers/NFTokenHelpers.h>
5#include <xrpl/protocol/Indexes.h>
6#include <xrpl/protocol/Protocol.h>
7#include <xrpl/protocol/SField.h>
8#include <xrpl/protocol/STLedgerEntry.h>
9#include <xrpl/protocol/STTx.h>
10#include <xrpl/protocol/TER.h>
11#include <xrpl/protocol/XRPAmount.h>
12#include <xrpl/protocol/nft.h>
13#include <xrpl/tx/Transactor.h>
14
15#include <cstddef>
16namespace xrpl {
17
20{
21 return tesSUCCESS;
22}
23
24TER
26{
27 auto const owner = [&ctx]() {
28 if (ctx.tx.isFieldPresent(sfOwner))
29 return ctx.tx.getAccountID(sfOwner);
30
31 return ctx.tx[sfAccount];
32 }();
33
34 if (!nft::findToken(ctx.view, owner, ctx.tx[sfNFTokenID]))
35 return tecNO_ENTRY;
36
37 // The owner of a token can always burn it, but the issuer can only
38 // do so if the token is marked as burnable.
39 if (auto const account = ctx.tx[sfAccount]; owner != account)
40 {
41 if ((nft::getFlags(ctx.tx[sfNFTokenID]) & nft::kFlagBurnable) == 0)
42 return tecNO_PERMISSION;
43
44 if (auto const issuer = nft::getIssuer(ctx.tx[sfNFTokenID]); issuer != account)
45 {
46 if (auto const sle = ctx.view.read(keylet::account(issuer)); sle)
47 {
48 if (auto const minter = (*sle)[~sfNFTokenMinter]; minter != account)
49 return tecNO_PERMISSION;
50 }
51 }
52 }
53
54 return tesSUCCESS;
55}
56
57TER
59{
60 // Remove the token, effectively burning it:
61 auto const ret = nft::removeToken(
62 view(),
63 ctx_.tx.isFieldPresent(sfOwner) ? ctx_.tx.getAccountID(sfOwner)
64 : ctx_.tx.getAccountID(sfAccount),
65 ctx_.tx[sfNFTokenID]);
66
67 // Should never happen since preclaim() verified the token is present.
68 if (!isTesSuccess(ret))
69 return ret;
70
71 if (auto issuer = view().peek(keylet::account(nft::getIssuer(ctx_.tx[sfNFTokenID]))))
72 {
73 (*issuer)[~sfBurnedNFTokens] = (*issuer)[~sfBurnedNFTokens].valueOr(0) + 1;
74 view().update(issuer);
75 }
76
77 // Delete up to 500 offers in total.
78 // Because the number of sell offers is likely to be less than
79 // the number of buy offers, we prioritize the deletion of sell
80 // offers in order to clean up sell offer directory
81 std::size_t const deletedSellOffers = nft::removeTokenOffersWithLimit(
83
84 if (kMaxDeletableTokenOfferEntries > deletedSellOffers)
85 {
87 view(),
88 keylet::nftBuys(ctx_.tx[sfNFTokenID]),
89 kMaxDeletableTokenOfferEntries - deletedSellOffers);
90 }
91
92 return tesSUCCESS;
93}
94
95void
97{
98 // No transaction-specific invariants yet (future work).
99}
100
101bool
103{
104 // No transaction-specific invariants yet (future work).
105 return true;
106}
107
108} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
virtual void update(SLE::ref sle)=0
Indicate changes to a peeked SLE.
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.
static TER preclaim(PreclaimContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static NotTEC preflight(PreflightContext const &ctx)
TER doApply() override
A view into a ledger.
Definition ReadView.h:31
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
std::shared_ptr< STLedgerEntry const > const & const_ref
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:454
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:633
ApplyView & view()
Definition Transactor.h:136
ApplyContext & ctx_
Definition Transactor.h:116
Keylet nftSells(uint256 const &id) noexcept
The directory of sell offers for the specified NFT.
Definition Indexes.cpp:419
Keylet nftBuys(uint256 const &id) noexcept
The directory of buy offers for the specified NFT.
Definition Indexes.cpp:413
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
TER removeToken(ApplyView &view, AccountID const &owner, uint256 const &nftokenID)
Remove the token from the owner's token directory.
std::size_t removeTokenOffersWithLimit(ApplyView &view, Keylet const &directory, std::size_t maxDeletableOffers)
Delete up to a specified number of offers from the specified token offer directory.
std::optional< STObject > findToken(ReadView const &view, AccountID const &owner, uint256 const &nftokenID)
Finds the specified token in the owner's token directory.
AccountID getIssuer(uint256 const &id)
Definition nft.h:99
constexpr std::uint16_t const kFlagBurnable
Definition nft.h:32
std::uint16_t getFlags(uint256 const &id)
Definition nft.h:39
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr std::size_t kMaxDeletableTokenOfferEntries
The maximum number of offers in an offer directory for NFT to be burnable.
Definition Protocol.h:59
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
bool isTesSuccess(TER x) noexcept
Definition TER.h:663
TERSubset< CanCvtToTER > TER
Definition TER.h:634
@ tecNO_ENTRY
Definition TER.h:304
@ tecNO_PERMISSION
Definition TER.h:303
@ 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