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#include <xrpld/ledger/View.h>
23
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));
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
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:119
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:130
bool empty() const
Definition: STObject.h:936
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()
Definition: STVector256.h:224
std::vector< uint256 >::iterator end()
Definition: STVector256.h:236
ApplyView & view()
Definition: Transactor.h:109
beast::Journal const j_
Definition: Transactor.h:91
ApplyContext & ctx_
Definition: Transactor.h:90
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:420
bool deleteTokenOffer(ApplyView &view, std::shared_ptr< SLE > const &offer)
Deletes the given token offer.
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition: offer.cpp:29
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::size_t constexpr maxTokenOfferCancelCount
The maximum number of token offers that can be canceled at once.
Definition: Protocol.h:69
bool isTesSuccess(TER x)
Definition: TER.h:672
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:83
@ tefBAD_LEDGER
Definition: TER.h:170
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:144
@ tecNO_PERMISSION
Definition: TER.h:305
@ tesSUCCESS
Definition: TER.h:244
constexpr std::uint32_t const tfNFTokenCancelOfferMask
Definition: TxFlags.h:203
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:603
@ 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:55
State information when preflighting a tx.
Definition: Transactor.h:34