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 ripple {
15
16static void
18 Application const& app,
19 std::shared_ptr<SLE const> const& offer,
20 Json::Value& offers)
21{
22 Json::Value& obj(offers.append(Json::objectValue));
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// }
44static Json::Value
46 RPC::JsonContext& context,
47 uint256 const& nftId,
48 Keylet const& directory)
49{
50 unsigned int limit;
51 if (auto err = readLimitField(limit, RPC::Tuning::nftOffers, context))
52 return *err;
53
55
56 if (auto result = RPC::lookupLedger(ledger, context); !ledger)
57 return result;
58
59 if (!ledger->exists(directory))
61
62 Json::Value result;
63 result[jss::nft_id] = to_string(nftId);
64
65 Json::Value& jsonOffers(result[jss::offers] = Json::arrayValue);
66
68 unsigned int reserve(limit);
69 uint256 startAfter;
70 std::uint64_t startHint = 0;
71
72 if (context.params.isMember(jss::marker))
73 {
74 // We have a start point. Use limit - 1 from the result and use the
75 // very last one for the resume.
76 Json::Value const& marker(context.params[jss::marker]);
77
78 if (!marker.isString())
79 return RPC::expected_field_error(jss::marker, "string");
80
81 if (!startAfter.parseHex(marker.asString()))
83
84 auto const sle = ledger->read(keylet::nftoffer(startAfter));
85
86 if (!sle || nftId != sle->getFieldH256(sfNFTokenID))
88
89 startHint = sle->getFieldU64(sfNFTokenOfferNode);
90 appendNftOfferJson(context.app, sle, jsonOffers);
91 offers.reserve(reserve);
92 }
93 else
94 {
95 // We have no start point, limit should be one higher than requested.
96 offers.reserve(++reserve);
97 }
98
100 *ledger,
101 directory,
102 startAfter,
103 startHint,
104 reserve,
105 [&offers](std::shared_ptr<SLE const> const& offer) {
106 if (offer->getType() == ltNFTOKEN_OFFER)
107 {
108 offers.emplace_back(offer);
109 return true;
110 }
111
112 return false;
113 }))
114 {
116 }
117
118 if (offers.size() == reserve)
119 {
120 result[jss::limit] = limit;
121 result[jss::marker] = to_string(offers.back()->key());
122 offers.pop_back();
123 }
124
125 for (auto const& offer : offers)
126 appendNftOfferJson(context.app, offer, jsonOffers);
127
129 return result;
130}
131
134{
135 if (!context.params.isMember(jss::nft_id))
136 return RPC::missing_field_error(jss::nft_id);
137
138 uint256 nftId;
139
140 if (!nftId.parseHex(context.params[jss::nft_id].asString()))
141 return RPC::invalid_field_error(jss::nft_id);
142
143 return enumerateNFTOffers(context, nftId, keylet::nft_sells(nftId));
144}
145
148{
149 if (!context.params.isMember(jss::nft_id))
150 return RPC::missing_field_error(jss::nft_id);
151
152 uint256 nftId;
153
154 if (!nftId.parseHex(context.params[jss::nft_id].asString()))
155 return RPC::invalid_field_error(jss::nft_id);
156
157 return enumerateNFTOffers(context, nftId, keylet::nft_buys(nftId));
158}
159
160} // namespace ripple
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:484
@ 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:306
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition ErrorCodes.h:330
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext &context, Json::Value &result)
Look up a ledger from a request and fill a Json::Result with the data representing a ledger.
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:264
Charge const feeMediumBurdenRPC
Keylet nft_buys(uint256 const &id) noexcept
The directory of buy offers for the specified NFT.
Definition Indexes.cpp:415
Keylet nft_sells(uint256 const &id) noexcept
The directory of sell offers for the specified NFT.
Definition Indexes.cpp:421
Keylet nftoffer(AccountID const &owner, std::uint32_t seq)
An offer from an account to buy or sell an NFT.
Definition Indexes.cpp:408
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
static Json::Value enumerateNFTOffers(RPC::JsonContext &context, uint256 const &nftId, Keylet const &directory)
Definition NFTOffers.cpp:45
Json::Value doNFTBuyOffers(RPC::JsonContext &)
@ rpcOBJECT_NOT_FOUND
Definition ErrorCodes.h:124
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:65
Json::Value rpcError(int iError)
Definition RPCErr.cpp:12
Json::Value doNFTSellOffers(RPC::JsonContext &)
static void appendNftOfferJson(Application const &app, std::shared_ptr< SLE const > const &offer, Json::Value &offers)
Definition NFTOffers.cpp:17
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:665
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
uint256 key
Definition Keylet.h:21
Resource::Charge & loadType
Definition Context.h:23
Application & app
Definition Context.h:22