xrpld
Loading...
Searching...
No Matches
AccountOffers.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/basics/StringUtilities.h>
7#include <xrpl/basics/base_uint.h>
8#include <xrpl/beast/utility/Zero.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/core/ServiceRegistry.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/ledger/ReadView.h>
13#include <xrpl/ledger/helpers/DirectoryHelpers.h>
14#include <xrpl/protocol/AccountID.h>
15#include <xrpl/protocol/ErrorCodes.h>
16#include <xrpl/protocol/Indexes.h>
17#include <xrpl/protocol/LedgerFormats.h>
18#include <xrpl/protocol/RPCErr.h>
19#include <xrpl/protocol/SField.h>
20#include <xrpl/protocol/STAmount.h>
21#include <xrpl/protocol/jss.h>
22#include <xrpl/resource/Fees.h>
23
24#include <cstdint>
25#include <memory>
26#include <optional>
27#include <sstream>
28#include <string>
29#include <vector>
30
31namespace xrpl {
32
33void
35{
36 STAmount const dirRate = amountFromQuality(getQuality(offer->getFieldH256(sfBookDirectory)));
37 json::Value& obj(offers.append(json::ValueType::Object));
38 offer->getFieldAmount(sfTakerPays).setJson(obj[jss::taker_pays]);
39 offer->getFieldAmount(sfTakerGets).setJson(obj[jss::taker_gets]);
40 obj[jss::seq] = offer->getFieldU32(sfSequence);
41 obj[jss::flags] = offer->getFieldU32(sfFlags);
42 obj[jss::quality] = dirRate.getText();
43 if (offer->isFieldPresent(sfExpiration))
44 obj[jss::expiration] = offer->getFieldU32(sfExpiration);
45};
46
47// {
48// account: <account>
49// ledger_hash : <ledger>
50// ledger_index : <ledger_index>
51// limit: integer // optional
52// marker: opaque // optional, resume previous query
53// }
56{
57 auto const& params(context.params);
58 if (!params.isMember(jss::account))
59 return rpc::missingFieldError(jss::account);
60
61 if (!params[jss::account].isString())
62 return rpc::invalidFieldError(jss::account);
63
65 auto result = rpc::lookupLedger(ledger, context);
66 if (!ledger)
67 return result;
68
69 auto id = parseBase58<AccountID>(params[jss::account].asString());
70 if (!id)
71 {
73 return result;
74 }
75 auto const accountID{id.value()};
76
77 // Get info on account.
78 result[jss::account] = toBase58(accountID);
79
80 if (!ledger->exists(keylet::account(accountID)))
82
83 unsigned int limit = 0;
84 if (auto err = readLimitField(limit, rpc::tuning::kAccountOffers, context))
85 return *err;
86
87 json::Value& jsonOffers(result[jss::offers] = json::ValueType::Array);
89 uint256 startAfter = beast::kZero;
90 std::uint64_t startHint = 0;
91
92 if (params.isMember(jss::marker))
93 {
94 if (!params[jss::marker].isString())
95 return rpc::expectedFieldError(jss::marker, "string");
96
97 // Marker is composed of a comma separated index and start hint. The
98 // former will be read as hex, and the latter as a decimal integer.
99 std::stringstream marker(params[jss::marker].asString());
100 std::string value;
101 if (!std::getline(marker, value, ','))
102 return rpc::invalidFieldError(jss::marker);
103
104 if (!startAfter.parseHex(value))
105 return rpc::invalidFieldError(jss::marker);
106
107 if (!std::getline(marker, value, ','))
108 return rpc::invalidFieldError(jss::marker);
109
110 auto const hint = toUInt64(value);
111 if (!hint.has_value())
112 return rpc::invalidFieldError(jss::marker);
113 startHint = *hint;
114
115 // We then must check if the object pointed to by the marker is actually
116 // owned by the account in the request.
117 auto const sle = ledger->read({ltANY, startAfter});
118
119 if (!sle)
121
122 if (!rpc::isRelatedToAccount(*ledger, sle, accountID))
124 }
125
126 auto count = 0;
127 std::optional<uint256> marker = {};
128 std::uint64_t nextHint = 0;
129 if (!forEachItemAfter(
130 *ledger,
131 accountID,
132 startAfter,
133 startHint,
134 limit + 1,
135 [&offers, &count, &marker, &limit, &nextHint, &accountID](SLE::const_ref sle) {
136 if (!sle)
137 {
138 // LCOV_EXCL_START
139 UNREACHABLE("xrpl::doAccountOffers : null SLE");
140 return false;
141 // LCOV_EXCL_STOP
142 }
143
144 if (++count == limit)
145 {
146 marker = sle->key();
147 nextHint = rpc::getStartHint(sle, accountID);
148 }
149
150 if (count <= limit && sle->getType() == ltOFFER)
151 {
152 offers.emplace_back(sle);
153 }
154
155 return true;
156 }))
157 {
159 }
160
161 // Both conditions need to be checked because marker is set on the limit-th
162 // item, but if there is no item on the limit + 1 iteration, then there is
163 // no need to return a marker.
164 if (count == limit + 1 && marker)
165 {
166 result[jss::limit] = limit;
167 result[jss::marker] = to_string(*marker) + "," + std::to_string(nextHint);
168 }
169
170 for (auto const& offer : offers)
171 appendOfferJson(offer, jsonOffers);
172
174 return result;
175}
176
177} // namespace xrpl
Represents a JSON value.
Definition json_value.h:117
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:525
std::string getText() const override
Definition STAmount.cpp:646
std::shared_ptr< STLedgerEntry const > const & const_ref
T getline(T... args)
constexpr Zero kZero
Definition Zero.h:30
@ Array
array value (ordered list)
Definition json_value.h:28
@ Object
object value (collection of name/value pairs).
Definition json_value.h:29
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:198
Charge const kFeeMediumBurdenRpc
static constexpr LimitRange kAccountOffers
Limits for the account_offers command.
json::Value expectedFieldError(std::string const &name, std::string const &type)
Definition ErrorCodes.h:309
std::uint64_t getStartHint(SLE::const_ref sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
bool isRelatedToAccount(ReadView const &ledger, SLE::const_ref sle, AccountID const &accountID)
Tests if a ledger entry (SLE) is owned by the specified account.
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
json::Value invalidFieldError(std::string const &name)
Definition ErrorCodes.h:285
json::Value missingFieldError(std::string const &name)
Definition ErrorCodes.h:243
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcActNotFound
Definition ErrorCodes.h:53
@ RpcActMalformed
Definition ErrorCodes.h:73
@ RpcInvalidParams
Definition ErrorCodes.h:67
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::uint64_t getQuality(uint256 const &uBase)
Definition Indexes.cpp:172
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:651
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
STAmount amountFromQuality(std::uint64_t rate)
Definition STAmount.cpp:895
json::Value doAccountOffers(rpc::JsonContext &context)
std::optional< std::uint64_t > toUInt64(std::string const &s)
@ ltANY
A special type, matching any ledger entry type.
BaseUInt< 256 > uint256
Definition base_uint.h:580
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.
void appendOfferJson(SLE::const_ref offer, json::Value &offers)
resource::Charge & loadType
Definition Context.h:30
json::Value params
Definition Context.h:51
T to_string(T... args)