xrpld
Loading...
Searching...
No Matches
TrustAndBalance_test.cpp
1#include <test/jtx/Env.h>
2#include <test/jtx/TestHelpers.h>
3#include <test/jtx/WSClient.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/balance.h>
6#include <test/jtx/jtx_json.h>
7#include <test/jtx/paths.h>
8#include <test/jtx/pay.h>
9#include <test/jtx/rate.h>
10#include <test/jtx/sendmax.h>
11#include <test/jtx/ter.h>
12#include <test/jtx/trust.h>
13
14#include <xrpl/basics/strHex.h>
15#include <xrpl/beast/unit_test/suite.h>
16#include <xrpl/json/json_value.h>
17#include <xrpl/protocol/ErrorCodes.h>
18#include <xrpl/protocol/Feature.h>
19#include <xrpl/protocol/SField.h>
20#include <xrpl/protocol/TER.h>
21#include <xrpl/protocol/jss.h>
22
23#include <chrono>
24
25namespace xrpl {
26
28{
29 void
31 {
32 testcase("Payment to Nonexistent Account");
33 using namespace test::jtx;
34
35 Env env{*this, features};
36 env(pay(env.master, "alice", XRP(1)), Ter(tecNO_DST_INSUF_XRP));
37 env.close();
38 }
39
40 void
42 {
43 testcase("Trust Nonexistent Account");
44 using namespace test::jtx;
45
46 Env env{*this};
47 Account const alice{"alice"};
48
49 env(trust(env.master, alice["USD"](100)), Ter(tecNO_DST));
50 }
51
52 void
54 {
55 testcase("Credit Limit");
56 using namespace test::jtx;
57
58 Env env{*this};
59 Account const gw{"gateway"};
60 Account const alice{"alice"};
61 Account const bob{"bob"};
62
63 env.fund(XRP(10000), gw, alice, bob);
64 env.close();
65
66 // credit limit doesn't exist yet - verify ledger_entry
67 // reflects this
68 auto jrr = ledgerEntryState(env, gw, alice, "USD");
69 BEAST_EXPECT(jrr[jss::error] == "entryNotFound");
70 BEAST_EXPECT(jrr[jss::error_code] == RpcEntryNotFound);
71 BEAST_EXPECT(jrr[jss::error_message] == "Entry not found.");
72
73 // now create a credit limit
74 env(trust(alice, gw["USD"](800)));
75
76 jrr = ledgerEntryState(env, gw, alice, "USD");
77 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "0");
78 BEAST_EXPECT(jrr[jss::node][sfHighLimit.fieldName][jss::value] == "800");
79 BEAST_EXPECT(jrr[jss::node][sfHighLimit.fieldName][jss::issuer] == alice.human());
80 BEAST_EXPECT(jrr[jss::node][sfHighLimit.fieldName][jss::currency] == "USD");
81 BEAST_EXPECT(jrr[jss::node][sfLowLimit.fieldName][jss::value] == "0");
82 BEAST_EXPECT(jrr[jss::node][sfLowLimit.fieldName][jss::issuer] == gw.human());
83 BEAST_EXPECT(jrr[jss::node][sfLowLimit.fieldName][jss::currency] == "USD");
84
85 // modify the credit limit
86 env(trust(alice, gw["USD"](700)));
87
88 jrr = ledgerEntryState(env, gw, alice, "USD");
89 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "0");
90 BEAST_EXPECT(jrr[jss::node][sfHighLimit.fieldName][jss::value] == "700");
91 BEAST_EXPECT(jrr[jss::node][sfHighLimit.fieldName][jss::issuer] == alice.human());
92 BEAST_EXPECT(jrr[jss::node][sfHighLimit.fieldName][jss::currency] == "USD");
93 BEAST_EXPECT(jrr[jss::node][sfLowLimit.fieldName][jss::value] == "0");
94 BEAST_EXPECT(jrr[jss::node][sfLowLimit.fieldName][jss::issuer] == gw.human());
95 BEAST_EXPECT(jrr[jss::node][sfLowLimit.fieldName][jss::currency] == "USD");
96
97 // set negative limit - expect failure
98 env(trust(alice, gw["USD"](-1)), Ter(temBAD_LIMIT));
99
100 // set zero limit
101 env(trust(alice, gw["USD"](0)));
102
103 // ensure line is deleted
104 jrr = ledgerEntryState(env, gw, alice, "USD");
105 BEAST_EXPECT(jrr[jss::error] == "entryNotFound");
106 BEAST_EXPECT(jrr[jss::error_code] == RpcEntryNotFound);
107 BEAST_EXPECT(jrr[jss::error_message] == "Entry not found.");
108
109 // TODO Check in both owner books.
110
111 // set another credit limit
112 env(trust(alice, bob["USD"](600)));
113
114 // set limit on other side
115 env(trust(bob, alice["USD"](500)));
116
117 // check the ledger state for the trust line
118 jrr = ledgerEntryState(env, alice, bob, "USD");
119 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "0");
120 BEAST_EXPECT(jrr[jss::node][sfHighLimit.fieldName][jss::value] == "500");
121 BEAST_EXPECT(jrr[jss::node][sfHighLimit.fieldName][jss::issuer] == bob.human());
122 BEAST_EXPECT(jrr[jss::node][sfHighLimit.fieldName][jss::currency] == "USD");
123 BEAST_EXPECT(jrr[jss::node][sfLowLimit.fieldName][jss::value] == "600");
124 BEAST_EXPECT(jrr[jss::node][sfLowLimit.fieldName][jss::issuer] == alice.human());
125 BEAST_EXPECT(jrr[jss::node][sfLowLimit.fieldName][jss::currency] == "USD");
126 }
127
128 void
130 {
131 testcase("Direct Payment, Ripple");
132 using namespace test::jtx;
133
134 Env env{*this, features};
135 Account const alice{"alice"};
136 Account const bob{"bob"};
137
138 env.fund(XRP(10000), alice, bob);
139 env.close();
140
141 env(trust(alice, bob["USD"](600)));
142 env(trust(bob, alice["USD"](700)));
143
144 // alice sends bob partial with alice as issuer
145 env(pay(alice, bob, alice["USD"](24)));
146 env.require(Balance(bob, alice["USD"](24)));
147
148 // alice sends bob more with bob as issuer
149 env(pay(alice, bob, bob["USD"](33)));
150 env.require(Balance(bob, alice["USD"](57)));
151
152 // bob sends back more than sent
153 env(pay(bob, alice, bob["USD"](90)));
154 env.require(Balance(bob, alice["USD"](-33)));
155
156 // alice sends to her limit
157 env(pay(alice, bob, bob["USD"](733)));
158 env.require(Balance(bob, alice["USD"](700)));
159
160 // bob sends to his limit
161 env(pay(bob, alice, bob["USD"](1300)));
162 env.require(Balance(bob, alice["USD"](-600)));
163
164 // bob sends past limit
165 env(pay(bob, alice, bob["USD"](1)), Ter(tecPATH_DRY));
166 env.require(Balance(bob, alice["USD"](-600)));
167 }
168
169 void
170 testWithTransferFee(bool subscribe, bool withRate, FeatureBitset features)
171 {
172 testcase(
173 std::string("Direct Payment: ") + (withRate ? "With " : "Without ") + " Xfer Fee, " +
174 (subscribe ? "With " : "Without ") + " Subscribe");
175 using namespace test::jtx;
176
177 Env env{*this, features};
178 auto wsc = test::makeWSClient(env.app().config());
179 Account const gw{"gateway"};
180 Account const alice{"alice"};
181 Account const bob{"bob"};
182
183 env.fund(XRP(10000), gw, alice, bob);
184 env.close();
185
186 env(trust(alice, gw["AUD"](100)));
187 env(trust(bob, gw["AUD"](100)));
188
189 env(pay(gw, alice, alice["AUD"](1)));
190 env.close();
191
192 env.require(Balance(alice, gw["AUD"](1)));
193
194 // alice sends bob 1 AUD
195 env(pay(alice, bob, gw["AUD"](1)));
196 env.close();
197
198 env.require(Balance(alice, gw["AUD"](0)));
199 env.require(Balance(bob, gw["AUD"](1)));
200 env.require(Balance(gw, bob["AUD"](-1)));
201
202 if (withRate)
203 {
204 // set a transfer rate
205 env(rate(gw, 1.1));
206 env.close();
207 // bob sends alice 0.5 AUD with a max to spend
208 env(pay(bob, alice, gw["AUD"](0.5)), Sendmax(gw["AUD"](0.55)));
209 }
210 else
211 {
212 // bob sends alice 0.5 AUD
213 env(pay(bob, alice, gw["AUD"](0.5)));
214 }
215
216 env.require(Balance(alice, gw["AUD"](0.5)));
217 env.require(Balance(bob, gw["AUD"](withRate ? 0.45 : 0.5)));
218 env.require(Balance(gw, bob["AUD"](withRate ? -0.45 : -0.5)));
219
220 if (subscribe)
221 {
222 json::Value jvs;
223 jvs[jss::accounts] = json::ValueType::Array;
224 jvs[jss::accounts].append(gw.human());
225 jvs[jss::streams] = json::ValueType::Array;
226 jvs[jss::streams].append("transactions");
227 jvs[jss::streams].append("ledger");
228 auto jv = wsc->invoke("subscribe", jvs);
229 BEAST_EXPECT(jv[jss::status] == "success");
230
231 env.close();
232
233 using namespace std::chrono_literals;
234 BEAST_EXPECT(wsc->findMsg(5s, [](auto const& jval) {
235 auto const& t = jval[jss::transaction];
236 return t[jss::TransactionType] == jss::Payment;
237 }));
238 BEAST_EXPECT(wsc->findMsg(
239 5s, [](auto const& jval) { return jval[jss::type] == "ledgerClosed"; }));
240
241 BEAST_EXPECT(wsc->invoke("unsubscribe", jv)[jss::status] == "success");
242 }
243 }
244
245 void
247 {
248 testcase("Payments With Paths and Fees");
249 using namespace test::jtx;
250
251 Env env{*this, features};
252 Account const gw{"gateway"};
253 Account const alice{"alice"};
254 Account const bob{"bob"};
255
256 env.fund(XRP(10000), gw, alice, bob);
257 env.close();
258
259 // set a transfer rate
260 env(rate(gw, 1.1));
261
262 env(trust(alice, gw["AUD"](100)));
263 env(trust(bob, gw["AUD"](100)));
264
265 env(pay(gw, alice, alice["AUD"](4.4)));
266 env.require(Balance(alice, gw["AUD"](4.4)));
267
268 // alice sends gw issues to bob with a max spend that allows for the
269 // xfer rate
270 env(pay(alice, bob, gw["AUD"](1)), Sendmax(gw["AUD"](1.1)));
271 env.require(Balance(alice, gw["AUD"](3.3)));
272 env.require(Balance(bob, gw["AUD"](1)));
273
274 // alice sends bob issues to bob with a max spend
275 env(pay(alice, bob, bob["AUD"](1)), Sendmax(gw["AUD"](1.1)));
276 env.require(Balance(alice, gw["AUD"](2.2)));
277 env.require(Balance(bob, gw["AUD"](2)));
278
279 // alice sends gw issues to bob with a max spend
280 env(pay(alice, bob, gw["AUD"](1)), Sendmax(alice["AUD"](1.1)));
281 env.require(Balance(alice, gw["AUD"](1.1)));
282 env.require(Balance(bob, gw["AUD"](3)));
283
284 // alice sends bob issues to bob with a max spend in alice issues.
285 // expect fail since gw is not involved
286 env(pay(alice, bob, bob["AUD"](1)), Sendmax(alice["AUD"](1.1)), Ter(tecPATH_DRY));
287
288 env.require(Balance(alice, gw["AUD"](1.1)));
289 env.require(Balance(bob, gw["AUD"](3)));
290 }
291
292 void
294 {
295 testcase("Indirect Payment");
296 using namespace test::jtx;
297
298 Env env{*this, features};
299 Account const gw{"gateway"};
300 Account const alice{"alice"};
301 Account const bob{"bob"};
302
303 env.fund(XRP(10000), gw, alice, bob);
304 env.close();
305
306 env(trust(alice, gw["USD"](600)));
307 env(trust(bob, gw["USD"](700)));
308
309 env(pay(gw, alice, alice["USD"](70)));
310 env(pay(gw, bob, bob["USD"](50)));
311
312 env.require(Balance(alice, gw["USD"](70)));
313 env.require(Balance(bob, gw["USD"](50)));
314
315 // alice sends more than has to issuer: 100 out of 70
316 env(pay(alice, gw, gw["USD"](100)), Ter(tecPATH_PARTIAL));
317
318 // alice sends more than has to bob: 100 out of 70
319 env(pay(alice, bob, gw["USD"](100)), Ter(tecPATH_PARTIAL));
320
321 env.close();
322
323 env.require(Balance(alice, gw["USD"](70)));
324 env.require(Balance(bob, gw["USD"](50)));
325
326 // send with an account path
327 env(pay(alice, bob, gw["USD"](5)), test::jtx::Path(gw));
328
329 env.require(Balance(alice, gw["USD"](65)));
330 env.require(Balance(bob, gw["USD"](55)));
331 }
332
333 void
334 testIndirectMultiPath(bool withRate, FeatureBitset features)
335 {
336 testcase(
337 std::string("Indirect Payment, Multi Path, ") + (withRate ? "With " : "Without ") +
338 " Xfer Fee, ");
339 using namespace test::jtx;
340
341 Env env{*this, features};
342 Account const gw{"gateway"};
343 Account const amazon{"amazon"};
344 Account const alice{"alice"};
345 Account const bob{"bob"};
346 Account const carol{"carol"};
347
348 env.fund(XRP(10000), gw, amazon, alice, bob, carol);
349 env.close();
350
351 env(trust(amazon, gw["USD"](2000)));
352 env(trust(bob, alice["USD"](600)));
353 env(trust(bob, gw["USD"](1000)));
354 env(trust(carol, alice["USD"](700)));
355 env(trust(carol, gw["USD"](1000)));
356
357 if (withRate)
358 env(rate(gw, 1.1));
359
360 env(pay(gw, bob, bob["USD"](100)));
361 env(pay(gw, carol, carol["USD"](100)));
362 env.close();
363
364 // alice pays amazon via multiple paths
365 if (withRate)
366 {
367 env(pay(alice, amazon, gw["USD"](150)),
368 Sendmax(alice["USD"](200)),
369 test::jtx::Path(bob),
370 test::jtx::Path(carol));
371 }
372 else
373 {
374 env(pay(alice, amazon, gw["USD"](150)), test::jtx::Path(bob), test::jtx::Path(carol));
375 }
376
377 if (withRate)
378 {
379 env.require(Balance(
380 alice,
381 STAmount(carol["USD"], 6500000000000000ull, -14, true, STAmount::Unchecked{})));
382 env.require(Balance(carol, gw["USD"](35)));
383 }
384 else
385 {
386 env.require(Balance(alice, carol["USD"](-50)));
387 env.require(Balance(carol, gw["USD"](50)));
388 }
389 env.require(Balance(alice, bob["USD"](-100)));
390 env.require(Balance(amazon, gw["USD"](150)));
391 env.require(Balance(bob, gw["USD"](0)));
392 }
393
394 void
396 {
397 testcase("Set Invoice ID on Payment");
398 using namespace test::jtx;
399
400 Env env{*this, features};
401 Account const alice{"alice"};
402 auto wsc = test::makeWSClient(env.app().config());
403
404 env.fund(XRP(10000), alice);
405 env.close();
406
407 json::Value jvs;
408 jvs[jss::accounts] = json::ValueType::Array;
409 jvs[jss::accounts].append(env.master.human());
410 jvs[jss::streams] = json::ValueType::Array;
411 jvs[jss::streams].append("transactions");
412 BEAST_EXPECT(wsc->invoke("subscribe", jvs)[jss::status] == "success");
413
414 char const* invoiceId = "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89";
415
416 json::Value jv;
417 auto const tx =
418 env.jt(pay(env.master, alice, XRP(10000)), Json(sfInvoiceID.fieldName, invoiceId));
419 jv[jss::tx_blob] = strHex(tx.stx->getSerializer().slice());
420 auto jrr = wsc->invoke("submit", jv)[jss::result];
421 BEAST_EXPECT(jrr[jss::status] == "success");
422 BEAST_EXPECT(jrr[jss::tx_json][sfInvoiceID.fieldName] == invoiceId);
423 env.close();
424
425 using namespace std::chrono_literals;
426 BEAST_EXPECT(wsc->findMsg(2s, [invoiceId](auto const& jval) {
427 auto const& t = jval[jss::transaction];
428 return t[jss::TransactionType] == jss::Payment && t[sfInvoiceID.fieldName] == invoiceId;
429 }));
430
431 BEAST_EXPECT(wsc->invoke("unsubscribe", jv)[jss::status] == "success");
432 }
433
434public:
435 void
436 run() override
437 {
440
441 auto const testWithFeatures = [this](FeatureBitset features) {
442 testPayNonexistent(features);
443 testDirectRipple(features);
444 testWithTransferFee(false, false, features);
445 testWithTransferFee(false, true, features);
446 testWithTransferFee(true, false, features);
447 testWithTransferFee(true, true, features);
448 testWithPath(features);
449 testIndirect(features);
450 testIndirectMultiPath(true, features);
451 testIndirectMultiPath(false, features);
452 testInvoiceID(features);
453 };
454
455 using namespace test::jtx;
456 auto const sa = testableAmendments();
457 testWithFeatures(sa - featurePermissionedDEX);
458 testWithFeatures(sa);
459 }
460};
461
462BEAST_DEFINE_TESTSUITE(TrustAndBalance, app, xrpl);
463
464} // namespace xrpl
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
Value & append(Value const &value)
Append value to array at the end.
void testIndirectMultiPath(bool withRate, FeatureBitset features)
void testInvoiceID(FeatureBitset features)
void testWithTransferFee(bool subscribe, bool withRate, FeatureBitset features)
void testPayNonexistent(FeatureBitset features)
void run() override
Runs the suite.
void testWithPath(FeatureBitset features)
void testDirectRipple(FeatureBitset features)
void testIndirect(FeatureBitset features)
Add a path.
Definition paths.h:47
@ Array
array value (ordered list)
Definition json_value.h:32
std::unique_ptr< WSClient > makeWSClient(Config const &cfg, bool v2, unsigned rpcVersion, std::unordered_map< std::string, std::string > const &headers)
Returns a client operating through WebSockets/S.
Definition WSClient.cpp:373
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcEntryNotFound
Definition ErrorCodes.h:144
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
@ temBAD_LIMIT
Definition TER.h:82
@ tecPATH_PARTIAL
Definition TER.h:293
@ tecPATH_DRY
Definition TER.h:305
@ tecNO_DST_INSUF_XRP
Definition TER.h:302
@ tecNO_DST
Definition TER.h:301
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)