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