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
36
39{
40 if (auto const& ids = ctx.tx[sfNFTokenOffers];
41 ids.empty() || (ids.size() > maxTokenOfferCancelCount))
42 return temMALFORMED;
43
44 // In order to prevent unnecessarily overlarge transactions, we
45 // disallow duplicates in the list of offers to cancel.
46 STVector256 ids = ctx.tx.getFieldV256(sfNFTokenOffers);
47 std::sort(ids.begin(), ids.end());
48 if (std::adjacent_find(ids.begin(), ids.end()) != ids.end())
49 return temMALFORMED;
50
51 return tesSUCCESS;
52}
53
54TER
56{
57 auto const account = ctx.tx[sfAccount];
58
59 auto const& ids = ctx.tx[sfNFTokenOffers];
60
61 auto ret = std::find_if(
62 ids.begin(), ids.end(), [&ctx, &account](uint256 const& id) {
63 auto const offer = ctx.view.read(keylet::child(id));
64
65 // If id is not in the ledger we assume the offer was consumed
66 // before we got here.
67 if (!offer)
68 return false;
69
70 // If id is in the ledger but is not an NFTokenOffer, then
71 // they have no permission.
72 if (offer->getType() != ltNFTOKEN_OFFER)
73 return true;
74
75 // Anyone can cancel, if expired
76 if (hasExpired(ctx.view, (*offer)[~sfExpiration]))
77 return false;
78
79 // The owner can always cancel
80 if ((*offer)[sfOwner] == account)
81 return false;
82
83 // The recipient can always cancel
84 if (auto const dest = (*offer)[~sfDestination]; dest == account)
85 return false;
86
87 return true;
88 });
89
90 if (ret != ids.end())
91 return tecNO_PERMISSION;
92
93 return tesSUCCESS;
94}
95
96TER
98{
99 for (auto const& id : ctx_.tx[sfNFTokenOffers])
100 {
101 if (auto offer = view().peek(keylet::nftoffer(id));
102 offer && !nft::deleteTokenOffer(view(), offer))
103 {
104 // LCOV_EXCL_START
105 JLOG(j_.fatal()) << "Unable to delete token offer " << id
106 << " (ledger " << view().seq() << ")";
107 return tefBAD_LEDGER;
108 // LCOV_EXCL_STOP
109 }
110 }
111
112 return tesSUCCESS;
113}
114
115} // 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 std::uint32_t getFlagsMask(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition ReadView.h:118
STVector256 const & getFieldV256(SField const &field) const
Definition STObject.cpp:685
std::vector< uint256 >::iterator begin()
std::vector< uint256 >::iterator end()
ApplyView & view()
Definition Transactor.h:163
beast::Journal const j_
Definition Transactor.h:145
ApplyContext & ctx_
Definition Transactor.h:143
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:71
@ tefBAD_LEDGER
Definition TER.h:170
@ tecNO_PERMISSION
Definition TER.h:305
@ tesSUCCESS
Definition TER.h:244
constexpr std::uint32_t const tfNFTokenCancelOfferMask
Definition TxFlags.h:235
@ temMALFORMED
Definition TER.h:87
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