xrpld
Loading...
Searching...
No Matches
AccountOffers_test.cpp
1
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/envconfig.h>
7#include <test/jtx/offer.h>
8#include <test/jtx/owners.h> // IWYU pragma: keep
9#include <test/jtx/pay.h>
10
11#include <xrpl/beast/unit_test/suite.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/json/to_string.h>
14#include <xrpl/protocol/ErrorCodes.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/jss.h>
17
18namespace xrpl::test {
19
21{
22public:
23 // test helper
24 static bool
26 {
27 return val.isMember(jss::marker) && val[jss::marker].isString() &&
28 !val[jss::marker].asString().empty();
29 }
30
31 void
33 {
34 testcase("Non-Admin Min Limit");
35
36 using namespace jtx;
37 Env env{*this, envconfig(noAdmin)};
38 Account const gw("G1");
39 auto const usdGw = gw["USD"];
40 Account const bob("bob");
41 auto const usdBob = bob["USD"];
42
43 env.fund(XRP(10000), gw, bob);
44 env.trust(usdGw(1000), bob);
45
46 // this is to provide some USD from gw in the
47 // bob account so that it can rightly
48 // make offers that give those USDs
49 env(pay(gw, bob, usdGw(10)));
50 unsigned const offerCount = 12u;
51 for (auto i = 0u; i < offerCount; i++)
52 {
53 json::Value jvo = offer(bob, XRP(100 + i), usdGw(1));
54 jvo[sfExpiration.fieldName] = 10000000u;
55 env(jvo);
56 }
57
58 // make non-limited RPC call
59 auto const jroNl = env.rpc("account_offers", bob.human())[jss::result][jss::offers];
60 BEAST_EXPECT(checkArraySize(jroNl, offerCount));
61
62 // now make a low-limit query, should get "corrected"
63 // to a min of 10 results with a marker set since there
64 // are more than 10 total
65 json::Value jvParams;
66 jvParams[jss::account] = bob.human();
67 jvParams[jss::limit] = 1u;
68 auto const jrrL = env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
69 auto const& jroL = jrrL[jss::offers];
70 BEAST_EXPECT(checkMarker(jrrL));
71 // 9u is the expected size, since one account object is a trustline
72 BEAST_EXPECT(checkArraySize(jroL, 9u));
73 }
74
75 void
76 testSequential(bool asAdmin)
77 {
78 testcase(std::string("Sequential - ") + (asAdmin ? "admin" : "non-admin"));
79
80 using namespace jtx;
81 Env env{*this, asAdmin ? envconfig() : envconfig(noAdmin)};
82 Account const gw("G1");
83 auto const usdGw = gw["USD"];
84 Account const bob("bob");
85 auto const usdBob = bob["USD"];
86
87 env.fund(XRP(10000), gw, bob);
88 env.trust(usdGw(1000), bob);
89
90 // this is to provide some USD from gw in the
91 // bob account so that it can rightly
92 // make offers that give those USDs
93 env(pay(gw, bob, usdGw(10)));
94
95 env(offer(bob, XRP(100), usdBob(1)));
96 env(offer(bob, XRP(200), usdGw(2)));
97 env(offer(bob, XRP(30), usdGw(6)));
98
99 // make the RPC call
100 auto const jroOuter = env.rpc("account_offers", bob.human())[jss::result][jss::offers];
101 if (BEAST_EXPECT(checkArraySize(jroOuter, 3u)))
102 {
103 // Note that the returned offers are sorted by index, not by
104 // order of insertion or by sequence number. There is no
105 // guarantee that their order will not change in the future
106 // if the sequence numbers or the account IDs change.
107 BEAST_EXPECT(jroOuter[0u][jss::quality] == "100000000");
108 BEAST_EXPECT(jroOuter[0u][jss::taker_gets][jss::currency] == "USD");
109 BEAST_EXPECT(jroOuter[0u][jss::taker_gets][jss::issuer] == gw.human());
110 BEAST_EXPECT(jroOuter[0u][jss::taker_gets][jss::value] == "2");
111 BEAST_EXPECT(jroOuter[0u][jss::taker_pays] == "200000000");
112
113 BEAST_EXPECT(jroOuter[1u][jss::quality] == "100000000");
114 BEAST_EXPECT(jroOuter[1u][jss::taker_gets][jss::currency] == "USD");
115 BEAST_EXPECT(jroOuter[1u][jss::taker_gets][jss::issuer] == bob.human());
116 BEAST_EXPECT(jroOuter[1u][jss::taker_gets][jss::value] == "1");
117 BEAST_EXPECT(jroOuter[1u][jss::taker_pays] == "100000000");
118
119 BEAST_EXPECT(jroOuter[2u][jss::quality] == "5000000");
120 BEAST_EXPECT(jroOuter[2u][jss::taker_gets][jss::currency] == "USD");
121 BEAST_EXPECT(jroOuter[2u][jss::taker_gets][jss::issuer] == gw.human());
122 BEAST_EXPECT(jroOuter[2u][jss::taker_gets][jss::value] == "6");
123 BEAST_EXPECT(jroOuter[2u][jss::taker_pays] == "30000000");
124 }
125
126 {
127 // now make a limit (= 1) query for the same data
128 json::Value jvParams;
129 jvParams[jss::account] = bob.human();
130 jvParams[jss::limit] = 1u;
131 auto const jrrL1 =
132 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
133 auto const& jroL1 = jrrL1[jss::offers];
134 // there is a difference in the validation of the limit param
135 // between admin and non-admin requests. with admin requests, the
136 // limit parameter is NOT subject to sane defaults, but with a
137 // non-admin there are pre-configured limit ranges applied. That's
138 // why we have different BEAST_EXPECT()s here for the two scenarios
139 BEAST_EXPECT(checkArraySize(jroL1, asAdmin ? 1u : 3u));
140 BEAST_EXPECT(asAdmin ? checkMarker(jrrL1) : (!jrrL1.isMember(jss::marker)));
141 if (asAdmin)
142 {
143 BEAST_EXPECT(jroOuter[0u] == jroL1[0u]);
144
145 // second item...with previous marker passed
146 jvParams[jss::marker] = jrrL1[jss::marker];
147 auto const jrrL2 =
148 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
149 auto const& jroL2 = jrrL2[jss::offers];
150 BEAST_EXPECT(checkMarker(jrrL2));
151 BEAST_EXPECT(checkArraySize(jroL2, 1u));
152 BEAST_EXPECT(jroOuter[1u] == jroL2[0u]);
153
154 // last item...with previous marker passed
155 jvParams[jss::marker] = jrrL2[jss::marker];
156 jvParams[jss::limit] = 10u;
157 auto const jrrL3 =
158 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
159 auto const& jroL3 = jrrL3[jss::offers];
160 BEAST_EXPECT(!jrrL3.isMember(jss::marker));
161 BEAST_EXPECT(checkArraySize(jroL3, 1u));
162 BEAST_EXPECT(jroOuter[2u] == jroL3[0u]);
163 }
164 else
165 {
166 BEAST_EXPECT(jroOuter == jroL1);
167 }
168 }
169
170 {
171 json::Value jvParams;
172 jvParams[jss::account] = bob.human();
173 jvParams[jss::limit] = 0u;
174 auto const jrr =
175 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
176 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
177 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
178 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'limit'.");
179 }
180 }
181
182 void
184 {
185 testcase("Bad input");
186
187 using namespace jtx;
188 Env env(*this);
189 Account const gw("G1");
190 auto const usdGw = gw["USD"];
191 Account const bob("bob");
192 auto const usdBob = bob["USD"];
193
194 env.fund(XRP(10000), gw, bob);
195 env.trust(usdGw(1000), bob);
196
197 {
198 // no account field
199 auto const jrr = env.rpc("account_offers");
200 BEAST_EXPECT(jrr[jss::status] == "error");
201 BEAST_EXPECT(jrr[jss::error] == "badSyntax");
202 BEAST_EXPECT(jrr[jss::error_code] == RpcBadSyntax);
203 BEAST_EXPECT(jrr[jss::error_message] == "Syntax error.");
204 }
205
206 {
207 // test account non-string
208 auto const testInvalidAccountParam = [&](auto const& param) {
209 json::Value params;
210 params[jss::account] = param;
211 auto jrr = env.rpc("json", "account_offers", to_string(params))[jss::result];
212 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
213 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
214 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'account'.");
215 };
216
217 testInvalidAccountParam(1);
218 testInvalidAccountParam(1.1);
219 testInvalidAccountParam(true);
220 testInvalidAccountParam(json::Value(json::ValueType::Null));
221 testInvalidAccountParam(json::Value(json::ValueType::Object));
222 testInvalidAccountParam(json::Value(json::ValueType::Array));
223 }
224
225 {
226 // empty string account
227 json::Value jvParams;
228 jvParams[jss::account] = "";
229 auto const jrr =
230 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
231 BEAST_EXPECT(jrr[jss::status] == "error");
232 BEAST_EXPECT(jrr[jss::error] == "actMalformed");
233 BEAST_EXPECT(jrr[jss::error_code] == RpcActMalformed);
234 BEAST_EXPECT(jrr[jss::error_message] == "Account malformed.");
235 BEAST_EXPECT(jrr.isMember(jss::ledger_current_index));
236 BEAST_EXPECT(jrr[jss::validated] == false);
237 }
238
239 {
240 // bogus account value
241 auto const jrr = env.rpc("account_offers", Account("bogus").human())[jss::result];
242 BEAST_EXPECT(jrr[jss::status] == "error");
243 BEAST_EXPECT(jrr[jss::error] == "actNotFound");
244 BEAST_EXPECT(jrr[jss::error_code] == RpcActNotFound);
245 BEAST_EXPECT(jrr[jss::error_message] == "Account not found.");
246 }
247
248 {
249 // bad limit
250 json::Value jvParams;
251 jvParams[jss::account] = bob.human();
252 jvParams[jss::limit] = "0"; // NOT an integer
253 auto const jrr =
254 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
255 BEAST_EXPECT(jrr[jss::status] == "error");
256 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
257 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
258 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'limit', not unsigned integer.");
259 }
260
261 {
262 // invalid marker
263 json::Value jvParams;
264 jvParams[jss::account] = bob.human();
265 jvParams[jss::marker] = "NOT_A_MARKER";
266 auto const jrr =
267 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
268 BEAST_EXPECT(jrr[jss::status] == "error");
269 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
270 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
271 BEAST_EXPECTS(
272 jrr[jss::error_message] == "Invalid field 'marker'.", jrr.toStyledString());
273 }
274
275 {
276 // invalid marker - not a string
277 json::Value jvParams;
278 jvParams[jss::account] = bob.human();
279 jvParams[jss::marker] = 1;
280 auto const jrr =
281 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
282 BEAST_EXPECT(jrr[jss::status] == "error");
283 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
284 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
285 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'marker', not string.");
286 }
287
288 {
289 // ask for a bad ledger index
290 json::Value jvParams;
291 jvParams[jss::account] = bob.human();
292 jvParams[jss::ledger_index] = 10u;
293 auto const jrr =
294 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
295 BEAST_EXPECT(jrr[jss::status] == "error");
296 BEAST_EXPECT(jrr[jss::error] == "lgrNotFound");
297 BEAST_EXPECT(jrr[jss::error_code] == RpcLgrNotFound);
298 BEAST_EXPECT(jrr[jss::error_message] == "ledgerNotFound");
299 }
300
301 {
302 // bad limit is reported before a malformed account
303 json::Value jvParams;
304 jvParams[jss::account] = "";
305 jvParams[jss::limit] = "0";
306 auto const jrr =
307 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
308 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
309 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
310 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'limit', not unsigned integer.");
311 }
312
313 {
314 // bad limit is reported before a nonexistent account
315 json::Value jvParams;
316 jvParams[jss::account] = Account("bogus").human();
317 jvParams[jss::limit] = "0";
318 auto const jrr =
319 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
320 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
321 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
322 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'limit', not unsigned integer.");
323 }
324
325 {
326 // invalid marker is reported before a missing ledger
327 json::Value jvParams;
328 jvParams[jss::account] = bob.human();
329 jvParams[jss::ledger_index] = 10u;
330 jvParams[jss::marker] = "NOT_A_MARKER";
331 auto const jrr =
332 env.rpc("json", "account_offers", jvParams.toStyledString())[jss::result];
333 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
334 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
335 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'marker'.");
336 }
337 }
338
339 void
340 run() override
341 {
342 testSequential(true);
343 testSequential(false);
344 testBadInput();
346 }
347};
348
350
351} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
std::string toStyledString() 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.
static bool checkMarker(json::Value const &val)
void run() override
Runs the suite.
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
A transaction testing environment.
Definition Env.h:161
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:1058
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:354
T empty(T... args)
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
@ Null
'null' value
Definition json_value.h:26
The api_version numbers this server serves.
Definition ApiVersion.h:26
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
std::unique_ptr< Config > noAdmin(std::unique_ptr< Config >)
adjust config so no admin ports are enabled
Definition envconfig.cpp:76
bool checkArraySize(json::Value const &val, unsigned int size)
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcLgrNotFound
Definition ErrorCodes.h:55
@ RpcActNotFound
Definition ErrorCodes.h:53
@ RpcActMalformed
Definition ErrorCodes.h:73
@ RpcInvalidParams
Definition ErrorCodes.h:67
@ RpcBadSyntax
Definition ErrorCodes.h:29
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658