rippled
Loading...
Searching...
No Matches
NFTokenBurn.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2021 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/NFTokenBurn.h>
21#include <xrpld/app/tx/detail/NFTokenUtils.h>
22
23#include <xrpl/protocol/Feature.h>
24#include <xrpl/protocol/Protocol.h>
25#include <xrpl/protocol/TxFlags.h>
26
27namespace ripple {
28
31{
32 if (!ctx.rules.enabled(featureNonFungibleTokensV1))
33 return temDISABLED;
34
35 if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
36 return ret;
37
38 if (ctx.tx.getFlags() & tfUniversalMask)
39 return temINVALID_FLAG;
40
41 return preflight2(ctx);
42}
43
44TER
46{
47 auto const owner = [&ctx]() {
48 if (ctx.tx.isFieldPresent(sfOwner))
49 return ctx.tx.getAccountID(sfOwner);
50
51 return ctx.tx[sfAccount];
52 }();
53
54 if (!nft::findToken(ctx.view, owner, ctx.tx[sfNFTokenID]))
55 return tecNO_ENTRY;
56
57 // The owner of a token can always burn it, but the issuer can only
58 // do so if the token is marked as burnable.
59 if (auto const account = ctx.tx[sfAccount]; owner != account)
60 {
61 if (!(nft::getFlags(ctx.tx[sfNFTokenID]) & nft::flagBurnable))
62 return tecNO_PERMISSION;
63
64 if (auto const issuer = nft::getIssuer(ctx.tx[sfNFTokenID]);
65 issuer != account)
66 {
67 if (auto const sle = ctx.view.read(keylet::account(issuer)); sle)
68 {
69 if (auto const minter = (*sle)[~sfNFTokenMinter];
70 minter != account)
71 return tecNO_PERMISSION;
72 }
73 }
74 }
75
76 if (!ctx.view.rules().enabled(fixNonFungibleTokensV1_2))
77 {
78 // If there are too many offers, then burning the token would produce
79 // too much metadata. Disallow burning a token with too many offers.
80 return nft::notTooManyOffers(ctx.view, ctx.tx[sfNFTokenID]);
81 }
82
83 return tesSUCCESS;
84}
85
86TER
88{
89 // Remove the token, effectively burning it:
90 auto const ret = nft::removeToken(
91 view(),
92 ctx_.tx.isFieldPresent(sfOwner) ? ctx_.tx.getAccountID(sfOwner)
93 : ctx_.tx.getAccountID(sfAccount),
94 ctx_.tx[sfNFTokenID]);
95
96 // Should never happen since preclaim() verified the token is present.
97 if (!isTesSuccess(ret))
98 return ret;
99
100 if (auto issuer =
101 view().peek(keylet::account(nft::getIssuer(ctx_.tx[sfNFTokenID]))))
102 {
103 (*issuer)[~sfBurnedNFTokens] =
104 (*issuer)[~sfBurnedNFTokens].value_or(0) + 1;
105 view().update(issuer);
106 }
107
108 if (ctx_.view().rules().enabled(fixNonFungibleTokensV1_2))
109 {
110 // Delete up to 500 offers in total.
111 // Because the number of sell offers is likely to be less than
112 // the number of buy offers, we prioritize the deletion of sell
113 // offers in order to clean up sell offer directory
114 std::size_t const deletedSellOffers = nft::removeTokenOffersWithLimit(
115 view(),
116 keylet::nft_sells(ctx_.tx[sfNFTokenID]),
118
119 if (maxDeletableTokenOfferEntries > deletedSellOffers)
120 {
122 view(),
123 keylet::nft_buys(ctx_.tx[sfNFTokenID]),
124 maxDeletableTokenOfferEntries - deletedSellOffers);
125 }
126 }
127 else
128 {
129 // Deletion of all offers.
131 view(),
132 keylet::nft_sells(ctx_.tx[sfNFTokenID]),
134
136 view(),
137 keylet::nft_buys(ctx_.tx[sfNFTokenID]),
139 }
140
141 return tesSUCCESS;
142}
143
144} // namespace ripple
ApplyView & view()
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static TER preclaim(PreclaimContext const &ctx)
TER doApply() override
static NotTEC preflight(PreflightContext const &ctx)
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:130
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:651
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:484
std::uint32_t getFlags() const
Definition STObject.cpp:537
ApplyView & view()
Definition Transactor.h:161
ApplyContext & ctx_
Definition Transactor.h:141
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:184
Keylet nft_buys(uint256 const &id) noexcept
The directory of buy offers for the specified NFT.
Definition Indexes.cpp:434
Keylet nft_sells(uint256 const &id) noexcept
The directory of sell offers for the specified NFT.
Definition Indexes.cpp:440
constexpr std::uint16_t const flagBurnable
Definition nft.h:53
std::uint16_t getFlags(uint256 const &id)
Definition nft.h:60
TER removeToken(ApplyView &view, AccountID const &owner, uint256 const &nftokenID)
Remove the token from the owner's token directory.
AccountID getIssuer(uint256 const &id)
Definition nft.h:120
TER notTooManyOffers(ReadView const &view, uint256 const &nftokenID)
Returns tesSUCCESS if NFToken has few enough offers that it can be burned.
std::optional< STObject > findToken(ReadView const &view, AccountID const &owner, uint256 const &nftokenID)
Finds the specified token in 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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::size_t constexpr maxDeletableTokenOfferEntries
The maximum number of offers in an offer directory for NFT to be burnable.
Definition Protocol.h:71
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
@ tecNO_ENTRY
Definition TER.h:306
@ tecNO_PERMISSION
Definition TER.h:305
@ tesSUCCESS
Definition TER.h:244
bool isTesSuccess(TER x) noexcept
Definition TER.h:674
constexpr std::uint32_t tfUniversalMask
Definition TxFlags.h:63
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:605
@ temINVALID_FLAG
Definition TER.h:111
@ temDISABLED
Definition TER.h:114
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:80
ReadView const & view
Definition Transactor.h:83
State information when preflighting a tx.
Definition Transactor.h:35