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