rippled
Loading...
Searching...
No Matches
NFTokenCancelOffer.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/NFTokenCancelOffer.h>
21#include <xrpld/app/tx/detail/NFTokenUtils.h>
22
23#include <xrpl/ledger/View.h>
24#include <xrpl/protocol/Feature.h>
25#include <xrpl/protocol/TxFlags.h>
26
27#include <boost/endian/conversion.hpp>
28
29namespace ripple {
30
33{
34 if (!ctx.rules.enabled(featureNonFungibleTokensV1))
35 return temDISABLED;
36
37 if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
38 return ret;
39
41 return temINVALID_FLAG;
42
43 if (auto const& ids = ctx.tx[sfNFTokenOffers];
44 ids.empty() || (ids.size() > maxTokenOfferCancelCount))
45 return temMALFORMED;
46
47 // In order to prevent unnecessarily overlarge transactions, we
48 // disallow duplicates in the list of offers to cancel.
49 STVector256 ids = ctx.tx.getFieldV256(sfNFTokenOffers);
50 std::sort(ids.begin(), ids.end());
51 if (std::adjacent_find(ids.begin(), ids.end()) != ids.end())
52 return temMALFORMED;
53
54 return preflight2(ctx);
55}
56
57TER
59{
60 auto const account = ctx.tx[sfAccount];
61
62 auto const& ids = ctx.tx[sfNFTokenOffers];
63
64 auto ret = std::find_if(
65 ids.begin(), ids.end(), [&ctx, &account](uint256 const& id) {
66 auto const offer = ctx.view.read(keylet::child(id));
67
68 // If id is not in the ledger we assume the offer was consumed
69 // before we got here.
70 if (!offer)
71 return false;
72
73 // If id is in the ledger but is not an NFTokenOffer, then
74 // they have no permission.
75 if (offer->getType() != ltNFTOKEN_OFFER)
76 return true;
77
78 // Anyone can cancel, if expired
79 if (hasExpired(ctx.view, (*offer)[~sfExpiration]))
80 return false;
81
82 // The owner can always cancel
83 if ((*offer)[sfOwner] == account)
84 return false;
85
86 // The recipient can always cancel
87 if (auto const dest = (*offer)[~sfDestination]; dest == account)
88 return false;
89
90 return true;
91 });
92
93 if (ret != ids.end())
94 return tecNO_PERMISSION;
95
96 return tesSUCCESS;
97}
98
99TER
101{
102 for (auto const& id : ctx_.tx[sfNFTokenOffers])
103 {
104 if (auto offer = view().peek(keylet::nftoffer(id));
105 offer && !nft::deleteTokenOffer(view(), offer))
106 {
107 JLOG(j_.fatal()) << "Unable to delete token offer " << id
108 << " (ledger " << view().seq() << ")";
109 return tefBAD_LEDGER;
110 }
111 }
112
113 return tesSUCCESS;
114}
115
116} // namespace ripple
T adjacent_find(T... args)
Stream fatal() const
Definition Journal.h:352
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 preclaim(PreclaimContext const &ctx)
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition ReadView.h:118
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:130
std::uint32_t getFlags() const
Definition STObject.cpp:537
STVector256 const & getFieldV256(SField const &field) const
Definition STObject.cpp:679
std::vector< uint256 >::iterator begin()
std::vector< uint256 >::iterator end()
ApplyView & view()
Definition Transactor.h:161
beast::Journal const j_
Definition Transactor.h:143
ApplyContext & ctx_
Definition Transactor.h:141
T find_if(T... args)
Keylet nftoffer(AccountID const &owner, std::uint32_t seq)
An offer from an account to buy or sell an NFT.
Definition Indexes.cpp:427
bool deleteTokenOffer(ApplyView &view, std::shared_ptr< SLE > const &offer)
Deletes the given token offer.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::size_t constexpr maxTokenOfferCancelCount
The maximum number of token offers that can be canceled at once.
Definition Protocol.h:68
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
@ tefBAD_LEDGER
Definition TER.h:170
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
@ tecNO_PERMISSION
Definition TER.h:305
@ tesSUCCESS
Definition TER.h:244
constexpr std::uint32_t const tfNFTokenCancelOfferMask
Definition TxFlags.h:235
bool isTesSuccess(TER x) noexcept
Definition TER.h:674
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:605
@ temMALFORMED
Definition TER.h:87
@ temINVALID_FLAG
Definition TER.h:111
@ temDISABLED
Definition TER.h:114
T sort(T... args)
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:80
State information when preflighting a tx.
Definition Transactor.h:35