xrpld
Loading...
Searching...
No Matches
NFTOffersCommon.cpp
1#include <xrpld/rpc/handlers/orderbook/NFTOffersCommon.h>
2
3#include <xrpld/rpc/Context.h>
4#include <xrpld/rpc/detail/RPCHelpers.h>
5#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
6#include <xrpld/rpc/detail/Tuning.h>
7
8#include <xrpl/basics/base_uint.h>
9#include <xrpl/json/json_value.h>
10#include <xrpl/ledger/ReadView.h>
11#include <xrpl/ledger/helpers/DirectoryHelpers.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/ErrorCodes.h>
14#include <xrpl/protocol/Indexes.h>
15#include <xrpl/protocol/Keylet.h>
16#include <xrpl/protocol/LedgerFormats.h>
17#include <xrpl/protocol/SField.h>
18#include <xrpl/protocol/STLedgerEntry.h>
19#include <xrpl/protocol/jss.h>
20#include <xrpl/resource/Fees.h>
21
22#include <rpcspec/Errors.hpp>
23#include <rpcspec/handlers/nft_offers_common/Types.hpp>
24
25#include <cstdint>
26#include <expected>
27#include <memory>
28
29namespace xrpl::rpc {
30
31static_assert(::rpc::spec::handlers::nft_offers_common::kLimitMin == tuning::kNftOffers.rmin);
32static_assert(
33 ::rpc::spec::handlers::nft_offers_common::kLimitDefault == tuning::kNftOffers.rDefault);
34static_assert(::rpc::spec::handlers::nft_offers_common::kLimitMax == tuning::kNftOffers.rmax);
35
36namespace {
37
38void
39appendNftOfferJson(SLE const& offer, json::Value& offers)
40{
41 json::Value& obj(offers.append(json::ValueType::Object));
42
43 obj[jss::nft_offer_index] = to_string(offer.key());
44 obj[jss::flags] = offer[sfFlags];
45 obj[jss::owner] = toBase58(offer.getAccountID(sfOwner));
46
47 if (offer.isFieldPresent(sfDestination))
48 obj[jss::destination] = toBase58(offer.getAccountID(sfDestination));
49
50 if (offer.isFieldPresent(sfExpiration))
51 obj[jss::expiration] = offer.getFieldU32(sfExpiration);
52
53 offer.getFieldAmount(sfAmount).setJson(obj[jss::amount]);
54}
55
56} // namespace
57
61
62std::expected<NFTOffersHandlerBase::Output, ::rpc::Status>
64{
65 auto& context = context_.get();
66 auto const limit = clampLimit(input.limit, tuning::kNftOffers, context.role);
67
69 if (auto const status = getLedger(ledger, input.ledger, context); status != ::rpc::Status::kOK)
70 return std::unexpected{status};
71
72 if (!ledger->exists(directory))
74
75 Output output;
76 output.nftID = input.nftID;
77
78 unsigned int reserve(limit);
79 UInt256 startAfter;
80 std::uint64_t startHint = 0;
81
82 if (input.marker.has_value())
83 {
84 // We have a start point. Use limit - 1 from the result and use the
85 // very last one for the resume.
86 startAfter = *input.marker;
87
88 auto const sle = ledger->read(keylet::nftokenOffer(startAfter));
89
90 if (!sle || input.nftID != sle->getFieldH256(sfNFTokenID))
92
93 // Reject a marker that references an offer on the opposite side
94 // (buy vs. sell) of the directory being enumerated. Without this
95 // check the marker's node hint points into the other directory, so
96 // forEachItemAfter never finds `startAfter` and instead scans every
97 // page of `directory` before returning invalidParams -- turning an
98 // O(1) rejection into an O(directory size) walk.
99 auto const offerDir = sle->isFlag(lsfSellNFToken) ? keylet::nftSells(input.nftID)
100 : keylet::nftBuys(input.nftID);
101 if (directory.key != offerDir.key)
103
104 startHint = sle->getFieldU64(sfNFTokenOfferNode);
105 output.offers.reserve(reserve + 1);
106 output.offers.emplace_back(sle);
107 }
108 else
109 {
110 // We have no start point, limit should be one higher than requested.
111 output.offers.reserve(++reserve);
112 }
113
114 auto const resumed = output.offers.size();
115 if (!forEachItemAfter(
116 *ledger, directory, startAfter, startHint, reserve, [&output](SLE::ConstRef offer) {
117 if (offer->getType() == ltNFTOKEN_OFFER)
118 {
119 output.offers.emplace_back(offer);
120 return true;
121 }
122
123 return false;
124 }))
125 {
127 }
128
129 if (output.offers.size() - resumed == reserve)
130 {
131 output.limit = limit;
132 output.marker = output.offers.back()->key();
133 output.offers.pop_back();
134 }
135
136 context.loadType = resource::kFeeMediumBurdenRpc;
137 return output;
138}
139
140void
142{
143 value[jss::nft_id] = to_string(output.nftID);
144
145 json::Value& offers(value[jss::offers] = json::ValueType::Array);
146 for (auto const& offer : output.offers)
147 appendNftOfferJson(*offer, offers);
148
149 if (output.marker.has_value())
150 {
151 value[jss::limit] = output.limit;
152 value[jss::marker] = to_string(*output.marker);
153 }
154}
155
156} // namespace xrpl::rpc
Represents a JSON value.
Definition json_value.h:121
Value & append(Value const &value)
Append value to array at the end.
std::shared_ptr< STLedgerEntry const > const & ConstRef
NFTOffersHandlerBase(JsonContext &context)
Construct a handler for a single request.
std::reference_wrapper< JsonContext > context_
std::expected< Output, ::rpc::Status > enumerateOffers(Input const &input, Keylet const &directory) const
Enumerate one page of an NFT offer directory.
static void writeResult(json::Value &value, Output const &output)
Write the output of process() into the response.
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
Keylet nftokenOffer(AccountID const &owner, SeqProxy const &seq)
An offer from an account to buy or sell an NFT.
Definition Indexes.cpp:465
Keylet offer(AccountID const &id, SeqProxy const &seq) noexcept
An offer from an account.
Definition Indexes.cpp:310
Keylet nftSells(UInt256 const &id) noexcept
The directory of sell offers for the specified NFT.
Definition Indexes.cpp:477
Keylet nftBuys(UInt256 const &id) noexcept
The directory of buy offers for the specified NFT.
Definition Indexes.cpp:471
Charge const kFeeMediumBurdenRpc
static constexpr LimitRange kNftOffers
Limits for the nft_buy_offers & nft_sell_offers commands.
The api_version numbers this server serves.
Definition ApiVersion.h:26
unsigned int clampLimit(unsigned int limit, tuning::LimitRange const &range, Role const &role)
Clamps a requested limit to the range a role may use.
::rpc::Status getLedger(T &ledger, UInt256 const &ledgerHash, Context const &context)
Retrieves a ledger by its hash.
@ RpcObjectNotFound
Definition ErrorCodes.h:126
@ RpcInvalidParams
Definition ErrorCodes.h:67
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
STLedgerEntry SLE
BaseUInt< 256 > UInt256
Definition base_uint.h:581
bool forEachItemAfter(ReadView const &view, Keylet const &root, UInt256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(SLE::ConstRef)> const &f)
Iterate all items after an item in the given directory.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
UInt256 key
Definition Keylet.h:21
The data an NFT offers request produces, written out by writeResult().
UInt256 nftID
The NFT whose offers are listed.
std::vector< SLE::const_pointer > offers
The offers on this page.
std::optional< UInt256 > marker
Where the next page starts, if there is one.
std::uint32_t limit
The limit used; written only with a marker.
T unexpected(T... args)