rippled
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/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
16void
18{
19 STAmount dirRate =
20 amountFromQuality(getQuality(offer->getFieldH256(sfBookDirectory)));
21 Json::Value& obj(offers.append(Json::objectValue));
22 offer->getFieldAmount(sfTakerPays).setJson(obj[jss::taker_pays]);
23 offer->getFieldAmount(sfTakerGets).setJson(obj[jss::taker_gets]);
24 obj[jss::seq] = offer->getFieldU32(sfSequence);
25 obj[jss::flags] = offer->getFieldU32(sfFlags);
26 obj[jss::quality] = dirRate.getText();
27 if (offer->isFieldPresent(sfExpiration))
28 obj[jss::expiration] = offer->getFieldU32(sfExpiration);
29};
30
31// {
32// account: <account>
33// ledger_hash : <ledger>
34// ledger_index : <ledger_index>
35// limit: integer // optional
36// marker: opaque // optional, resume previous query
37// }
40{
41 auto const& params(context.params);
42 if (!params.isMember(jss::account))
43 return RPC::missing_field_error(jss::account);
44
45 if (!params[jss::account].isString())
46 return RPC::invalid_field_error(jss::account);
47
49 auto result = RPC::lookupLedger(ledger, context);
50 if (!ledger)
51 return result;
52
53 auto id = parseBase58<AccountID>(params[jss::account].asString());
54 if (!id)
55 {
57 return result;
58 }
59 auto const accountID{std::move(id.value())};
60
61 // Get info on account.
62 result[jss::account] = toBase58(accountID);
63
64 if (!ledger->exists(keylet::account(accountID)))
66
67 unsigned int limit;
68 if (auto err = readLimitField(limit, RPC::Tuning::accountOffers, context))
69 return *err;
70
71 Json::Value& jsonOffers(result[jss::offers] = Json::arrayValue);
73 uint256 startAfter = beast::zero;
74 std::uint64_t startHint = 0;
75
76 if (params.isMember(jss::marker))
77 {
78 if (!params[jss::marker].isString())
79 return RPC::expected_field_error(jss::marker, "string");
80
81 // Marker is composed of a comma separated index and start hint. The
82 // former will be read as hex, and the latter using boost lexical cast.
83 std::stringstream marker(params[jss::marker].asString());
84 std::string value;
85 if (!std::getline(marker, value, ','))
86 return RPC::invalid_field_error(jss::marker);
87
88 if (!startAfter.parseHex(value))
89 return RPC::invalid_field_error(jss::marker);
90
91 if (!std::getline(marker, value, ','))
92 return RPC::invalid_field_error(jss::marker);
93
94 try
95 {
96 startHint = boost::lexical_cast<std::uint64_t>(value);
97 }
98 catch (boost::bad_lexical_cast&)
99 {
100 return RPC::invalid_field_error(jss::marker);
101 }
102
103 // We then must check if the object pointed to by the marker is actually
104 // owned by the account in the request.
105 auto const sle = ledger->read({ltANY, startAfter});
106
107 if (!sle)
109
110 if (!RPC::isRelatedToAccount(*ledger, sle, accountID))
112 }
113
114 auto count = 0;
115 std::optional<uint256> marker = {};
116 std::uint64_t nextHint = 0;
117 if (!forEachItemAfter(
118 *ledger,
119 accountID,
120 startAfter,
121 startHint,
122 limit + 1,
123 [&offers, &count, &marker, &limit, &nextHint, &accountID](
124 std::shared_ptr<SLE const> const& sle) {
125 if (!sle)
126 {
127 // LCOV_EXCL_START
128 UNREACHABLE("ripple::doAccountOffers : null SLE");
129 return false;
130 // LCOV_EXCL_STOP
131 }
132
133 if (++count == limit)
134 {
135 marker = sle->key();
136 nextHint = RPC::getStartHint(sle, accountID);
137 }
138
139 if (count <= limit && sle->getType() == ltOFFER)
140 {
141 offers.emplace_back(sle);
142 }
143
144 return true;
145 }))
146 {
148 }
149
150 // Both conditions need to be checked because marker is set on the limit-th
151 // item, but if there is no item on the limit + 1 iteration, then there is
152 // no need to return a marker.
153 if (count == limit + 1 && marker)
154 {
155 result[jss::limit] = limit;
156 result[jss::marker] =
157 to_string(*marker) + "," + std::to_string(nextHint);
158 }
159
160 for (auto const& offer : offers)
161 appendOfferJson(offer, jsonOffers);
162
164 return result;
165}
166
167} // namespace ripple
Represents a JSON value.
Definition json_value.h:131
std::string getText() const override
Definition STAmount.cpp:664
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:484
T getline(T... args)
@ 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 accountOffers
Limits for the account_offers command.
Json::Value invalid_field_error(std::string const &name)
Definition ErrorCodes.h:306
bool isRelatedToAccount(ReadView const &ledger, std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Tests if a SLE is owned by accountID.
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition ErrorCodes.h:214
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.
std::uint64_t getStartHint(std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:264
Charge const feeMediumBurdenRPC
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
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
std::uint64_t getQuality(uint256 const &uBase)
Definition Indexes.cpp:130
@ rpcACT_NOT_FOUND
Definition ErrorCodes.h:51
@ rpcACT_MALFORMED
Definition ErrorCodes.h:71
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:65
STAmount amountFromQuality(std::uint64_t rate)
Definition STAmount.cpp:965
Json::Value rpcError(int iError)
Definition RPCErr.cpp:12
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
Json::Value doAccountOffers(RPC::JsonContext &context)
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
@ ltANY
A special type, matching any ledger entry type.
void appendOfferJson(std::shared_ptr< SLE const > const &offer, Json::Value &offers)
Resource::Charge & loadType
Definition Context.h:23
T to_string(T... args)