rippled
Loading...
Searching...
No Matches
BookOffers.cpp
1#include <xrpld/app/main/Application.h>
2#include <xrpld/app/misc/NetworkOPs.h>
3#include <xrpld/rpc/BookChanges.h>
4#include <xrpld/rpc/Context.h>
5#include <xrpld/rpc/detail/RPCHelpers.h>
6#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
7
8#include <xrpl/basics/Log.h>
9#include <xrpl/ledger/ReadView.h>
10#include <xrpl/protocol/ErrorCodes.h>
11#include <xrpl/protocol/RPCErr.h>
12#include <xrpl/protocol/UintTypes.h>
13#include <xrpl/protocol/jss.h>
14#include <xrpl/resource/Fees.h>
15
16namespace xrpl {
17
20{
21 // VFALCO TODO Here is a terrible place for this kind of business
22 // logic. It needs to be moved elsewhere and documented,
23 // and encapsulated into a function.
24 if (context.app.getJobQueue().getJobCountGE(jtCLIENT) > 200)
25 return rpcError(rpcTOO_BUSY);
26
28 auto jvResult = RPC::lookupLedger(lpLedger, context);
29
30 if (!lpLedger)
31 return jvResult;
32
33 if (!context.params.isMember(jss::taker_pays))
34 return RPC::missing_field_error(jss::taker_pays);
35
36 if (!context.params.isMember(jss::taker_gets))
37 return RPC::missing_field_error(jss::taker_gets);
38
39 Json::Value const& taker_pays = context.params[jss::taker_pays];
40 Json::Value const& taker_gets = context.params[jss::taker_gets];
41
42 if (!taker_pays.isObjectOrNull())
43 return RPC::object_field_error(jss::taker_pays);
44
45 if (!taker_gets.isObjectOrNull())
46 return RPC::object_field_error(jss::taker_gets);
47
48 if (!taker_pays.isMember(jss::currency))
49 return RPC::missing_field_error("taker_pays.currency");
50
51 if (!taker_pays[jss::currency].isString())
52 return RPC::expected_field_error("taker_pays.currency", "string");
53
54 if (!taker_gets.isMember(jss::currency))
55 return RPC::missing_field_error("taker_gets.currency");
56
57 if (!taker_gets[jss::currency].isString())
58 return RPC::expected_field_error("taker_gets.currency", "string");
59
60 Currency pay_currency;
61
62 if (!to_currency(pay_currency, taker_pays[jss::currency].asString()))
63 {
64 JLOG(context.j.info()) << "Bad taker_pays currency.";
65 return RPC::make_error(rpcSRC_CUR_MALFORMED, "Invalid field 'taker_pays.currency', bad currency.");
66 }
67
68 Currency get_currency;
69
70 if (!to_currency(get_currency, taker_gets[jss::currency].asString()))
71 {
72 JLOG(context.j.info()) << "Bad taker_gets currency.";
73 return RPC::make_error(rpcDST_AMT_MALFORMED, "Invalid field 'taker_gets.currency', bad currency.");
74 }
75
76 AccountID pay_issuer;
77
78 if (taker_pays.isMember(jss::issuer))
79 {
80 if (!taker_pays[jss::issuer].isString())
81 return RPC::expected_field_error("taker_pays.issuer", "string");
82
83 if (!to_issuer(pay_issuer, taker_pays[jss::issuer].asString()))
84 return RPC::make_error(rpcSRC_ISR_MALFORMED, "Invalid field 'taker_pays.issuer', bad issuer.");
85
86 if (pay_issuer == noAccount())
87 return RPC::make_error(rpcSRC_ISR_MALFORMED, "Invalid field 'taker_pays.issuer', bad issuer account one.");
88 }
89 else
90 {
91 pay_issuer = xrpAccount();
92 }
93
94 if (isXRP(pay_currency) && !isXRP(pay_issuer))
95 return RPC::make_error(
97 "Unneeded field 'taker_pays.issuer' for "
98 "XRP currency specification.");
99
100 if (!isXRP(pay_currency) && isXRP(pay_issuer))
101 return RPC::make_error(rpcSRC_ISR_MALFORMED, "Invalid field 'taker_pays.issuer', expected non-XRP issuer.");
102
103 AccountID get_issuer;
104
105 if (taker_gets.isMember(jss::issuer))
106 {
107 if (!taker_gets[jss::issuer].isString())
108 return RPC::expected_field_error("taker_gets.issuer", "string");
109
110 if (!to_issuer(get_issuer, taker_gets[jss::issuer].asString()))
111 return RPC::make_error(rpcDST_ISR_MALFORMED, "Invalid field 'taker_gets.issuer', bad issuer.");
112
113 if (get_issuer == noAccount())
114 return RPC::make_error(rpcDST_ISR_MALFORMED, "Invalid field 'taker_gets.issuer', bad issuer account one.");
115 }
116 else
117 {
118 get_issuer = xrpAccount();
119 }
120
121 if (isXRP(get_currency) && !isXRP(get_issuer))
122 return RPC::make_error(
124 "Unneeded field 'taker_gets.issuer' for "
125 "XRP currency specification.");
126
127 if (!isXRP(get_currency) && isXRP(get_issuer))
128 return RPC::make_error(rpcDST_ISR_MALFORMED, "Invalid field 'taker_gets.issuer', expected non-XRP issuer.");
129
131 if (context.params.isMember(jss::taker))
132 {
133 if (!context.params[jss::taker].isString())
134 return RPC::expected_field_error(jss::taker, "string");
135
136 takerID = parseBase58<AccountID>(context.params[jss::taker].asString());
137 if (!takerID)
138 return RPC::invalid_field_error(jss::taker);
139 }
140
142 if (context.params.isMember(jss::domain))
143 {
144 uint256 num;
145 if (!context.params[jss::domain].isString() || !num.parseHex(context.params[jss::domain].asString()))
146 {
147 return RPC::make_error(rpcDOMAIN_MALFORMED, "Unable to parse domain.");
148 }
149 else
150 {
151 domain = num;
152 }
153 }
154
155 if (pay_currency == get_currency && pay_issuer == get_issuer)
156 {
157 JLOG(context.j.info()) << "taker_gets same as taker_pays.";
159 }
160
161 unsigned int limit;
162 if (auto err = readLimitField(limit, RPC::Tuning::bookOffers, context))
163 return *err;
164
165 bool const bProof(context.params.isMember(jss::proof));
166
167 Json::Value const jvMarker(
168 context.params.isMember(jss::marker) ? context.params[jss::marker] : Json::Value(Json::nullValue));
169
170 context.netOps.getBookPage(
171 lpLedger,
172 {{pay_currency, pay_issuer}, {get_currency, get_issuer}, domain},
173 takerID ? *takerID : beast::zero,
174 bProof,
175 limit,
176 jvMarker,
177 jvResult);
178
180
181 return jvResult;
182}
183
186{
188
189 Json::Value result = RPC::lookupLedger(ledger, context);
190 if (ledger == nullptr)
191 return result;
192
193 return RPC::computeBookChanges(ledger);
194}
195
196} // namespace xrpl
Represents a JSON value.
Definition json_value.h:131
bool isObjectOrNull() const
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.
Stream info() const
Definition Journal.h:307
virtual JobQueue & getJobQueue()=0
int getJobCountGE(JobType t) const
All waiting jobs at or greater than this priority.
Definition JobQueue.cpp:126
virtual void getBookPage(std::shared_ptr< ReadView const > &lpLedger, Book const &book, AccountID const &uTakerID, bool const bProof, unsigned int iLimit, Json::Value const &jvMarker, Json::Value &jvResult)=0
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:472
@ nullValue
'null' value
Definition json_value.h:20
static LimitRange constexpr bookOffers
Limits for the book_offers command.
Json::Value invalid_field_error(std::string const &name)
Definition ErrorCodes.h:269
Json::Value computeBookChanges(std::shared_ptr< L const > const &lpAccepted)
Definition BookChanges.h:28
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
Json::Value object_field_error(std::string const &name)
Definition ErrorCodes.h:245
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.
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
Charge const feeMediumBurdenRPC
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
bool isXRP(AccountID const &c)
Definition AccountID.h:71
Json::Value doBookChanges(RPC::JsonContext &context)
Json::Value doBookOffers(RPC::JsonContext &context)
@ jtCLIENT
Definition Job.h:25
Json::Value rpcError(error_code_i iError)
Definition RPCErr.cpp:12
bool to_issuer(AccountID &, std::string const &)
Convert hex or base58 string to AccountID.
AccountID const & noAccount()
A placeholder for empty accounts.
AccountID const & xrpAccount()
Compute AccountID from public key.
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:62
@ rpcSRC_CUR_MALFORMED
Definition ErrorCodes.h:105
@ rpcTOO_BUSY
Definition ErrorCodes.h:37
@ rpcBAD_MARKET
Definition ErrorCodes.h:78
@ rpcSRC_ISR_MALFORMED
Definition ErrorCodes.h:106
@ rpcDST_AMT_MALFORMED
Definition ErrorCodes.h:87
@ rpcDOMAIN_MALFORMED
Definition ErrorCodes.h:139
@ rpcDST_ISR_MALFORMED
Definition ErrorCodes.h:89
beast::Journal const j
Definition Context.h:21
Application & app
Definition Context.h:22
Resource::Charge & loadType
Definition Context.h:23
NetworkOPs & netOps
Definition Context.h:24
Json::Value params
Definition Context.h:44