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