rippled
Loading...
Searching...
No Matches
NFTOffers.cpp
1#include <xrpld/rpc/Context.h>
2#include <xrpld/rpc/detail/RPCHelpers.h>
3#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
4#include <xrpld/rpc/detail/Tuning.h>
5
6#include <xrpl/json/json_value.h>
7#include <xrpl/ledger/ReadView.h>
8#include <xrpl/ledger/View.h>
9#include <xrpl/protocol/ErrorCodes.h>
10#include <xrpl/protocol/RPCErr.h>
11#include <xrpl/protocol/jss.h>
12#include <xrpl/resource/Fees.h>
13
14namespace xrpl {
15
16static void
18{
19 Json::Value& obj(offers.append(Json::objectValue));
20
21 obj[jss::nft_offer_index] = to_string(offer->key());
22 obj[jss::flags] = (*offer)[sfFlags];
23 obj[jss::owner] = toBase58(offer->getAccountID(sfOwner));
24
25 if (offer->isFieldPresent(sfDestination))
26 obj[jss::destination] = toBase58(offer->getAccountID(sfDestination));
27
28 if (offer->isFieldPresent(sfExpiration))
29 obj[jss::expiration] = offer->getFieldU32(sfExpiration);
30
31 offer->getFieldAmount(sfAmount).setJson(obj[jss::amount]);
32}
33
34// {
35// nft_id: <token hash>
36// ledger_hash : <ledger>
37// ledger_index : <ledger_index>
38// limit: integer // optional
39// marker: opaque // optional, resume previous query
40// }
41static Json::Value
42enumerateNFTOffers(RPC::JsonContext& context, uint256 const& nftId, Keylet const& directory)
43{
44 unsigned int limit;
45 if (auto err = readLimitField(limit, RPC::Tuning::nftOffers, context))
46 return *err;
47
49
50 if (auto result = RPC::lookupLedger(ledger, context); !ledger)
51 return result;
52
53 if (!ledger->exists(directory))
55
56 Json::Value result;
57 result[jss::nft_id] = to_string(nftId);
58
59 Json::Value& jsonOffers(result[jss::offers] = Json::arrayValue);
60
62 unsigned int reserve(limit);
63 uint256 startAfter;
64 std::uint64_t startHint = 0;
65
66 if (context.params.isMember(jss::marker))
67 {
68 // We have a start point. Use limit - 1 from the result and use the
69 // very last one for the resume.
70 Json::Value const& marker(context.params[jss::marker]);
71
72 if (!marker.isString())
73 return RPC::expected_field_error(jss::marker, "string");
74
75 if (!startAfter.parseHex(marker.asString()))
77
78 auto const sle = ledger->read(keylet::nftoffer(startAfter));
79
80 if (!sle || nftId != sle->getFieldH256(sfNFTokenID))
82
83 startHint = sle->getFieldU64(sfNFTokenOfferNode);
84 appendNftOfferJson(context.app, sle, jsonOffers);
85 offers.reserve(reserve);
86 }
87 else
88 {
89 // We have no start point, limit should be one higher than requested.
90 offers.reserve(++reserve);
91 }
92
94 *ledger, directory, startAfter, startHint, reserve, [&offers](std::shared_ptr<SLE const> const& offer) {
95 if (offer->getType() == ltNFTOKEN_OFFER)
96 {
97 offers.emplace_back(offer);
98 return true;
99 }
100
101 return false;
102 }))
103 {
105 }
106
107 if (offers.size() == reserve)
108 {
109 result[jss::limit] = limit;
110 result[jss::marker] = to_string(offers.back()->key());
111 offers.pop_back();
112 }
113
114 for (auto const& offer : offers)
115 appendNftOfferJson(context.app, offer, jsonOffers);
116
118 return result;
119}
120
123{
124 if (!context.params.isMember(jss::nft_id))
125 return RPC::missing_field_error(jss::nft_id);
126
127 uint256 nftId;
128
129 if (!nftId.parseHex(context.params[jss::nft_id].asString()))
130 return RPC::invalid_field_error(jss::nft_id);
131
132 return enumerateNFTOffers(context, nftId, keylet::nft_sells(nftId));
133}
134
137{
138 if (!context.params.isMember(jss::nft_id))
139 return RPC::missing_field_error(jss::nft_id);
140
141 uint256 nftId;
142
143 if (!nftId.parseHex(context.params[jss::nft_id].asString()))
144 return RPC::invalid_field_error(jss::nft_id);
145
146 return enumerateNFTOffers(context, nftId, keylet::nft_buys(nftId));
147}
148
149} // namespace xrpl
Represents a JSON value.
Definition json_value.h:131
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:472
@ arrayValue
array value (ordered list)
Definition json_value.h:26
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:27
static LimitRange constexpr nftOffers
Limits for the nft_buy_offers & nft_sell_offers commands.
Json::Value invalid_field_error(std::string const &name)
Definition ErrorCodes.h:269
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition ErrorCodes.h:293
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:227
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.
Charge const feeMediumBurdenRPC
Keylet nftoffer(AccountID const &owner, std::uint32_t seq)
An offer from an account to buy or sell an NFT.
Definition Indexes.cpp:375
Keylet nft_buys(uint256 const &id) noexcept
The directory of buy offers for the specified NFT.
Definition Indexes.cpp:381
Keylet nft_sells(uint256 const &id) noexcept
The directory of sell offers for the specified NFT.
Definition Indexes.cpp:387
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
Json::Value doNFTSellOffers(RPC::JsonContext &)
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:92
Json::Value rpcError(error_code_i iError)
Definition RPCErr.cpp:12
Json::Value doNFTBuyOffers(RPC::JsonContext &)
static Json::Value enumerateNFTOffers(RPC::JsonContext &context, uint256 const &nftId, Keylet const &directory)
Definition NFTOffers.cpp:42
bool forEachItemAfter(ReadView const &view, Keylet const &root, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items after an item in the given directory.
Definition View.cpp:622
static void appendNftOfferJson(Application const &app, std::shared_ptr< SLE const > const &offer, Json::Value &offers)
Definition NFTOffers.cpp:17
@ rpcOBJECT_NOT_FOUND
Definition ErrorCodes.h:124
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:65
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
uint256 key
Definition Keylet.h:21
Application & app
Definition Context.h:22
Resource::Charge & loadType
Definition Context.h:23
Json::Value params
Definition Context.h:44