xrpld
Loading...
Searching...
No Matches
Offer_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/PathSet.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/WSClient.h>
6#include <test/jtx/acctdelete.h>
7#include <test/jtx/amount.h>
8#include <test/jtx/balance.h>
9#include <test/jtx/fee.h>
10#include <test/jtx/flags.h>
11#include <test/jtx/jtx_json.h>
12#include <test/jtx/noop.h>
13#include <test/jtx/offer.h>
14#include <test/jtx/owners.h>
15#include <test/jtx/paths.h>
16#include <test/jtx/pay.h>
17#include <test/jtx/quality.h>
18#include <test/jtx/rate.h>
19#include <test/jtx/require.h>
20#include <test/jtx/sendmax.h>
21#include <test/jtx/tags.h>
22#include <test/jtx/ter.h>
23#include <test/jtx/ticket.h>
24#include <test/jtx/trust.h>
25#include <test/jtx/txflags.h>
26
27#include <xrpl/beast/unit_test/suite.h>
28#include <xrpl/core/ServiceRegistry.h>
29#include <xrpl/json/json_value.h>
30#include <xrpl/json/to_string.h>
31#include <xrpl/ledger/helpers/DirectoryHelpers.h>
32#include <xrpl/protocol/AccountID.h>
33#include <xrpl/protocol/ErrorCodes.h>
34#include <xrpl/protocol/Feature.h>
35#include <xrpl/protocol/Indexes.h>
36#include <xrpl/protocol/Issue.h>
37#include <xrpl/protocol/LedgerFormats.h>
38#include <xrpl/protocol/Quality.h>
39#include <xrpl/protocol/SField.h>
40#include <xrpl/protocol/STAmount.h>
41#include <xrpl/protocol/Seed.h>
42#include <xrpl/protocol/SeqProxy.h>
43#include <xrpl/protocol/TER.h>
44#include <xrpl/protocol/TxFlags.h>
45#include <xrpl/protocol/UintTypes.h>
46#include <xrpl/protocol/XRPAmount.h>
47#include <xrpl/protocol/jss.h>
48
49#include <algorithm>
50#include <cstddef>
51#include <cstdint>
52#include <iterator>
53#include <map>
54#include <string>
55#include <utility>
56#include <vector>
57
58namespace xrpl::test {
59
61{
62 static XRPAmount
64 {
65 return env.current()->fees().accountReserve(count, 1);
66 }
67
68 static std::uint32_t
70 {
71 return env.current()->header().parentCloseTime.time_since_epoch().count();
72 }
73
74 static auto
76 {
77 json::Value jvParams;
78 jvParams[jss::offer][jss::account] = acct.human();
79 jvParams[jss::offer][jss::seq] = offerSeq;
80 return env.rpc("json", "ledger_entry", to_string(jvParams))[jss::result];
81 }
82
83 static auto
84 getBookOffers(jtx::Env& env, Issue const& takerPays, Issue const& takerGets)
85 {
86 json::Value jvbp;
87 jvbp[jss::ledger_index] = "current";
88 jvbp[jss::taker_pays][jss::currency] = to_string(takerPays.currency);
89 jvbp[jss::taker_pays][jss::issuer] = to_string(takerPays.account);
90 jvbp[jss::taker_gets][jss::currency] = to_string(takerGets.currency);
91 jvbp[jss::taker_gets][jss::issuer] = to_string(takerGets.account);
92 return env.rpc("json", "book_offers", to_string(jvbp))[jss::result];
93 }
94
95public:
96 void
98 {
99 testcase("Incorrect Removal of Funded Offers");
100
101 // We need at least two paths. One at good quality and one at bad
102 // quality. The bad quality path needs two offer books in a row.
103 // Each offer book should have two offers at the same quality, the
104 // offers should be completely consumed, and the payment should
105 // should require both offers to be satisfied. The first offer must
106 // be "taker gets" XRP. Old, broken would remove the first
107 // "taker gets" xrp offer, even though the offer is still funded and
108 // not used for the payment.
109
110 using namespace jtx;
111 Env env{*this, features};
112
113 auto const gw = Account{"gateway"};
114 auto const usd = gw["USD"];
115 auto const btc = gw["BTC"];
116 Account const alice{"alice"};
117 Account const bob{"bob"};
118 Account const carol{"carol"};
119
120 env.fund(XRP(10000), alice, bob, carol, gw);
121 env.close();
122 env.trust(usd(1000), alice, bob, carol);
123 env.trust(btc(1000), alice, bob, carol);
124
125 env(pay(gw, alice, btc(1000)));
126
127 env(pay(gw, carol, usd(1000)));
128 env(pay(gw, carol, btc(1000)));
129
130 // Must be two offers at the same quality
131 // "taker gets" must be XRP
132 // (Different amounts so I can distinguish the offers)
133 env(offer(carol, btc(49), XRP(49)));
134 env(offer(carol, btc(51), XRP(51)));
135
136 // Offers for the poor quality path
137 // Must be two offers at the same quality
138 env(offer(carol, XRP(50), usd(50)));
139 env(offer(carol, XRP(50), usd(50)));
140
141 // Offers for the good quality path
142 env(offer(carol, btc(1), usd(100)));
143
144 PathSet const paths(TestPath(XRP, usd), TestPath(usd));
145
146 env(pay(alice, bob, usd(100)),
147 Json(paths.json()),
148 Sendmax(btc(1000)),
149 Txflags(tfPartialPayment));
150
151 env.require(Balance(bob, usd(100)));
152 BEAST_EXPECT(
153 !isOffer(env, carol, btc(1), usd(100)) && isOffer(env, carol, btc(49), XRP(49)));
154 }
155
156 void
158 {
159 testcase("Removing Canceled Offers");
160
161 using namespace jtx;
162 Env env{*this, features};
163
164 auto const gw = Account{"gateway"};
165 auto const alice = Account{"alice"};
166 auto const usd = gw["USD"];
167
168 env.fund(XRP(10000), alice, gw);
169 env.close();
170 env.trust(usd(100), alice);
171 env.close();
172
173 env(pay(gw, alice, usd(50)));
174 env.close();
175
176 auto const offer1Seq = env.seq(alice);
177
178 env(offer(alice, XRP(500), usd(100)), Require(Offers(alice, 1)));
179 env.close();
180
181 BEAST_EXPECT(isOffer(env, alice, XRP(500), usd(100)));
182
183 // cancel the offer above and replace it with a new offer
184 auto const offer2Seq = env.seq(alice);
185
186 env(offer(alice, XRP(300), usd(100)),
187 Json(jss::OfferSequence, offer1Seq),
188 Require(Offers(alice, 1)));
189 env.close();
190
191 BEAST_EXPECT(
192 isOffer(env, alice, XRP(300), usd(100)) && !isOffer(env, alice, XRP(500), usd(100)));
193
194 // Test canceling non-existent offer.
195 // auto const offer3Seq = env.seq (alice);
196
197 env(offer(alice, XRP(400), usd(200)),
198 Json(jss::OfferSequence, offer1Seq),
199 Require(Offers(alice, 2)));
200 env.close();
201
202 BEAST_EXPECT(
203 isOffer(env, alice, XRP(300), usd(100)) && isOffer(env, alice, XRP(400), usd(200)));
204
205 // Test cancellation now with OfferCancel tx
206 auto const offer4Seq = env.seq(alice);
207 env(offer(alice, XRP(222), usd(111)), Require(Offers(alice, 3)));
208 env.close();
209
210 BEAST_EXPECT(isOffer(env, alice, XRP(222), usd(111)));
211 env(offerCancel(alice, offer4Seq));
212 env.close();
213 BEAST_EXPECT(env.seq(alice) == offer4Seq + 2);
214
215 BEAST_EXPECT(!isOffer(env, alice, XRP(222), usd(111)));
216
217 // Create an offer that both fails with a tecEXPIRED code and removes
218 // an offer. Show that the attempt to remove the offer fails.
219 env.require(Offers(alice, 2));
220
221 env(offer(alice, XRP(5), usd(2)),
222 Json(sfExpiration.fieldName, lastClose(env)),
223 Json(jss::OfferSequence, offer2Seq),
224 Ter(tecEXPIRED));
225 env.close();
226
227 env.require(Offers(alice, 2));
228 BEAST_EXPECT(isOffer(env, alice, XRP(300), usd(100))); // offer2
229 BEAST_EXPECT(!isOffer(env, alice, XRP(5), usd(2))); // expired
230 }
231
232 void
234 {
235 testcase("Tiny payments");
236
237 // Regression test for tiny payments
238 using namespace jtx;
239 using namespace std::chrono_literals;
240 auto const alice = Account{"alice"};
241 auto const bob = Account{"bob"};
242 auto const carol = Account{"carol"};
243 auto const gw = Account{"gw"};
244
245 auto const usd = gw["USD"];
246 auto const eur = gw["EUR"];
247
248 Env env{*this, features};
249
250 env.fund(XRP(10000), alice, bob, carol, gw);
251 env.close();
252 env.trust(usd(1000), alice, bob, carol);
253 env.trust(eur(1000), alice, bob, carol);
254 env(pay(gw, alice, usd(100)));
255 env(pay(gw, carol, eur(100)));
256
257 // Create more offers than the loop max count in DeliverNodeReverse
258 // Note: the DeliverNodeReverse code has been removed; however since
259 // this is a regression test the original test is being left as-is for
260 // now.
261 for (int i = 0; i < 101; ++i)
262 env(offer(carol, usd(1), eur(2)));
263
264 env(pay(alice, bob, eur(kEpsilon)), Path(~eur), Sendmax(usd(100)));
265 }
266
267 void
269 {
270 testcase("XRP Tiny payments");
271
272 // Regression test for tiny xrp payments
273 // In some cases, when the payment code calculates
274 // the amount of xrp needed as input to an xrp->iou offer
275 // it would incorrectly round the amount to zero (even when
276 // round-up was set to true).
277 // The bug would cause funded offers to be incorrectly removed
278 // because the code thought they were unfunded.
279 // The conditions to trigger the bug are:
280 // 1) When we calculate the amount of input xrp needed for an offer
281 // from xrp->iou, the amount is less than 1 drop (after rounding
282 // up the float representation).
283 // 2) There is another offer in the same book with a quality
284 // sufficiently bad that when calculating the input amount
285 // needed the amount is not set to zero.
286
287 using namespace jtx;
288 using namespace std::chrono_literals;
289 auto const alice = Account{"alice"};
290 auto const bob = Account{"bob"};
291 auto const carol = Account{"carol"};
292 auto const dan = Account{"dan"};
293 auto const erin = Account{"erin"};
294 auto const gw = Account{"gw"};
295
296 auto const usd = gw["USD"];
297 Env env{*this, features};
298
299 env.fund(XRP(10000), alice, bob, carol, dan, erin, gw);
300 env.close();
301 env.trust(usd(1000), alice, bob, carol, dan, erin);
302 env.close();
303 env(pay(gw, carol, usd(0.99999)));
304 env(pay(gw, dan, usd(1)));
305 env(pay(gw, erin, usd(1)));
306 env.close();
307
308 // Carol doesn't quite have enough funds for this offer
309 // The amount left after this offer is taken will cause
310 // STAmount to incorrectly round to zero when the next offer
311 // (at a good quality) is considered. (when the now removed
312 // stAmountCalcSwitchover2 patch was inactive)
313 env(offer(carol, drops(1), usd(0.99999)));
314 // Offer at a quality poor enough so when the input xrp is
315 // calculated in the reverse pass, the amount is not zero.
316 env(offer(dan, XRP(100), usd(1)));
317
318 env.close();
319 // This is the funded offer that will be incorrectly removed.
320 // It is considered after the offer from carol, which leaves a
321 // tiny amount left to pay. When calculating the amount of xrp
322 // needed for this offer, it will incorrectly compute zero in both
323 // the forward and reverse passes (when the now removed
324 // stAmountCalcSwitchover2 was inactive.)
325 env(offer(erin, drops(2), usd(1)));
326
327 env(pay(alice, bob, usd(1)),
328 Path(~usd),
329 Sendmax(XRP(102)),
330 Txflags(tfNoRippleDirect | tfPartialPayment));
331
332 env.require(Offers(carol, 0), Offers(dan, 1));
333
334 // offer was correctly consumed. There is still some
335 // liquidity left on that offer.
336 env.require(Balance(erin, usd(0.99999)), Offers(erin, 1));
337 }
338
339 void
341 {
342 testcase("Rm small increased q offers XRP");
343
344 // Carol places an offer, but cannot fully fund the offer. When her
345 // funding is taken into account, the offer's quality drops below its
346 // initial quality and has an input amount of 1 drop. This is removed as
347 // an offer that may block offer books.
348
349 using namespace jtx;
350 using namespace std::chrono_literals;
351 auto const alice = Account{"alice"};
352 auto const bob = Account{"bob"};
353 auto const carol = Account{"carol"};
354 auto const gw = Account{"gw"};
355
356 auto const usd = gw["USD"];
357
358 // Test offer crossing
359 for (auto const crossBothOffers : {false, true})
360 {
361 Env env{*this, features};
362
363 env.fund(XRP(10000), alice, bob, carol, gw);
364 env.close();
365 env.trust(usd(1000), alice, bob, carol);
366 // underfund carol's offer
367 auto const initialCarolUSD = usd(0.499);
368 env(pay(gw, carol, initialCarolUSD));
369 env(pay(gw, bob, usd(100)));
370 env.close();
371 // This offer is underfunded
372 env(offer(carol, drops(1), usd(1)));
373 env.close();
374 // offer at a lower quality
375 env(offer(bob, drops(2), usd(1), tfPassive));
376 env.close();
377 env.require(Offers(bob, 1), Offers(carol, 1));
378
379 // alice places an offer that crosses carol's; depending on
380 // "crossBothOffers" it may cross bob's as well
381 auto const aliceTakerGets = crossBothOffers ? drops(2) : drops(1);
382 env(offer(alice, usd(1), aliceTakerGets));
383 env.close();
384
385 env.require(
386 Offers(carol, 0),
387 Balance(
388 carol,
389 initialCarolUSD)); // offer is removed but not taken
390 if (crossBothOffers)
391 {
392 env.require(Offers(alice, 0), Balance(alice, usd(1))); // alice's offer is crossed
393 }
394 else
395 {
396 env.require(
397 Offers(alice, 1), Balance(alice, usd(0))); // alice's offer is not crossed
398 }
399 }
400
401 // Test payments
402 for (auto const partialPayment : {false, true})
403 {
404 Env env{*this, features};
405
406 env.fund(XRP(10000), alice, bob, carol, gw);
407 env.close();
408 env.trust(usd(1000), alice, bob, carol);
409 env.close();
410 auto const initialCarolUSD = usd(0.999);
411 env(pay(gw, carol, initialCarolUSD));
412 env.close();
413 env(pay(gw, bob, usd(100)));
414 env.close();
415 env(offer(carol, drops(1), usd(1)));
416 env.close();
417 env(offer(bob, drops(2), usd(2), tfPassive));
418 env.close();
419 env.require(Offers(bob, 1), Offers(carol, 1));
420
421 std::uint32_t const flags =
422 partialPayment ? (tfNoRippleDirect | tfPartialPayment) : tfNoRippleDirect;
423
424 TER const expectedTer = partialPayment ? TER{tesSUCCESS} : TER{tecPATH_PARTIAL};
425
426 env(pay(alice, bob, usd(5)),
427 Path(~usd),
428 Sendmax(XRP(1)),
429 Txflags(flags),
430 Ter(expectedTer));
431 env.close();
432
433 if (isTesSuccess(expectedTer))
434 {
435 env.require(Offers(carol, 0));
436 env.require(Balance(carol,
437 initialCarolUSD)); // offer is removed but not taken
438 }
439 else
440 {
441 // TODO: Offers are not removed when payments fail
442 // If that is addressed, the test should show that carol's
443 // offer is removed but not taken, as in the other branch of
444 // this if statement
445 }
446 }
447 }
448
449 void
451 {
452 testcase("Rm small increased q offers clamp native");
453
454 // Carol's partially funded offer is reduced to her funds. With
455 // fixCleanup3_5_0 the reduced input is clamped to the offer's input.
456
457 using namespace jtx;
458 auto const alice = Account{"alice"};
459 auto const bob = Account{"bob"};
460 auto const carol = Account{"carol"};
461 auto const dan = Account{"dan"};
462 auto const gw = Account{"gw"};
463
464 auto const usd = gw["USD"];
465
466 STAmount const carolPays{XRPAmount{static_cast<std::int64_t>(STAmount::kMaxNativeN)}};
467 STAmount const carolGets{usd.issue(), UINT64_C(9'999'999'999'999'995), -15};
468 STAmount const carolFunds{usd.issue(), UINT64_C(9'999'999'999'999'994), -15};
469
470 auto const setup = [&](Env& env, bool bobOffer) {
471 env.fund(XRP(10'000), alice, bob, carol, dan, gw);
472 env.close();
473 env.trust(usd(1'000), alice, bob, carol, dan);
474 env.close();
475 env(pay(gw, carol, carolFunds));
476 env(pay(gw, bob, usd(10)));
477 env.close();
478 // bob's offer is better than carol's, so it is taken first
479 if (bobOffer)
480 env(offer(bob, XRP(10), usd(10)));
481 env(offer(carol, carolPays, carolGets));
482 env.close();
483 env.require(Offers(bob, bobOffer ? 1 : 0), Offers(carol, 1));
484 };
485
486 for (auto const feat : {features, features - fixCleanup3_5_0})
487 {
488 bool const withFix = feat[fixCleanup3_5_0];
489
490 // Test payments: the payment needs more than bob's offer, so it
491 // reaches carol's offer.
492 {
493 Env env{*this, feat};
494 setup(env, true);
495
496 env(pay(alice, dan, usd(20)),
497 Path(~usd),
498 Sendmax(XRP(5'000)),
499 Txflags(tfPartialPayment),
500 Ter(withFix ? TER{tesSUCCESS} : TER{tecINTERNAL}));
501 env.close();
502
503 if (withFix)
504 {
505 // bob's offer is consumed and carol's is partially taken
506 env.require(Offers(bob, 0), Offers(carol, 1));
507 BEAST_EXPECT(env.balance(dan, usd).value() > usd(10).value());
508 BEAST_EXPECT(env.balance(carol, usd).value() < carolFunds);
509 }
510 else
511 {
512 env.require(Offers(bob, 1), Offers(carol, 1), Balance(dan, usd(0)));
513 env.require(Balance(carol, carolFunds));
514 }
515 }
516
517 // Test offer crossing: alice's offer crosses carol's quality
518 {
519 Env env{*this, feat};
520 setup(env, false);
521
522 STAmount const aliceGets{usd.issue(), 1, -7};
523 env(offer(alice, aliceGets, XRP(5'000)),
524 Ter(withFix ? TER{tesSUCCESS} : TER{tecINTERNAL}));
525 env.close();
526
527 env.require(Offers(carol, 1));
528 if (withFix)
529 {
530 env.require(Offers(alice, 0), Balance(alice, aliceGets));
531 }
532 else
533 {
534 env.require(
535 Offers(alice, 0), Balance(alice, usd(0)), Balance(carol, carolFunds));
536 }
537 }
538 }
539 }
540
541 void
543 {
544 testcase("Rm small increased q offers IOU");
545
546 // Carol places an offer, but cannot fully fund the offer. When her
547 // funding is taken into account, the offer's quality drops below its
548 // initial quality and has an input amount of 1 drop. This is removed as
549 // an offer that may block offer books.
550
551 using namespace jtx;
552 using namespace std::chrono_literals;
553 auto const alice = Account{"alice"};
554 auto const bob = Account{"bob"};
555 auto const carol = Account{"carol"};
556 auto const gw = Account{"gw"};
557
558 auto const usd = gw["USD"];
559 auto const eur = gw["EUR"];
560
561 auto const tinyAmount = [&](IOU const& iou) -> PrettyAmount {
562 STAmount const amt(
563 iou,
564 /*mantissa*/ 1,
565 /*exponent*/ -81);
566 return PrettyAmount(amt, iou.account.name());
567 };
568
569 // Test offer crossing
570 for (auto const crossBothOffers : {false, true})
571 {
572 Env env{*this, features};
573
574 env.fund(XRP(10000), alice, bob, carol, gw);
575 env.close();
576 env.trust(usd(1000), alice, bob, carol);
577 env.trust(eur(1000), alice, bob, carol);
578 // underfund carol's offer
579 auto const initialCarolUSD = tinyAmount(usd);
580 env(pay(gw, carol, initialCarolUSD));
581 env(pay(gw, bob, usd(100)));
582 env(pay(gw, alice, eur(100)));
583 env.close();
584 // This offer is underfunded
585 env(offer(carol, eur(1), usd(10)));
586 env.close();
587 // offer at a lower quality
588 env(offer(bob, eur(1), usd(5), tfPassive));
589 env.close();
590 env.require(Offers(bob, 1), Offers(carol, 1));
591
592 // alice places an offer that crosses carol's; depending on
593 // "crossBothOffers" it may cross bob's as well
594 // Whatever
595 auto const aliceTakerGets = crossBothOffers ? eur(0.2) : eur(0.1);
596 env(offer(alice, usd(1), aliceTakerGets));
597 env.close();
598
599 env.require(
600 Offers(carol, 0),
601 Balance(
602 carol,
603 initialCarolUSD)); // offer is removed but not taken
604 if (crossBothOffers)
605 {
606 env.require(Offers(alice, 0), Balance(alice, usd(1))); // alice's offer is crossed
607 }
608 else
609 {
610 env.require(
611 Offers(alice, 1), Balance(alice, usd(0))); // alice's offer is not crossed
612 }
613 }
614
615 // Test payments
616 for (auto const partialPayment : {false, true})
617 {
618 Env env{*this, features};
619
620 env.fund(XRP(10000), alice, bob, carol, gw);
621 env.close();
622 env.trust(usd(1000), alice, bob, carol);
623 env.trust(eur(1000), alice, bob, carol);
624 env.close();
625 // underfund carol's offer
626 auto const initialCarolUSD = tinyAmount(usd);
627 env(pay(gw, carol, initialCarolUSD));
628 env(pay(gw, bob, usd(100)));
629 env(pay(gw, alice, eur(100)));
630 env.close();
631 // This offer is underfunded
632 env(offer(carol, eur(1), usd(2)));
633 env.close();
634 env(offer(bob, eur(2), usd(4), tfPassive));
635 env.close();
636 env.require(Offers(bob, 1), Offers(carol, 1));
637
638 std::uint32_t const flags =
639 partialPayment ? (tfNoRippleDirect | tfPartialPayment) : tfNoRippleDirect;
640
641 TER const expectedTer = partialPayment ? TER{tesSUCCESS} : TER{tecPATH_PARTIAL};
642
643 env(pay(alice, bob, usd(5)),
644 Path(~usd),
645 Sendmax(eur(10)),
646 Txflags(flags),
647 Ter(expectedTer));
648 env.close();
649
650 if (isTesSuccess(expectedTer))
651 {
652 env.require(Offers(carol, 0));
653 env.require(Balance(carol,
654 initialCarolUSD)); // offer is removed but not taken
655 }
656 else
657 {
658 // TODO: Offers are not removed when payments fail
659 // If that is addressed, the test should show that carol's
660 // offer is removed but not taken, as in the other branch of
661 // this if statement
662 }
663 }
664 }
665
666 void
668 {
669 testcase("Enforce No Ripple");
670
671 using namespace jtx;
672
673 auto const gw = Account{"gateway"};
674 auto const usd = gw["USD"];
675 auto const btc = gw["BTC"];
676 auto const eur = gw["EUR"];
677 Account const alice{"alice"};
678 Account const bob{"bob"};
679 Account const carol{"carol"};
680 Account const dan{"dan"};
681
682 {
683 // No ripple with an implied account step after an offer
684 Env env{*this, features};
685
686 auto const gw1 = Account{"gw1"};
687 auto const usD1 = gw1["USD"];
688 auto const gw2 = Account{"gw2"};
689 auto const usD2 = gw2["USD"];
690
691 env.fund(XRP(10000), alice, noripple(bob), carol, dan, gw1, gw2);
692 env.close();
693 env.trust(usD1(1000), alice, carol, dan);
694 env(trust(bob, usD1(1000), tfSetNoRipple));
695 env.trust(usD2(1000), alice, carol, dan);
696 env(trust(bob, usD2(1000), tfSetNoRipple));
697
698 env(pay(gw1, dan, usD1(50)));
699 env(pay(gw1, bob, usD1(50)));
700 env(pay(gw2, bob, usD2(50)));
701
702 env(offer(dan, XRP(50), usD1(50)));
703
704 env(pay(alice, carol, usD2(50)),
705 Path(~usD1, bob),
706 Sendmax(XRP(50)),
707 Txflags(tfNoRippleDirect),
709 }
710 {
711 // Make sure payment works with default flags
712 Env env{*this, features};
713
714 auto const gw1 = Account{"gw1"};
715 auto const usD1 = gw1["USD"];
716 auto const gw2 = Account{"gw2"};
717 auto const usD2 = gw2["USD"];
718
719 env.fund(XRP(10000), alice, bob, carol, dan, gw1, gw2);
720 env.close();
721 env.trust(usD1(1000), alice, bob, carol, dan);
722 env.trust(usD2(1000), alice, bob, carol, dan);
723
724 env(pay(gw1, dan, usD1(50)));
725 env(pay(gw1, bob, usD1(50)));
726 env(pay(gw2, bob, usD2(50)));
727
728 env(offer(dan, XRP(50), usD1(50)));
729
730 env(pay(alice, carol, usD2(50)),
731 Path(~usD1, bob),
732 Sendmax(XRP(50)),
733 Txflags(tfNoRippleDirect));
734
735 env.require(Balance(alice, xrpMinusFee(env, 10000 - 50)));
736 env.require(Balance(bob, usD1(100)));
737 env.require(Balance(bob, usD2(0)));
738 env.require(Balance(carol, usD2(50)));
739 }
740 }
741
742 void
744 {
745 testcase("Insufficient Reserve");
746
747 // If an account places an offer and its balance
748 // *before* the transaction began isn't high enough
749 // to meet the reserve *after* the transaction runs,
750 // then no offer should go on the books but if the
751 // offer partially or fully crossed the tx succeeds.
752
753 using namespace jtx;
754
755 auto const gw = Account{"gateway"};
756 auto const alice = Account{"alice"};
757 auto const bob = Account{"bob"};
758 auto const carol = Account{"carol"};
759 auto const usd = gw["USD"];
760
761 auto const usdOffer = usd(1000);
762 auto const xrpOffer = XRP(1000);
763
764 // No crossing:
765 {
766 Env env{*this, features};
767
768 env.fund(XRP(1000000), gw);
769
770 auto const f = env.current()->fees().base;
771 auto const r = reserve(env, 0);
772
773 env.fund(r + f, alice);
774
775 env(trust(alice, usdOffer), Ter(tesSUCCESS));
776 env(pay(gw, alice, usdOffer), Ter(tesSUCCESS));
777 env(offer(alice, xrpOffer, usdOffer), Ter(tecINSUF_RESERVE_OFFER));
778
779 env.require(Balance(alice, r - f), Owners(alice, 1));
780 }
781
782 // Partial cross:
783 {
784 Env env{*this, features};
785
786 env.fund(XRP(1000000), gw);
787
788 auto const f = env.current()->fees().base;
789 auto const r = reserve(env, 0);
790
791 auto const usdOffer2 = usd(500);
792 auto const xrpOffer2 = XRP(500);
793
794 env.fund(r + f + xrpOffer, bob);
795
796 env(offer(bob, usdOffer2, xrpOffer2), Ter(tesSUCCESS));
797 env.fund(r + f, alice);
798
799 env(trust(alice, usdOffer), Ter(tesSUCCESS));
800 env(pay(gw, alice, usdOffer), Ter(tesSUCCESS));
801 env(offer(alice, xrpOffer, usdOffer), Ter(tesSUCCESS));
802
803 env.require(
804 Balance(alice, r - f + xrpOffer2),
805 Balance(alice, usdOffer2),
806 Owners(alice, 1),
807 Balance(bob, r + xrpOffer2),
808 Balance(bob, usdOffer2),
809 Owners(bob, 1));
810 }
811
812 // Account has enough reserve as is, but not enough
813 // if an offer were added. Attempt to sell IOUs to
814 // buy XRP. If it fully crosses, we succeed.
815 {
816 Env env{*this, features};
817
818 env.fund(XRP(1000000), gw);
819
820 auto const f = env.current()->fees().base;
821 auto const r = reserve(env, 0);
822
823 auto const usdOffer2 = usd(500);
824 auto const xrpOffer2 = XRP(500);
825
826 env.fund(r + f + xrpOffer, bob, carol);
827
828 env(offer(bob, usdOffer2, xrpOffer2), Ter(tesSUCCESS));
829 env(offer(carol, usdOffer, xrpOffer), Ter(tesSUCCESS));
830
831 env.fund(r + f, alice);
832
833 env(trust(alice, usdOffer), Ter(tesSUCCESS));
834 env(pay(gw, alice, usdOffer), Ter(tesSUCCESS));
835 env(offer(alice, xrpOffer, usdOffer), Ter(tesSUCCESS));
836
837 env.require(
838 Balance(alice, r - f + xrpOffer),
839 Balance(alice, usd(0)),
840 Owners(alice, 1),
841 Balance(bob, r + xrpOffer2),
842 Balance(bob, usdOffer2),
843 Owners(bob, 1),
844 Balance(carol, r + xrpOffer2),
845 Balance(carol, usdOffer2),
846 Owners(carol, 2));
847 }
848 }
849
850 // Helper function that returns the Offers on an account.
853 {
855 forEachItem(*env.current(), account, [&result](SLE::ConstRef sle) {
856 if (sle->getType() == ltOFFER)
857 result.push_back(sle);
858 });
859 return result;
860 }
861
862 void
864 {
865 testcase("Fill Modes");
866
867 using namespace jtx;
868
869 auto const startBalance = XRP(1000000);
870 auto const gw = Account{"gateway"};
871 auto const alice = Account{"alice"};
872 auto const bob = Account{"bob"};
873 auto const usd = gw["USD"];
874
875 // Fill or Kill - unless we fully cross, just charge a fee and don't
876 // place the offer on the books. But also clean up expired offers
877 // that are discovered along the way.
878 {
879 Env env{*this, features};
880
881 auto const f = env.current()->fees().base;
882
883 env.fund(startBalance, gw, alice, bob);
884 env.close();
885
886 // bob creates an offer that expires before the next ledger close.
887 env(offer(bob, usd(500), XRP(500)),
888 Json(sfExpiration.fieldName, lastClose(env) + 1),
889 Ter(tesSUCCESS));
890
891 // The offer expires (it's not removed yet).
892 env.close();
893 env.require(Owners(bob, 1), Offers(bob, 1));
894 auto const expiredBobOffer =
895 keylet::offer(bob, SeqProxy::rawSequence(env.seq(bob) - 1));
896
897 // bob creates the offer that will be crossed.
898 env(offer(bob, usd(500), XRP(500)), Ter(tesSUCCESS));
899 env.close();
900 env.require(Owners(bob, 2), Offers(bob, 2));
901 auto const crossedBobOffer =
902 keylet::offer(bob, SeqProxy::rawSequence(env.seq(bob) - 1));
903
904 env(trust(alice, usd(1000)), Ter(tesSUCCESS));
905 env(pay(gw, alice, usd(1000)), Ter(tesSUCCESS));
906
907 // Order that can't be filled but will remove bob's expired offer:
908 {
909 TER const killedCode{TER{tecKILLED}};
910 env(offer(alice, XRP(1000), usd(1000)), Txflags(tfFillOrKill), Ter(killedCode));
911 }
912 env.require(
913 Balance(alice, startBalance - (f * 2)),
914 Balance(alice, usd(1000)),
915 Owners(alice, 1),
916 Offers(alice, 0),
917 Balance(bob, startBalance - (f * 2)),
918 Balance(bob, usd(kNone)),
919 Owners(bob, 1),
920 Offers(bob, 1));
921 BEAST_EXPECT(!env.current()->exists(expiredBobOffer));
922 BEAST_EXPECT(env.current()->exists(crossedBobOffer));
923
924 // Order that can be filled
925 env(offer(alice, XRP(500), usd(500)), Txflags(tfFillOrKill), Ter(tesSUCCESS));
926
927 env.require(
928 Balance(alice, startBalance - (f * 3) + XRP(500)),
929 Balance(alice, usd(500)),
930 Owners(alice, 1),
931 Offers(alice, 0),
932 Balance(bob, startBalance - (f * 2) - XRP(500)),
933 Balance(bob, usd(500)),
934 Owners(bob, 1),
935 Offers(bob, 0));
936 }
937
938 // A failed Fill-or-Kill may tentatively consume a funded offer before
939 // the transaction is reset. That offer must not be treated as an
940 // unfunded offer cleanup.
941 {
942 Env env{*this, features};
943
944 env.fund(startBalance, gw, alice, bob);
945 env.close();
946
947 env(offer(bob, usd(500), XRP(500)), Ter(tesSUCCESS));
948 env.close();
949 auto const bobOffer = keylet::offer(bob, SeqProxy::rawSequence(env.seq(bob) - 1));
950
951 env(trust(alice, usd(1000)), Ter(tesSUCCESS));
952 env(pay(gw, alice, usd(1000)), Ter(tesSUCCESS));
953 env(offer(alice, XRP(1000), usd(1000)), Txflags(tfFillOrKill), Ter(tecKILLED));
954
955 env.require(Offers(alice, 0), Offers(bob, 1), Balance(alice, usd(1000)));
956 BEAST_EXPECT(env.current()->exists(bobOffer));
957 }
958
959 // Immediate or Cancel - cross as much as possible
960 // and add nothing on the books:
961 {
962 Env env{*this, features};
963
964 auto const f = env.current()->fees().base;
965
966 env.fund(startBalance, gw, alice, bob);
967 env.close();
968
969 env(trust(alice, usd(1000)), Ter(tesSUCCESS));
970 env(pay(gw, alice, usd(1000)), Ter(tesSUCCESS));
971
972 // No cross:
973 {
974 TER const expectedCode = tecKILLED;
975 env(offer(alice, XRP(1000), usd(1000)),
976 Txflags(tfImmediateOrCancel),
977 Ter(expectedCode));
978 }
979
980 env.require(
981 Balance(alice, startBalance - f - f),
982 Balance(alice, usd(1000)),
983 Owners(alice, 1),
984 Offers(alice, 0));
985
986 // Partially cross:
987 env(offer(bob, usd(50), XRP(50)), Ter(tesSUCCESS));
988 env(offer(alice, XRP(1000), usd(1000)), Txflags(tfImmediateOrCancel), Ter(tesSUCCESS));
989
990 env.require(
991 Balance(alice, startBalance - f - f - f + XRP(50)),
992 Balance(alice, usd(950)),
993 Owners(alice, 1),
994 Offers(alice, 0),
995 Balance(bob, startBalance - f - XRP(50)),
996 Balance(bob, usd(50)),
997 Owners(bob, 1),
998 Offers(bob, 0));
999
1000 // Fully cross:
1001 env(offer(bob, usd(50), XRP(50)), Ter(tesSUCCESS));
1002 env(offer(alice, XRP(50), usd(50)), Txflags(tfImmediateOrCancel), Ter(tesSUCCESS));
1003
1004 env.require(
1005 Balance(alice, startBalance - f - f - f - f + XRP(100)),
1006 Balance(alice, usd(900)),
1007 Owners(alice, 1),
1008 Offers(alice, 0),
1009 Balance(bob, startBalance - f - f - XRP(100)),
1010 Balance(bob, usd(100)),
1011 Owners(bob, 1),
1012 Offers(bob, 0));
1013 }
1014
1015 // tfPassive -- place the offer without crossing it.
1016 {
1017 Env env(*this, features);
1018
1019 env.fund(startBalance, gw, alice, bob);
1020 env.close();
1021
1022 env(trust(bob, usd(1000)));
1023 env.close();
1024
1025 env(pay(gw, bob, usd(1000)));
1026 env.close();
1027
1028 env(offer(alice, usd(1000), XRP(2000)));
1029 env.close();
1030
1031 auto const aliceOffers = offersOnAccount(env, alice);
1032 BEAST_EXPECT(aliceOffers.size() == 1);
1033 for (auto const& offerPtr : aliceOffers)
1034 {
1035 auto const& offer = *offerPtr;
1036 BEAST_EXPECT(offer[sfTakerGets] == XRP(2000));
1037 BEAST_EXPECT(offer[sfTakerPays] == usd(1000));
1038 }
1039
1040 // bob creates a passive offer that could cross alice's.
1041 // bob's offer should stay in the ledger.
1042 env(offer(bob, XRP(2000), usd(1000), tfPassive));
1043 env.close();
1044 env.require(Offers(alice, 1));
1045
1046 auto const bobOffers = offersOnAccount(env, bob);
1047 BEAST_EXPECT(bobOffers.size() == 1);
1048 for (auto const& offerPtr : bobOffers)
1049 {
1050 auto const& offer = *offerPtr;
1051 BEAST_EXPECT(offer[sfTakerGets] == usd(1000));
1052 BEAST_EXPECT(offer[sfTakerPays] == XRP(2000));
1053 }
1054
1055 // It should be possible for gw to cross both of those offers.
1056 env(offer(gw, XRP(2000), usd(1000)));
1057 env.close();
1058 env.require(Offers(alice, 0));
1059 env.require(Offers(gw, 0));
1060 env.require(Offers(bob, 1));
1061
1062 env(offer(gw, usd(1000), XRP(2000)));
1063 env.close();
1064 env.require(Offers(bob, 0));
1065 env.require(Offers(gw, 0));
1066 }
1067
1068 // tfPassive -- cross only offers of better quality.
1069 {
1070 Env env(*this, features);
1071
1072 env.fund(startBalance, gw, "alice", "bob");
1073 env.close();
1074
1075 env(trust("bob", usd(1000)));
1076 env.close();
1077
1078 env(pay(gw, "bob", usd(1000)));
1079 env(offer("alice", usd(500), XRP(1001)));
1080 env.close();
1081
1082 env(offer("alice", usd(500), XRP(1000)));
1083 env.close();
1084
1085 auto const aliceOffers = offersOnAccount(env, "alice");
1086 BEAST_EXPECT(aliceOffers.size() == 2);
1087
1088 // bob creates a passive offer. That offer should cross one
1089 // of alice's (the one with better quality) and leave alice's
1090 // other offer untouched.
1091 env(offer("bob", XRP(2000), usd(1000), tfPassive));
1092 env.close();
1093 env.require(Offers("alice", 1));
1094
1095 auto const bobOffers = offersOnAccount(env, "bob");
1096 BEAST_EXPECT(bobOffers.size() == 1);
1097 for (auto const& offerPtr : bobOffers)
1098 {
1099 auto const& offer = *offerPtr;
1100 BEAST_EXPECT(offer[sfTakerGets] == usd(499.5));
1101 BEAST_EXPECT(offer[sfTakerPays] == XRP(999));
1102 }
1103 }
1104 }
1105
1106 void
1108 {
1109 testcase("Malformed Detection");
1110
1111 using namespace jtx;
1112
1113 auto const startBalance = XRP(1000000);
1114 auto const gw = Account{"gateway"};
1115 auto const alice = Account{"alice"};
1116 auto const usd = gw["USD"];
1117
1118 Env env{*this, features};
1119
1120 env.fund(startBalance, gw, alice);
1121 env.close();
1122
1123 // Order that has invalid flags
1124 env(offer(alice, usd(1000), XRP(1000)),
1125 Txflags(tfImmediateOrCancel + 1),
1127 env.require(Balance(alice, startBalance), Owners(alice, 0), Offers(alice, 0));
1128
1129 // Order with incompatible flags
1130 env(offer(alice, usd(1000), XRP(1000)),
1131 Txflags(tfImmediateOrCancel | tfFillOrKill),
1133 env.require(Balance(alice, startBalance), Owners(alice, 0), Offers(alice, 0));
1134
1135 // Sell and buy the same asset
1136 {
1137 // Alice tries an XRP to XRP order:
1138 env(offer(alice, XRP(1000), XRP(1000)), Ter(temBAD_OFFER));
1139 env.require(Owners(alice, 0), Offers(alice, 0));
1140
1141 // Alice tries an IOU to IOU order:
1142 env(trust(alice, usd(1000)), Ter(tesSUCCESS));
1143 env(pay(gw, alice, usd(1000)), Ter(tesSUCCESS));
1144 env(offer(alice, usd(1000), usd(1000)), Ter(temREDUNDANT));
1145 env.require(Owners(alice, 1), Offers(alice, 0));
1146 }
1147
1148 // Offers with negative amounts
1149 {
1150 env(offer(alice, -usd(1000), XRP(1000)), Ter(temBAD_OFFER));
1151 env.require(Owners(alice, 1), Offers(alice, 0));
1152
1153 env(offer(alice, usd(1000), -XRP(1000)), Ter(temBAD_OFFER));
1154 env.require(Owners(alice, 1), Offers(alice, 0));
1155 }
1156
1157 // Offer with a bad expiration
1158 {
1159 env(offer(alice, usd(1000), XRP(1000)),
1160 Json(sfExpiration.fieldName, std::uint32_t(0)),
1162 env.require(Owners(alice, 1), Offers(alice, 0));
1163 }
1164
1165 // Offer with a bad offer sequence
1166 {
1167 env(offer(alice, usd(1000), XRP(1000)),
1168 Json(jss::OfferSequence, std::uint32_t(0)),
1170 env.require(Owners(alice, 1), Offers(alice, 0));
1171 }
1172
1173 // Use XRP as a currency code
1174 {
1175 auto const bad = IOU(gw, badCurrency());
1176
1177 env(offer(alice, XRP(1000), bad(1000)), Ter(temBAD_CURRENCY));
1178 env.require(Owners(alice, 1), Offers(alice, 0));
1179 }
1180 }
1181
1182 void
1184 {
1185 testcase("Offer Expiration");
1186
1187 using namespace jtx;
1188
1189 auto const gw = Account{"gateway"};
1190 auto const alice = Account{"alice"};
1191 auto const bob = Account{"bob"};
1192 auto const usd = gw["USD"];
1193
1194 auto const startBalance = XRP(1000000);
1195 auto const usdOffer = usd(1000);
1196 auto const xrpOffer = XRP(1000);
1197
1198 Env env{*this, features};
1199
1200 env.fund(startBalance, gw, alice, bob);
1201 env.close();
1202
1203 auto const f = env.current()->fees().base;
1204
1205 env(trust(alice, usdOffer), Ter(tesSUCCESS));
1206 env(pay(gw, alice, usdOffer), Ter(tesSUCCESS));
1207 env.close();
1208 env.require(
1209 Balance(alice, startBalance - f),
1210 Balance(alice, usdOffer),
1211 Offers(alice, 0),
1212 Owners(alice, 1));
1213
1214 env(offer(alice, xrpOffer, usdOffer),
1215 Json(sfExpiration.fieldName, lastClose(env)),
1216 Ter(tecEXPIRED));
1217
1218 env.require(
1219 Balance(alice, startBalance - f - f),
1220 Balance(alice, usdOffer),
1221 Offers(alice, 0),
1222 Owners(alice, 1));
1223 env.close();
1224
1225 // Add an offer that expires before the next ledger close
1226 env(offer(alice, xrpOffer, usdOffer),
1227 Json(sfExpiration.fieldName, lastClose(env) + 1),
1228 Ter(tesSUCCESS));
1229 env.require(
1230 Balance(alice, startBalance - f - f - f),
1231 Balance(alice, usdOffer),
1232 Offers(alice, 1),
1233 Owners(alice, 2));
1234
1235 // The offer expires (it's not removed yet)
1236 env.close();
1237 env.require(
1238 Balance(alice, startBalance - f - f - f),
1239 Balance(alice, usdOffer),
1240 Offers(alice, 1),
1241 Owners(alice, 2));
1242
1243 // Add offer - the expired offer is removed
1244 env(offer(bob, usdOffer, xrpOffer), Ter(tesSUCCESS));
1245 env.require(
1246 Balance(alice, startBalance - f - f - f),
1247 Balance(alice, usdOffer),
1248 Offers(alice, 0),
1249 Owners(alice, 1),
1250 Balance(bob, startBalance - f),
1251 Balance(bob, usd(kNone)),
1252 Offers(bob, 1),
1253 Owners(bob, 1));
1254 }
1255
1256 void
1258 {
1259 testcase("Unfunded Crossing");
1260
1261 using namespace jtx;
1262
1263 auto const gw = Account{"gateway"};
1264 auto const usd = gw["USD"];
1265
1266 auto const usdOffer = usd(1000);
1267 auto const xrpOffer = XRP(1000);
1268
1269 Env env{*this, features};
1270
1271 env.fund(XRP(1000000), gw);
1272 env.close();
1273
1274 // The fee that's charged for transactions
1275 auto const f = env.current()->fees().base;
1276
1277 // Account is at the reserve, and will dip below once
1278 // fees are subtracted.
1279 env.fund(reserve(env, 0), "alice");
1280 env.close();
1281 env(offer("alice", usdOffer, xrpOffer), Ter(tecUNFUNDED_OFFER));
1282 env.require(Balance("alice", reserve(env, 0) - f), Owners("alice", 0));
1283
1284 // Account has just enough for the reserve and the
1285 // fee.
1286 env.fund(reserve(env, 0) + f, "bob");
1287 env.close();
1288 env(offer("bob", usdOffer, xrpOffer), Ter(tecUNFUNDED_OFFER));
1289 env.require(Balance("bob", reserve(env, 0)), Owners("bob", 0));
1290
1291 // Account has enough for the reserve, the fee and
1292 // the offer, and a bit more, but not enough for the
1293 // reserve after the offer is placed.
1294 env.fund(reserve(env, 0) + f + XRP(1), "carol");
1295 env.close();
1296 env(offer("carol", usdOffer, xrpOffer), Ter(tecINSUF_RESERVE_OFFER));
1297 env.require(Balance("carol", reserve(env, 0) + XRP(1)), Owners("carol", 0));
1298
1299 // Account has enough for the reserve plus one
1300 // offer, and the fee.
1301 env.fund(reserve(env, 1) + f, "dan");
1302 env.close();
1303 env(offer("dan", usdOffer, xrpOffer), Ter(tesSUCCESS));
1304 env.require(Balance("dan", reserve(env, 1)), Owners("dan", 1));
1305
1306 // Account has enough for the reserve plus one
1307 // offer, the fee and the entire offer amount.
1308 env.fund(reserve(env, 1) + f + xrpOffer, "eve");
1309 env.close();
1310 env(offer("eve", usdOffer, xrpOffer), Ter(tesSUCCESS));
1311 env.require(Balance("eve", reserve(env, 1) + xrpOffer), Owners("eve", 1));
1312 }
1313
1314 void
1315 testSelfCross(bool usePartner, FeatureBitset features)
1316 {
1317 testcase(std::string("Self-crossing") + (usePartner ? ", with partner account" : ""));
1318
1319 using namespace jtx;
1320
1321 auto const gw = Account{"gateway"};
1322 auto const partner = Account{"partner"};
1323 auto const usd = gw["USD"];
1324 auto const btc = gw["BTC"];
1325
1326 Env env{*this, features};
1327 env.close();
1328
1329 env.fund(XRP(10000), gw);
1330 if (usePartner)
1331 {
1332 env.fund(XRP(10000), partner);
1333 env.close();
1334 env(trust(partner, usd(100)));
1335 env(trust(partner, btc(500)));
1336 env.close();
1337 env(pay(gw, partner, usd(100)));
1338 env(pay(gw, partner, btc(500)));
1339 }
1340 auto const& accountToTest = usePartner ? partner : gw;
1341
1342 env.close();
1343 env.require(Offers(accountToTest, 0));
1344
1345 // PART 1:
1346 // we will make two offers that can be used to bridge BTC to USD
1347 // through XRP
1348 env(offer(accountToTest, btc(250), XRP(1000)));
1349 env.require(Offers(accountToTest, 1));
1350
1351 // validate that the book now shows a BTC for XRP offer
1352 BEAST_EXPECT(isOffer(env, accountToTest, btc(250), XRP(1000)));
1353
1354 auto const secondLegSeq = env.seq(accountToTest);
1355 env(offer(accountToTest, XRP(1000), usd(50)));
1356 env.require(Offers(accountToTest, 2));
1357
1358 // validate that the book also shows a XRP for USD offer
1359 BEAST_EXPECT(isOffer(env, accountToTest, XRP(1000), usd(50)));
1360
1361 // now make an offer that will cross and auto-bridge, meaning
1362 // the outstanding offers will be taken leaving us with kNone
1363 env(offer(accountToTest, usd(50), btc(250)));
1364
1365 auto jrr = getBookOffers(env, usd, btc);
1366 BEAST_EXPECT(jrr[jss::offers].isArray());
1367 BEAST_EXPECT(jrr[jss::offers].size() == 0);
1368
1369 jrr = getBookOffers(env, btc, XRP);
1370 BEAST_EXPECT(jrr[jss::offers].isArray());
1371 BEAST_EXPECT(jrr[jss::offers].size() == 0);
1372
1373 // At this point, all offers are expected to be consumed.
1374 {
1375 auto const acctOffers = offersOnAccount(env, accountToTest);
1376
1377 BEAST_EXPECT(acctOffers.empty());
1378 for (auto const& offerPtr : acctOffers)
1379 {
1380 auto const& offer = *offerPtr;
1381 BEAST_EXPECT(offer[sfLedgerEntryType] == ltOFFER);
1382 BEAST_EXPECT(offer[sfTakerGets] == usd(0));
1383 BEAST_EXPECT(offer[sfTakerPays] == XRP(0));
1384 }
1385 }
1386
1387 // cancel that lingering second offer so that it doesn't interfere
1388 // with the next set of offers we test. This will not be needed once
1389 // the bridging bug is fixed
1390 env(offerCancel(accountToTest, secondLegSeq));
1391 env.require(Offers(accountToTest, 0));
1392
1393 // PART 2:
1394 // simple direct crossing BTC to USD and then USD to BTC which causes
1395 // the first offer to be replaced
1396 env(offer(accountToTest, btc(250), usd(50)));
1397 env.require(Offers(accountToTest, 1));
1398
1399 // validate that the book shows one BTC for USD offer and no USD for
1400 // BTC offers
1401 BEAST_EXPECT(isOffer(env, accountToTest, btc(250), usd(50)));
1402
1403 jrr = getBookOffers(env, usd, btc);
1404 BEAST_EXPECT(jrr[jss::offers].isArray());
1405 BEAST_EXPECT(jrr[jss::offers].size() == 0);
1406
1407 // this second offer would self-cross directly, so it causes the first
1408 // offer by the same owner/taker to be removed
1409 env(offer(accountToTest, usd(50), btc(250)));
1410 env.require(Offers(accountToTest, 1));
1411
1412 // validate that we now have just the second offer...the first
1413 // was removed
1414 jrr = getBookOffers(env, btc, usd);
1415 BEAST_EXPECT(jrr[jss::offers].isArray());
1416 BEAST_EXPECT(jrr[jss::offers].size() == 0);
1417
1418 BEAST_EXPECT(isOffer(env, accountToTest, usd(50), btc(250)));
1419 }
1420
1421 void
1423 {
1424 // This test creates an offer test for negative balance
1425 // with transfer fees and minuscule funds.
1426 testcase("Negative Balance");
1427
1428 using namespace jtx;
1429
1430 // This is one of the few tests where fixReducedOffersV2 changes the
1431 // results. So test both with and without fixReducedOffersV2.
1432 for (FeatureBitset localFeatures :
1433 {features - fixReducedOffersV2, features | fixReducedOffersV2})
1434 {
1435 Env env{*this, localFeatures};
1436
1437 auto const gw = Account{"gateway"};
1438 auto const alice = Account{"alice"};
1439 auto const bob = Account{"bob"};
1440 auto const usd = gw["USD"];
1441 auto const btc = gw["BTC"];
1442
1443 // these *interesting* amounts were taken
1444 // from the original JS test that was ported here
1445 auto const gwInitialBalance = drops(1149999730);
1446 auto const aliceInitialBalance = drops(499946999680);
1447 auto const bobInitialBalance = drops(10199999920);
1448 auto const smallAmount = STAmount{bob["USD"], UINT64_C(2710505431213761), -33};
1449
1450 env.fund(gwInitialBalance, gw);
1451 env.fund(aliceInitialBalance, alice);
1452 env.fund(bobInitialBalance, bob);
1453 env.close();
1454
1455 env(rate(gw, 1.005));
1456
1457 env(trust(alice, usd(500)));
1458 env(trust(bob, usd(50)));
1459 env(trust(gw, alice["USD"](100)));
1460
1461 env(pay(gw, alice, alice["USD"](50)));
1462 env(pay(gw, bob, smallAmount));
1463
1464 env(offer(alice, usd(50), XRP(150000)));
1465
1466 // unfund the offer
1467 env(pay(alice, gw, usd(100)));
1468
1469 // drop the trust line (set to 0)
1470 env(trust(gw, alice["USD"](0)));
1471
1472 // verify balances
1473 auto jrr = ledgerEntryState(env, alice, gw, "USD");
1474 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "50");
1475
1476 jrr = ledgerEntryState(env, bob, gw, "USD");
1477 BEAST_EXPECT(
1478 jrr[jss::node][sfBalance.fieldName][jss::value] == "-2710505431213761e-33");
1479
1480 // create crossing offer
1481 std::uint32_t const bobOfferSeq = env.seq(bob);
1482 env(offer(bob, XRP(2000), usd(1)));
1483
1484 if (localFeatures[fixReducedOffersV2])
1485 {
1486 // With the rounding introduced by fixReducedOffersV2, bob's
1487 // offer does not cross alice's offer and goes straight into
1488 // the ledger.
1489 jrr = ledgerEntryState(env, bob, gw, "USD");
1490 BEAST_EXPECT(
1491 jrr[jss::node][sfBalance.fieldName][jss::value] == "-2710505431213761e-33");
1492
1493 json::Value const bobOffer = ledgerEntryOffer(env, bob, bobOfferSeq)[jss::node];
1494 BEAST_EXPECT(bobOffer[sfTakerGets.jsonName][jss::value] == "1");
1495 BEAST_EXPECT(bobOffer[sfTakerPays.jsonName] == "2000000000");
1496 return;
1497 }
1498
1499 // verify balances again.
1500 //
1501 // NOTE:
1502 // Here a difference in the rounding modes of our two offer
1503 // crossing algorithms becomes apparent. The old offer crossing
1504 // would consume small_amount and transfer no XRP. The new offer
1505 // crossing transfers a single drop, rather than no drops.
1506 auto const crossingDelta = drops(1);
1507
1508 jrr = ledgerEntryState(env, alice, gw, "USD");
1509 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "50");
1510 BEAST_EXPECT(
1511 env.balance(alice, xrpIssue()) ==
1512 aliceInitialBalance - env.current()->fees().base * 3 - crossingDelta);
1513
1514 jrr = ledgerEntryState(env, bob, gw, "USD");
1515 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "0");
1516 BEAST_EXPECT(
1517 env.balance(bob, xrpIssue()) ==
1518 bobInitialBalance - env.current()->fees().base * 2 + crossingDelta);
1519 }
1520 }
1521
1522 void
1523 testOfferCrossWithXRP(bool reverseOrder, FeatureBitset features)
1524 {
1525 testcase(
1526 std::string("Offer Crossing with XRP, ") + (reverseOrder ? "Reverse" : "Normal") +
1527 " order");
1528
1529 using namespace jtx;
1530
1531 Env env{*this, features};
1532
1533 auto const gw = Account{"gateway"};
1534 auto const alice = Account{"alice"};
1535 auto const bob = Account{"bob"};
1536 auto const usd = gw["USD"];
1537
1538 env.fund(XRP(10000), gw, alice, bob);
1539 env.close();
1540
1541 env(trust(alice, usd(1000)));
1542 env(trust(bob, usd(1000)));
1543
1544 env(pay(gw, alice, alice["USD"](500)));
1545
1546 if (reverseOrder)
1547 env(offer(bob, usd(1), XRP(4000)));
1548
1549 env(offer(alice, XRP(150000), usd(50)));
1550
1551 if (!reverseOrder)
1552 env(offer(bob, usd(1), XRP(4000)));
1553
1554 // Existing offer pays better than this wants.
1555 // Fully consume existing offer.
1556 // Pay 1 USD, get 4000 XRP.
1557
1558 auto jrr = ledgerEntryState(env, bob, gw, "USD");
1559 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-1");
1560 jrr = ledgerEntryRoot(env, bob);
1561 BEAST_EXPECT(
1562 jrr[jss::node][sfBalance.fieldName] ==
1563 to_string(
1564 (XRP(10000) - XRP(reverseOrder ? 4000 : 3000) - env.current()->fees().base * 2)
1565 .xrp()));
1566
1567 jrr = ledgerEntryState(env, alice, gw, "USD");
1568 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-499");
1569 jrr = ledgerEntryRoot(env, alice);
1570 BEAST_EXPECT(
1571 jrr[jss::node][sfBalance.fieldName] ==
1572 to_string(
1573 (XRP(10000) + XRP(reverseOrder ? 4000 : 3000) - env.current()->fees().base * 2)
1574 .xrp()));
1575 }
1576
1577 void
1579 {
1580 testcase("Offer Crossing with Limit Override");
1581
1582 using namespace jtx;
1583
1584 Env env{*this, features};
1585
1586 auto const gw = Account{"gateway"};
1587 auto const alice = Account{"alice"};
1588 auto const bob = Account{"bob"};
1589 auto const usd = gw["USD"];
1590
1591 env.fund(XRP(100000), gw, alice, bob);
1592 env.close();
1593
1594 env(trust(alice, usd(1000)));
1595
1596 env(pay(gw, alice, alice["USD"](500)));
1597
1598 env(offer(alice, XRP(150000), usd(50)));
1599 env(offer(bob, usd(1), XRP(3000)));
1600
1601 auto jrr = ledgerEntryState(env, bob, gw, "USD");
1602 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-1");
1603 jrr = ledgerEntryRoot(env, bob);
1604 BEAST_EXPECT(
1605 jrr[jss::node][sfBalance.fieldName] ==
1606 to_string((XRP(100000) - XRP(3000) - env.current()->fees().base * 1).xrp()));
1607
1608 jrr = ledgerEntryState(env, alice, gw, "USD");
1609 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-499");
1610 jrr = ledgerEntryRoot(env, alice);
1611 BEAST_EXPECT(
1612 jrr[jss::node][sfBalance.fieldName] ==
1613 to_string((XRP(100000) + XRP(3000) - env.current()->fees().base * 2).xrp()));
1614 }
1615
1616 void
1618 {
1619 testcase("Offer Accept then Cancel.");
1620
1621 using namespace jtx;
1622
1623 Env env{*this, features};
1624
1625 auto const usd = env.master["USD"];
1626
1627 auto const nextOfferSeq = env.seq(env.master);
1628 env(offer(env.master, XRP(500), usd(100)));
1629 env.close();
1630
1631 env(offerCancel(env.master, nextOfferSeq));
1632 BEAST_EXPECT(env.seq(env.master) == nextOfferSeq + 2);
1633
1634 // ledger_accept, call twice and verify no odd behavior
1635 env.close();
1636 env.close();
1637 BEAST_EXPECT(env.seq(env.master) == nextOfferSeq + 2);
1638 }
1639
1640 void
1642 {
1643 testcase("Offer Cancel Past and Future Sequence.");
1644
1645 using namespace jtx;
1646
1647 Env env{*this, features};
1648
1649 auto const alice = Account{"alice"};
1650
1651 auto const nextOfferSeq = env.seq(env.master);
1652 env.fund(XRP(10000), alice);
1653 env.close();
1654
1655 env(offerCancel(env.master, nextOfferSeq));
1656
1657 env(offerCancel(env.master, env.seq(env.master)), Ter(temBAD_SEQUENCE));
1658
1659 env(offerCancel(env.master, env.seq(env.master) + 1), Ter(temBAD_SEQUENCE));
1660
1661 env.close();
1662 }
1663
1664 void
1666 {
1667 testcase("Currency Conversion: Entire Offer");
1668
1669 using namespace jtx;
1670
1671 Env env{*this, features};
1672
1673 auto const gw = Account{"gateway"};
1674 auto const alice = Account{"alice"};
1675 auto const bob = Account{"bob"};
1676 auto const usd = gw["USD"];
1677
1678 env.fund(XRP(10000), gw, alice, bob);
1679 env.close();
1680 env.require(Owners(bob, 0));
1681
1682 env(trust(alice, usd(100)));
1683 env(trust(bob, usd(1000)));
1684
1685 env.require(Owners(alice, 1), Owners(bob, 1));
1686
1687 env(pay(gw, alice, alice["USD"](100)));
1688 auto const bobOfferSeq = env.seq(bob);
1689 env(offer(bob, usd(100), XRP(500)));
1690
1691 env.require(Owners(alice, 1), Owners(bob, 2));
1692 auto jro = ledgerEntryOffer(env, bob, bobOfferSeq);
1693 BEAST_EXPECT(jro[jss::node][jss::TakerGets] == XRP(500).value().getText());
1694 BEAST_EXPECT(
1695 jro[jss::node][jss::TakerPays] == usd(100).value().getJson(JsonOptions::Values::None));
1696
1697 env(pay(alice, alice, XRP(500)), Sendmax(usd(100)));
1698
1699 auto jrr = ledgerEntryState(env, alice, gw, "USD");
1700 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "0");
1701 jrr = ledgerEntryRoot(env, alice);
1702 BEAST_EXPECT(
1703 jrr[jss::node][sfBalance.fieldName] ==
1704 to_string((XRP(10000) + XRP(500) - env.current()->fees().base * 2).xrp()));
1705
1706 jrr = ledgerEntryState(env, bob, gw, "USD");
1707 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-100");
1708
1709 jro = ledgerEntryOffer(env, bob, bobOfferSeq);
1710 BEAST_EXPECT(jro[jss::error] == "entryNotFound");
1711 BEAST_EXPECT(jro[jss::error_code] == RpcEntryNotFound);
1712 BEAST_EXPECT(jro[jss::error_message] == "Entry not found.");
1713
1714 env.require(Owners(alice, 1), Owners(bob, 1));
1715 }
1716
1717 void
1719 {
1720 testcase("Currency Conversion: Offerer Into Debt");
1721
1722 using namespace jtx;
1723
1724 Env env{*this, features};
1725
1726 auto const alice = Account{"alice"};
1727 auto const bob = Account{"bob"};
1728 auto const carol = Account{"carol"};
1729
1730 env.fund(XRP(10000), alice, bob, carol);
1731 env.close();
1732
1733 env(trust(alice, carol["EUR"](2000)));
1734 env(trust(bob, alice["USD"](100)));
1735 env(trust(carol, bob["EUR"](1000)));
1736
1737 auto const bobOfferSeq = env.seq(bob);
1738 env(offer(bob, alice["USD"](50), carol["EUR"](200)), Ter(tecUNFUNDED_OFFER));
1739
1740 env(offer(alice, carol["EUR"](200), alice["USD"](50)));
1741
1742 auto jro = ledgerEntryOffer(env, bob, bobOfferSeq);
1743 BEAST_EXPECT(jro[jss::error] == "entryNotFound");
1744 BEAST_EXPECT(jro[jss::error_code] == RpcEntryNotFound);
1745 BEAST_EXPECT(jro[jss::error_message] == "Entry not found.");
1746 }
1747
1748 void
1750 {
1751 testcase("Currency Conversion: In Parts");
1752
1753 using namespace jtx;
1754
1755 Env env{*this, features};
1756
1757 auto const gw = Account{"gateway"};
1758 auto const alice = Account{"alice"};
1759 auto const bob = Account{"bob"};
1760 auto const usd = gw["USD"];
1761
1762 env.fund(XRP(10000), gw, alice, bob);
1763 env.close();
1764
1765 env(trust(alice, usd(200)));
1766 env(trust(bob, usd(1000)));
1767
1768 env(pay(gw, alice, alice["USD"](200)));
1769
1770 auto const bobOfferSeq = env.seq(bob);
1771 env(offer(bob, usd(100), XRP(500)));
1772
1773 env(pay(alice, alice, XRP(200)), Sendmax(usd(100)));
1774
1775 // The previous payment reduced the remaining offer amount by 200 XRP
1776 auto jro = ledgerEntryOffer(env, bob, bobOfferSeq);
1777 BEAST_EXPECT(jro[jss::node][jss::TakerGets] == XRP(300).value().getText());
1778 BEAST_EXPECT(
1779 jro[jss::node][jss::TakerPays] == usd(60).value().getJson(JsonOptions::Values::None));
1780
1781 // the balance between alice and gw is 160 USD..200 less the 40 taken
1782 // by the offer
1783 auto jrr = ledgerEntryState(env, alice, gw, "USD");
1784 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-160");
1785 // alice now has 200 more XRP from the payment
1786 jrr = ledgerEntryRoot(env, alice);
1787 BEAST_EXPECT(
1788 jrr[jss::node][sfBalance.fieldName] ==
1789 to_string((XRP(10000) + XRP(200) - env.current()->fees().base * 2).xrp()));
1790
1791 // bob got 40 USD from partial consumption of the offer
1792 jrr = ledgerEntryState(env, bob, gw, "USD");
1793 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-40");
1794
1795 // Alice converts USD to XRP which should fail
1796 // due to PartialPayment.
1797 env(pay(alice, alice, XRP(600)), Sendmax(usd(100)), Ter(tecPATH_PARTIAL));
1798
1799 // Alice converts USD to XRP, should succeed because
1800 // we permit partial payment
1801 env(pay(alice, alice, XRP(600)), Sendmax(usd(100)), Txflags(tfPartialPayment));
1802
1803 // Verify the offer was consumed
1804 jro = ledgerEntryOffer(env, bob, bobOfferSeq);
1805 BEAST_EXPECT(jro[jss::error] == "entryNotFound");
1806 BEAST_EXPECT(jro[jss::error_code] == RpcEntryNotFound);
1807 BEAST_EXPECT(jro[jss::error_message] == "Entry not found.");
1808
1809 // verify balances look right after the partial payment
1810 // only 300 XRP should be have been payed since that's all
1811 // that remained in the offer from bob. The alice balance is now
1812 // 100 USD because another 60 USD were transferred to bob in the second
1813 // payment
1814 jrr = ledgerEntryState(env, alice, gw, "USD");
1815 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-100");
1816 jrr = ledgerEntryRoot(env, alice);
1817 BEAST_EXPECT(
1818 jrr[jss::node][sfBalance.fieldName] ==
1819 to_string((XRP(10000) + XRP(200) + XRP(300) - env.current()->fees().base * 4).xrp()));
1820
1821 // bob now has 100 USD - 40 from the first payment and 60 from the
1822 // second (partial) payment
1823 jrr = ledgerEntryState(env, bob, gw, "USD");
1824 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-100");
1825 }
1826
1827 void
1829 {
1830 testcase("Cross Currency Payment: Start with XRP");
1831
1832 using namespace jtx;
1833
1834 Env env{*this, features};
1835
1836 auto const gw = Account{"gateway"};
1837 auto const alice = Account{"alice"};
1838 auto const bob = Account{"bob"};
1839 auto const carol = Account{"carol"};
1840 auto const usd = gw["USD"];
1841
1842 env.fund(XRP(10000), gw, alice, bob, carol);
1843 env.close();
1844
1845 env(trust(carol, usd(1000)));
1846 env(trust(bob, usd(2000)));
1847
1848 env(pay(gw, carol, carol["USD"](500)));
1849
1850 auto const carolOfferSeq = env.seq(carol);
1851 env(offer(carol, XRP(500), usd(50)));
1852
1853 env(pay(alice, bob, usd(25)), Sendmax(XRP(333)));
1854
1855 auto jrr = ledgerEntryState(env, bob, gw, "USD");
1856 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-25");
1857
1858 jrr = ledgerEntryState(env, carol, gw, "USD");
1859 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-475");
1860
1861 auto jro = ledgerEntryOffer(env, carol, carolOfferSeq);
1862 BEAST_EXPECT(
1863 jro[jss::node][jss::TakerGets] == usd(25).value().getJson(JsonOptions::Values::None));
1864 BEAST_EXPECT(jro[jss::node][jss::TakerPays] == XRP(250).value().getText());
1865 }
1866
1867 void
1869 {
1870 testcase("Cross Currency Payment: End with XRP");
1871
1872 using namespace jtx;
1873
1874 Env env{*this, features};
1875
1876 auto const gw = Account{"gateway"};
1877 auto const alice = Account{"alice"};
1878 auto const bob = Account{"bob"};
1879 auto const carol = Account{"carol"};
1880 auto const usd = gw["USD"];
1881
1882 env.fund(XRP(10000), gw, alice, bob, carol);
1883 env.close();
1884
1885 env(trust(alice, usd(1000)));
1886 env(trust(carol, usd(2000)));
1887
1888 env(pay(gw, alice, alice["USD"](500)));
1889
1890 auto const carolOfferSeq = env.seq(carol);
1891 env(offer(carol, usd(50), XRP(500)));
1892
1893 env(pay(alice, bob, XRP(250)), Sendmax(usd(333)));
1894
1895 auto jrr = ledgerEntryState(env, alice, gw, "USD");
1896 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-475");
1897
1898 jrr = ledgerEntryState(env, carol, gw, "USD");
1899 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-25");
1900
1901 jrr = ledgerEntryRoot(env, bob);
1902 BEAST_EXPECT(
1903 jrr[jss::node][sfBalance.fieldName] ==
1904 std::to_string(XRP(10000).value().mantissa() + XRP(250).value().mantissa()));
1905
1906 auto jro = ledgerEntryOffer(env, carol, carolOfferSeq);
1907 BEAST_EXPECT(jro[jss::node][jss::TakerGets] == XRP(250).value().getText());
1908 BEAST_EXPECT(
1909 jro[jss::node][jss::TakerPays] == usd(25).value().getJson(JsonOptions::Values::None));
1910 }
1911
1912 void
1914 {
1915 testcase("Cross Currency Payment: Bridged");
1916
1917 using namespace jtx;
1918
1919 Env env{*this, features};
1920
1921 auto const gw1 = Account{"gateway_1"};
1922 auto const gw2 = Account{"gateway_2"};
1923 auto const alice = Account{"alice"};
1924 auto const bob = Account{"bob"};
1925 auto const carol = Account{"carol"};
1926 auto const dan = Account{"dan"};
1927 auto const usd = gw1["USD"];
1928 auto const eur = gw2["EUR"];
1929
1930 env.fund(XRP(10000), gw1, gw2, alice, bob, carol, dan);
1931 env.close();
1932
1933 env(trust(alice, usd(1000)));
1934 env(trust(bob, eur(1000)));
1935 env(trust(carol, usd(1000)));
1936 env(trust(dan, eur(1000)));
1937
1938 env(pay(gw1, alice, alice["USD"](500)));
1939 env(pay(gw2, dan, dan["EUR"](400)));
1940
1941 auto const carolOfferSeq = env.seq(carol);
1942 env(offer(carol, usd(50), XRP(500)));
1943
1944 auto const danOfferSeq = env.seq(dan);
1945 env(offer(dan, XRP(500), eur(50)));
1946
1948 jtp[0u][0u][jss::currency] = "XRP";
1949 env(pay(alice, bob, eur(30)), Json(jss::Paths, jtp), Sendmax(usd(333)));
1950
1951 auto jrr = ledgerEntryState(env, alice, gw1, "USD");
1952 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "470");
1953
1954 jrr = ledgerEntryState(env, bob, gw2, "EUR");
1955 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-30");
1956
1957 jrr = ledgerEntryState(env, carol, gw1, "USD");
1958 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-30");
1959
1960 jrr = ledgerEntryState(env, dan, gw2, "EUR");
1961 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-370");
1962
1963 auto jro = ledgerEntryOffer(env, carol, carolOfferSeq);
1964 BEAST_EXPECT(jro[jss::node][jss::TakerGets] == XRP(200).value().getText());
1965 BEAST_EXPECT(
1966 jro[jss::node][jss::TakerPays] == usd(20).value().getJson(JsonOptions::Values::None));
1967
1968 jro = ledgerEntryOffer(env, dan, danOfferSeq);
1969 BEAST_EXPECT(
1970 jro[jss::node][jss::TakerGets] ==
1971 gw2["EUR"](20).value().getJson(JsonOptions::Values::None));
1972 BEAST_EXPECT(jro[jss::node][jss::TakerPays] == XRP(200).value().getText());
1973 }
1974
1975 void
1977 {
1978 // At least with Taker bridging, a sensitivity was identified if the
1979 // second leg goes dry before the first one. This test exercises that
1980 // case.
1981 testcase("Auto Bridged Second Leg Dry");
1982
1983 using namespace jtx;
1984 Env env(*this, features);
1985
1986 Account const alice{"alice"};
1987 Account const bob{"bob"};
1988 Account const carol{"carol"};
1989 Account const gw{"gateway"};
1990 auto const usd = gw["USD"];
1991 auto const eur = gw["EUR"];
1992
1993 env.fund(XRP(100000000), alice, bob, carol, gw);
1994 env.close();
1995
1996 env.trust(usd(10), alice);
1997 env.close();
1998 env(pay(gw, alice, usd(10)));
1999 env.trust(usd(10), carol);
2000 env.close();
2001 env(pay(gw, carol, usd(3)));
2002
2003 env(offer(alice, eur(2), XRP(1)));
2004 env(offer(alice, eur(2), XRP(1)));
2005
2006 env(offer(alice, XRP(1), usd(4)));
2007 env(offer(carol, XRP(1), usd(3)));
2008 env.close();
2009
2010 // Bob offers to buy 10 USD for 10 EUR.
2011 // 1. He spends 2 EUR taking Alice's auto-bridged offers and
2012 // gets 4 USD for that.
2013 // 2. He spends another 2 EUR taking Alice's last EUR->XRP offer and
2014 // Carol's XRP-USD offer. He gets 3 USD for that.
2015 // The key for this test is that Alice's XRP->USD leg goes dry before
2016 // Alice's EUR->XRP. The XRP->USD leg is the second leg which showed
2017 // some sensitivity.
2018 env.trust(eur(10), bob);
2019 env.close();
2020 env(pay(gw, bob, eur(10)));
2021 env.close();
2022 env(offer(bob, usd(10), eur(10)));
2023 env.close();
2024
2025 env.require(Balance(bob, usd(7)));
2026 env.require(Balance(bob, eur(6)));
2027 env.require(Offers(bob, 1));
2028 env.require(Owners(bob, 3));
2029
2030 env.require(Balance(alice, usd(6)));
2031 env.require(Balance(alice, eur(4)));
2032 env.require(Offers(alice, 0));
2033 env.require(Owners(alice, 2));
2034
2035 env.require(Balance(carol, usd(0)));
2036 env.require(Balance(carol, eur(kNone)));
2037
2038 env.require(Offers(carol, 0));
2039 env.require(Owners(carol, 1));
2040 }
2041
2042 void
2044 {
2045 testcase("Offer Fees Consume Funds");
2046
2047 using namespace jtx;
2048
2049 Env env{*this, features};
2050
2051 auto const gw1 = Account{"gateway_1"};
2052 auto const gw2 = Account{"gateway_2"};
2053 auto const gw3 = Account{"gateway_3"};
2054 auto const alice = Account{"alice"};
2055 auto const bob = Account{"bob"};
2056 auto const usD1 = gw1["USD"];
2057 auto const usD2 = gw2["USD"];
2058 auto const usD3 = gw3["USD"];
2059
2060 // Provide micro amounts to compensate for fees to make results round
2061 // nice.
2062 // reserve: Alice has 3 entries in the ledger, via trust lines
2063 // fees:
2064 // 1 for each trust limit == 3 (alice < mtgox/amazon/bitstamp) +
2065 // 1 for payment == 4
2066 auto const startingXrp =
2067 XRP(100) + env.current()->fees().accountReserve(3, 1) + env.current()->fees().base * 4;
2068
2069 env.fund(startingXrp, gw1, gw2, gw3, alice, bob);
2070 env.close();
2071
2072 env(trust(alice, usD1(1000)));
2073 env(trust(alice, usD2(1000)));
2074 env(trust(alice, usD3(1000)));
2075 env(trust(bob, usD1(1000)));
2076 env(trust(bob, usD2(1000)));
2077
2078 env(pay(gw1, bob, bob["USD"](500)));
2079
2080 env(offer(bob, XRP(200), usD1(200)));
2081 // Alice has 350 fees - a reserve of 50 = 250 reserve = 100 available.
2082 // Ask for more than available to prove reserve works.
2083 env(offer(alice, usD1(200), XRP(200)));
2084
2085 auto jrr = ledgerEntryState(env, alice, gw1, "USD");
2086 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "100");
2087 jrr = ledgerEntryRoot(env, alice);
2088 BEAST_EXPECT(
2089 jrr[jss::node][sfBalance.fieldName] ==
2090 STAmount(env.current()->fees().accountReserve(3, 1)).getText());
2091
2092 jrr = ledgerEntryState(env, bob, gw1, "USD");
2093 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-400");
2094 }
2095
2096 void
2098 {
2099 testcase("Offer Create, then Cross");
2100
2101 using namespace jtx;
2102
2103 Env env{*this, features};
2104
2105 auto const gw = Account{"gateway"};
2106 auto const alice = Account{"alice"};
2107 auto const bob = Account{"bob"};
2108 auto const usd = gw["USD"];
2109
2110 env.fund(XRP(10000), gw, alice, bob);
2111 env.close();
2112
2113 env(rate(gw, 1.005));
2114
2115 env(trust(alice, usd(1000)));
2116 env(trust(bob, usd(1000)));
2117 env(trust(gw, alice["USD"](50)));
2118
2119 env(pay(gw, bob, bob["USD"](1)));
2120 env(pay(alice, gw, usd(50)));
2121
2122 env(trust(gw, alice["USD"](0)));
2123
2124 env(offer(alice, usd(50), XRP(150000)));
2125 env(offer(bob, XRP(100), usd(0.1)));
2126
2127 auto jrr = ledgerEntryState(env, alice, gw, "USD");
2128 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "49.96666666666667");
2129
2130 jrr = ledgerEntryState(env, bob, gw, "USD");
2131 json::Value const bobUSD = jrr[jss::node][sfBalance.fieldName][jss::value];
2132 BEAST_EXPECT(bobUSD == "-0.9665000000333333");
2133 }
2134
2135 void
2137 {
2138 testcase("Offer tfSell: Basic Sell");
2139
2140 using namespace jtx;
2141
2142 Env env{*this, features};
2143
2144 auto const gw = Account{"gateway"};
2145 auto const alice = Account{"alice"};
2146 auto const bob = Account{"bob"};
2147 auto const usd = gw["USD"];
2148
2149 auto const startingXrp =
2150 XRP(100) + env.current()->fees().accountReserve(1, 1) + env.current()->fees().base * 2;
2151
2152 env.fund(startingXrp, gw, alice, bob);
2153 env.close();
2154
2155 env(trust(alice, usd(1000)));
2156 env(trust(bob, usd(1000)));
2157
2158 env(pay(gw, bob, bob["USD"](500)));
2159
2160 env(offer(bob, XRP(200), usd(200)), Json(jss::Flags, tfSell));
2161 // Alice has 350 + fees - a reserve of 50 = 250 reserve = 100 available.
2162 // Alice has 350 + fees - a reserve of 50 = 250 reserve = 100 available.
2163 // Ask for more than available to prove reserve works.
2164 env(offer(alice, usd(200), XRP(200)), Json(jss::Flags, tfSell));
2165
2166 auto jrr = ledgerEntryState(env, alice, gw, "USD");
2167 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-100");
2168 jrr = ledgerEntryRoot(env, alice);
2169 BEAST_EXPECT(
2170 jrr[jss::node][sfBalance.fieldName] ==
2171 STAmount(env.current()->fees().accountReserve(1, 1)).getText());
2172
2173 jrr = ledgerEntryState(env, bob, gw, "USD");
2174 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-400");
2175 }
2176
2177 void
2179 {
2180 testcase("Offer tfSell: 2x Sell Exceed Limit");
2181
2182 using namespace jtx;
2183
2184 Env env{*this, features};
2185
2186 auto const gw = Account{"gateway"};
2187 auto const alice = Account{"alice"};
2188 auto const bob = Account{"bob"};
2189 auto const usd = gw["USD"];
2190
2191 auto const startingXrp =
2192 XRP(100) + env.current()->fees().accountReserve(1, 1) + env.current()->fees().base * 2;
2193
2194 env.fund(startingXrp, gw, alice, bob);
2195 env.close();
2196
2197 env(trust(alice, usd(150)));
2198 env(trust(bob, usd(1000)));
2199
2200 env(pay(gw, bob, bob["USD"](500)));
2201
2202 env(offer(bob, XRP(100), usd(200)));
2203 // Alice has 350 fees - a reserve of 50 = 250 reserve = 100 available.
2204 // Ask for more than available to prove reserve works.
2205 // Taker pays 100 USD for 100 XRP.
2206 // Selling XRP.
2207 // Will sell all 100 XRP and get more USD than asked for.
2208 env(offer(alice, usd(100), XRP(100)), Json(jss::Flags, tfSell));
2209
2210 auto jrr = ledgerEntryState(env, alice, gw, "USD");
2211 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-200");
2212 jrr = ledgerEntryRoot(env, alice);
2213 BEAST_EXPECT(
2214 jrr[jss::node][sfBalance.fieldName] ==
2215 STAmount(env.current()->fees().accountReserve(1, 1)).getText());
2216
2217 jrr = ledgerEntryState(env, bob, gw, "USD");
2218 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-300");
2219 }
2220
2221 void
2223 {
2224 testcase("Client Issue #535: Gateway Cross Currency");
2225
2226 using namespace jtx;
2227
2228 Env env{*this, features};
2229
2230 auto const gw = Account{"gateway"};
2231 auto const alice = Account{"alice"};
2232 auto const bob = Account{"bob"};
2233 auto const xts = gw["XTS"];
2234 auto const xxx = gw["XXX"];
2235
2236 auto const startingXrp = XRP(100.1) + env.current()->fees().accountReserve(1, 1) +
2237 env.current()->fees().base * 2;
2238
2239 env.fund(startingXrp, gw, alice, bob);
2240 env.close();
2241
2242 env(trust(alice, xts(1000)));
2243 env(trust(alice, xxx(1000)));
2244 env(trust(bob, xts(1000)));
2245 env(trust(bob, xxx(1000)));
2246
2247 env(pay(gw, alice, alice["XTS"](100)));
2248 env(pay(gw, alice, alice["XXX"](100)));
2249 env(pay(gw, bob, bob["XTS"](100)));
2250 env(pay(gw, bob, bob["XXX"](100)));
2251
2252 env(offer(alice, xts(100), xxx(100)));
2253
2254 // WS client is used here because the RPC client could not
2255 // be convinced to pass the build_path argument
2256 auto wsc = makeWSClient(env.app().config());
2257 json::Value payment;
2258 payment[jss::secret] = toBase58(generateSeed("bob"));
2259 payment[jss::id] = env.seq(bob);
2260 payment[jss::build_path] = true;
2261 payment[jss::tx_json] = pay(bob, bob, bob["XXX"](1));
2262 payment[jss::tx_json][jss::Sequence] =
2263 env.current()->read(keylet::account(bob.id()))->getFieldU32(sfSequence);
2264 payment[jss::tx_json][jss::Fee] = to_string(env.current()->fees().base);
2265 payment[jss::tx_json][jss::SendMax] =
2266 bob["XTS"](1.5).value().getJson(JsonOptions::Values::None);
2267 auto jrr = wsc->invoke("submit", payment);
2268 BEAST_EXPECT(jrr[jss::status] == "success");
2269 BEAST_EXPECT(jrr[jss::result][jss::engine_result] == "tesSUCCESS");
2270 if (wsc->version() == 2)
2271 {
2272 BEAST_EXPECT(hasEnvelope2(jrr));
2273 BEAST_EXPECT(jrr.isMember(jss::id) && jrr[jss::id] == 5);
2274 }
2275
2276 jrr = ledgerEntryState(env, alice, gw, "XTS");
2277 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-101");
2278 jrr = ledgerEntryState(env, alice, gw, "XXX");
2279 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-99");
2280
2281 jrr = ledgerEntryState(env, bob, gw, "XTS");
2282 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-99");
2283 jrr = ledgerEntryState(env, bob, gw, "XXX");
2284 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-101");
2285 }
2286
2287 // Helper function that validates a *defaulted* trustline: one that has
2288 // no unusual flags set and doesn't have high or low limits set. Such a
2289 // trustline may have an actual balance (it can be created automatically
2290 // if a user places an offer to acquire an IOU for which they don't have
2291 // a trust line defined). If the trustline is not defaulted then the tests
2292 // will not pass.
2293 void
2295 jtx::Env& env,
2296 jtx::Account const& account,
2297 jtx::PrettyAmount const& expectBalance)
2298 {
2299 auto const& issue = expectBalance.value().get<Issue>();
2300 auto const sleTrust = env.le(keylet::trustLine(account.id(), issue));
2301 BEAST_EXPECT(sleTrust);
2302 if (sleTrust)
2303 {
2304 bool const accountLow = account.id() < issue.account;
2305
2306 STAmount low{issue};
2307 STAmount high{issue};
2308
2309 low.get<Issue>().account = (accountLow ? account.id() : issue.account);
2310 high.get<Issue>().account = (accountLow ? issue.account : account.id());
2311
2312 BEAST_EXPECT(sleTrust->getFieldAmount(sfLowLimit) == low);
2313 BEAST_EXPECT(sleTrust->getFieldAmount(sfHighLimit) == high);
2314
2315 STAmount actualBalance{sleTrust->getFieldAmount(sfBalance)};
2316 if (!accountLow)
2317 actualBalance.negate();
2318
2319 BEAST_EXPECT(actualBalance == expectBalance);
2320 }
2321 }
2322
2323 void
2325 {
2326 // Test a number of different corner cases regarding adding a
2327 // possibly crossable offer to an account. The test is table
2328 // driven so it should be easy to add or remove tests.
2329 testcase("Partial Crossing");
2330
2331 using namespace jtx;
2332
2333 auto const gw = Account("gateway");
2334 auto const usd = gw["USD"];
2335
2336 Env env{*this, features};
2337
2338 env.fund(XRP(10000000), gw);
2339 env.close();
2340
2341 // The fee that's charged for transactions
2342 auto const f = env.current()->fees().base;
2343
2344 // To keep things simple all offers are 1 : 1 for XRP : USD.
2345 enum class PreTrustType { NoPreTrust, GwPreTrust, AcctPreTrust };
2346 struct TestData
2347 {
2348 std::string account; // Account operated on
2349 STAmount fundXrp; // Account funded with
2350 int bookAmount; // USD -> XRP offer on the books
2351 PreTrustType preTrust; // If true, pre-establish trust line
2352 int offerAmount; // Account offers this much XRP -> USD
2353 TER tec; // Returned tec code
2354 STAmount spentXrp; // Amount removed from fundXrp
2355 PrettyAmount balanceUsd; // Balance on account end
2356 int offers; // Offers on account
2357 int owners; // Owners on account
2358 };
2359
2360 // clang-format off
2361 TestData const tests[]{
2362 // acct fundXrp bookAmt preTrust offerAmount tec spentXrp balanceUSD offers owners
2363 {.account="ann", .fundXrp=reserve(env, 0) + 0 * f, .bookAmount=1, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tecUNFUNDED_OFFER, .spentXrp=f, .balanceUsd=usd( 0), .offers=0, .owners=0}, // Account is at the reserve, and will dip below once fees are subtracted.
2364 {.account="bev", .fundXrp=reserve(env, 0) + 1 * f, .bookAmount=1, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tecUNFUNDED_OFFER, .spentXrp=f, .balanceUsd=usd( 0), .offers=0, .owners=0}, // Account has just enough for the reserve and the fee.
2365 {.account="cam", .fundXrp=reserve(env, 0) + 2 * f, .bookAmount=0, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tecINSUF_RESERVE_OFFER, .spentXrp=f, .balanceUsd=usd( 0), .offers=0, .owners=0}, // Account has enough for the reserve, the fee and the offer, and a bit more, but not enough for the reserve after the offer is placed.
2366 {.account="deb", .fundXrp=drops(10) + reserve(env, 0) + 1 * f, .bookAmount=1, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=drops(10) + f, .balanceUsd=usd(0.00001), .offers=0, .owners=1}, // Account has enough to buy a little USD then the offer runs dry.
2367 {.account="eve", .fundXrp=reserve(env, 1) + 0 * f, .bookAmount=0, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=f, .balanceUsd=usd( 0), .offers=1, .owners=1}, // No offer to cross
2368 {.account="flo", .fundXrp=reserve(env, 1) + 0 * f, .bookAmount=1, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 1) + f, .balanceUsd=usd( 1), .offers=0, .owners=1},
2369 {.account="gay", .fundXrp=reserve(env, 1) + 1 * f, .bookAmount=1000, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 50) + f, .balanceUsd=usd( 50), .offers=0, .owners=1},
2370 {.account="hye", .fundXrp=XRP(1000) + 1 * f, .bookAmount=1000, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 800) + f, .balanceUsd=usd( 800), .offers=0, .owners=1},
2371 {.account="ivy", .fundXrp=XRP( 1) + reserve(env, 1) + 1 * f, .bookAmount=1, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 1) + f, .balanceUsd=usd( 1), .offers=0, .owners=1},
2372 {.account="joy", .fundXrp=XRP( 1) + reserve(env, 2) + 1 * f, .bookAmount=1, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 1) + f, .balanceUsd=usd( 1), .offers=1, .owners=2},
2373 {.account="kim", .fundXrp=XRP( 900) + reserve(env, 2) + 1 * f, .bookAmount=999, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 999) + f, .balanceUsd=usd( 999), .offers=0, .owners=1},
2374 {.account="liz", .fundXrp=XRP( 998) + reserve(env, 0) + 1 * f, .bookAmount=999, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 998) + f, .balanceUsd=usd( 998), .offers=0, .owners=1},
2375 {.account="meg", .fundXrp=XRP( 998) + reserve(env, 1) + 1 * f, .bookAmount=999, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 999) + f, .balanceUsd=usd( 999), .offers=0, .owners=1},
2376 {.account="nia", .fundXrp=XRP( 998) + reserve(env, 2) + 1 * f, .bookAmount=999, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 999) + f, .balanceUsd=usd( 999), .offers=1, .owners=2},
2377 {.account="ova", .fundXrp=XRP( 999) + reserve(env, 0) + 1 * f, .bookAmount=1000, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 999) + f, .balanceUsd=usd( 999), .offers=0, .owners=1},
2378 {.account="pam", .fundXrp=XRP( 999) + reserve(env, 1) + 1 * f, .bookAmount=1000, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP(1000) + f, .balanceUsd=usd( 1000), .offers=0, .owners=1},
2379 {.account="rae", .fundXrp=XRP( 999) + reserve(env, 2) + 1 * f, .bookAmount=1000, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP(1000) + f, .balanceUsd=usd( 1000), .offers=0, .owners=1},
2380 {.account="sue", .fundXrp=XRP(1000) + reserve(env, 2) + 1 * f, .bookAmount=0, .preTrust=PreTrustType::NoPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=f, .balanceUsd=usd( 0), .offers=1, .owners=1},
2381 //---------------- Pre-established trust lines ---------------------
2382 {.account="abe", .fundXrp=reserve(env, 0) + 0 * f, .bookAmount=1, .preTrust=PreTrustType::GwPreTrust, .offerAmount=1000, .tec=tecUNFUNDED_OFFER, .spentXrp=f, .balanceUsd=usd( 0), .offers=0, .owners=0},
2383 {.account="bud", .fundXrp=reserve(env, 0) + 1 * f, .bookAmount=1, .preTrust=PreTrustType::GwPreTrust, .offerAmount=1000, .tec=tecUNFUNDED_OFFER, .spentXrp=f, .balanceUsd=usd( 0), .offers=0, .owners=0},
2384 {.account="che", .fundXrp=reserve(env, 0) + 2 * f, .bookAmount=0, .preTrust=PreTrustType::GwPreTrust, .offerAmount=1000, .tec=tecINSUF_RESERVE_OFFER, .spentXrp=f, .balanceUsd=usd( 0), .offers=0, .owners=0},
2385 {.account="dan", .fundXrp=drops(10) + reserve(env, 0) + 1 * f, .bookAmount=1, .preTrust=PreTrustType::GwPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=drops(10) + f, .balanceUsd=usd(0.00001), .offers=0, .owners=0},
2386 {.account="eli", .fundXrp=XRP( 20) + reserve(env, 0) + 1 * f, .bookAmount=1000, .preTrust=PreTrustType::GwPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP(20) + 1 * f, .balanceUsd=usd( 20), .offers=0, .owners=0},
2387 {.account="fyn", .fundXrp=reserve(env, 1) + 0 * f, .bookAmount=0, .preTrust=PreTrustType::GwPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=f, .balanceUsd=usd( 0), .offers=1, .owners=1},
2388 {.account="gar", .fundXrp=reserve(env, 1) + 0 * f, .bookAmount=1, .preTrust=PreTrustType::GwPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 1) + f, .balanceUsd=usd( 1), .offers=1, .owners=1},
2389 {.account="hal", .fundXrp=reserve(env, 1) + 1 * f, .bookAmount=1, .preTrust=PreTrustType::GwPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 1) + f, .balanceUsd=usd( 1), .offers=1, .owners=1},
2390
2391 {.account="ned", .fundXrp=reserve(env, 1) + 0 * f, .bookAmount=1, .preTrust=PreTrustType::AcctPreTrust, .offerAmount=1000, .tec=tecUNFUNDED_OFFER, .spentXrp=2 * f, .balanceUsd=usd( 0), .offers=0, .owners=1},
2392 {.account="ole", .fundXrp=reserve(env, 1) + 1 * f, .bookAmount=1, .preTrust=PreTrustType::AcctPreTrust, .offerAmount=1000, .tec=tecUNFUNDED_OFFER, .spentXrp=2 * f, .balanceUsd=usd( 0), .offers=0, .owners=1},
2393 {.account="pat", .fundXrp=reserve(env, 1) + 2 * f, .bookAmount=0, .preTrust=PreTrustType::AcctPreTrust, .offerAmount=1000, .tec=tecUNFUNDED_OFFER, .spentXrp=2 * f, .balanceUsd=usd( 0), .offers=0, .owners=1},
2394 {.account="quy", .fundXrp=reserve(env, 1) + 2 * f, .bookAmount=1, .preTrust=PreTrustType::AcctPreTrust, .offerAmount=1000, .tec=tecUNFUNDED_OFFER, .spentXrp=2 * f, .balanceUsd=usd( 0), .offers=0, .owners=1},
2395 {.account="ron", .fundXrp=reserve(env, 1) + 3 * f, .bookAmount=0, .preTrust=PreTrustType::AcctPreTrust, .offerAmount=1000, .tec=tecINSUF_RESERVE_OFFER, .spentXrp=2 * f, .balanceUsd=usd( 0), .offers=0, .owners=1},
2396 {.account="syd", .fundXrp=drops(10) + reserve(env, 1) + 2 * f, .bookAmount=1, .preTrust=PreTrustType::AcctPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=drops(10) + 2 * f, .balanceUsd=usd(0.00001), .offers=0, .owners=1},
2397 {.account="ted", .fundXrp=XRP( 20) + reserve(env, 1) + 2 * f, .bookAmount=1000, .preTrust=PreTrustType::AcctPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP(20) + 2 * f, .balanceUsd=usd( 20), .offers=0, .owners=1},
2398 {.account="uli", .fundXrp=reserve(env, 2) + 0 * f, .bookAmount=0, .preTrust=PreTrustType::AcctPreTrust, .offerAmount=1000, .tec=tecINSUF_RESERVE_OFFER, .spentXrp=2 * f, .balanceUsd=usd( 0), .offers=0, .owners=1},
2399 {.account="vic", .fundXrp=reserve(env, 2) + 0 * f, .bookAmount=1, .preTrust=PreTrustType::AcctPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 1) + 2 * f, .balanceUsd=usd( 1), .offers=0, .owners=1},
2400 {.account="wes", .fundXrp=reserve(env, 2) + 1 * f, .bookAmount=0, .preTrust=PreTrustType::AcctPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=2 * f, .balanceUsd=usd( 0), .offers=1, .owners=2},
2401 {.account="xan", .fundXrp=reserve(env, 2) + 1 * f, .bookAmount=1, .preTrust=PreTrustType::AcctPreTrust, .offerAmount=1000, .tec=tesSUCCESS, .spentXrp=XRP( 1) + 2 * f, .balanceUsd=usd( 1), .offers=1, .owners=2},
2402 };
2403 // clang-format on
2404
2405 for (auto const& t : tests)
2406 {
2407 auto const acct = Account(t.account);
2408 env.fund(t.fundXrp, acct);
2409 env.close();
2410
2411 // Make sure gateway has no current offers.
2412 env.require(Offers(gw, 0));
2413
2414 // The gateway optionally creates an offer that would be crossed.
2415 auto const book = t.bookAmount;
2416 if (book != 0)
2417 env(offer(gw, XRP(book), usd(book)));
2418 env.close();
2419 std::uint32_t const gwOfferSeq = env.seq(gw) - 1;
2420
2421 // Optionally pre-establish a trustline between gw and acct.
2422 if (t.preTrust == PreTrustType::GwPreTrust)
2423 env(trust(gw, acct["USD"](1)));
2424 env.close();
2425
2426 // Optionally pre-establish a trustline between acct and gw.
2427 // Note this is not really part of the test, so we expect there
2428 // to be enough XRP reserve for acct to create the trust line.
2429 if (t.preTrust == PreTrustType::AcctPreTrust)
2430 env(trust(acct, usd(1)));
2431 env.close();
2432
2433 {
2434 // Acct creates an offer. This is the heart of the test.
2435 auto const acctOffer = t.offerAmount;
2436 env(offer(acct, usd(acctOffer), XRP(acctOffer)), Ter(t.tec));
2437 env.close();
2438 }
2439 std::uint32_t const acctOfferSeq = env.seq(acct) - 1;
2440
2441 BEAST_EXPECT(env.balance(acct, usd) == t.balanceUsd);
2442 BEAST_EXPECT(env.balance(acct, xrpIssue()) == t.fundXrp - t.spentXrp);
2443 env.require(Offers(acct, t.offers));
2444 env.require(Owners(acct, t.owners));
2445
2446 auto acctOffers = offersOnAccount(env, acct);
2447 BEAST_EXPECT(acctOffers.size() == t.offers);
2448 if (!acctOffers.empty() && (t.offers != 0))
2449 {
2450 auto const& acctOffer = *acctOffers.front();
2451
2452 auto const leftover = t.offerAmount - t.bookAmount;
2453 BEAST_EXPECT(acctOffer[sfTakerGets] == XRP(leftover));
2454 BEAST_EXPECT(acctOffer[sfTakerPays] == usd(leftover));
2455 }
2456
2457 if (t.preTrust == PreTrustType::NoPreTrust)
2458 {
2459 if (t.balanceUsd.value().signum() != 0)
2460 {
2461 // Verify the correct contents of the trustline
2462 verifyDefaultTrustline(env, acct, t.balanceUsd);
2463 }
2464 else
2465 {
2466 // Verify that no trustline was created.
2467 auto const sleTrust = env.le(keylet::trustLine(acct, usd));
2468 BEAST_EXPECT(!sleTrust);
2469 }
2470 }
2471
2472 // Give the next loop a clean slate by canceling any left-overs
2473 // in the offers.
2474 env(offerCancel(acct, acctOfferSeq));
2475 env(offerCancel(gw, gwOfferSeq));
2476 env.close();
2477 }
2478 }
2479
2480 void
2482 {
2483 testcase("XRP Direct Crossing");
2484
2485 using namespace jtx;
2486
2487 auto const gw = Account("gateway");
2488 auto const alice = Account("alice");
2489 auto const bob = Account("bob");
2490 auto const usd = gw["USD"];
2491
2492 auto const usdOffer = usd(1000);
2493 auto const xrpOffer = XRP(1000);
2494
2495 Env env{*this, features};
2496
2497 env.fund(XRP(1000000), gw, bob);
2498 env.close();
2499
2500 // The fee that's charged for transactions.
2501 auto const fee = env.current()->fees().base;
2502
2503 // alice's account has enough for the reserve, one trust line plus two
2504 // offers, and two fees.
2505 env.fund(reserve(env, 2) + fee * 2, alice);
2506 env.close();
2507
2508 env(trust(alice, usdOffer));
2509
2510 env.close();
2511
2512 env(pay(gw, alice, usdOffer));
2513 env.close();
2514 env.require(Balance(alice, usdOffer), Offers(alice, 0), Offers(bob, 0));
2515
2516 // The scenario:
2517 // o alice has USD but wants XRP.
2518 // o bob has XRP but wants USD.
2519 auto const aliceXRP = env.balance(alice);
2520 auto const bobXRP = env.balance(bob);
2521
2522 env(offer(alice, xrpOffer, usdOffer));
2523 env.close();
2524 env(offer(bob, usdOffer, xrpOffer));
2525
2526 env.close();
2527 env.require(
2528 Balance(alice, usd(0)),
2529 Balance(bob, usdOffer),
2530 Balance(alice, aliceXRP + xrpOffer - fee),
2531 Balance(bob, bobXRP - xrpOffer - fee),
2532 Offers(alice, 0),
2533 Offers(bob, 0));
2534
2535 verifyDefaultTrustline(env, bob, usdOffer);
2536
2537 // Make two more offers that leave one of the offers non-dry.
2538 env(offer(alice, usd(999), XRP(999)));
2539 env(offer(bob, xrpOffer, usdOffer));
2540
2541 env.close();
2542 env.require(Balance(alice, usd(999)));
2543 env.require(Balance(bob, usd(1)));
2544 env.require(Offers(alice, 0));
2545 verifyDefaultTrustline(env, bob, usd(1));
2546 {
2547 auto const bobOffers = offersOnAccount(env, bob);
2548 BEAST_EXPECT(bobOffers.size() == 1);
2549 auto const& bobOffer = *bobOffers.front();
2550
2551 BEAST_EXPECT(bobOffer[sfLedgerEntryType] == ltOFFER);
2552 BEAST_EXPECT(bobOffer[sfTakerGets] == usd(1));
2553 BEAST_EXPECT(bobOffer[sfTakerPays] == XRP(1));
2554 }
2555 }
2556
2557 void
2559 {
2560 testcase("Direct Crossing");
2561
2562 using namespace jtx;
2563
2564 auto const gw = Account("gateway");
2565 auto const alice = Account("alice");
2566 auto const bob = Account("bob");
2567 auto const usd = gw["USD"];
2568 auto const eur = gw["EUR"];
2569
2570 auto const usdOffer = usd(1000);
2571 auto const eurOffer = eur(1000);
2572
2573 Env env{*this, features};
2574
2575 env.fund(XRP(1000000), gw);
2576 env.close();
2577
2578 // The fee that's charged for transactions.
2579 auto const fee = env.current()->fees().base;
2580
2581 // Each account has enough for the reserve, two trust lines, one
2582 // offer, and two fees.
2583 env.fund(reserve(env, 3) + fee * 3, alice);
2584 env.fund(reserve(env, 3) + fee * 2, bob);
2585 env.close();
2586
2587 env(trust(alice, usdOffer));
2588 env(trust(bob, eurOffer));
2589 env.close();
2590
2591 env(pay(gw, alice, usdOffer));
2592 env(pay(gw, bob, eurOffer));
2593 env.close();
2594
2595 env.require(Balance(alice, usdOffer), Balance(bob, eurOffer));
2596
2597 // The scenario:
2598 // o alice has USD but wants EUR.
2599 // o bob has EUR but wants USD.
2600 env(offer(alice, eurOffer, usdOffer));
2601 env(offer(bob, usdOffer, eurOffer));
2602
2603 env.close();
2604 env.require(
2605 Balance(alice, eurOffer), Balance(bob, usdOffer), Offers(alice, 0), Offers(bob, 0));
2606
2607 // Alice's offer crossing created a default EUR trustline and
2608 // Bob's offer crossing created a default USD trustline:
2609 verifyDefaultTrustline(env, alice, eurOffer);
2610 verifyDefaultTrustline(env, bob, usdOffer);
2611
2612 // Make two more offers that leave one of the offers non-dry.
2613 // Guarantee the order of application by putting a close()
2614 // between them.
2615 env(offer(bob, eurOffer, usdOffer));
2616 env.close();
2617
2618 env(offer(alice, usd(999), eurOffer));
2619 env.close();
2620
2621 env.require(Offers(alice, 0));
2622 env.require(Offers(bob, 1));
2623
2624 env.require(Balance(alice, usd(999)));
2625 env.require(Balance(alice, eur(1)));
2626 env.require(Balance(bob, usd(1)));
2627 env.require(Balance(bob, eur(999)));
2628
2629 {
2630 auto bobOffers = offersOnAccount(env, bob);
2631 if (BEAST_EXPECT(bobOffers.size() == 1))
2632 {
2633 auto const& bobOffer = *bobOffers.front();
2634
2635 BEAST_EXPECT(bobOffer[sfTakerGets] == usd(1));
2636 BEAST_EXPECT(bobOffer[sfTakerPays] == eur(1));
2637 }
2638 }
2639
2640 // alice makes one more offer that cleans out bob's offer.
2641 env(offer(alice, usd(1), eur(1)));
2642 env.close();
2643
2644 env.require(Balance(alice, usd(1000)));
2645 env.require(Balance(alice, eur(kNone)));
2646 env.require(Balance(bob, usd(kNone)));
2647 env.require(Balance(bob, eur(1000)));
2648 env.require(Offers(alice, 0));
2649 env.require(Offers(bob, 0));
2650
2651 // The two trustlines that were generated by offers should be gone.
2652 BEAST_EXPECT(!env.le(keylet::trustLine(alice.id(), eur)));
2653 BEAST_EXPECT(!env.le(keylet::trustLine(bob.id(), usd)));
2654
2655 // Make two more offers that leave one of the offers non-dry. We
2656 // need to properly sequence the transactions:
2657 env(offer(alice, eur(999), usdOffer));
2658 env.close();
2659
2660 env(offer(bob, usdOffer, eurOffer));
2661 env.close();
2662
2663 env.require(Offers(alice, 0));
2664 env.require(Offers(bob, 0));
2665
2666 env.require(Balance(alice, usd(0)));
2667 env.require(Balance(alice, eur(999)));
2668 env.require(Balance(bob, usd(1000)));
2669 env.require(Balance(bob, eur(1)));
2670 }
2671
2672 void
2674 {
2675 testcase("Bridged Crossing");
2676
2677 using namespace jtx;
2678
2679 auto const gw = Account("gateway");
2680 auto const alice = Account("alice");
2681 auto const bob = Account("bob");
2682 auto const carol = Account("carol");
2683 auto const usd = gw["USD"];
2684 auto const eur = gw["EUR"];
2685
2686 auto const usdOffer = usd(1000);
2687 auto const eurOffer = eur(1000);
2688
2689 Env env{*this, features};
2690
2691 env.fund(XRP(1000000), gw, alice, bob, carol);
2692 env.close();
2693
2694 env(trust(alice, usdOffer));
2695 env(trust(carol, eurOffer));
2696 env.close();
2697 env(pay(gw, alice, usdOffer));
2698 env(pay(gw, carol, eurOffer));
2699 env.close();
2700
2701 // The scenario:
2702 // o alice has USD but wants XRP.
2703 // o bob has XRP but wants EUR.
2704 // o carol has EUR but wants USD.
2705 // Note that carol's offer must come last. If carol's offer is placed
2706 // before bob's or alice's, then autobridging will not occur.
2707 env(offer(alice, XRP(1000), usdOffer));
2708 env(offer(bob, eurOffer, XRP(1000)));
2709 auto const bobXrpBalance = env.balance(bob);
2710 env.close();
2711
2712 // carol makes an offer that partially consumes alice and bob's offers.
2713 env(offer(carol, usd(400), eur(400)));
2714 env.close();
2715
2716 env.require(
2717 Balance(alice, usd(600)),
2718 Balance(bob, eur(400)),
2719 Balance(carol, usd(400)),
2720 Balance(bob, bobXrpBalance - XRP(400)),
2721 Offers(carol, 0));
2722 verifyDefaultTrustline(env, bob, eur(400));
2723 verifyDefaultTrustline(env, carol, usd(400));
2724 {
2725 auto const aliceOffers = offersOnAccount(env, alice);
2726 BEAST_EXPECT(aliceOffers.size() == 1);
2727 auto const& aliceOffer = *aliceOffers.front();
2728
2729 BEAST_EXPECT(aliceOffer[sfLedgerEntryType] == ltOFFER);
2730 BEAST_EXPECT(aliceOffer[sfTakerGets] == usd(600));
2731 BEAST_EXPECT(aliceOffer[sfTakerPays] == XRP(600));
2732 }
2733 {
2734 auto const bobOffers = offersOnAccount(env, bob);
2735 BEAST_EXPECT(bobOffers.size() == 1);
2736 auto const& bobOffer = *bobOffers.front();
2737
2738 BEAST_EXPECT(bobOffer[sfLedgerEntryType] == ltOFFER);
2739 BEAST_EXPECT(bobOffer[sfTakerGets] == XRP(600));
2740 BEAST_EXPECT(bobOffer[sfTakerPays] == eur(600));
2741 }
2742
2743 // carol makes an offer that exactly consumes alice and bob's offers.
2744 env(offer(carol, usd(600), eur(600)));
2745 env.close();
2746
2747 env.require(
2748 Balance(alice, usd(0)),
2749 Balance(bob, eurOffer),
2750 Balance(carol, usdOffer),
2751 Balance(bob, bobXrpBalance - XRP(1000)),
2752 Offers(bob, 0),
2753 Offers(carol, 0));
2754 verifyDefaultTrustline(env, bob, eur(1000));
2755 verifyDefaultTrustline(env, carol, usd(1000));
2756
2757 // In pre-flow code alice's offer is left empty in the ledger.
2758 auto const aliceOffers = offersOnAccount(env, alice);
2759 if (!aliceOffers.empty())
2760 {
2761 BEAST_EXPECT(aliceOffers.size() == 1);
2762 auto const& aliceOffer = *aliceOffers.front();
2763
2764 BEAST_EXPECT(aliceOffer[sfLedgerEntryType] == ltOFFER);
2765 BEAST_EXPECT(aliceOffer[sfTakerGets] == usd(0));
2766 BEAST_EXPECT(aliceOffer[sfTakerPays] == XRP(0));
2767 }
2768 }
2769
2770 void
2772 {
2773 // Test a number of different corner cases regarding offer crossing
2774 // when the tfSell flag is set. The test is table driven so it
2775 // should be easy to add or remove tests.
2776 testcase("Sell Offer");
2777
2778 using namespace jtx;
2779
2780 auto const gw = Account("gateway");
2781 auto const usd = gw["USD"];
2782
2783 Env env{*this, features};
2784
2785 env.fund(XRP(10000000), gw);
2786 env.close();
2787
2788 // The fee that's charged for transactions
2789 auto const f = env.current()->fees().base;
2790
2791 // To keep things simple all offers are 1 : 1 for XRP : USD.
2792 enum class PreTrustType { NoPreTrust, GwPreTrust, AcctPreTrust };
2793 struct TestData
2794 {
2795 std::string account; // Account operated on
2796 STAmount fundXrp; // XRP acct funded with
2797 STAmount fundUSD; // USD acct funded with
2798 STAmount gwGets; // gw's offer
2799 STAmount gwPays; //
2800 STAmount acctGets; // acct's offer
2801 STAmount acctPays; //
2802 TER tec; // Returned tec code
2803 STAmount spentXrp; // Amount removed from fundXrp
2804 STAmount finalUsd; // Final USD balance on acct
2805 int offers; // Offers on acct
2806 int owners; // Owners on acct
2807 STAmount takerGets; // Remainder of acct's offer
2808 STAmount takerPays; //
2809
2810 // Constructor with takerGets/takerPays
2811 TestData(
2812 std::string&& account, // Account operated on
2813 STAmount fundXrp, // XRP acct funded with
2814 STAmount fundUsd, // USD acct funded with
2815 STAmount gwGets, // gw's offer
2816 STAmount gwPays, //
2817 STAmount acctGets, // acct's offer
2818 STAmount acctPays, //
2819 TER tec, // Returned tec code
2820 STAmount spentXrp, // Amount removed from fundXrp
2821 STAmount finalUsd, // Final USD balance on acct
2822 int offers, // Offers on acct
2823 int owners, // Owners on acct
2824 STAmount takerGets, // Remainder of acct's offer
2825 STAmount takerPays) //
2826 : account(std::move(account))
2827 , fundXrp(std::move(fundXrp))
2828 , fundUSD(std::move(fundUsd))
2829 , gwGets(std::move(gwGets))
2830 , gwPays(std::move(gwPays))
2831 , acctGets(std::move(acctGets))
2832 , acctPays(std::move(acctPays))
2833 , tec(tec)
2834 , spentXrp(std::move(spentXrp))
2835 , finalUsd(std::move(finalUsd))
2836 , offers(offers)
2837 , owners(owners)
2838 , takerGets(std::move(takerGets))
2839 , takerPays(std::move(takerPays))
2840 {
2841 }
2842
2843 // Constructor without takerGets/takerPays
2844 TestData(
2845 std::string&& account, // Account operated on
2846 STAmount const& fundXrp, // XRP acct funded with
2847 STAmount const& fundUsd, // USD acct funded with
2848 STAmount const& gwGets, // gw's offer
2849 STAmount const& gwPays, //
2850 STAmount const& acctGets, // acct's offer
2851 STAmount const& acctPays, //
2852 TER tec, // Returned tec code
2853 STAmount const& spentXrp, // Amount removed from fundXrp
2854 STAmount const& finalUsd, // Final USD balance on acct
2855 int offers, // Offers on acct
2856 int owners) // Owners on acct
2857 : TestData(
2858 std::move(account),
2859 fundXrp,
2860 fundUsd,
2861 gwGets,
2862 gwPays,
2863 acctGets,
2864 acctPays,
2865 tec,
2866 spentXrp,
2867 finalUsd,
2868 offers,
2869 owners,
2870 STAmount{0},
2871 STAmount{0})
2872 {
2873 }
2874 };
2875
2876 // clang-format off
2877 TestData const tests[]{
2878 // acct pays XRP
2879 // acct fundXrp fundUSD gwGets gwPays acctGets acctPays tec spentXrp finalUSD offers owners takerGets takerPays
2880 {"ann", XRP(10) + reserve(env, 0) + 1 * f, usd( 0), XRP(10), usd( 5), usd(10), XRP(10), tecINSUF_RESERVE_OFFER, XRP( 0) + (1 * f), usd( 0), 0, 0},
2881 {"bev", XRP(10) + reserve(env, 1) + 1 * f, usd( 0), XRP(10), usd( 5), usd(10), XRP(10), tesSUCCESS, XRP( 0) + (1 * f), usd( 0), 1, 1, XRP(10), usd(10)},
2882 {"cam", XRP(10) + reserve(env, 0) + 1 * f, usd( 0), XRP(10), usd(10), usd(10), XRP(10), tesSUCCESS, XRP( 10) + (1 * f), usd(10), 0, 1},
2883 {"deb", XRP(10) + reserve(env, 0) + 1 * f, usd( 0), XRP(10), usd(20), usd(10), XRP(10), tesSUCCESS, XRP( 10) + (1 * f), usd(20), 0, 1},
2884 {"eve", XRP(10) + reserve(env, 0) + 1 * f, usd( 0), XRP(10), usd(20), usd( 5), XRP( 5), tesSUCCESS, XRP( 5) + (1 * f), usd(10), 0, 1},
2885 {"flo", XRP(10) + reserve(env, 0) + 1 * f, usd( 0), XRP(10), usd(20), usd(20), XRP(20), tesSUCCESS, XRP( 10) + (1 * f), usd(20), 0, 1},
2886 {"gay", XRP(20) + reserve(env, 1) + 1 * f, usd( 0), XRP(10), usd(20), usd(20), XRP(20), tesSUCCESS, XRP( 10) + (1 * f), usd(20), 0, 1},
2887 {"hye", XRP(20) + reserve(env, 2) + 1 * f, usd( 0), XRP(10), usd(20), usd(20), XRP(20), tesSUCCESS, XRP( 10) + (1 * f), usd(20), 1, 2, XRP(10), usd(10)},
2888 // acct pays USD
2889 {"meg", reserve(env, 1) + 2 * f, usd(10), usd(10), XRP( 5), XRP(10), usd(10), tecINSUF_RESERVE_OFFER, XRP( 0) + (2 * f), usd(10), 0, 1},
2890 {"nia", reserve(env, 2) + 2 * f, usd(10), usd(10), XRP( 5), XRP(10), usd(10), tesSUCCESS, XRP( 0) + (2 * f), usd(10), 1, 2, usd(10), XRP(10)},
2891 {"ova", reserve(env, 1) + 2 * f, usd(10), usd(10), XRP(10), XRP(10), usd(10), tesSUCCESS, XRP(-10) + (2 * f), usd( 0), 0, 1},
2892 {"pam", reserve(env, 1) + 2 * f, usd(10), usd(10), XRP(20), XRP(10), usd(10), tesSUCCESS, XRP(-20) + (2 * f), usd( 0), 0, 1},
2893 {"qui", reserve(env, 1) + 2 * f, usd(10), usd(20), XRP(40), XRP(10), usd(10), tesSUCCESS, XRP(-20) + (2 * f), usd( 0), 0, 1},
2894 {"rae", reserve(env, 2) + 2 * f, usd(10), usd( 5), XRP( 5), XRP(10), usd(10), tesSUCCESS, XRP( -5) + (2 * f), usd( 5), 1, 2, usd( 5), XRP( 5)},
2895 {"sue", reserve(env, 2) + 2 * f, usd(10), usd( 5), XRP(10), XRP(10), usd(10), tesSUCCESS, XRP(-10) + (2 * f), usd( 5), 1, 2, usd( 5), XRP( 5)},
2896 };
2897 // clang-format on
2898
2899 auto const zeroUsd = usd(0);
2900 for (auto const& t : tests)
2901 {
2902 // Make sure gateway has no current offers.
2903 env.require(Offers(gw, 0));
2904
2905 auto const acct = Account(t.account);
2906
2907 env.fund(t.fundXrp, acct);
2908 env.close();
2909
2910 // Optionally give acct some USD. This is not part of the test,
2911 // so we assume that acct has sufficient USD to cover the reserve
2912 // on the trust line.
2913 if (t.fundUSD != zeroUsd)
2914 {
2915 env(trust(acct, t.fundUSD));
2916 env.close();
2917 env(pay(gw, acct, t.fundUSD));
2918 env.close();
2919 }
2920
2921 env(offer(gw, t.gwGets, t.gwPays));
2922 env.close();
2923 std::uint32_t const gwOfferSeq = env.seq(gw) - 1;
2924
2925 // Acct creates a tfSell offer. This is the heart of the test.
2926 env(offer(acct, t.acctGets, t.acctPays, tfSell), Ter(t.tec));
2927 env.close();
2928 std::uint32_t const acctOfferSeq = env.seq(acct) - 1;
2929
2930 // Check results
2931 BEAST_EXPECT(env.balance(acct, usd) == t.finalUsd);
2932 BEAST_EXPECT(env.balance(acct, xrpIssue()) == t.fundXrp - t.spentXrp);
2933 env.require(Offers(acct, t.offers));
2934 env.require(Owners(acct, t.owners));
2935
2936 if (t.offers != 0)
2937 {
2938 auto const acctOffers = offersOnAccount(env, acct);
2939 if (!acctOffers.empty())
2940 {
2941 BEAST_EXPECT(acctOffers.size() == 1);
2942 auto const& acctOffer = *acctOffers.front();
2943
2944 BEAST_EXPECT(acctOffer[sfLedgerEntryType] == ltOFFER);
2945 BEAST_EXPECT(acctOffer[sfTakerGets] == t.takerGets);
2946 BEAST_EXPECT(acctOffer[sfTakerPays] == t.takerPays);
2947 }
2948 }
2949
2950 // Give the next loop a clean slate by canceling any left-overs
2951 // in the offers.
2952 env(offerCancel(acct, acctOfferSeq));
2953 env(offerCancel(gw, gwOfferSeq));
2954 env.close();
2955 }
2956 }
2957
2958 void
2960 {
2961 // Test a number of different corner cases regarding offer crossing
2962 // when both the tfSell flag and tfFillOrKill flags are set.
2963 testcase("Combine tfSell with tfFillOrKill");
2964
2965 using namespace jtx;
2966
2967 auto const gw = Account("gateway");
2968 auto const alice = Account("alice");
2969 auto const bob = Account("bob");
2970 auto const usd = gw["USD"];
2971
2972 Env env{*this, features};
2973
2974 env.fund(XRP(10000000), gw, alice, bob);
2975 env.close();
2976
2977 // Code returned if an offer is killed.
2978 TER const killedCode{TER{tecKILLED}};
2979
2980 // bob offers XRP for USD.
2981 env(trust(bob, usd(200)));
2982 env.close();
2983 env(pay(gw, bob, usd(100)));
2984 env.close();
2985 env(offer(bob, XRP(2000), usd(20)));
2986 env.close();
2987 {
2988 // alice submits a tfSell | tfFillOrKill offer that does not cross.
2989 env(offer(alice, usd(21), XRP(2100), tfSell | tfFillOrKill), Ter(killedCode));
2990 env.close();
2991 env.require(Balance(alice, usd(kNone)));
2992 env.require(Offers(alice, 0));
2993 env.require(Balance(bob, usd(100)));
2994 }
2995 {
2996 // alice submits a tfSell | tfFillOrKill offer that crosses.
2997 // Even though tfSell is present it doesn't matter this time.
2998 env(offer(alice, usd(20), XRP(2000), tfSell | tfFillOrKill));
2999 env.close();
3000 env.require(Balance(alice, usd(20)));
3001 env.require(Offers(alice, 0));
3002 env.require(Balance(bob, usd(80)));
3003 }
3004 {
3005 // alice submits a tfSell | tfFillOrKill offer that crosses and
3006 // returns more than was asked for (because of the tfSell flag).
3007 env(offer(bob, XRP(2000), usd(20)));
3008 env.close();
3009 env(offer(alice, usd(10), XRP(1500), tfSell | tfFillOrKill));
3010 env.close();
3011 env.require(Balance(alice, usd(35)));
3012 env.require(Offers(alice, 0));
3013 env.require(Balance(bob, usd(65)));
3014 }
3015 {
3016 // alice submits a tfSell | tfFillOrKill offer that doesn't cross.
3017 // This would have succeeded with a regular tfSell, but the
3018 // fillOrKill prevents the transaction from crossing since not
3019 // all of the offer is consumed.
3020
3021 // We're using bob's left-over offer for XRP(500), USD(5)
3022 env(offer(alice, usd(1), XRP(501), tfSell | tfFillOrKill), Ter(killedCode));
3023 env.close();
3024 env.require(Balance(alice, usd(35)));
3025 env.require(Offers(alice, 0));
3026 env.require(Balance(bob, usd(65)));
3027 }
3028 {
3029 // Alice submits a tfSell | tfFillOrKill offer that finishes
3030 // off the remainder of bob's offer.
3031
3032 // We're using bob's left-over offer for XRP(500), USD(5)
3033 env(offer(alice, usd(1), XRP(500), tfSell | tfFillOrKill));
3034 env.close();
3035 env.require(Balance(alice, usd(40)));
3036 env.require(Offers(alice, 0));
3037 env.require(Balance(bob, usd(60)));
3038 }
3039 }
3040
3041 void
3043 {
3044 testcase("Transfer Rate Offer");
3045
3046 using namespace jtx;
3047
3048 auto const gw1 = Account("gateway1");
3049 auto const usd = gw1["USD"];
3050
3051 Env env{*this, features};
3052
3053 // The fee that's charged for transactions.
3054 auto const fee = env.current()->fees().base;
3055
3056 env.fund(XRP(100000), gw1);
3057 env.close();
3058
3059 env(rate(gw1, 1.25));
3060 {
3061 auto const ann = Account("ann");
3062 auto const bob = Account("bob");
3063 env.fund(XRP(100) + reserve(env, 2) + (fee * 2), ann, bob);
3064 env.close();
3065
3066 env(trust(ann, usd(200)));
3067 env(trust(bob, usd(200)));
3068 env.close();
3069
3070 env(pay(gw1, bob, usd(125)));
3071 env.close();
3072
3073 // bob offers to sell USD(100) for XRP. alice takes bob's offer.
3074 // Notice that although bob only offered USD(100), USD(125) was
3075 // removed from his account due to the gateway fee.
3076 //
3077 // A comparable payment would look like this:
3078 // env (pay (bob, alice, USD(100)), sendmax(USD(125)))
3079 env(offer(bob, XRP(1), usd(100)));
3080 env.close();
3081
3082 env(offer(ann, usd(100), XRP(1)));
3083 env.close();
3084
3085 env.require(Balance(ann, usd(100)));
3086 env.require(Balance(ann, XRP(99) + reserve(env, 2)));
3087 env.require(Offers(ann, 0));
3088
3089 env.require(Balance(bob, usd(0)));
3090 env.require(Balance(bob, XRP(101) + reserve(env, 2)));
3091 env.require(Offers(bob, 0));
3092 }
3093 {
3094 // Reverse the order, so the offer in the books is to sell XRP
3095 // in return for USD. Gateway rate should still apply identically.
3096 auto const che = Account("che");
3097 auto const deb = Account("deb");
3098 env.fund(XRP(100) + reserve(env, 2) + (fee * 2), che, deb);
3099 env.close();
3100
3101 env(trust(che, usd(200)));
3102 env(trust(deb, usd(200)));
3103 env.close();
3104
3105 env(pay(gw1, deb, usd(125)));
3106 env.close();
3107
3108 env(offer(che, usd(100), XRP(1)));
3109 env.close();
3110
3111 env(offer(deb, XRP(1), usd(100)));
3112 env.close();
3113
3114 env.require(Balance(che, usd(100)));
3115 env.require(Balance(che, XRP(99) + reserve(env, 2)));
3116 env.require(Offers(che, 0));
3117
3118 env.require(Balance(deb, usd(0)));
3119 env.require(Balance(deb, XRP(101) + reserve(env, 2)));
3120 env.require(Offers(deb, 0));
3121 }
3122 {
3123 auto const eve = Account("eve");
3124 auto const fyn = Account("fyn");
3125
3126 env.fund(XRP(20000) + (fee * 2), eve, fyn);
3127 env.close();
3128
3129 env(trust(eve, usd(1000)));
3130 env(trust(fyn, usd(1000)));
3131 env.close();
3132
3133 env(pay(gw1, eve, usd(100)));
3134 env(pay(gw1, fyn, usd(100)));
3135 env.close();
3136
3137 // This test verifies that the amount removed from an offer
3138 // accounts for the transfer fee that is removed from the
3139 // account but not from the remaining offer.
3140 env(offer(eve, usd(10), XRP(4000)));
3141 env.close();
3142 std::uint32_t const eveOfferSeq = env.seq(eve) - 1;
3143
3144 env(offer(fyn, XRP(2000), usd(5)));
3145 env.close();
3146
3147 env.require(Balance(eve, usd(105)));
3148 env.require(Balance(eve, XRP(18000)));
3149 auto const evesOffers = offersOnAccount(env, eve);
3150 BEAST_EXPECT(evesOffers.size() == 1);
3151 if (!evesOffers.empty())
3152 {
3153 auto const& evesOffer = *evesOffers.front();
3154 BEAST_EXPECT(evesOffer[sfLedgerEntryType] == ltOFFER);
3155 BEAST_EXPECT(evesOffer[sfTakerGets] == XRP(2000));
3156 BEAST_EXPECT(evesOffer[sfTakerPays] == usd(5));
3157 }
3158 env(offerCancel(eve, eveOfferSeq)); // For later tests
3159
3160 env.require(Balance(fyn, usd(93.75)));
3161 env.require(Balance(fyn, XRP(22000)));
3162 env.require(Offers(fyn, 0));
3163 }
3164 // Start messing with two non-native currencies.
3165 auto const gw2 = Account("gateway2");
3166 auto const eur = gw2["EUR"];
3167
3168 env.fund(XRP(100000), gw2);
3169 env.close();
3170
3171 env(rate(gw2, 1.5));
3172 {
3173 // Remove XRP from the equation. Give the two currencies two
3174 // different transfer rates so we can see both transfer rates
3175 // apply in the same transaction.
3176 auto const gay = Account("gay");
3177 auto const hal = Account("hal");
3178 env.fund(reserve(env, 3) + (fee * 3), gay, hal);
3179 env.close();
3180
3181 env(trust(gay, usd(200)));
3182 env(trust(gay, eur(200)));
3183 env(trust(hal, usd(200)));
3184 env(trust(hal, eur(200)));
3185 env.close();
3186
3187 env(pay(gw1, gay, usd(125)));
3188 env(pay(gw2, hal, eur(150)));
3189 env.close();
3190
3191 env(offer(gay, eur(100), usd(100)));
3192 env.close();
3193
3194 env(offer(hal, usd(100), eur(100)));
3195 env.close();
3196
3197 env.require(Balance(gay, usd(0)));
3198 env.require(Balance(gay, eur(100)));
3199 env.require(Balance(gay, reserve(env, 3)));
3200 env.require(Offers(gay, 0));
3201
3202 env.require(Balance(hal, usd(100)));
3203 env.require(Balance(hal, eur(0)));
3204 env.require(Balance(hal, reserve(env, 3)));
3205 env.require(Offers(hal, 0));
3206 }
3207 {
3208 // A trust line's QualityIn should not affect offer crossing.
3209 auto const ivy = Account("ivy");
3210 auto const joe = Account("joe");
3211 env.fund(reserve(env, 3) + (fee * 3), ivy, joe);
3212 env.close();
3213
3214 env(trust(ivy, usd(400)), QualityInPercent(90));
3215 env(trust(ivy, eur(400)), QualityInPercent(80));
3216 env(trust(joe, usd(400)), QualityInPercent(70));
3217 env(trust(joe, eur(400)), QualityInPercent(60));
3218 env.close();
3219
3220 env(pay(gw1, ivy, usd(270)), Sendmax(usd(500)));
3221 env(pay(gw2, joe, eur(150)), Sendmax(eur(300)));
3222 env.close();
3223 env.require(Balance(ivy, usd(300)));
3224 env.require(Balance(joe, eur(250)));
3225
3226 env(offer(ivy, eur(100), usd(200)));
3227 env.close();
3228
3229 env(offer(joe, usd(200), eur(100)));
3230 env.close();
3231
3232 env.require(Balance(ivy, usd(50)));
3233 env.require(Balance(ivy, eur(100)));
3234 env.require(Balance(ivy, reserve(env, 3)));
3235 env.require(Offers(ivy, 0));
3236
3237 env.require(Balance(joe, usd(200)));
3238 env.require(Balance(joe, eur(100)));
3239 env.require(Balance(joe, reserve(env, 3)));
3240 env.require(Offers(joe, 0));
3241 }
3242 {
3243 // A trust line's QualityOut should not affect offer crossing.
3244 auto const kim = Account("kim");
3245 auto const kBux = kim["BUX"];
3246 auto const lex = Account("lex");
3247 auto const meg = Account("meg");
3248 auto const ned = Account("ned");
3249 auto const nBux = ned["BUX"];
3250
3251 // Verify trust line QualityOut affects payments.
3252 env.fund(reserve(env, 4) + (fee * 4), kim, lex, meg, ned);
3253 env.close();
3254
3255 env(trust(lex, kBux(400)));
3256 env(trust(lex, nBux(200)), QualityOutPercent(120));
3257 env(trust(meg, nBux(100)));
3258 env.close();
3259 env(pay(ned, lex, nBux(100)));
3260 env.close();
3261 env.require(Balance(lex, nBux(100)));
3262
3263 env(pay(kim, meg, nBux(60)), Path(lex, ned), Sendmax(kBux(200)));
3264 env.close();
3265
3266 env.require(Balance(kim, kBux(kNone)));
3267 env.require(Balance(kim, nBux(kNone)));
3268 env.require(Balance(lex, kBux(72)));
3269 env.require(Balance(lex, nBux(40)));
3270 env.require(Balance(meg, kBux(kNone)));
3271 env.require(Balance(meg, nBux(60)));
3272 env.require(Balance(ned, kBux(kNone)));
3273 env.require(Balance(ned, nBux(kNone)));
3274
3275 // Now verify that offer crossing is unaffected by QualityOut.
3276 env(offer(lex, kBux(30), nBux(30)));
3277 env.close();
3278
3279 env(offer(kim, nBux(30), kBux(30)));
3280 env.close();
3281
3282 env.require(Balance(kim, kBux(kNone)));
3283 env.require(Balance(kim, nBux(30)));
3284 env.require(Balance(lex, kBux(102)));
3285 env.require(Balance(lex, nBux(10)));
3286 env.require(Balance(meg, kBux(kNone)));
3287 env.require(Balance(meg, nBux(60)));
3288 env.require(Balance(ned, kBux(-30)));
3289 env.require(Balance(ned, nBux(kNone)));
3290 }
3291 {
3292 // Make sure things work right when we're auto-bridging as well.
3293 auto const ova = Account("ova");
3294 auto const pat = Account("pat");
3295 auto const qae = Account("qae");
3296 env.fund(XRP(2) + reserve(env, 3) + (fee * 3), ova, pat, qae);
3297 env.close();
3298
3299 // o ova has USD but wants XRP.
3300 // o pat has XRP but wants EUR.
3301 // o qae has EUR but wants USD.
3302 env(trust(ova, usd(200)));
3303 env(trust(ova, eur(200)));
3304 env(trust(pat, usd(200)));
3305 env(trust(pat, eur(200)));
3306 env(trust(qae, usd(200)));
3307 env(trust(qae, eur(200)));
3308 env.close();
3309
3310 env(pay(gw1, ova, usd(125)));
3311 env(pay(gw2, qae, eur(150)));
3312 env.close();
3313
3314 env(offer(ova, XRP(2), usd(100)));
3315 env(offer(pat, eur(100), XRP(2)));
3316 env.close();
3317
3318 env(offer(qae, usd(100), eur(100)));
3319 env.close();
3320
3321 env.require(Balance(ova, usd(0)));
3322 env.require(Balance(ova, eur(0)));
3323 env.require(Balance(ova, XRP(4) + reserve(env, 3)));
3324
3325 // In pre-flow code ova's offer is left empty in the ledger.
3326 auto const ovasOffers = offersOnAccount(env, ova);
3327 if (!ovasOffers.empty())
3328 {
3329 BEAST_EXPECT(ovasOffers.size() == 1);
3330 auto const& ovasOffer = *ovasOffers.front();
3331
3332 BEAST_EXPECT(ovasOffer[sfLedgerEntryType] == ltOFFER);
3333 BEAST_EXPECT(ovasOffer[sfTakerGets] == usd(0));
3334 BEAST_EXPECT(ovasOffer[sfTakerPays] == XRP(0));
3335 }
3336
3337 env.require(Balance(pat, usd(0)));
3338 env.require(Balance(pat, eur(100)));
3339 env.require(Balance(pat, XRP(0) + reserve(env, 3)));
3340 env.require(Offers(pat, 0));
3341
3342 env.require(Balance(qae, usd(100)));
3343 env.require(Balance(qae, eur(0)));
3344 env.require(Balance(qae, XRP(2) + reserve(env, 3)));
3345 env.require(Offers(qae, 0));
3346 }
3347 }
3348
3349 void
3351 {
3352 // The following test verifies some correct but slightly surprising
3353 // behavior in offer crossing. The scenario:
3354 //
3355 // o An entity has created one or more offers.
3356 // o The entity creates another offer that can be directly crossed
3357 // (not autobridged) by the previously created offer(s).
3358 // o Rather than self crossing the offers, delete the old offer(s).
3359 //
3360 // See a more complete explanation in the comments for
3361 // BookOfferCrossingStep::limitSelfCrossQuality().
3362 //
3363 // Note that, in this particular example, one offer causes several
3364 // crossable offers (worth considerably more than the new offer)
3365 // to be removed from the book.
3366 using namespace jtx;
3367
3368 auto const gw = Account("gateway");
3369 auto const usd = gw["USD"];
3370
3371 Env env{*this, features};
3372
3373 // The fee that's charged for transactions.
3374 auto const fee = env.current()->fees().base;
3375 auto const startBalance = XRP(1000000);
3376
3377 env.fund(startBalance + (fee * 4), gw);
3378 env.close();
3379
3380 env(offer(gw, usd(60), XRP(600)));
3381 env.close();
3382 env(offer(gw, usd(60), XRP(600)));
3383 env.close();
3384 env(offer(gw, usd(60), XRP(600)));
3385 env.close();
3386
3387 env.require(Owners(gw, 3));
3388 env.require(Balance(gw, startBalance + fee));
3389
3390 auto gwOffers = offersOnAccount(env, gw);
3391 BEAST_EXPECT(gwOffers.size() == 3);
3392 for (auto const& offerPtr : gwOffers)
3393 {
3394 auto const& offer = *offerPtr;
3395 BEAST_EXPECT(offer[sfLedgerEntryType] == ltOFFER);
3396 BEAST_EXPECT(offer[sfTakerGets] == XRP(600));
3397 BEAST_EXPECT(offer[sfTakerPays] == usd(60));
3398 }
3399
3400 // Since this offer crosses the first offers, the previous offers
3401 // will be deleted and this offer will be put on the order book.
3402 env(offer(gw, XRP(1000), usd(100)));
3403 env.close();
3404 env.require(Owners(gw, 1));
3405 env.require(Offers(gw, 1));
3406 env.require(Balance(gw, startBalance));
3407
3408 gwOffers = offersOnAccount(env, gw);
3409 BEAST_EXPECT(gwOffers.size() == 1);
3410 for (auto const& offerPtr : gwOffers)
3411 {
3412 auto const& offer = *offerPtr;
3413 BEAST_EXPECT(offer[sfLedgerEntryType] == ltOFFER);
3414 BEAST_EXPECT(offer[sfTakerGets] == usd(100));
3415 BEAST_EXPECT(offer[sfTakerPays] == XRP(1000));
3416 }
3417 }
3418
3419 void
3421 {
3422 using namespace jtx;
3423
3424 auto const gw1 = Account("gateway1");
3425 auto const gw2 = Account("gateway2");
3426 auto const alice = Account("alice");
3427 auto const usd = gw1["USD"];
3428 auto const eur = gw2["EUR"];
3429
3430 Env env{*this, features};
3431
3432 env.fund(XRP(1000000), gw1, gw2);
3433 env.close();
3434
3435 // The fee that's charged for transactions.
3436 auto const f = env.current()->fees().base;
3437
3438 // Test cases
3439 struct TestData
3440 {
3441 std::string acct; // Account operated on
3442 STAmount fundXRP; // XRP acct funded with
3443 STAmount fundUSD; // USD acct funded with
3444 STAmount fundEUR; // EUR acct funded with
3445 TER firstOfferTec; // tec code on first offer
3446 TER secondOfferTec; // tec code on second offer
3447 };
3448
3449 // clang-format off
3450 TestData const tests[]{
3451 // acct fundXRP fundUSD fundEUR firstOfferTec secondOfferTec
3452 {.acct="ann", .fundXRP=reserve(env, 3) + f * 4, .fundUSD=usd(1000), .fundEUR=eur(1000), .firstOfferTec=tesSUCCESS, .secondOfferTec=tesSUCCESS},
3453 {.acct="bev", .fundXRP=reserve(env, 3) + f * 4, .fundUSD=usd( 1), .fundEUR=eur(1000), .firstOfferTec=tesSUCCESS, .secondOfferTec=tesSUCCESS},
3454 {.acct="cam", .fundXRP=reserve(env, 3) + f * 4, .fundUSD=usd(1000), .fundEUR=eur( 1), .firstOfferTec=tesSUCCESS, .secondOfferTec=tesSUCCESS},
3455 {.acct="deb", .fundXRP=reserve(env, 3) + f * 4, .fundUSD=usd( 0), .fundEUR=eur( 1), .firstOfferTec=tesSUCCESS, .secondOfferTec=tecUNFUNDED_OFFER},
3456 {.acct="eve", .fundXRP=reserve(env, 3) + f * 4, .fundUSD=usd( 1), .fundEUR=eur( 0), .firstOfferTec=tecUNFUNDED_OFFER, .secondOfferTec=tesSUCCESS},
3457 {.acct="flo", .fundXRP=reserve(env, 3) + 0, .fundUSD=usd(1000), .fundEUR=eur(1000), .firstOfferTec=tecINSUF_RESERVE_OFFER, .secondOfferTec=tecINSUF_RESERVE_OFFER},
3458 };
3459 //clang-format on
3460
3461 for (auto const& t : tests)
3462 {
3463 auto const acct = Account{t.acct};
3464 env.fund(t.fundXRP, acct);
3465 env.close();
3466
3467 env(trust(acct, usd(1000)));
3468 env(trust(acct, eur(1000)));
3469 env.close();
3470
3471 if (t.fundUSD > usd(0))
3472 env(pay(gw1, acct, t.fundUSD));
3473 if (t.fundEUR > eur(0))
3474 env(pay(gw2, acct, t.fundEUR));
3475 env.close();
3476
3477 env(offer(acct, usd(500), eur(600)), Ter(t.firstOfferTec));
3478 env.close();
3479 std::uint32_t const firstOfferSeq = env.seq(acct) - 1;
3480
3481 int offerCount = isTesSuccess(t.firstOfferTec) ? 1 : 0;
3482 env.require(Owners(acct, 2 + offerCount));
3483 env.require(Balance(acct, t.fundUSD));
3484 env.require(Balance(acct, t.fundEUR));
3485
3486 auto acctOffers = offersOnAccount(env, acct);
3487 BEAST_EXPECT(acctOffers.size() == offerCount);
3488 for (auto const& offerPtr : acctOffers)
3489 {
3490 auto const& offer = *offerPtr;
3491 BEAST_EXPECT(offer[sfLedgerEntryType] == ltOFFER);
3492 BEAST_EXPECT(offer[sfTakerGets] == eur(600));
3493 BEAST_EXPECT(offer[sfTakerPays] == usd(500));
3494 }
3495
3496 env(offer(acct, eur(600), usd(500)), Ter(t.secondOfferTec));
3497 env.close();
3498 std::uint32_t const secondOfferSeq = env.seq(acct) - 1;
3499
3500 offerCount = isTesSuccess(t.secondOfferTec) ? 1 : offerCount;
3501 env.require(Owners(acct, 2 + offerCount));
3502 env.require(Balance(acct, t.fundUSD));
3503 env.require(Balance(acct, t.fundEUR));
3504
3505 acctOffers = offersOnAccount(env, acct);
3506 BEAST_EXPECT(acctOffers.size() == offerCount);
3507 for (auto const& offerPtr : acctOffers)
3508 {
3509 auto const& offer = *offerPtr;
3510 BEAST_EXPECT(offer[sfLedgerEntryType] == ltOFFER);
3511 if (offer[sfSequence] == firstOfferSeq)
3512 {
3513 BEAST_EXPECT(offer[sfTakerGets] == eur(600));
3514 BEAST_EXPECT(offer[sfTakerPays] == usd(500));
3515 }
3516 else
3517 {
3518 BEAST_EXPECT(offer[sfTakerGets] == usd(500));
3519 BEAST_EXPECT(offer[sfTakerPays] == eur(600));
3520 }
3521 }
3522
3523 // Remove any offers from acct for the next pass.
3524 env(offerCancel(acct, firstOfferSeq));
3525 env.close();
3526 env(offerCancel(acct, secondOfferSeq));
3527 env.close();
3528 }
3529 }
3530
3531 void
3533 {
3534 testcase("Self Cross Offer");
3535 testSelfCrossOffer1(features);
3536 testSelfCrossOffer2(features);
3537 }
3538
3539 void
3541 {
3542 // Folks who issue their own currency have, in effect, as many
3543 // funds as they are trusted for. This test used to fail because
3544 // self-issuing was not properly checked. Verify that it works
3545 // correctly now.
3546 using namespace jtx;
3547
3548 Env env{*this, features};
3549
3550 auto const alice = Account("alice");
3551 auto const bob = Account("bob");
3552 auto const usd = bob["USD"];
3553 auto const f = env.current()->fees().base;
3554
3555 env.fund(XRP(50000) + f, alice, bob);
3556 env.close();
3557
3558 env(offer(alice, usd(5000), XRP(50000)));
3559 env.close();
3560
3561 // This offer should take alice's offer up to Alice's reserve.
3562 env(offer(bob, XRP(50000), usd(5000)));
3563 env.close();
3564
3565 // alice's offer should have been removed, since she's down to her
3566 // XRP reserve.
3567 env.require(Balance(alice, XRP(250)));
3568 env.require(Owners(alice, 1));
3569 env.require(Lines(alice, 1));
3570
3571 // However bob's offer should be in the ledger, since it was not
3572 // fully crossed.
3573 auto const bobOffers = offersOnAccount(env, bob);
3574 BEAST_EXPECT(bobOffers.size() == 1);
3575 for (auto const& offerPtr : bobOffers)
3576 {
3577 auto const& offer = *offerPtr;
3578 BEAST_EXPECT(offer[sfLedgerEntryType] == ltOFFER);
3579 BEAST_EXPECT(offer[sfTakerGets] == usd(25));
3580 BEAST_EXPECT(offer[sfTakerPays] == XRP(250));
3581 }
3582 }
3583
3584 void
3586 {
3587 // At one point in the past this invalid path caused an assert. It
3588 // should not be possible for user-supplied data to cause an assert.
3589 // Make sure the assert is gone.
3590 testcase("Bad path assert");
3591
3592 using namespace jtx;
3593
3594 Env env{*this, features};
3595
3596 // The fee that's charged for transactions.
3597 auto const fee = env.current()->fees().base;
3598 {
3599 // A trust line's QualityOut should not affect offer crossing.
3600 auto const ann = Account("ann");
3601 auto const aBux = ann["BUX"];
3602 auto const bob = Account("bob");
3603 auto const cam = Account("cam");
3604 auto const dan = Account("dan");
3605 auto const dBux = dan["BUX"];
3606
3607 // Verify trust line QualityOut affects payments.
3608 env.fund(reserve(env, 4) + (fee * 4), ann, bob, cam, dan);
3609 env.close();
3610
3611 env(trust(bob, aBux(400)));
3612 env(trust(bob, dBux(200)), QualityOutPercent(120));
3613 env(trust(cam, dBux(100)));
3614 env.close();
3615 env(pay(dan, bob, dBux(100)));
3616 env.close();
3617 env.require(Balance(bob, dBux(100)));
3618
3619 env(pay(ann, cam, dBux(60)), Path(bob, dan), Sendmax(aBux(200)));
3620 env.close();
3621
3622 env.require(Balance(ann, aBux(kNone)));
3623 env.require(Balance(ann, dBux(kNone)));
3624 env.require(Balance(bob, aBux(72)));
3625 env.require(Balance(bob, dBux(40)));
3626 env.require(Balance(cam, aBux(kNone)));
3627 env.require(Balance(cam, dBux(60)));
3628 env.require(Balance(dan, aBux(kNone)));
3629 env.require(Balance(dan, dBux(kNone)));
3630
3631 env(offer(bob, aBux(30), dBux(30)));
3632 env.close();
3633
3634 env(trust(ann, dBux(100)));
3635 env.close();
3636
3637 // This payment caused the assert.
3638 env(pay(ann, ann, dBux(30)),
3639 Path(aBux, dBux),
3640 Sendmax(aBux(30)),
3641 Ter(temBAD_PATH));
3642 env.close();
3643
3644 env.require(Balance(ann, aBux(kNone)));
3645 env.require(Balance(ann, dBux(0)));
3646 env.require(Balance(bob, aBux(72)));
3647 env.require(Balance(bob, dBux(40)));
3648 env.require(Balance(cam, aBux(kNone)));
3649 env.require(Balance(cam, dBux(60)));
3650 env.require(Balance(dan, aBux(0)));
3651 env.require(Balance(dan, dBux(kNone)));
3652 }
3653 }
3654
3655 void
3657 {
3658 // The offer crossing code expects that a DirectStep is always
3659 // preceded by a BookStep. In one instance the default path
3660 // was not matching that assumption. Here we recreate that case
3661 // so we can prove the bug stays fixed.
3662 testcase("Direct to Direct path");
3663
3664 using namespace jtx;
3665
3666 Env env{*this, features};
3667
3668 auto const ann = Account("ann");
3669 auto const bob = Account("bob");
3670 auto const cam = Account("cam");
3671 auto const aBux = ann["BUX"];
3672 auto const bBux = bob["BUX"];
3673
3674 auto const fee = env.current()->fees().base;
3675 env.fund(reserve(env, 4) + (fee * 5), ann, bob, cam);
3676 env.close();
3677
3678 env(trust(ann, bBux(40)));
3679 env(trust(cam, aBux(40)));
3680 env(trust(cam, bBux(40)));
3681 env.close();
3682
3683 env(pay(ann, cam, aBux(35)));
3684 env(pay(bob, cam, bBux(35)));
3685
3686 env(offer(bob, aBux(30), bBux(30)));
3687 env.close();
3688
3689 // cam puts an offer on the books that her upcoming offer could cross.
3690 // But this offer should be deleted, not crossed, by her upcoming
3691 // offer.
3692 env(offer(cam, aBux(29), bBux(30), tfPassive));
3693 env.close();
3694 env.require(Balance(cam, aBux(35)));
3695 env.require(Balance(cam, bBux(35)));
3696 env.require(Offers(cam, 1));
3697
3698 // This offer caused the assert.
3699 env(offer(cam, bBux(30), aBux(30)));
3700 env.close();
3701
3702 env.require(Balance(bob, aBux(30)));
3703 env.require(Balance(cam, aBux(5)));
3704 env.require(Balance(cam, bBux(65)));
3705 env.require(Offers(cam, 0));
3706 }
3707
3708 void
3710 {
3711 // The Flow offer crossing code used to assert if an offer was made
3712 // for more XRP than the offering account held. This unit test
3713 // reproduces that failing case.
3714 testcase("Self crossing low quality offer");
3715
3716 using namespace jtx;
3717
3718 Env env{*this, features};
3719
3720 auto const ann = Account("ann");
3721 auto const gw = Account("gateway");
3722 auto const btc = gw["BTC"];
3723
3724 auto const fee = env.current()->fees().base;
3725 env.fund(reserve(env, 2) + drops(9999640) + fee, ann);
3726 env.fund(reserve(env, 2) + (fee * 4), gw);
3727 env.close();
3728
3729 env(rate(gw, 1.002));
3730 env(trust(ann, btc(10)));
3731 env.close();
3732
3733 env(pay(gw, ann, btc(2.856)));
3734 env.close();
3735
3736 env(offer(ann, drops(365611702030), btc(5.713)));
3737 env.close();
3738
3739 // This offer caused the assert.
3740 env(offer(ann, btc(0.687), drops(20000000000)),
3742 }
3743
3744 void
3746 {
3747 // The Flow offer crossing code had a case where it was not rounding
3748 // the offer crossing correctly after a partial crossing. The
3749 // failing case was found on the network. Here we add the case to
3750 // the unit tests.
3751 testcase("Offer In Scaling");
3752
3753 using namespace jtx;
3754
3755 Env env{*this, features};
3756
3757 auto const gw = Account("gateway");
3758 auto const alice = Account("alice");
3759 auto const bob = Account("bob");
3760 auto const cny = gw["CNY"];
3761
3762 auto const fee = env.current()->fees().base;
3763 env.fund(reserve(env, 2) + drops(400000000000) + fee, alice, bob);
3764 env.fund(reserve(env, 2) + (fee * 4), gw);
3765 env.close();
3766
3767 env(trust(bob, cny(500)));
3768 env.close();
3769
3770 env(pay(gw, bob, cny(300)));
3771 env.close();
3772
3773 env(offer(bob, drops(5400000000), cny(216.054)));
3774 env.close();
3775
3776 // This offer did not round result of partial crossing correctly.
3777 env(offer(alice, cny(13562.0001), drops(339000000000)));
3778 env.close();
3779
3780 auto const aliceOffers = offersOnAccount(env, alice);
3781 BEAST_EXPECT(aliceOffers.size() == 1);
3782 for (auto const& offerPtr : aliceOffers)
3783 {
3784 auto const& offer = *offerPtr;
3785 BEAST_EXPECT(offer[sfLedgerEntryType] == ltOFFER);
3786 BEAST_EXPECT(offer[sfTakerGets] == drops(333599446582));
3787 BEAST_EXPECT(offer[sfTakerPays] == cny(13345.9461));
3788 }
3789 }
3790
3791 void
3793 {
3794 // After adding the previous case, there were still failing rounding
3795 // cases in Flow offer crossing. This one was because the gateway
3796 // transfer rate was not being correctly handled.
3797 testcase("Offer In Scaling With Xfer Rate");
3798
3799 using namespace jtx;
3800
3801 Env env{*this, features};
3802
3803 auto const gw = Account("gateway");
3804 auto const alice = Account("alice");
3805 auto const bob = Account("bob");
3806 auto const btc = gw["BTC"];
3807 auto const jpy = gw["JPY"];
3808
3809 auto const fee = env.current()->fees().base;
3810 env.fund(reserve(env, 2) + drops(400000000000) + fee, alice, bob);
3811 env.fund(reserve(env, 2) + (fee * 4), gw);
3812 env.close();
3813
3814 env(rate(gw, 1.002));
3815 env(trust(alice, jpy(4000)));
3816 env(trust(bob, btc(2)));
3817 env.close();
3818
3819 env(pay(gw, alice, jpy(3699.034802280317)));
3820 env(pay(gw, bob, btc(1.156722559140311)));
3821 env.close();
3822
3823 env(offer(bob, jpy(1241.913390770747), btc(0.01969825690469254)));
3824 env.close();
3825
3826 // This offer did not round result of partial crossing correctly.
3827 env(offer(alice, btc(0.05507568706427876), jpy(3472.696773391072)));
3828 env.close();
3829
3830 auto const aliceOffers = offersOnAccount(env, alice);
3831 BEAST_EXPECT(aliceOffers.size() == 1);
3832 for (auto const& offerPtr : aliceOffers)
3833 {
3834 auto const& offer = *offerPtr;
3835 BEAST_EXPECT(offer[sfLedgerEntryType] == ltOFFER);
3836 BEAST_EXPECT(
3837 offer[sfTakerGets] ==
3838 STAmount(jpy, std::uint64_t(2230682446713524ul), -12));
3839 BEAST_EXPECT(offer[sfTakerPays] == btc(0.035378));
3840 }
3841 }
3842
3843 void
3845 {
3846 // Another instance where Flow offer crossing was not always
3847 // working right was if the Taker had fewer funds than the Offer
3848 // was offering. The basis for this test came off the network.
3849 testcase("Offer Threshold With Reduced Funds");
3850
3851 using namespace jtx;
3852
3853 Env env{*this, features};
3854
3855 auto const gw1 = Account("gw1");
3856 auto const gw2 = Account("gw2");
3857 auto const alice = Account("alice");
3858 auto const bob = Account("bob");
3859 auto const usd = gw1["USD"];
3860 auto const jpy = gw2["JPY"];
3861
3862 auto const fee = env.current()->fees().base;
3863 env.fund(reserve(env, 2) + drops(400000000000) + fee, alice, bob);
3864 env.fund(reserve(env, 2) + (fee * 4), gw1, gw2);
3865 env.close();
3866
3867 env(rate(gw1, 1.002));
3868 env(trust(alice, usd(1000)));
3869 env(trust(bob, jpy(100000)));
3870 env.close();
3871
3872 env(
3873 pay(gw1,
3874 alice,
3875 STAmount{usd, std::uint64_t(2185410179555600), -14}));
3876 env(
3877 pay(gw2,
3878 bob,
3879 STAmount{jpy, std::uint64_t(6351823459548956), -12}));
3880 env.close();
3881
3882 env(offer(
3883 bob,
3884 STAmount{usd, std::uint64_t(4371257532306000), -17},
3885 STAmount{jpy, std::uint64_t(4573216636606000), -15}));
3886 env.close();
3887
3888 // This offer did not partially cross correctly.
3889 env(offer(
3890 alice,
3891 STAmount{jpy, std::uint64_t(2291181510070762), -12},
3892 STAmount{usd, std::uint64_t(2190218999914694), -14}));
3893 env.close();
3894
3895 auto const aliceOffers = offersOnAccount(env, alice);
3896 BEAST_EXPECT(aliceOffers.size() == 1);
3897 for (auto const& offerPtr : aliceOffers)
3898 {
3899 auto const& offer = *offerPtr;
3900 BEAST_EXPECT(offer[sfLedgerEntryType] == ltOFFER);
3901 BEAST_EXPECT(
3902 offer[sfTakerGets] ==
3903 STAmount(usd, std::uint64_t(2185847305256635), -14));
3904 BEAST_EXPECT(
3905 offer[sfTakerPays] ==
3906 STAmount(jpy, std::uint64_t(2286608293434156), -12));
3907 }
3908 }
3909
3910 void
3912 {
3913 testcase("Tiny Offer");
3914
3915 using namespace jtx;
3916
3917 Env env{*this, features};
3918
3919 auto const gw = Account("gw");
3920 auto const alice = Account("alice");
3921 auto const bob = Account("bob");
3922 auto const cny = gw["CNY"];
3923 auto const fee = env.current()->fees().base;
3924 auto const startXrpBalance = drops(400000000000) + (fee * 2);
3925
3926 env.fund(startXrpBalance, gw, alice, bob);
3927 env.close();
3928
3929 env(trust(bob, cny(100000)));
3930 env.close();
3931
3932 // Place alice's tiny offer in the book first. Let's see what happens
3933 // when a reasonable offer crosses it.
3934 STAmount const aliceCnyOffer{
3935 cny, std::uint64_t(4926000000000000), -23};
3936
3937 env(offer(alice, aliceCnyOffer, drops(1), tfPassive));
3938 env.close();
3939
3940 // bob places an ordinary offer
3941 STAmount const bobCnyStartBalance{
3942 cny, std::uint64_t(3767479960090235), -15};
3943 env(pay(gw, bob, bobCnyStartBalance));
3944 env.close();
3945
3946 env(offer(
3947 bob,
3948 drops(203),
3949 STAmount{cny, std::uint64_t(1000000000000000), -20}));
3950 env.close();
3951
3952 env.require(Balance(alice, aliceCnyOffer));
3953 env.require(Balance(alice, startXrpBalance - fee - drops(1)));
3954 env.require(Balance(bob, bobCnyStartBalance - aliceCnyOffer));
3955 env.require(Balance(bob, startXrpBalance - (fee * 2) + drops(1)));
3956 }
3957
3958 void
3960 {
3961 testcase("Self Pay Xfer Fee");
3962 // The old offer crossing code does not charge a transfer fee
3963 // if alice pays alice. That's different from how payments work.
3964 // Payments always charge a transfer fee even if the money is staying
3965 // in the same hands.
3966 //
3967 // What's an example where alice pays alice? There are three actors:
3968 // gw, alice, and bob.
3969 //
3970 // 1. gw issues BTC and USD. qw charges a 0.2% transfer fee.
3971 //
3972 // 2. alice makes an offer to buy XRP and sell USD.
3973 // 3. bob makes an offer to buy BTC and sell XRP.
3974 //
3975 // 4. alice now makes an offer to sell BTC and buy USD.
3976 //
3977 // This last offer crosses using auto-bridging.
3978 // o alice's last offer sells BTC to...
3979 // o bob' offer which takes alice's BTC and sells XRP to...
3980 // o alice's first offer which takes bob's XRP and sells USD to...
3981 // o alice's last offer.
3982 //
3983 // So alice sells USD to herself.
3984 //
3985 // There are six cases that we need to test:
3986 // o alice crosses her own offer on the first leg (BTC).
3987 // o alice crosses her own offer on the second leg (USD).
3988 // o alice crosses her own offers on both legs.
3989 // All three cases need to be tested:
3990 // o In reverse (alice has enough BTC to cover her offer) and
3991 // o Forward (alice owns less BTC than is in her final offer.
3992 //
3993 // It turns out that two of the forward cases fail for a different
3994 // reason. They are therefore commented out here, But they are
3995 // revisited in the testSelfPayUnlimitedFunds() unit test.
3996
3997 using namespace jtx;
3998
3999 Env env{*this, features};
4000 auto const baseFee = env.current()->fees().base.drops();
4001
4002 auto const gw = Account("gw");
4003 auto const btc = gw["BTC"];
4004 auto const usd = gw["USD"];
4005 auto const startXrpBalance = XRP(4000000);
4006
4007 env.fund(startXrpBalance, gw);
4008 env.close();
4009
4010 env(rate(gw, 1.25));
4011 env.close();
4012
4013 // Test cases
4014 struct Actor
4015 {
4016 Account acct;
4017 int offers; // offers on account after crossing
4018 PrettyAmount xrp; // final expected after crossing
4019 PrettyAmount btc; // final expected after crossing
4020 PrettyAmount usd; // final expected after crossing
4021 };
4022 struct TestData
4023 {
4024 // The first three three integers give the *index* in actors
4025 // to assign each of the three roles. By using indices it is
4026 // easy for alice to own the offer in the first leg, the second
4027 // leg, or both.
4028 std::size_t self;
4029 std::size_t leg0;
4030 std::size_t leg1;
4031 PrettyAmount btcStart;
4032 std::vector<Actor> actors;
4033 };
4034
4035 // clang-format off
4036 TestData const tests[]{
4037 // btcStart --------------------- actor[0] --------------------- -------------------- actor[1] -------------------
4038 {.self=0, .leg0=0, .leg1=1, .btcStart=btc(20), .actors={{.acct="ann", .offers=0, .xrp=drops(3900000'000000 - (4 * baseFee)), .btc=btc(20.0), .usd=usd(3000)}, {.acct="abe", .offers=0, .xrp=drops(4100000'000000 - (3 * baseFee)), .btc=btc( 0), .usd=usd(750)}}}, // no BTC xfer fee
4039 {.self=0, .leg0=1, .leg1=0, .btcStart=btc(20), .actors={{.acct="bev", .offers=0, .xrp=drops(4100000'000000 - (4 * baseFee)), .btc=btc( 7.5), .usd=usd(2000)}, {.acct="bob", .offers=0, .xrp=drops(3900000'000000 - (3 * baseFee)), .btc=btc(10), .usd=usd( 0)}}}, // no USD xfer fee
4040 {.self=0, .leg0=0, .leg1=0, .btcStart=btc(20), .actors={{.acct="cam", .offers=0, .xrp=drops(4000000'000000 - (5 * baseFee)), .btc=btc(20.0), .usd=usd(2000)} }}, // no xfer fee
4041 {.self=0, .leg0=1, .leg1=0, .btcStart=btc( 5), .actors={{.acct="deb", .offers=1, .xrp=drops(4040000'000000 - (4 * baseFee)), .btc=btc( 0.0), .usd=usd(2000)}, {.acct="dan", .offers=1, .xrp=drops(3960000'000000 - (3 * baseFee)), .btc=btc( 4), .usd=usd( 0)}}}, // no USD xfer fee
4042 };
4043 // clang-format on
4044
4045 for (auto const& t : tests)
4046 {
4047 Account const& self = t.actors[t.self].acct;
4048 Account const& leg0 = t.actors[t.leg0].acct;
4049 Account const& leg1 = t.actors[t.leg1].acct;
4050
4051 for (auto const& actor : t.actors)
4052 {
4053 env.fund(XRP(4000000), actor.acct);
4054 env.close();
4055
4056 env(trust(actor.acct, btc(40)));
4057 env(trust(actor.acct, usd(8000)));
4058 env.close();
4059 }
4060
4061 env(pay(gw, self, t.btcStart));
4062 env(pay(gw, self, usd(2000)));
4063 if (self.id() != leg1.id())
4064 env(pay(gw, leg1, usd(2000)));
4065 env.close();
4066
4067 // Get the initial offers in place. Remember their sequences
4068 // so we can delete them later.
4069 env(offer(leg0, btc(10), XRP(100000), tfPassive));
4070 env.close();
4071 std::uint32_t const leg0OfferSeq = env.seq(leg0) - 1;
4072
4073 env(offer(leg1, XRP(100000), usd(1000), tfPassive));
4074 env.close();
4075 std::uint32_t const leg1OfferSeq = env.seq(leg1) - 1;
4076
4077 // This is the offer that matters.
4078 env(offer(self, usd(1000), btc(10)));
4079 env.close();
4080 std::uint32_t const selfOfferSeq = env.seq(self) - 1;
4081
4082 // Verify results.
4083 for (auto const& actor : t.actors)
4084 {
4085 // Sometimes Taker crossing gets lazy about deleting offers.
4086 // Treat an empty offer as though it is deleted.
4087 auto actorOffers = offersOnAccount(env, actor.acct);
4088 auto const offerCount = std::distance(
4089 actorOffers.begin(),
4091 return (*offer)[sfTakerGets].signum() == 0;
4092 }).begin());
4093 BEAST_EXPECT(offerCount == actor.offers);
4094
4095 env.require(Balance(actor.acct, actor.xrp));
4096 env.require(Balance(actor.acct, actor.btc));
4097 env.require(Balance(actor.acct, actor.usd));
4098 }
4099 // Remove any offers that might be left hanging around. They
4100 // could bollix up later loops.
4101 env(offerCancel(leg0, leg0OfferSeq));
4102 env.close();
4103 env(offerCancel(leg1, leg1OfferSeq));
4104 env.close();
4105 env(offerCancel(self, selfOfferSeq));
4106 env.close();
4107 }
4108 }
4109
4110 void
4112 {
4113 testcase("Self Pay Unlimited Funds");
4114 // The Taker offer crossing code recognized when Alice was paying
4115 // Alice the same denomination. In this case, as long as Alice
4116 // has a little bit of that denomination, it treats Alice as though
4117 // she has unlimited funds in that denomination.
4118 //
4119 // Huh? What kind of sense does that make?
4120 //
4121 // One way to think about it is to break a single payment into a
4122 // series of very small payments executed sequentially but very
4123 // quickly. Alice needs to pay herself 1 USD, but she only has
4124 // 0.01 USD. Alice says, "Hey Alice, let me pay you a penny."
4125 // Alice does this, taking the penny out of her pocket and then
4126 // putting it back in her pocket. Then she says, "Hey Alice,
4127 // I found another penny. I can pay you another penny." Repeat
4128 // these steps 100 times and Alice has paid herself 1 USD even though
4129 // she only owns 0.01 USD.
4130 //
4131 // That's all very nice, but the payment code does not support this
4132 // optimization. In part that's because the payment code can
4133 // operate on a whole batch of offers. As a matter of fact, it can
4134 // deal in two consecutive batches of offers. It would take a great
4135 // deal of sorting out to figure out which offers in the two batches
4136 // had the same owner and give them special processing. And,
4137 // honestly, it's a weird little corner case.
4138 //
4139 // So, since Flow offer crossing uses the payments engine, Flow
4140 // offer crossing no longer supports this optimization.
4141 //
4142 // The following test shows the difference in the behaviors between
4143 // Taker offer crossing and Flow offer crossing.
4144
4145 using namespace jtx;
4146
4147 Env env{*this, features};
4148 auto const baseFee = env.current()->fees().base.drops();
4149
4150 auto const gw = Account("gw");
4151 auto const btc = gw["BTC"];
4152 auto const usd = gw["USD"];
4153 auto const startXrpBalance = XRP(4000000);
4154
4155 env.fund(startXrpBalance, gw);
4156 env.close();
4157
4158 env(rate(gw, 1.25));
4159 env.close();
4160
4161 // Test cases
4162 struct Actor
4163 {
4164 Account acct;
4165 int offers; // offers on account after crossing
4166 PrettyAmount xrp; // final expected after crossing
4167 PrettyAmount btc; // final expected after crossing
4168 PrettyAmount usd; // final expected after crossing
4169 };
4170 struct TestData
4171 {
4172 // The first three three integers give the *index* in actors
4173 // to assign each of the three roles. By using indices it is
4174 // easy for alice to own the offer in the first leg, the second
4175 // leg, or both.
4176 std::size_t self;
4177 std::size_t leg0;
4178 std::size_t leg1;
4179 PrettyAmount btcStart;
4180 std::vector<Actor> actors;
4181 };
4182
4183 // clang-format off
4184 TestData const tests[]{
4185 // btcStart ------------------- actor[0] -------------------- ------------------- actor[1] --------------------
4186 {.self=0, .leg0=0, .leg1=1, .btcStart=btc(5), .actors={{.acct="gay", .offers=1, .xrp=drops(3950000'000000 - (4 * baseFee)), .btc=btc(5), .usd=usd(2500)}, {.acct="gar", .offers=1, .xrp=drops(4050000'000000 - (3 * baseFee)), .btc=btc(0), .usd=usd(1375)}}}, // no BTC xfer fee
4187 {.self=0, .leg0=0, .leg1=0, .btcStart=btc(5), .actors={{.acct="hye", .offers=2, .xrp=drops(4000000'000000 - (5 * baseFee)), .btc=btc(5), .usd=usd(2000)} }}, // no xfer fee
4188 };
4189 // clang-format on
4190
4191 for (auto const& t : tests)
4192 {
4193 Account const& self = t.actors[t.self].acct;
4194 Account const& leg0 = t.actors[t.leg0].acct;
4195 Account const& leg1 = t.actors[t.leg1].acct;
4196
4197 for (auto const& actor : t.actors)
4198 {
4199 env.fund(XRP(4000000), actor.acct);
4200 env.close();
4201
4202 env(trust(actor.acct, btc(40)));
4203 env(trust(actor.acct, usd(8000)));
4204 env.close();
4205 }
4206
4207 env(pay(gw, self, t.btcStart));
4208 env(pay(gw, self, usd(2000)));
4209 if (self.id() != leg1.id())
4210 env(pay(gw, leg1, usd(2000)));
4211 env.close();
4212
4213 // Get the initial offers in place. Remember their sequences
4214 // so we can delete them later.
4215 env(offer(leg0, btc(10), XRP(100000), tfPassive));
4216 env.close();
4217 std::uint32_t const leg0OfferSeq = env.seq(leg0) - 1;
4218
4219 env(offer(leg1, XRP(100000), usd(1000), tfPassive));
4220 env.close();
4221 std::uint32_t const leg1OfferSeq = env.seq(leg1) - 1;
4222
4223 // This is the offer that matters.
4224 env(offer(self, usd(1000), btc(10)));
4225 env.close();
4226 std::uint32_t const selfOfferSeq = env.seq(self) - 1;
4227
4228 // Verify results.
4229 for (auto const& actor : t.actors)
4230 {
4231 // Sometimes Taker offer crossing gets lazy about deleting
4232 // offers. Treat an empty offer as though it is deleted.
4233 auto actorOffers = offersOnAccount(env, actor.acct);
4234 auto const offerCount = std::distance(
4235 actorOffers.begin(),
4237 return (*offer)[sfTakerGets].signum() == 0;
4238 }).begin());
4239 BEAST_EXPECT(offerCount == actor.offers);
4240
4241 env.require(Balance(actor.acct, actor.xrp));
4242 env.require(Balance(actor.acct, actor.btc));
4243 env.require(Balance(actor.acct, actor.usd));
4244 }
4245 // Remove any offers that might be left hanging around. They
4246 // could bollix up later loops.
4247 env(offerCancel(leg0, leg0OfferSeq));
4248 env.close();
4249 env(offerCancel(leg1, leg1OfferSeq));
4250 env.close();
4251 env(offerCancel(self, selfOfferSeq));
4252 env.close();
4253 }
4254 }
4255
4256 void
4258 {
4259 testcase("lsfRequireAuth");
4260
4261 using namespace jtx;
4262
4263 Env env{*this, features};
4264
4265 auto const gw = Account("gw");
4266 auto const alice = Account("alice");
4267 auto const bob = Account("bob");
4268 auto const gwUSD = gw["USD"];
4269 auto const aliceUSD = alice["USD"];
4270 auto const bobUSD = bob["USD"];
4271
4272 env.fund(XRP(400000), gw, alice, bob);
4273 env.close();
4274
4275 // GW requires authorization for holders of its IOUs
4276 env(fset(gw, asfRequireAuth));
4277 env.close();
4278
4279 // Properly set trust and have gw authorize bob and alice
4280 env(trust(gw, bobUSD(100)), Txflags(tfSetfAuth));
4281 env(trust(bob, gwUSD(100)));
4282 env(trust(gw, aliceUSD(100)), Txflags(tfSetfAuth));
4283 env(trust(alice, gwUSD(100)));
4284 // Alice is able to place the offer since the GW has authorized her
4285 env(offer(alice, gwUSD(40), XRP(4000)));
4286 env.close();
4287
4288 env.require(Offers(alice, 1));
4289 env.require(Balance(alice, gwUSD(0)));
4290
4291 env(pay(gw, bob, gwUSD(50)));
4292 env.close();
4293
4294 env.require(Balance(bob, gwUSD(50)));
4295
4296 // Bob's offer should cross Alice's
4297 env(offer(bob, XRP(4000), gwUSD(40)));
4298 env.close();
4299
4300 env.require(Offers(alice, 0));
4301 env.require(Balance(alice, gwUSD(40)));
4302
4303 env.require(Offers(bob, 0));
4304 env.require(Balance(bob, gwUSD(10)));
4305 }
4306
4307 void
4309 {
4310 testcase("Missing Auth");
4311 // 1. alice creates an offer to acquire USD/gw, an asset for which
4312 // she does not have a trust line. At some point in the future,
4313 // gw adds lsfRequireAuth. Then, later, alice's offer is crossed.
4314 // Alice's offer is deleted, not consumed, since alice is not
4315 // authorized to hold USD/gw.
4316 //
4317 // 2. alice tries to create an offer for USD/gw, now that gw has
4318 // lsfRequireAuth set. This time the offer create fails because
4319 // alice is not authorized to hold USD/gw.
4320 //
4321 // 3. Next, gw creates a trust line to alice, but does not set
4322 // tfSetfAuth on that trust line. alice attempts to create an
4323 // offer and again fails.
4324 //
4325 // 4. Finally, gw sets tsfSetAuth on the trust line authorizing
4326 // alice to own USD/gw. At this point alice successfully
4327 // creates and crosses an offer for USD/gw.
4328
4329 using namespace jtx;
4330
4331 Env env{*this, features};
4332
4333 auto const gw = Account("gw");
4334 auto const alice = Account("alice");
4335 auto const bob = Account("bob");
4336 auto const gwUSD = gw["USD"];
4337 auto const aliceUSD = alice["USD"];
4338 auto const bobUSD = bob["USD"];
4339
4340 env.fund(XRP(400000), gw, alice, bob);
4341 env.close();
4342
4343 env(offer(alice, gwUSD(40), XRP(4000)));
4344 env.close();
4345
4346 env.require(Offers(alice, 1));
4347 env.require(Balance(alice, gwUSD(kNone)));
4348 env(fset(gw, asfRequireAuth));
4349 env.close();
4350
4351 env(trust(gw, bobUSD(100)), Txflags(tfSetfAuth));
4352 env.close();
4353 env(trust(bob, gwUSD(100)));
4354 env.close();
4355
4356 env(pay(gw, bob, gwUSD(50)));
4357 env.close();
4358 env.require(Balance(bob, gwUSD(50)));
4359
4360 // gw now requires authorization and bob has gwUSD(50). Let's see if
4361 // bob can cross alice's offer.
4362 //
4363 // Bob's offer shouldn't cross and alice's unauthorized offer should be
4364 // deleted.
4365 env(offer(bob, XRP(4000), gwUSD(40)));
4366 env.close();
4367 std::uint32_t const bobOfferSeq = env.seq(bob) - 1;
4368
4369 env.require(Offers(alice, 0));
4370 // alice's unauthorized offer is deleted & bob's offer not crossed.
4371 env.require(Balance(alice, gwUSD(kNone)));
4372 env.require(Offers(bob, 1));
4373 env.require(Balance(bob, gwUSD(50)));
4374
4375 // See if alice can create an offer without authorization. alice
4376 // should not be able to create the offer and bob's offer should be
4377 // untouched.
4378 env(offer(alice, gwUSD(40), XRP(4000)), Ter(tecNO_LINE));
4379 env.close();
4380
4381 env.require(Offers(alice, 0));
4382 env.require(Balance(alice, gwUSD(kNone)));
4383
4384 env.require(Offers(bob, 1));
4385 env.require(Balance(bob, gwUSD(50)));
4386
4387 // Set up a trust line for alice, but don't authorize it. alice
4388 // should still not be able to create an offer for USD/gw.
4389 env(trust(gw, aliceUSD(100)));
4390 env.close();
4391
4392 env(offer(alice, gwUSD(40), XRP(4000)), Ter(tecNO_AUTH));
4393 env.close();
4394
4395 env.require(Offers(alice, 0));
4396 env.require(Balance(alice, gwUSD(0)));
4397
4398 env.require(Offers(bob, 1));
4399 env.require(Balance(bob, gwUSD(50)));
4400
4401 // Delete bob's offer so alice can create an offer without crossing.
4402 env(offerCancel(bob, bobOfferSeq));
4403 env.close();
4404 env.require(Offers(bob, 0));
4405
4406 // Finally, set up an authorized trust line for alice. Now alice's
4407 // offer should succeed. Note that, since this is an offer rather
4408 // than a payment, alice does not need to set a trust line limit.
4409 env(trust(gw, aliceUSD(100)), Txflags(tfSetfAuth));
4410 env.close();
4411
4412 env(offer(alice, gwUSD(40), XRP(4000)));
4413 env.close();
4414
4415 env.require(Offers(alice, 1));
4416
4417 // Now bob creates his offer again. alice's offer should cross.
4418 env(offer(bob, XRP(4000), gwUSD(40)));
4419 env.close();
4420
4421 env.require(Offers(alice, 0));
4422 env.require(Balance(alice, gwUSD(40)));
4423
4424 env.require(Offers(bob, 0));
4425 env.require(Balance(bob, gwUSD(10)));
4426 }
4427
4428 void
4430 {
4431 testcase("DisallowIncomingTrustline in OfferCreate");
4432
4433 // Test that asfDisallowIncomingTrustline flag prevents offer crossing
4434 // when the taker doesn't have a trustline.
4435 //
4436 // 1. alice creates a trustline and sells USD/gw tokens.
4437 //
4438 // 2. gw sets asfDisallowIncomingTrustline flag.
4439 //
4440 // 3. An account without a trustline tries to create an offer for USD/gw.
4441 // Without amendment: succeeds and crosses alice's offer (backward compatible).
4442 // With amendment: fails with tecNO_LINE (new behavior).
4443 //
4444 // 4. An account WITH an existing trustline can create an offer.
4445 // The offer succeeds and crosses alice's offer.
4446 //
4447 // Note: The DisallowIncomingTrustline flag also prevents NEW trustlines
4448 // from being created via TrustSet (enforced by fixDisallowIncomingV1).
4449 // So accounts must create trustlines BEFORE the issuer sets the flag.
4450
4451 using namespace jtx;
4452 auto const gw = Account("gw");
4453 auto const alice = Account("alice");
4454 auto const bob = Account("bob");
4455 auto const carol = Account("carol");
4456 auto const dan = Account("dan");
4457 auto const eve = Account("eve");
4458 auto const gwUSD = gw["USD"];
4459
4460 // Test without fixCleanup3_4_0 amendment
4461 {
4462 Env env{*this, features - fixCleanup3_4_0};
4463
4464 env.fund(XRP(400000), gw, alice, bob);
4465 env.close();
4466
4467 // Alice creates trustline and gets some USD
4468 env(trust(alice, gwUSD(100)));
4469 env.close();
4470 env(pay(gw, alice, gwUSD(50)));
4471 env.close();
4472
4473 // Alice creates sell offer
4474 env(offer(alice, XRP(4000), gwUSD(40)));
4475 env.close();
4476 env.require(Offers(alice, 1));
4477
4478 // GW sets DisallowIncomingTrustline flag
4479 env(fset(gw, asfDisallowIncomingTrustline));
4480 env.close();
4481
4482 // Without the amendment, bob can still create offer without trustline
4483 // and the offer should cross (old behavior)
4484 env(offer(bob, gwUSD(40), XRP(4000)));
4485 env.close();
4486
4487 // Offer should have crossed
4488 env.require(Offers(alice, 0));
4489 env.require(Offers(bob, 0));
4490 env.require(Balance(bob, gwUSD(40)));
4491 }
4492
4493 // Test with fixCleanup3_4_0 amendment
4494 {
4495 Env env{*this, features};
4496
4497 env.fund(XRP(400000), gw, alice, bob, carol, dan);
4498 env.close();
4499
4500 // Alice creates trustline and gets some USD
4501 env(trust(alice, gwUSD(100)));
4502 env.close();
4503 env(pay(gw, alice, gwUSD(50)));
4504 env.close();
4505
4506 // Bob and carol create trustlines BEFORE the flag is set
4507 env(trust(bob, gwUSD(100)));
4508 env.close();
4509 env(trust(carol, gwUSD(100)));
4510 env.close();
4511
4512 // Alice creates sell offer
4513 env(offer(alice, XRP(4000), gwUSD(40)));
4514 env.close();
4515 env.require(Offers(alice, 1));
4516 env.require(Balance(alice, gwUSD(50)));
4517
4518 // GW sets DisallowIncomingTrustline flag
4519 env(fset(gw, asfDisallowIncomingTrustline));
4520 env.close();
4521
4522 // Dan tries to create offer without trustline - should fail
4523 env(offer(dan, gwUSD(40), XRP(4000)), Ter(tecNO_LINE));
4524 env.close();
4525
4526 // Alice's offer should still exist
4527 env.require(Offers(alice, 1));
4528 env.require(Balance(alice, gwUSD(50)));
4529
4530 // Dan shouldn't have any offers or balance
4531 env.require(Offers(dan, 0));
4532 BEAST_EXPECT(env.le(keylet::trustLine(dan, gwUSD)) == nullptr);
4533
4534 // Bob already has trustline, so his offer should succeed and cross
4535 env(offer(bob, gwUSD(40), XRP(4000)));
4536 env.close();
4537
4538 // Offer should have crossed
4539 env.require(Offers(alice, 0));
4540 env.require(Offers(bob, 0));
4541 env.require(Balance(alice, gwUSD(10)));
4542 env.require(Balance(bob, gwUSD(40)));
4543
4544 // Test scenario where carol already has a trustline (created before flag was set)
4545 // Carol should be able to create offer since trustline already exists
4546 env(pay(gw, alice, gwUSD(50)));
4547 env.close();
4548 env(offer(alice, XRP(1000), gwUSD(10)));
4549 env.close();
4550 env.require(Offers(alice, 1));
4551
4552 env(offer(carol, gwUSD(10), XRP(1000)));
4553 env.close();
4554
4555 // Offer should have crossed
4556 env.require(Offers(alice, 0));
4557 env.require(Offers(carol, 0));
4558 env.require(Balance(alice, gwUSD(50)));
4559 env.require(Balance(carol, gwUSD(10)));
4560
4561 // Test that gw can clear the flag
4562 env(fclear(gw, asfDisallowIncomingTrustline));
4563 env.close();
4564
4565 // Create new account eve without trustline
4566 env.fund(XRP(400000), eve);
4567 env.close();
4568
4569 // Bob creates another sell offer
4570 env(pay(gw, bob, gwUSD(50)));
4571 env.close();
4572 env(offer(bob, XRP(5000), gwUSD(50)));
4573 env.close();
4574 env.require(Offers(bob, 1));
4575
4576 // Eve should now be able to create offer without trustline (flag is cleared)
4577 env(offer(eve, gwUSD(50), XRP(5000)));
4578 env.close();
4579
4580 // Offer should have crossed
4581 env.require(Offers(bob, 0));
4582 env.require(Offers(eve, 0));
4583 env.require(Balance(eve, gwUSD(50)));
4584 }
4585 }
4586
4587 void
4589 {
4590 testcase("RippleConnect Smoketest payment flow");
4591 using namespace jtx;
4592
4593 Env env{*this, features};
4594
4595 // This test mimics a payment flow. The players:
4596 // A USD gateway with hot and cold wallets
4597 // A EUR gateway with hot and cold walllets
4598 // A MM gateway that will provide offers from USD->EUR and EUR->USD
4599 // A path from hot US to cold EUR is found and then used to send
4600 // USD for EUR that goes through the market maker
4601
4602 auto const hotUS = Account("hotUS");
4603 auto const coldUS = Account("coldUS");
4604 auto const hotEU = Account("hotEU");
4605 auto const coldEU = Account("coldEU");
4606 auto const mm = Account("mm");
4607
4608 auto const usd = coldUS["USD"];
4609 auto const eur = coldEU["EUR"];
4610
4611 env.fund(XRP(100000), hotUS, coldUS, hotEU, coldEU, mm);
4612 env.close();
4613
4614 // Cold wallets require trust but will ripple by default
4615 for (auto const& cold : {coldUS, coldEU})
4616 {
4617 env(fset(cold, asfRequireAuth));
4618 env(fset(cold, asfDefaultRipple));
4619 }
4620 env.close();
4621
4622 // Each hot wallet trusts the related cold wallet for a large amount
4623 env(trust(hotUS, usd(10000000)), Txflags(tfSetNoRipple));
4624 env(trust(hotEU, eur(10000000)), Txflags(tfSetNoRipple));
4625 // Market maker trusts both cold wallets for a large amount
4626 env(trust(mm, usd(10000000)), Txflags(tfSetNoRipple));
4627 env(trust(mm, eur(10000000)), Txflags(tfSetNoRipple));
4628 env.close();
4629
4630 // Gateways authorize the trustlines of hot and market maker
4631 env(trust(coldUS, usd(0), hotUS, tfSetfAuth));
4632 env(trust(coldEU, eur(0), hotEU, tfSetfAuth));
4633 env(trust(coldUS, usd(0), mm, tfSetfAuth));
4634 env(trust(coldEU, eur(0), mm, tfSetfAuth));
4635 env.close();
4636
4637 // Issue currency from cold wallets to hot and market maker
4638 env(pay(coldUS, hotUS, usd(5000000)));
4639 env(pay(coldEU, hotEU, eur(5000000)));
4640 env(pay(coldUS, mm, usd(5000000)));
4641 env(pay(coldEU, mm, eur(5000000)));
4642 env.close();
4643
4644 // MM places offers
4645 float const rate = 0.9f; // 0.9 USD = 1 EUR
4646 env(offer(mm, eur(4000000 * rate), usd(4000000)), Json(jss::Flags, tfSell));
4647
4648 float const reverseRate = 1.0f / rate * 1.00101f;
4649 env(offer(mm, usd(4000000 * reverseRate), eur(4000000)), Json(jss::Flags, tfSell));
4650 env.close();
4651
4652 // There should be a path available from hot US to cold EUR
4653 {
4654 json::Value jvParams;
4655 jvParams[jss::destination_account] = coldEU.human();
4656 jvParams[jss::destination_amount][jss::issuer] = coldEU.human();
4657 jvParams[jss::destination_amount][jss::currency] = "EUR";
4658 jvParams[jss::destination_amount][jss::value] = 10;
4659 jvParams[jss::source_account] = hotUS.human();
4660
4661 json::Value const jrr{
4662 env.rpc("json", "ripple_path_find", to_string(jvParams))[jss::result]};
4663
4664 BEAST_EXPECT(jrr[jss::status] == "success");
4665 BEAST_EXPECT(jrr[jss::alternatives].isArray() && jrr[jss::alternatives].size() > 0);
4666 }
4667 // Send the payment using the found path.
4668 env(pay(hotUS, coldEU, eur(10)), Sendmax(usd(11.1223326)));
4669 }
4670
4671 void
4673 {
4674 testcase("Self Auth");
4675
4676 using namespace jtx;
4677
4678 Env env{*this, features};
4679
4680 auto const gw = Account("gw");
4681 auto const alice = Account("alice");
4682 auto const gwUSD = gw["USD"];
4683 auto const aliceUSD = alice["USD"];
4684
4685 env.fund(XRP(400000), gw, alice);
4686 env.close();
4687
4688 // Test that gw can create an offer to buy gw's currency.
4689 env(offer(gw, gwUSD(40), XRP(4000)));
4690 env.close();
4691 std::uint32_t const gwOfferSeq = env.seq(gw) - 1;
4692 env.require(Offers(gw, 1));
4693
4694 // Since gw has an offer out, gw should not be able to set RequireAuth.
4695 env(fset(gw, asfRequireAuth), Ter(tecOWNERS));
4696 env.close();
4697
4698 // Cancel gw's offer so we can set RequireAuth.
4699 env(offerCancel(gw, gwOfferSeq));
4700 env.close();
4701 env.require(Offers(gw, 0));
4702
4703 // gw now requires authorization for holders of its IOUs
4704 env(fset(gw, asfRequireAuth));
4705 env.close();
4706
4707 // Before DepositPreauth an account with lsfRequireAuth set could not
4708 // create an offer to buy their own currency. After DepositPreauth
4709 // they can.
4710 env(offer(gw, gwUSD(40), XRP(4000)), Ter(tesSUCCESS));
4711 env.close();
4712
4713 env.require(Offers(gw, 1));
4714
4715 // Set up an authorized trust line and pay alice gwUSD 50.
4716 env(trust(gw, aliceUSD(100)), Txflags(tfSetfAuth));
4717 env(trust(alice, gwUSD(100)));
4718 env.close();
4719
4720 env(pay(gw, alice, gwUSD(50)));
4721 env.close();
4722
4723 env.require(Balance(alice, gwUSD(50)));
4724
4725 // alice's offer should cross gw's
4726 env(offer(alice, XRP(4000), gwUSD(40)));
4727 env.close();
4728
4729 env.require(Offers(alice, 0));
4730 env.require(Balance(alice, gwUSD(10)));
4731
4732 env.require(Offers(gw, 0));
4733 }
4734
4735 void
4737 {
4738 // Show that an offer who's issuer has been deleted cannot be crossed.
4739 using namespace jtx;
4740
4741 testcase("Deleted offer issuer");
4742
4743 auto const trustLineExists = [](jtx::Env const& env,
4744 jtx::Account const& src,
4745 jtx::Account const& dst,
4746 Currency const& cur) -> bool {
4747 return bool(env.le(keylet::trustLine(src, dst, cur)));
4748 };
4749
4750 Account const alice("alice");
4751 Account const becky("becky");
4752 Account const carol("carol");
4753 Account const gw("gateway");
4754 auto const usd = gw["USD"];
4755 auto const bux = alice["BUX"];
4756
4757 Env env{*this, features};
4758
4759 env.fund(XRP(10000), alice, becky, carol, noripple(gw));
4760 env.close();
4761 env.trust(usd(1000), becky);
4762 env(pay(gw, becky, usd(5)));
4763 env.close();
4764 BEAST_EXPECT(trustLineExists(env, gw, becky, usd.currency));
4765
4766 // Make offers that produce USD and can be crossed two ways:
4767 // direct XRP -> USD
4768 // direct BUX -> USD
4769 env(offer(becky, XRP(2), usd(2)), Txflags(tfPassive));
4770 std::uint32_t const beckyBuxUsdSeq{env.seq(becky)};
4771 env(offer(becky, bux(3), usd(3)), Txflags(tfPassive));
4772 env.close();
4773
4774 // becky keeps the offers, but removes the trustline.
4775 env(pay(becky, gw, usd(5)));
4776 env.trust(usd(0), becky);
4777 env.close();
4778 BEAST_EXPECT(!trustLineExists(env, gw, becky, usd.currency));
4779 BEAST_EXPECT(isOffer(env, becky, XRP(2), usd(2)));
4780 BEAST_EXPECT(isOffer(env, becky, bux(3), usd(3)));
4781
4782 // Delete gw's account.
4783 {
4784 // The ledger sequence needs to far enough ahead of the account
4785 // sequence before the account can be deleted.
4786 int const delta = [&env, &gw, openLedgerSeq = env.current()->seq()] -> int {
4787 std::uint32_t const gwSeq{env.seq(gw)};
4788 if (gwSeq + 255 > openLedgerSeq)
4789 return gwSeq - openLedgerSeq + 255;
4790 return 0;
4791 }();
4792
4793 for (int i = 0; i < delta; ++i)
4794 env.close();
4795
4796 // Account deletion has a high fee. Account for that.
4797 env(acctdelete(gw, alice), Fee(drops(env.current()->fees().increment)));
4798 env.close();
4799
4800 // Verify that gw's account root is gone from the ledger.
4801 BEAST_EXPECT(!env.closed()->exists(keylet::account(gw.id())));
4802 }
4803
4804 // alice crosses becky's first offer. The offer create fails because
4805 // the USD issuer is not in the ledger.
4806 env(offer(alice, usd(2), XRP(2)), Ter(tecNO_ISSUER));
4807 env.close();
4808 env.require(Offers(alice, 0));
4809 BEAST_EXPECT(isOffer(env, becky, XRP(2), usd(2)));
4810 BEAST_EXPECT(isOffer(env, becky, bux(3), usd(3)));
4811
4812 // alice crosses becky's second offer. Again, the offer create fails
4813 // because the USD issuer is not in the ledger.
4814 env(offer(alice, usd(3), bux(3)), Ter(tecNO_ISSUER));
4815 env.require(Offers(alice, 0));
4816 BEAST_EXPECT(isOffer(env, becky, XRP(2), usd(2)));
4817 BEAST_EXPECT(isOffer(env, becky, bux(3), usd(3)));
4818
4819 // Cancel becky's BUX -> USD offer so we can try auto-bridging.
4820 env(offerCancel(becky, beckyBuxUsdSeq));
4821 env.close();
4822 BEAST_EXPECT(!isOffer(env, becky, bux(3), usd(3)));
4823
4824 // alice creates an offer that can be auto-bridged with becky's
4825 // remaining offer.
4826 env.trust(bux(1000), carol);
4827 env(pay(alice, carol, bux(2)));
4828
4829 env(offer(alice, bux(2), XRP(2)));
4830 env.close();
4831
4832 // carol attempts the auto-bridge. Again, the offer create fails
4833 // because the USD issuer is not in the ledger.
4834 env(offer(carol, usd(2), bux(2)), Ter(tecNO_ISSUER));
4835 env.close();
4836 BEAST_EXPECT(isOffer(env, alice, bux(2), XRP(2)));
4837 BEAST_EXPECT(isOffer(env, becky, XRP(2), usd(2)));
4838 }
4839
4840 void
4842 {
4843 testcase("Tick Size");
4844
4845 using namespace jtx;
4846
4847 // Try to set tick size out of range
4848 {
4849 Env env{*this, features};
4850 auto const gw = Account{"gateway"};
4851 env.fund(XRP(10000), gw);
4852 env.close();
4853
4854 auto txn = noop(gw);
4855 txn[sfTickSize.fieldName] = Quality::kMinTickSize - 1;
4856 env(txn, Ter(temBAD_TICK_SIZE));
4857
4858 txn[sfTickSize.fieldName] = Quality::kMinTickSize;
4859 env(txn);
4860 BEAST_EXPECT((*env.le(gw))[sfTickSize] == Quality::kMinTickSize);
4861
4862 txn = noop(gw);
4863 txn[sfTickSize.fieldName] = Quality::kMaxTickSize;
4864 env(txn);
4865 BEAST_EXPECT(!env.le(gw)->isFieldPresent(sfTickSize));
4866
4867 txn = noop(gw);
4868 txn[sfTickSize.fieldName] = Quality::kMaxTickSize - 1;
4869 env(txn);
4870 BEAST_EXPECT((*env.le(gw))[sfTickSize] == Quality::kMaxTickSize - 1);
4871
4872 txn = noop(gw);
4873 txn[sfTickSize.fieldName] = Quality::kMaxTickSize + 1;
4874 env(txn, Ter(temBAD_TICK_SIZE));
4875
4876 txn[sfTickSize.fieldName] = 0;
4877 env(txn);
4878 BEAST_EXPECT(!env.le(gw)->isFieldPresent(sfTickSize));
4879 }
4880
4881 Env env{*this, features};
4882 auto const gw = Account{"gateway"};
4883 auto const alice = Account{"alice"};
4884 auto const xts = gw["XTS"];
4885 auto const xxx = gw["XXX"];
4886
4887 env.fund(XRP(10000), gw, alice);
4888 env.close();
4889
4890 {
4891 // Gateway sets its tick size to 5
4892 auto txn = noop(gw);
4893 txn[sfTickSize.fieldName] = 5;
4894 env(txn);
4895 BEAST_EXPECT((*env.le(gw))[sfTickSize] == 5);
4896 }
4897
4898 env(trust(alice, xts(1000)));
4899 env(trust(alice, xxx(1000)));
4900
4901 env(pay(gw, alice, alice["XTS"](100)));
4902 env(pay(gw, alice, alice["XXX"](100)));
4903
4904 env(offer(alice, xts(10), xxx(30)));
4905 env(offer(alice, xts(30), xxx(10)));
4906 env(offer(alice, xts(10), xxx(30)), Json(jss::Flags, tfSell));
4907 env(offer(alice, xts(30), xxx(10)), Json(jss::Flags, tfSell));
4908
4910 forEachItem(*env.current(), alice, [&](SLE::ConstRef sle) {
4911 if (sle->getType() == ltOFFER)
4912 {
4913 offers.emplace(
4914 (*sle)[sfSequence], std::make_pair((*sle)[sfTakerPays], (*sle)[sfTakerGets]));
4915 }
4916 });
4917
4918 // first offer
4919 auto it = offers.begin();
4920 BEAST_EXPECT(it != offers.end());
4921 BEAST_EXPECT(
4922 it->second.first == xts(10) && it->second.second < xxx(30) &&
4923 it->second.second > xxx(29.9994));
4924
4925 // second offer
4926 ++it;
4927 BEAST_EXPECT(it != offers.end());
4928 BEAST_EXPECT(it->second.first == xts(30) && it->second.second == xxx(10));
4929
4930 // third offer
4931 ++it;
4932 BEAST_EXPECT(it != offers.end());
4933 BEAST_EXPECT(it->second.first == xts(10.0002) && it->second.second == xxx(30));
4934
4935 // fourth offer
4936 // exact TakerPays is XTS(1/.033333)
4937 ++it;
4938 BEAST_EXPECT(it != offers.end());
4939 BEAST_EXPECT(it->second.first == xts(30) && it->second.second == xxx(10));
4940
4941 BEAST_EXPECT(++it == offers.end());
4942 }
4943
4944 // Helper function that returns offers on an account sorted by sequence.
4947 {
4949 std::ranges::sort(offers, [](SLE::ConstRef rhs, SLE::ConstRef lhs) {
4950 return (*rhs)[sfSequence] < (*lhs)[sfSequence];
4951 });
4952 return offers;
4953 }
4954
4955 void
4957 {
4958 testcase("Ticket Offers");
4959
4960 using namespace jtx;
4961
4962 // Two goals for this test.
4963 //
4964 // o Verify that offers can be created using tickets.
4965 //
4966 // o Show that offers in the _same_ order book remain in
4967 // chronological order regardless of sequence/ticket numbers.
4968 Env env{*this, features};
4969 auto const gw = Account{"gateway"};
4970 auto const alice = Account{"alice"};
4971 auto const bob = Account{"bob"};
4972 auto const usd = gw["USD"];
4973
4974 env.fund(XRP(10000), gw, alice, bob);
4975 env.close();
4976
4977 env(trust(alice, usd(1000)));
4978 env(trust(bob, usd(1000)));
4979 env.close();
4980
4981 env(pay(gw, alice, usd(200)));
4982 env.close();
4983
4984 // Create four offers from the same account with identical quality
4985 // so they go in the same order book. Each offer goes in a different
4986 // ledger so the chronology is clear.
4987 std::uint32_t const offerId0{env.seq(alice)};
4988 env(offer(alice, XRP(50), usd(50)));
4989 env.close();
4990
4991 // Create two tickets.
4992 std::uint32_t const ticketSeq{env.seq(alice) + 1};
4993 env(ticket::create(alice, 2));
4994 env.close();
4995
4996 // Create another sequence-based offer.
4997 std::uint32_t const offerId1{env.seq(alice)};
4998 BEAST_EXPECT(offerId1 == offerId0 + 4);
4999 env(offer(alice, XRP(50), usd(50)));
5000 env.close();
5001
5002 // Create two ticket based offers in reverse order.
5003 std::uint32_t const offerId2{ticketSeq + 1};
5004 env(offer(alice, XRP(50), usd(50)), ticket::Use(offerId2));
5005 env.close();
5006
5007 // Create the last offer.
5008 std::uint32_t const offerId3{ticketSeq};
5009 env(offer(alice, XRP(50), usd(50)), ticket::Use(offerId3));
5010 env.close();
5011
5012 // Verify that all of alice's offers are present.
5013 {
5014 auto offers = sortedOffersOnAccount(env, alice);
5015 BEAST_EXPECT(offers.size() == 4);
5016 BEAST_EXPECT(offers[0]->getFieldU32(sfSequence) == offerId0);
5017 BEAST_EXPECT(offers[1]->getFieldU32(sfSequence) == offerId3);
5018 BEAST_EXPECT(offers[2]->getFieldU32(sfSequence) == offerId2);
5019 BEAST_EXPECT(offers[3]->getFieldU32(sfSequence) == offerId1);
5020 env.require(Balance(alice, usd(200)));
5021 env.require(Owners(alice, 5));
5022 }
5023
5024 // Cross alice's first offer.
5025 env(offer(bob, usd(50), XRP(50)));
5026 env.close();
5027
5028 // Verify that the first offer alice created was consumed.
5029 {
5030 auto offers = sortedOffersOnAccount(env, alice);
5031 BEAST_EXPECT(offers.size() == 3);
5032 BEAST_EXPECT(offers[0]->getFieldU32(sfSequence) == offerId3);
5033 BEAST_EXPECT(offers[1]->getFieldU32(sfSequence) == offerId2);
5034 BEAST_EXPECT(offers[2]->getFieldU32(sfSequence) == offerId1);
5035 }
5036
5037 // Cross alice's second offer.
5038 env(offer(bob, usd(50), XRP(50)));
5039 env.close();
5040
5041 // Verify that the second offer alice created was consumed.
5042 {
5043 auto offers = sortedOffersOnAccount(env, alice);
5044 BEAST_EXPECT(offers.size() == 2);
5045 BEAST_EXPECT(offers[0]->getFieldU32(sfSequence) == offerId3);
5046 BEAST_EXPECT(offers[1]->getFieldU32(sfSequence) == offerId2);
5047 }
5048
5049 // Cross alice's third offer.
5050 env(offer(bob, usd(50), XRP(50)));
5051 env.close();
5052
5053 // Verify that the third offer alice created was consumed.
5054 {
5055 auto offers = sortedOffersOnAccount(env, alice);
5056 BEAST_EXPECT(offers.size() == 1);
5057 BEAST_EXPECT(offers[0]->getFieldU32(sfSequence) == offerId3);
5058 }
5059
5060 // Cross alice's last offer.
5061 env(offer(bob, usd(50), XRP(50)));
5062 env.close();
5063
5064 // Verify that the third offer alice created was consumed.
5065 {
5066 auto const offers = sortedOffersOnAccount(env, alice);
5067 BEAST_EXPECT(offers.empty());
5068 }
5069 env.require(Balance(alice, usd(0)));
5070 env.require(Owners(alice, 1));
5071 env.require(Balance(bob, usd(200)));
5072 env.require(Owners(bob, 1));
5073 }
5074
5075 void
5077 {
5078 testcase("Ticket Cancel Offers");
5079
5080 using namespace jtx;
5081
5082 // Verify that offers created with or without tickets can be canceled
5083 // by transactions with or without tickets.
5084 Env env{*this, features};
5085 auto const gw = Account{"gateway"};
5086 auto const alice = Account{"alice"};
5087 auto const usd = gw["USD"];
5088
5089 env.fund(XRP(10000), gw, alice);
5090 env.close();
5091
5092 env(trust(alice, usd(1000)));
5093 env.close();
5094 env.require(Owners(alice, 1), Tickets(alice, 0));
5095
5096 env(pay(gw, alice, usd(200)));
5097 env.close();
5098
5099 // Create the first of four offers using a sequence.
5100 std::uint32_t const offerSeqId0{env.seq(alice)};
5101 env(offer(alice, XRP(50), usd(50)));
5102 env.close();
5103 env.require(Owners(alice, 2), Tickets(alice, 0));
5104
5105 // Create four tickets.
5106 std::uint32_t const ticketSeq{env.seq(alice) + 1};
5107 env(ticket::create(alice, 4));
5108 env.close();
5109 env.require(Owners(alice, 6), Tickets(alice, 4));
5110
5111 // Create the second (also sequence-based) offer.
5112 std::uint32_t const offerSeqId1{env.seq(alice)};
5113 BEAST_EXPECT(offerSeqId1 == offerSeqId0 + 6);
5114 env(offer(alice, XRP(50), usd(50)));
5115 env.close();
5116
5117 // Create the third (ticket-based) offer.
5118 std::uint32_t const offerTixId0{ticketSeq + 1};
5119 env(offer(alice, XRP(50), usd(50)), ticket::Use(offerTixId0));
5120 env.close();
5121
5122 // Create the last offer.
5123 std::uint32_t const offerTixId1{ticketSeq};
5124 env(offer(alice, XRP(50), usd(50)), ticket::Use(offerTixId1));
5125 env.close();
5126
5127 // Verify that all of alice's offers are present.
5128 {
5129 auto offers = sortedOffersOnAccount(env, alice);
5130 BEAST_EXPECT(offers.size() == 4);
5131 BEAST_EXPECT(offers[0]->getFieldU32(sfSequence) == offerSeqId0);
5132 BEAST_EXPECT(offers[1]->getFieldU32(sfSequence) == offerTixId1);
5133 BEAST_EXPECT(offers[2]->getFieldU32(sfSequence) == offerTixId0);
5134 BEAST_EXPECT(offers[3]->getFieldU32(sfSequence) == offerSeqId1);
5135 env.require(Balance(alice, usd(200)));
5136 env.require(Owners(alice, 7));
5137 }
5138
5139 // Use a ticket to cancel an offer created with a sequence.
5140 env(offerCancel(alice, offerSeqId0), ticket::Use(ticketSeq + 2));
5141 env.close();
5142
5143 // Verify that offerSeqId_0 was canceled.
5144 {
5145 auto offers = sortedOffersOnAccount(env, alice);
5146 BEAST_EXPECT(offers.size() == 3);
5147 BEAST_EXPECT(offers[0]->getFieldU32(sfSequence) == offerTixId1);
5148 BEAST_EXPECT(offers[1]->getFieldU32(sfSequence) == offerTixId0);
5149 BEAST_EXPECT(offers[2]->getFieldU32(sfSequence) == offerSeqId1);
5150 }
5151
5152 // Use a ticket to cancel an offer created with a ticket.
5153 env(offerCancel(alice, offerTixId0), ticket::Use(ticketSeq + 3));
5154 env.close();
5155
5156 // Verify that offerTixId_0 was canceled.
5157 {
5158 auto offers = sortedOffersOnAccount(env, alice);
5159 BEAST_EXPECT(offers.size() == 2);
5160 BEAST_EXPECT(offers[0]->getFieldU32(sfSequence) == offerTixId1);
5161 BEAST_EXPECT(offers[1]->getFieldU32(sfSequence) == offerSeqId1);
5162 }
5163
5164 // All of alice's tickets should now be used up.
5165 env.require(Owners(alice, 3), Tickets(alice, 0));
5166
5167 // Use a sequence to cancel an offer created with a ticket.
5168 env(offerCancel(alice, offerTixId1));
5169 env.close();
5170
5171 // Verify that offerTixId_1 was canceled.
5172 {
5173 auto offers = sortedOffersOnAccount(env, alice);
5174 BEAST_EXPECT(offers.size() == 1);
5175 BEAST_EXPECT(offers[0]->getFieldU32(sfSequence) == offerSeqId1);
5176 }
5177
5178 // Use a sequence to cancel an offer created with a sequence.
5179 env(offerCancel(alice, offerSeqId1));
5180 env.close();
5181
5182 // Verify that offerSeqId_1 was canceled.
5183 // All of alice's tickets should now be used up.
5184 env.require(Owners(alice, 1), Tickets(alice, 0), Offers(alice, 0));
5185 }
5186
5187 void
5189 {
5190 // An assert was falsely triggering when computing rates for offers.
5191 // This unit test would trigger that assert (which has been removed).
5192 testcase("incorrect assert fixed");
5193 using namespace jtx;
5194
5195 Env env{*this};
5196 auto const alice = Account("alice");
5197 auto const usd = alice["USD"];
5198
5199 env.fund(XRP(10000), alice);
5200 env.close();
5201 env(offer(alice, XRP(100000000000), usd(100000000)));
5202 pass();
5203 }
5204
5205 void
5207 {
5208 testcase("fixFillOrKill");
5209 using namespace jtx;
5210 Env env(*this, features);
5211 Account const issuer("issuer");
5212 Account const maker("maker");
5213 Account const taker("taker");
5214 auto const usd = issuer["USD"];
5215 auto const eur = issuer["EUR"];
5216
5217 env.fund(XRP(1'000), issuer);
5218 env.fund(XRP(1'000), maker, taker);
5219 env.close();
5220
5221 env.trust(usd(1'000), maker, taker);
5222 env.trust(eur(1'000), maker, taker);
5223 env.close();
5224
5225 env(pay(issuer, maker, usd(1'000)));
5226 env(pay(issuer, taker, usd(1'000)));
5227 env(pay(issuer, maker, eur(1'000)));
5228 env.close();
5229
5230 auto makerUSDBalance = env.balance(maker, usd).value();
5231 auto takerUSDBalance = env.balance(taker, usd).value();
5232 auto makerEURBalance = env.balance(maker, eur).value();
5233 auto takerEURBalance = env.balance(taker, eur).value();
5234 auto makerXRPBalance = env.balance(maker, XRP).value();
5235 auto takerXRPBalance = env.balance(taker, XRP).value();
5236
5237 // TakerPays must be filled
5238 {
5239 env(offer(maker, XRP(100), usd(100)));
5240 env.close();
5241
5242 env(offer(taker, usd(100), XRP(101)), Txflags(tfFillOrKill));
5243 env.close();
5244
5245 makerXRPBalance -= txFee(env, 1);
5246 takerXRPBalance -= txFee(env, 1);
5247 makerUSDBalance -= usd(100);
5248 takerUSDBalance += usd(100);
5249 makerXRPBalance += XRP(100).value();
5250 takerXRPBalance -= XRP(100).value();
5251 BEAST_EXPECT(expectOffers(env, taker, 0));
5252
5253 env(offer(maker, usd(100), XRP(100)));
5254 env.close();
5255
5256 env(offer(taker, XRP(100), usd(101)), Txflags(tfFillOrKill));
5257 env.close();
5258
5259 makerXRPBalance -= txFee(env, 1);
5260 takerXRPBalance -= txFee(env, 1);
5261 makerUSDBalance += usd(100);
5262 takerUSDBalance -= usd(100);
5263 makerXRPBalance -= XRP(100).value();
5264 takerXRPBalance += XRP(100).value();
5265 BEAST_EXPECT(expectOffers(env, taker, 0));
5266
5267 env(offer(maker, usd(100), eur(100)));
5268 env.close();
5269
5270 env(offer(taker, eur(100), usd(101)), Txflags(tfFillOrKill));
5271 env.close();
5272
5273 makerXRPBalance -= txFee(env, 1);
5274 takerXRPBalance -= txFee(env, 1);
5275 makerUSDBalance += usd(100);
5276 takerUSDBalance -= usd(100);
5277 makerEURBalance -= eur(100);
5278 takerEURBalance += eur(100);
5279 BEAST_EXPECT(expectOffers(env, taker, 0));
5280 }
5281
5282 // tfFillOrKill + tfSell, TakerGets must be filled
5283 {
5284 env(offer(maker, XRP(101), usd(101)));
5285 env.close();
5286
5287 env(offer(taker, usd(100), XRP(101)), Txflags(tfFillOrKill | tfSell));
5288 env.close();
5289
5290 makerUSDBalance -= usd(101);
5291 takerUSDBalance += usd(101);
5292 makerXRPBalance += XRP(101).value() - txFee(env, 1);
5293 takerXRPBalance -= XRP(101).value() + txFee(env, 1);
5294 BEAST_EXPECT(expectOffers(env, taker, 0));
5295
5296 env(offer(maker, usd(101), XRP(101)));
5297 env.close();
5298
5299 env(offer(taker, XRP(100), usd(101)), Txflags(tfFillOrKill | tfSell));
5300 env.close();
5301
5302 makerUSDBalance += usd(101);
5303 takerUSDBalance -= usd(101);
5304 makerXRPBalance -= XRP(101).value() + txFee(env, 1);
5305 takerXRPBalance += XRP(101).value() - txFee(env, 1);
5306 BEAST_EXPECT(expectOffers(env, taker, 0));
5307
5308 env(offer(maker, usd(101), eur(101)));
5309 env.close();
5310
5311 env(offer(taker, eur(100), usd(101)), Txflags(tfFillOrKill | tfSell));
5312 env.close();
5313
5314 makerUSDBalance += usd(101);
5315 takerUSDBalance -= usd(101);
5316 makerEURBalance -= eur(101);
5317 takerEURBalance += eur(101);
5318 makerXRPBalance -= txFee(env, 1);
5319 takerXRPBalance -= txFee(env, 1);
5320 BEAST_EXPECT(expectOffers(env, taker, 0));
5321 }
5322
5323 // Fail regardless of fixFillOrKill amendment
5324 for (auto const flags : {tfFillOrKill, tfFillOrKill + tfSell})
5325 {
5326 env(offer(maker, XRP(100), usd(100)));
5327 env.close();
5328
5329 env(offer(taker, usd(100), XRP(99)), Txflags(flags), Ter(tecKILLED));
5330 env.close();
5331
5332 makerXRPBalance -= txFee(env, 1);
5333 takerXRPBalance -= txFee(env, 1);
5334 BEAST_EXPECT(expectOffers(env, taker, 0));
5335
5336 env(offer(maker, usd(100), XRP(100)));
5337 env.close();
5338
5339 env(offer(taker, XRP(100), usd(99)), Txflags(flags), Ter(tecKILLED));
5340 env.close();
5341
5342 makerXRPBalance -= txFee(env, 1);
5343 takerXRPBalance -= txFee(env, 1);
5344 BEAST_EXPECT(expectOffers(env, taker, 0));
5345
5346 env(offer(maker, usd(100), eur(100)));
5347 env.close();
5348
5349 env(offer(taker, eur(100), usd(99)), Txflags(flags), Ter(tecKILLED));
5350 env.close();
5351
5352 makerXRPBalance -= txFee(env, 1);
5353 takerXRPBalance -= txFee(env, 1);
5354 BEAST_EXPECT(expectOffers(env, taker, 0));
5355 }
5356
5357 BEAST_EXPECT(
5358 env.balance(maker, usd) == makerUSDBalance &&
5359 env.balance(taker, usd) == takerUSDBalance &&
5360 env.balance(maker, eur) == makerEURBalance &&
5361 env.balance(taker, eur) == takerEURBalance &&
5362 env.balance(maker, XRP) == makerXRPBalance &&
5363 env.balance(taker, XRP) == takerXRPBalance);
5364 }
5365
5366 void
5368 {
5369 testCanceledOffer(features);
5370 testRmFundedOffer(features);
5371 testTinyPayment(features);
5372 testXRPTinyPayment(features);
5373 testEnforceNoRipple(features);
5374 testInsufficientReserve(features);
5375 testFillModes(features);
5376 testMalformed(features);
5377 testExpiration(features);
5378 testUnfundedCross(features);
5379 testSelfCross(false, features);
5380 testSelfCross(true, features);
5381 testNegativeBalance(features);
5382 testOfferCrossWithXRP(true, features);
5383 testOfferCrossWithXRP(false, features);
5385 testOfferAcceptThenCancel(features);
5390 testCrossCurrencyStartXRP(features);
5391 testCrossCurrencyEndXRP(features);
5392 testCrossCurrencyBridged(features);
5393 testBridgedSecondLegDry(features);
5394 testOfferFeesConsumeFunds(features);
5395 testOfferCreateThenCross(features);
5396 testSellFlagBasic(features);
5397 testSellFlagExceedLimit(features);
5398 testGatewayCrossCurrency(features);
5399 testPartialCross(features);
5400 testXRPDirectCross(features);
5401 testDirectCross(features);
5402 testBridgedCross(features);
5403 testSellOffer(features);
5404 testSellWithFillOrKill(features);
5405 testTransferRateOffer(features);
5406 testSelfCrossOffer(features);
5407 testSelfIssueOffer(features);
5408 testBadPathAssert(features);
5409 testDirectToDirectPath(features);
5411 testOfferInScaling(features);
5414 testTinyOffer(features);
5415 testSelfPayXferFeeOffer(features);
5416 testSelfPayUnlimitedFunds(features);
5417 testRequireAuth(features);
5418 testMissingAuth(features);
5420 testRCSmoketest(features);
5421 testSelfAuth(features);
5422 testDeletedOfferIssuer(features);
5423 testTickSize(features);
5424 testTicketOffer(features);
5425 testTicketCancelOffer(features);
5429 testFillOrKill(features);
5430 }
5431
5433
5434 void
5435 run() override
5436 {
5437 testAll(allFeatures - featurePermissionedDEX);
5440 }
5441};
5442
5443BEAST_DEFINE_TESTSUITE_PRIO(OfferBaseUtil, app, xrpl, 2);
5444
5445} // namespace xrpl::test
T begin(T... args)
A testsuite class.
Definition suite.h:52
void pass()
Record a successful test condition.
Definition suite.h:532
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
virtual Config & config()=0
A currency issued by an account.
Definition Issue.h:18
Currency currency
Definition Issue.h:20
AccountID account
Definition Issue.h:21
static int const kMaxTickSize
Definition Quality.h:97
static int const kMinTickSize
Definition Quality.h:96
constexpr TIss const & get() const
void negate()
Definition STAmount.h:586
std::string getText() const override
static constexpr std::uint64_t kMaxNativeN
Definition STAmount.h:72
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::shared_ptr< STLedgerEntry const > const_pointer
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
static std::vector< SLE::const_pointer > sortedOffersOnAccount(jtx::Env &env, jtx::Account const &acct)
void testRequireAuth(FeatureBitset features)
void testCrossCurrencyEndXRP(FeatureBitset features)
void testXRPDirectCross(FeatureBitset features)
void testNegativeBalance(FeatureBitset features)
void testGatewayCrossCurrency(FeatureBitset features)
void testXRPTinyPayment(FeatureBitset features)
void testTicketOffer(FeatureBitset features)
void testCurrencyConversionIntoDebt(FeatureBitset features)
void testCanceledOffer(FeatureBitset features)
void testDirectToDirectPath(FeatureBitset features)
void testMissingAuth(FeatureBitset features)
static auto getBookOffers(jtx::Env &env, Issue const &takerPays, Issue const &takerGets)
void run() override
Runs the suite.
static XRPAmount reserve(jtx::Env &env, std::uint32_t count)
void testCrossCurrencyStartXRP(FeatureBitset features)
void testRmFundedOffer(FeatureBitset features)
void testSelfCrossOffer1(FeatureBitset features)
void testSellWithFillOrKill(FeatureBitset features)
void testDisallowIncomingTrustline(FeatureBitset features)
void testRCSmoketest(FeatureBitset features)
void testAll(FeatureBitset features)
void testSellOffer(FeatureBitset features)
void testOfferFeesConsumeFunds(FeatureBitset features)
void testSellFlagBasic(FeatureBitset features)
static std::vector< SLE::const_pointer > offersOnAccount(jtx::Env &env, jtx::Account const &account)
void testTinyOffer(FeatureBitset features)
void testSelfCrossOffer(FeatureBitset features)
void testSelfCrossLowQualityOffer(FeatureBitset features)
void testRmSmallIncreasedQOffersXRP(FeatureBitset features)
void verifyDefaultTrustline(jtx::Env &env, jtx::Account const &account, jtx::PrettyAmount const &expectBalance)
void testSelfCross(bool usePartner, FeatureBitset features)
void testFillModes(FeatureBitset features)
void testTicketCancelOffer(FeatureBitset features)
void testTickSize(FeatureBitset features)
void testOfferAcceptThenCancel(FeatureBitset features)
void testExpiration(FeatureBitset features)
void testRmSmallIncreasedQOffersIOU(FeatureBitset features)
void testSelfPayUnlimitedFunds(FeatureBitset features)
void testMalformed(FeatureBitset features)
void testDeletedOfferIssuer(FeatureBitset features)
void testRmSmallIncreasedQOffersClampNative(FeatureBitset features)
void testOfferCreateThenCross(FeatureBitset features)
void testTinyPayment(FeatureBitset features)
void testSellFlagExceedLimit(FeatureBitset features)
void testTransferRateOffer(FeatureBitset features)
void testOfferThresholdWithReducedFunds(FeatureBitset features)
void testInsufficientReserve(FeatureBitset features)
void testCrossCurrencyBridged(FeatureBitset features)
void testUnfundedCross(FeatureBitset features)
void testBridgedSecondLegDry(FeatureBitset features)
void testBadPathAssert(FeatureBitset features)
void testOfferInScalingWithXferRate(FeatureBitset features)
void testOfferCrossWithLimitOverride(FeatureBitset features)
void testSelfPayXferFeeOffer(FeatureBitset features)
void testSelfAuth(FeatureBitset features)
FeatureBitset const allFeatures
void testSelfIssueOffer(FeatureBitset features)
void testPartialCross(FeatureBitset features)
void testBridgedCross(FeatureBitset features)
void testCurrencyConversionEntire(FeatureBitset features)
void testCurrencyConversionInParts(FeatureBitset features)
static std::uint32_t lastClose(jtx::Env &env)
void testSelfCrossOffer2(FeatureBitset features)
void testOfferInScaling(FeatureBitset features)
static auto ledgerEntryOffer(jtx::Env &env, jtx::Account const &acct, std::uint32_t offerSeq)
void testOfferCancelPastAndFuture(FeatureBitset features)
void testFillOrKill(FeatureBitset features)
void testDirectCross(FeatureBitset features)
void testOfferCrossWithXRP(bool reverseOrder, FeatureBitset features)
void testEnforceNoRipple(FeatureBitset features)
json::Value json() const
Definition PathSet.h:187
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
AccountID id() const
Returns the Account ID.
A balance matches.
Definition balance.h:24
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:311
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
Definition Env.cpp:127
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition Env.cpp:302
Account const & master
Definition Env.h:165
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
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
Definition Env.cpp:201
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:354
void require(Args const &... args)
Check a set of requirements.
Definition Env.h:766
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
Set the fee on a JTx.
Definition fee.h:20
Converts to IOU Issue or STAmount.
Inject raw JSON.
Definition jtx_json.h:16
Match the number of items in the account's owner directory.
Definition owners.h:55
Add a path.
Definition paths.h:47
Sets the QualityIn on a trust JTx.
Definition quality.h:31
Sets the QualityOut on a trust JTx as a percentage.
Definition quality.h:63
Check a set of conditions.
Definition require.h:49
Sets the SendMax on a JTx.
Definition sendmax.h:16
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
Set the flags on a JTx.
Definition txflags.h:14
Set a ticket sequence on a JTx.
Definition ticket.h:36
T distance(T... args)
@ Array
array value (ordered list)
Definition json_value.h:32
Keylet offer(AccountID const &id, SeqProxy const &seq) noexcept
An offer from an account.
Definition Indexes.cpp:310
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:287
json::Value create(Account const &account, std::uint32_t count)
Create one of more tickets.
Definition ticket.cpp:16
static NoneT const kNone
Definition tags.h:9
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
PrettyAmount xrpMinusFee(Env const &env, std::int64_t xrpAmount)
static EpsilonT const kEpsilon
json::Value offerCancel(Account const &account, std::uint32_t offerSeq)
Cancel an offer.
Definition offer.cpp:31
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
bool hasEnvelope2(json::Value const &reply)
Reports whether reply carries the version 2 envelope: jsonrpc equal to rpc::kJsonRpcVersion and rippl...
json::Value noop(Account const &account)
The null transaction.
Definition noop.h:14
bool expectOffers(Env const &env, AccountID const &account, std::uint16_t size, std::vector< Amounts > const &toMatch)
XRPAmount txFee(Env const &env, std::uint16_t n)
json::Value fclear(Account const &account, std::uint32_t off)
Remove account flag.
Definition flags.h:110
FeatureBitset testableAmendments()
Definition Env.h:92
OwnerCount< ltRIPPLE_STATE > Lines
Match the number of trust lines in the account's owner directory.
Definition owners.h:132
OwnerCount< ltOFFER > Offers
Match the number of offers in the account's owner directory.
Definition owners.h:137
json::Value ledgerEntryState(Env &env, Account const &acctA, Account const &acctB, std::string const &currency)
json::Value acctdelete(Account const &account, Account const &dest)
Delete account.
std::array< Account, 1+sizeof...(Args)> noripple(Account const &account, Args const &... args)
Designate accounts as no-ripple in Env::fund.
Definition Env.h:86
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
json::Value ledgerEntryRoot(Env &env, Account const &acct)
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Definition trust.cpp:18
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
Definition rate.cpp:15
OwnerCount< ltTICKET > Tickets
Match the number of tickets on the account.
Definition ticket.h:54
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
Definition flags.cpp:15
bool isOffer(jtx::Env const &env, jtx::Account const &account, STAmount const &takerPays, STAmount const &takerGets)
An offer exists.
Definition PathSet.h:60
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
BEAST_DEFINE_TESTSUITE_PRIO(AccountDelete, app, xrpl, 2)
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
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(SLE::ConstRef)> const &f)
Iterate all items in the given directory.
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
@ RpcEntryNotFound
Definition ErrorCodes.h:144
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:58
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
@ Actor
Another account signed and submitted transactions on behalf of this account (this account is the owne...
json::Value getJson(LedgerFill const &fill)
Return a new json::Value representing the ledger with given options.
@ temBAD_PATH
Definition TER.h:84
@ temBAD_CURRENCY
Definition TER.h:78
@ temBAD_EXPIRATION
Definition TER.h:79
@ temBAD_SEQUENCE
Definition TER.h:92
@ temINVALID_FLAG
Definition TER.h:99
@ temBAD_TICK_SIZE
Definition TER.h:106
@ temBAD_OFFER
Definition TER.h:83
@ temREDUNDANT
Definition TER.h:100
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecINSUF_RESERVE_OFFER
Definition TER.h:300
@ tecPATH_PARTIAL
Definition TER.h:293
@ tecPATH_DRY
Definition TER.h:305
@ tecNO_AUTH
Definition TER.h:311
@ tecINTERNAL
Definition TER.h:321
@ tecUNFUNDED_OFFER
Definition TER.h:295
@ tecEXPIRED
Definition TER.h:325
@ tecNO_LINE
Definition TER.h:312
@ tecOWNERS
Definition TER.h:309
@ tecKILLED
Definition TER.h:327
@ tecNO_ISSUER
Definition TER.h:310
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
@ tesSUCCESS
Definition TER.h:253
T remove_if(T... args)
T sort(T... args)
Represents an XRP, IOU, or MPT quantity This customizes the string conversion and supports XRP conver...
STAmount const & value() const
T to_string(T... args)