xrpld
Loading...
Searching...
No Matches
NFTOffersHelpers.h
1#pragma once
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/json/json_value.h>
9#include <xrpl/ledger/ReadView.h>
10#include <xrpl/ledger/View.h>
11#include <xrpl/ledger/helpers/DirectoryHelpers.h>
12#include <xrpl/protocol/ErrorCodes.h>
13#include <xrpl/protocol/RPCErr.h>
14#include <xrpl/protocol/jss.h>
15#include <xrpl/resource/Fees.h>
16
17namespace xrpl {
18
19inline void
21{
22 json::Value& obj(offers.append(json::ValueType::Object));
23
24 obj[jss::nft_offer_index] = to_string(offer->key());
25 obj[jss::flags] = (*offer)[sfFlags];
26 obj[jss::owner] = toBase58(offer->getAccountID(sfOwner));
27
28 if (offer->isFieldPresent(sfDestination))
29 obj[jss::destination] = toBase58(offer->getAccountID(sfDestination));
30
31 if (offer->isFieldPresent(sfExpiration))
32 obj[jss::expiration] = offer->getFieldU32(sfExpiration);
33
34 offer->getFieldAmount(sfAmount).setJson(obj[jss::amount]);
35}
36
37// {
38// nft_id: <token hash>
39// ledger_hash : <ledger>
40// ledger_index : <ledger_index>
41// limit: integer // optional
42// marker: opaque // optional, resume previous query
43// }
44inline json::Value
46{
47 unsigned int limit = 0;
48 if (auto err = readLimitField(limit, RPC::Tuning::kNftOffers, context))
49 return *err;
50
52
53 if (auto result = RPC::lookupLedger(ledger, context); !ledger)
54 return result;
55
56 if (!ledger->exists(directory))
58
59 json::Value result;
60 result[jss::nft_id] = to_string(nftId);
61
62 json::Value& jsonOffers(result[jss::offers] = json::ValueType::Array);
63
65 unsigned int reserve(limit);
66 uint256 startAfter;
67 std::uint64_t startHint = 0;
68
69 if (context.params.isMember(jss::marker))
70 {
71 // We have a start point. Use limit - 1 from the result and use the
72 // very last one for the resume.
73 json::Value const& marker(context.params[jss::marker]);
74
75 if (!marker.isString())
76 return RPC::expectedFieldError(jss::marker, "string");
77
78 if (!startAfter.parseHex(marker.asString()))
80
81 auto const sle = ledger->read(keylet::nftokenOffer(startAfter));
82
83 if (!sle || nftId != sle->getFieldH256(sfNFTokenID))
85
86 startHint = sle->getFieldU64(sfNFTokenOfferNode);
87 appendNftOfferJson(context.app, sle, jsonOffers);
88 offers.reserve(reserve);
89 }
90 else
91 {
92 // We have no start point, limit should be one higher than requested.
93 offers.reserve(++reserve);
94 }
95
97 *ledger, directory, startAfter, startHint, reserve, [&offers](SLE::const_ref offer) {
98 if (offer->getType() == ltNFTOKEN_OFFER)
99 {
100 offers.emplace_back(offer);
101 return true;
102 }
103
104 return false;
105 }))
106 {
108 }
109
110 if (offers.size() == reserve)
111 {
112 result[jss::limit] = limit;
113 result[jss::marker] = to_string(offers.back()->key());
114 offers.pop_back();
115 }
116
117 for (auto const& offer : offers)
118 appendNftOfferJson(context.app, offer, jsonOffers);
119
121 return result;
122}
123
124} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
bool isString() const
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:507
std::shared_ptr< STLedgerEntry const > const & const_ref
@ Array
array value (ordered list)
Definition json_value.h:25
@ Object
object value (collection of name/value pairs).
Definition json_value.h:26
static constexpr LimitRange kNftOffers
Limits for the nft_buy_offers & nft_sell_offers commands.
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext const &context, json::Value &result)
Looks up a ledger from a request and fills a json::Value with ledger data.
json::Value expectedFieldError(std::string const &name, std::string const &type)
Definition ErrorCodes.h:297
Charge const kFeeMediumBurdenRpc
Keylet nftokenOffer(AccountID const &owner, std::uint32_t seq)
An offer from an account to buy or sell an NFT.
Definition Indexes.cpp:407
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void appendNftOfferJson(Application const &app, SLE::const_ref offer, json::Value &offers)
@ RpcObjectNotFound
Definition ErrorCodes.h:125
@ RpcInvalidParams
Definition ErrorCodes.h:66
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:93
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
json::Value enumerateNFTOffers(RPC::JsonContext &context, uint256 const &nftId, Keylet const &directory)
BaseUInt< 256 > uint256
Definition base_uint.h:562
bool forEachItemAfter(ReadView const &view, Keylet const &root, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(SLE::const_ref)> const &f)
Iterate all items after an item in the given directory.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
uint256 key
Definition Keylet.h:20
Application & app
Definition Context.h:21
Resource::Charge & loadType
Definition Context.h:22
json::Value params
Definition Context.h:43