xrpld
Loading...
Searching...
No Matches
AMMExtended_test.cpp
1#include <test/jtx/AMM.h>
2#include <test/jtx/AMMTest.h>
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/PathSet.h>
6#include <test/jtx/TestHelpers.h>
7#include <test/jtx/amount.h>
8#include <test/jtx/balance.h>
9#include <test/jtx/delivermin.h>
10#include <test/jtx/fee.h>
11#include <test/jtx/flags.h>
12#include <test/jtx/jtx_json.h>
13#include <test/jtx/multisign.h>
14#include <test/jtx/noop.h>
15#include <test/jtx/offer.h>
16#include <test/jtx/owners.h>
17#include <test/jtx/paths.h>
18#include <test/jtx/pay.h>
19#include <test/jtx/quality.h>
20#include <test/jtx/rate.h>
21#include <test/jtx/regkey.h>
22#include <test/jtx/require.h>
23#include <test/jtx/sendmax.h>
24#include <test/jtx/sig.h>
25#include <test/jtx/tags.h>
26#include <test/jtx/ter.h>
27#include <test/jtx/trust.h>
28#include <test/jtx/txflags.h>
29
30#include <xrpl/basics/Number.h>
31#include <xrpl/beast/unit_test/suite.h>
32#include <xrpl/json/json_value.h>
33#include <xrpl/json/to_string.h>
34#include <xrpl/ledger/ApplyView.h>
35#include <xrpl/ledger/OpenView.h>
36#include <xrpl/ledger/PaymentSandbox.h>
37#include <xrpl/ledger/Sandbox.h>
38#include <xrpl/ledger/helpers/OfferHelpers.h>
39#include <xrpl/protocol/AccountID.h>
40#include <xrpl/protocol/Feature.h>
41#include <xrpl/protocol/Indexes.h>
42#include <xrpl/protocol/Issue.h>
43#include <xrpl/protocol/KeyType.h>
44#include <xrpl/protocol/LedgerFormats.h>
45#include <xrpl/protocol/SField.h>
46#include <xrpl/protocol/STPathSet.h>
47#include <xrpl/protocol/Seed.h>
48#include <xrpl/protocol/TER.h>
49#include <xrpl/protocol/TxFlags.h>
50#include <xrpl/protocol/UintTypes.h>
51#include <xrpl/protocol/XRPAmount.h>
52#include <xrpl/protocol/jss.h>
53#include <xrpl/tx/paths/Flow.h>
54#include <xrpl/tx/paths/detail/Steps.h>
55
56#include <cstdint>
57#include <optional>
58#include <set>
59#include <tuple>
60#include <utility>
61#include <vector>
62
63namespace xrpl::test {
64
69{
70 // Use small Number mantissas for the life of this test.
72
73 // For now, just disable SAV entirely, which locks in the small Number
74 // mantissas
76 testableAmendments() - featureSingleAssetVault - featureLendingProtocol - featureMPTokensV2,
77 };
78
79private:
80 void
82 {
83 testcase("Incorrect Removal of Funded Offers");
84
85 // We need at least two paths. One at good quality and one at bad
86 // quality. The bad quality path needs two offer books in a row.
87 // Each offer book should have two offers at the same quality, the
88 // offers should be completely consumed, and the payment should
89 // require both offers to be satisfied. The first offer must
90 // be "taker gets" XRP. Ensure that the payment engine does not remove
91 // the first "taker gets" xrp offer, because the offer is still
92 // funded and not used for the payment.
93
94 using namespace jtx;
95
96 Env env{*this, features};
97
98 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000), {USD(200'000), BTC(2'000)});
99
100 // Must be two offers at the same quality
101 // "taker gets" must be XRP
102 // (Different amounts so I can distinguish the offers)
103 env(offer(carol_, BTC(49), XRP(49)));
104 env(offer(carol_, BTC(51), XRP(51)));
105
106 // Offers for the poor quality path
107 // Must be two offers at the same quality
108 env(offer(carol_, XRP(50), USD(50)));
109 env(offer(carol_, XRP(50), USD(50)));
110
111 // Good quality path
112 AMM const ammCarol(env, carol_, BTC(1'000), USD(100'100));
113
114 PathSet const paths(TestPath(XRP, USD), TestPath(USD));
115
116 env(pay(alice_, bob_, USD(100)),
117 Json(paths.json()),
118 Sendmax(BTC(1'000)),
119 Txflags(tfPartialPayment));
120
121 BEAST_EXPECT(ammCarol.expectBalances(
122 STAmount{BTC, UINT64_C(1'001'000000374815), -12}, USD(100'000), ammCarol.tokens()));
123
124 env.require(Balance(bob_, USD(200'100)));
125 BEAST_EXPECT(isOffer(env, carol_, BTC(49), XRP(49)));
126 }
127
128 void
130 {
131 testcase("Enforce No Ripple");
132 using namespace jtx;
133
134 {
135 // No ripple with an implied account step after AMM
136 Env env{*this, features};
137
138 Account const dan("dan");
139 Account const gw1("gw1");
140 Account const gw2("gw2");
141 auto const usD1 = gw1["USD"];
142 auto const usD2 = gw2["USD"];
143
144 env.fund(XRP(20'000), alice_, noripple(bob_), carol_, dan, gw1, gw2);
145 env.close();
146 env.trust(usD1(20'000), alice_, carol_, dan);
147 env(trust(bob_, usD1(1'000), tfSetNoRipple));
148 env.trust(usD2(1'000), alice_, carol_, dan);
149 env(trust(bob_, usD2(1'000), tfSetNoRipple));
150 env.close();
151
152 env(pay(gw1, dan, usD1(10'000)));
153 env(pay(gw1, bob_, usD1(50)));
154 env(pay(gw2, bob_, usD2(50)));
155 env.close();
156
157 AMM const ammDan(env, dan, XRP(10'000), usD1(10'000));
158
159 env(pay(alice_, carol_, usD2(50)),
160 Path(~usD1, bob_),
161 Sendmax(XRP(50)),
162 Txflags(tfNoRippleDirect),
164 }
165
166 {
167 // Make sure payment works with default flags
168 Env env{*this, features};
169
170 Account const dan("dan");
171 Account const gw1("gw1");
172 Account const gw2("gw2");
173 auto const usD1 = gw1["USD"];
174 auto const usD2 = gw2["USD"];
175
176 env.fund(XRP(20'000), alice_, bob_, carol_, gw1, gw2);
177 env.fund(XRP(20'000), dan);
178 env.close();
179 env.trust(usD1(20'000), alice_, bob_, carol_, dan);
180 env.trust(usD2(1'000), alice_, bob_, carol_, dan);
181 env.close();
182
183 env(pay(gw1, dan, usD1(10'050)));
184 env(pay(gw1, bob_, usD1(50)));
185 env(pay(gw2, bob_, usD2(50)));
186 env.close();
187
188 AMM const ammDan(env, dan, XRP(10'000), usD1(10'050));
189
190 env(pay(alice_, carol_, usD2(50)),
191 Path(~usD1, bob_),
192 Sendmax(XRP(50)),
193 Txflags(tfNoRippleDirect));
194 BEAST_EXPECT(ammDan.expectBalances(XRP(10'050), usD1(10'000), ammDan.tokens()));
195
196 BEAST_EXPECT(expectLedgerEntryRoot(env, alice_, XRP(20'000) - XRP(50) - txFee(env, 1)));
197 BEAST_EXPECT(expectHolding(env, bob_, usD1(100)));
198 BEAST_EXPECT(expectHolding(env, bob_, usD2(0)));
199 BEAST_EXPECT(expectHolding(env, carol_, usD2(50)));
200 }
201 }
202
203 void
205 {
206 testcase("Fill Modes");
207 using namespace jtx;
208
209 auto const startBalance = XRP(1'000'000);
210
211 // Fill or Kill - unless we fully cross, just charge a fee and don't
212 // place the offer on the books. But also clean up expired offers
213 // that are discovered along the way.
214 testAMM(
215 [&](AMM& ammAlice, Env& env) {
216 // Order that can't be filled
217 TER const killedCode{TER{tecKILLED}};
218 env(offer(carol_, USD(100), XRP(100)), Txflags(tfFillOrKill), Ter(killedCode));
219 env.close();
220 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), USD(10'000), ammAlice.tokens()));
221 // fee = AMM
222 BEAST_EXPECT(expectLedgerEntryRoot(env, carol_, XRP(30'000) - (txFee(env, 1))));
223 BEAST_EXPECT(expectOffers(env, carol_, 0));
224 BEAST_EXPECT(expectHolding(env, carol_, USD(30'000)));
225
226 // Order that can be filled
227 env(offer(carol_, XRP(100), USD(100)), Txflags(tfFillOrKill), Ter(tesSUCCESS));
228 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), USD(10'100), ammAlice.tokens()));
229 BEAST_EXPECT(
230 expectLedgerEntryRoot(env, carol_, XRP(30'000) + XRP(100) - txFee(env, 2)));
231 BEAST_EXPECT(expectHolding(env, carol_, USD(29'900)));
232 BEAST_EXPECT(expectOffers(env, carol_, 0));
233 },
234 {{XRP(10'100), USD(10'000)}},
235 0,
236 std::nullopt,
237 {features});
238
239 // Immediate or Cancel - cross as much as possible
240 // and add nothing on the books.
241 testAMM(
242 [&](AMM& ammAlice, Env& env) {
243 env(offer(carol_, XRP(200), USD(200)),
244 Txflags(tfImmediateOrCancel),
245 Ter(tesSUCCESS));
246
247 // AMM generates a synthetic offer of 100USD/100XRP
248 // to match the CLOB offer quality.
249 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), USD(10'100), ammAlice.tokens()));
250 // +AMM - offer * fee
251 BEAST_EXPECT(
252 expectLedgerEntryRoot(env, carol_, XRP(30'000) + XRP(100) - txFee(env, 1)));
253 // AMM
254 BEAST_EXPECT(expectHolding(env, carol_, USD(29'900)));
255 BEAST_EXPECT(expectOffers(env, carol_, 0));
256 },
257 {{XRP(10'100), USD(10'000)}},
258 0,
259 std::nullopt,
260 {features});
261
262 // tfPassive -- place the offer without crossing it.
263 testAMM(
264 [&](AMM& ammAlice, Env& env) {
265 // Carol creates a passive offer that could cross AMM.
266 // Carol's offer should stay in the ledger.
267 env(offer(carol_, XRP(100), USD(100), tfPassive));
268 env.close();
269 BEAST_EXPECT(
270 ammAlice.expectBalances(XRP(10'100), STAmount{USD, 10'000}, ammAlice.tokens()));
271 BEAST_EXPECT(expectOffers(env, carol_, 1, {{{XRP(100), STAmount{USD, 100}}}}));
272 },
273 {{XRP(10'100), USD(10'000)}},
274 0,
275 std::nullopt,
276 {features});
277
278 // tfPassive -- cross only offers of better quality.
279 testAMM(
280 [&](AMM& ammAlice, Env& env) {
281 env(offer(alice_, USD(110), XRP(100)));
282 env.close();
283
284 // Carol creates a passive offer. That offer should cross
285 // AMM and leave Alice's offer untouched.
286 env(offer(carol_, XRP(100), USD(100), tfPassive));
287 env.close();
288 BEAST_EXPECT(ammAlice.expectBalances(
289 XRP(10'900),
290 STAmount{USD, UINT64_C(9'082'56880733945), -11},
291 ammAlice.tokens()));
292 BEAST_EXPECT(expectOffers(env, carol_, 0));
293 BEAST_EXPECT(expectOffers(env, alice_, 1));
294 },
295 {{XRP(11'000), USD(9'000)}},
296 0,
297 std::nullopt,
298 {features});
299 }
300
301 void
303 {
304 testcase("Offer Crossing with XRP, Normal order");
305
306 using namespace jtx;
307
308 Env env{*this, features};
309
310 fund(env, gw_, {bob_, alice_}, XRP(300'000), {USD(100)}, Fund::All);
311
312 AMM const ammAlice(env, alice_, XRP(150'000), USD(50));
313
314 // Existing offer pays better than this wants.
315 // Partially consume existing offer.
316 // Pay 1 USD, get 3061224490 Drops.
317 auto const xrpTransferred = XRPAmount{3'061'224'490};
318 env(offer(bob_, USD(1), XRP(4'000)));
319
320 BEAST_EXPECT(ammAlice.expectBalances(
321 XRP(150'000) + xrpTransferred, USD(49), IOUAmount{273'861'278752583, -8}));
322
323 BEAST_EXPECT(expectHolding(env, bob_, STAmount{USD, 101}));
324 BEAST_EXPECT(
325 expectLedgerEntryRoot(env, bob_, XRP(300'000) - xrpTransferred - txFee(env, 1)));
326 BEAST_EXPECT(expectOffers(env, bob_, 0));
327 }
328
329 void
331 {
332 testcase("Offer Crossing with Limit Override");
333
334 using namespace jtx;
335
336 Env env{*this, features};
337
338 env.fund(XRP(200'000), gw_, alice_, bob_);
339 env.close();
340
341 env(trust(alice_, USD(1'000)));
342
343 env(pay(gw_, alice_, alice_["USD"](500)));
344
345 AMM const ammAlice(env, alice_, XRP(150'000), USD(51));
346 env(offer(bob_, USD(1), XRP(3'000)));
347
348 BEAST_EXPECT(ammAlice.expectBalances(XRP(153'000), USD(50), ammAlice.tokens()));
349
350 auto jrr = ledgerEntryState(env, bob_, gw_, "USD");
351 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-1");
352 jrr = ledgerEntryRoot(env, bob_);
353 BEAST_EXPECT(
354 jrr[jss::node][sfBalance.fieldName] ==
355 to_string((XRP(200'000) - XRP(3'000) - env.current()->fees().base * 1).xrp()));
356 }
357
358 void
360 {
361 testcase("Currency Conversion: Entire Offer");
362
363 using namespace jtx;
364
365 Env env{*this, features};
366
367 fund(env, gw_, {alice_, bob_}, XRP(10'000));
368 env.require(Owners(bob_, 0));
369
370 env(trust(alice_, USD(100)));
371 env(trust(bob_, USD(1'000)));
372 env(pay(gw_, bob_, USD(1'000)));
373
374 env.require(Owners(alice_, 1), Owners(bob_, 1));
375
376 env(pay(gw_, alice_, alice_["USD"](100)));
377 AMM const ammBob(env, bob_, USD(200), XRP(1'500));
378
379 env(pay(alice_, alice_, XRP(500)), Sendmax(USD(100)));
380
381 BEAST_EXPECT(ammBob.expectBalances(USD(300), XRP(1'000), ammBob.tokens()));
382 BEAST_EXPECT(expectHolding(env, alice_, USD(0)));
383
384 auto jrr = ledgerEntryRoot(env, alice_);
385 BEAST_EXPECT(
386 jrr[jss::node][sfBalance.fieldName] ==
387 to_string((XRP(10'000) + XRP(500) - env.current()->fees().base * 2).xrp()));
388 }
389
390 void
392 {
393 testcase("Currency Conversion: In Parts");
394
395 using namespace jtx;
396
397 testAMM(
398 [&](AMM& ammAlice, Env& env) {
399 // Alice converts USD to XRP which should fail
400 // due to PartialPayment.
401 env(pay(alice_, alice_, XRP(100)), Sendmax(USD(100)), Ter(tecPATH_PARTIAL));
402
403 // Alice converts USD to XRP, should succeed because
404 // we permit partial payment
405 env(pay(alice_, alice_, XRP(100)), Sendmax(USD(100)), Txflags(tfPartialPayment));
406 env.close();
407 BEAST_EXPECT(ammAlice.expectBalances(
408 XRPAmount{9'900'990'100}, USD(10'100), ammAlice.tokens()));
409 // initial 30,000 - 10,000AMM - 100pay
410 BEAST_EXPECT(expectHolding(env, alice_, USD(19'900)));
411 // initial 30,000 - 10,0000AMM + 99.009900pay - fee*3
412 BEAST_EXPECT(expectLedgerEntryRoot(
413 env,
414 alice_,
415 XRP(30'000) - XRP(10'000) + XRPAmount{99'009'900} - ammCrtFee(env) -
416 txFee(env, 2)));
417 },
418 {{XRP(10'000), USD(10'000)}},
419 0,
420 std::nullopt,
421 {features});
422 }
423
424 void
426 {
427 testcase("Cross Currency Payment: Start with XRP");
428
429 using namespace jtx;
430
431 testAMM(
432 [&](AMM& ammAlice, Env& env) {
433 env.fund(XRP(1'000), bob_);
434 env.close();
435 env(trust(bob_, USD(100)));
436 env.close();
437 env(pay(alice_, bob_, USD(100)), Sendmax(XRP(100)));
438 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), USD(10'000), ammAlice.tokens()));
439 BEAST_EXPECT(expectHolding(env, bob_, USD(100)));
440 },
441 {{XRP(10'000), USD(10'100)}},
442 0,
443 std::nullopt,
444 {features});
445 }
446
447 void
449 {
450 testcase("Cross Currency Payment: End with XRP");
451
452 using namespace jtx;
453
454 testAMM(
455 [&](AMM& ammAlice, Env& env) {
456 env.fund(XRP(1'000), bob_);
457 env.close();
458 env(trust(bob_, USD(100)));
459 env.close();
460 env(pay(alice_, bob_, XRP(100)), Sendmax(USD(100)));
461 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), USD(10'100), ammAlice.tokens()));
462 BEAST_EXPECT(
463 expectLedgerEntryRoot(env, bob_, XRP(1'000) + XRP(100) - txFee(env, 1)));
464 },
465 {{XRP(10'100), USD(10'000)}},
466 0,
467 std::nullopt,
468 {features});
469 }
470
471 void
473 {
474 testcase("Cross Currency Payment: Bridged");
475
476 using namespace jtx;
477
478 Env env{*this, features};
479
480 auto const gw1 = Account{"gateway_1"};
481 auto const gw2 = Account{"gateway_2"};
482 auto const dan = Account{"dan"};
483 auto const usD1 = gw1["USD"];
484 auto const euR1 = gw2["EUR"];
485
486 fund(env, gw1, {gw2, alice_, bob_, carol_, dan}, XRP(60'000));
487 env(trust(alice_, usD1(1'000)));
488 env.close();
489 env(trust(bob_, euR1(1'000)));
490 env.close();
491 env(trust(carol_, usD1(10'000)));
492 env.close();
493 env(trust(dan, euR1(1'000)));
494 env.close();
495
496 env(pay(gw1, alice_, alice_["USD"](500)));
497 env.close();
498 env(pay(gw1, carol_, carol_["USD"](6'000)));
499 env(pay(gw2, dan, dan["EUR"](400)));
500 env.close();
501
502 AMM const ammCarol(env, carol_, usD1(5'000), XRP(50'000));
503
504 env(offer(dan, XRP(500), euR1(50)));
505 env.close();
506
508 jtp[0u][0u][jss::currency] = "XRP";
509 env(pay(alice_, bob_, euR1(30)), Json(jss::Paths, jtp), Sendmax(usD1(333)));
510 env.close();
511 BEAST_EXPECT(ammCarol.expectBalances(
512 XRP(49'700), STAmount{usD1, UINT64_C(5'030'181086519115), -12}, ammCarol.tokens()));
513 BEAST_EXPECT(expectOffers(env, dan, 1, {{Amounts{XRP(200), EUR(20)}}}));
514 BEAST_EXPECT(expectHolding(env, bob_, STAmount{euR1, 30}));
515 }
516
517 void
519 {
520 testcase("Offer Fees Consume Funds");
521
522 using namespace jtx;
523
524 Env env{*this, features};
525
526 auto const gw1 = Account{"gateway_1"};
527 auto const gw2 = Account{"gateway_2"};
528 auto const gw3 = Account{"gateway_3"};
529 auto const localAlice = Account{"alice"};
530 auto const localBob = Account{"bob"};
531 auto const usD1 = gw1["USD"];
532 auto const usD2 = gw2["USD"];
533 auto const usD3 = gw3["USD"];
534
535 // Provide micro amounts to compensate for fees to make results round
536 // nice.
537 // reserve: Alice has 3 entries in the ledger, via trust lines
538 // fees:
539 // 1 for each trust limit == 3 (alice_ < mtgox/amazon/bitstamp) +
540 // 1 for payment == 4
541 auto const startingXrp =
542 XRP(100) + env.current()->fees().accountReserve(3, 1) + env.current()->fees().base * 4;
543
544 env.fund(startingXrp, gw1, gw2, gw3, localAlice);
545 env.fund(XRP(2'000), localBob);
546 env.close();
547
548 env(trust(localAlice, usD1(1'000)));
549 env(trust(localAlice, usD2(1'000)));
550 env(trust(localAlice, usD3(1'000)));
551 env(trust(localBob, usD1(1'200)));
552 env(trust(localBob, usD2(1'100)));
553
554 env(pay(gw1, localBob, localBob["USD"](1'200)));
555
556 AMM const ammBob(env, localBob, XRP(1'000), usD1(1'200));
557 // Alice has 350 fees - a reserve of 50 = 250 reserve = 100 available.
558 // Ask for more than available to prove reserve works.
559 env(offer(localAlice, usD1(200), XRP(200)));
560
561 // The pool gets only 100XRP for ~109.09USD, even though
562 // it can exchange more.
563 BEAST_EXPECT(ammBob.expectBalances(
564 XRP(1'100), STAmount{usD1, UINT64_C(1'090'909090909091), -12}, ammBob.tokens()));
565
566 auto jrr = ledgerEntryState(env, localAlice, gw1, "USD");
567 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "109.090909090909");
568 jrr = ledgerEntryRoot(env, localAlice);
569 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName] == XRP(350).value().getText());
570 }
571
572 void
574 {
575 testcase("Offer Create, then Cross");
576
577 using namespace jtx;
578
579 Env env{*this, features};
580
581 fund(env, gw_, {alice_, bob_}, XRP(200'000));
582
583 env(rate(gw_, 1.005));
584
585 env(trust(alice_, USD(1'000)));
586 env(trust(bob_, USD(1'000)));
587
588 env(pay(gw_, bob_, USD(1)));
589 env(pay(gw_, alice_, USD(200)));
590
591 AMM const ammAlice(env, alice_, USD(150), XRP(150'100));
592 env(offer(bob_, XRP(100), USD(0.1)));
593
594 BEAST_EXPECT(ammAlice.expectBalances(USD(150.1), XRP(150'000), ammAlice.tokens()));
595
596 auto const jrr = ledgerEntryState(env, bob_, gw_, "USD");
597 // Bob pays 0.005 transfer fee. Note 10**-10 round-off.
598 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] == "-0.8995000001");
599 }
600
601 void
603 {
604 testcase("Offer tfSell: Basic Sell");
605
606 using namespace jtx;
607
608 testAMM(
609 [&](AMM& ammAlice, Env& env) {
610 env(offer(carol_, USD(100), XRP(100)), Json(jss::Flags, tfSell));
611 env.close();
612 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), USD(9'999), ammAlice.tokens()));
613 BEAST_EXPECT(expectOffers(env, carol_, 0));
614 BEAST_EXPECT(expectHolding(env, carol_, USD(30'101)));
615 BEAST_EXPECT(
616 expectLedgerEntryRoot(env, carol_, XRP(30'000) - XRP(100) - txFee(env, 1)));
617 },
618 {{XRP(9'900), USD(10'100)}},
619 0,
620 std::nullopt,
621 {features});
622 }
623
624 void
626 {
627 testcase("Offer tfSell: 2x Sell Exceed Limit");
628
629 using namespace jtx;
630
631 Env env{*this, features};
632
633 auto const startingXrp = XRP(100) + reserve(env, 1) + env.current()->fees().base * 2;
634
635 env.fund(startingXrp, gw_, alice_);
636 env.fund(XRP(2'000), bob_);
637 env.close();
638
639 env(trust(alice_, USD(150)));
640 env(trust(bob_, USD(4'000)));
641
642 env(pay(gw_, bob_, bob_["USD"](2'200)));
643
644 AMM const ammBob(env, bob_, XRP(1'000), USD(2'200));
645 // Alice has 350 fees - a reserve of 50 = 250 reserve = 100 available.
646 // Ask for more than available to prove reserve works.
647 // Taker pays 100 USD for 100 XRP.
648 // Selling XRP.
649 // Will sell all 100 XRP and get more USD than asked for.
650 env(offer(alice_, USD(100), XRP(200)), Json(jss::Flags, tfSell));
651 BEAST_EXPECT(ammBob.expectBalances(XRP(1'100), USD(2'000), ammBob.tokens()));
652 BEAST_EXPECT(expectHolding(env, alice_, USD(200)));
653 BEAST_EXPECT(expectLedgerEntryRoot(env, alice_, XRP(250)));
654 BEAST_EXPECT(expectOffers(env, alice_, 0));
655 }
656
657 void
659 {
660 testcase("Client Issue: Gateway Cross Currency");
661
662 using namespace jtx;
663
664 Env env{*this, features};
665
666 auto const xts = gw_["XTS"];
667 auto const xxx = gw_["XXX"];
668
669 auto const startingXrp = XRP(100.1) + reserve(env, 1) + env.current()->fees().base * 2;
670 fund(env, gw_, {alice_, bob_}, startingXrp, {xts(100), xxx(100)}, Fund::All);
671
672 AMM const ammAlice(env, alice_, xts(100), xxx(100));
673
674 json::Value payment;
675 payment[jss::secret] = toBase58(generateSeed("bob"));
676 payment[jss::id] = env.seq(bob_);
677 payment[jss::build_path] = true;
678 payment[jss::tx_json] = pay(bob_, bob_, bob_["XXX"](1));
679 payment[jss::tx_json][jss::Sequence] =
680 env.current()->read(keylet::account(bob_.id()))->getFieldU32(sfSequence);
681 payment[jss::tx_json][jss::Fee] = to_string(env.current()->fees().base);
682 payment[jss::tx_json][jss::SendMax] =
683 bob_["XTS"](1.5).value().getJson(JsonOptions::Values::None);
684 payment[jss::tx_json][jss::Flags] = tfPartialPayment;
685 auto const jrr = env.rpc("json", "submit", to_string(payment));
686 BEAST_EXPECT(jrr[jss::result][jss::status] == "success");
687 BEAST_EXPECT(jrr[jss::result][jss::engine_result] == "tesSUCCESS");
688 BEAST_EXPECT(ammAlice.expectBalances(
689 STAmount(xts, UINT64_C(101'0101010101011), -13), xxx(99), ammAlice.tokens()));
690 BEAST_EXPECT(expectHolding(env, bob_, STAmount{xts, UINT64_C(98'9898989898989), -13}));
691 BEAST_EXPECT(expectHolding(env, bob_, xxx(101)));
692 }
693
694 void
696 {
697 testcase("Bridged Crossing");
698
699 using namespace jtx;
700
701 {
702 Env env{*this, features};
703
704 fund(env, gw_, {alice_, bob_, carol_}, {USD(15'000), EUR(15'000)}, Fund::All);
705
706 // The scenario:
707 // o USD/XRP AMM is created.
708 // o EUR/XRP AMM is created.
709 // o carol_ has EUR but wants USD.
710 // Note that carol_'s offer must come last. If carol_'s offer is
711 // placed before AMM is created, then autobridging will not occur.
712 AMM const ammAlice(env, alice_, XRP(10'000), USD(10'100));
713 AMM const ammBob(env, bob_, EUR(10'000), XRP(10'100));
714
715 // Carol makes an offer that consumes AMM liquidity and
716 // fully consumes Carol's offer.
717 env(offer(carol_, USD(100), EUR(100)));
718 env.close();
719
720 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), USD(10'000), ammAlice.tokens()));
721 BEAST_EXPECT(ammBob.expectBalances(XRP(10'000), EUR(10'100), ammBob.tokens()));
722 BEAST_EXPECT(expectHolding(env, carol_, USD(15'100)));
723 BEAST_EXPECT(expectHolding(env, carol_, EUR(14'900)));
724 BEAST_EXPECT(expectOffers(env, carol_, 0));
725 }
726
727 {
728 Env env{*this, features};
729
730 fund(env, gw_, {alice_, bob_, carol_}, {USD(15'000), EUR(15'000)}, Fund::All);
731
732 // The scenario:
733 // o USD/XRP AMM is created.
734 // o EUR/XRP offer is created.
735 // o carol_ has EUR but wants USD.
736 // Note that carol_'s offer must come last. If carol_'s offer is
737 // placed before AMM and bob_'s offer are created, then autobridging
738 // will not occur.
739 AMM const ammAlice(env, alice_, XRP(10'000), USD(10'100));
740 env(offer(bob_, EUR(100), XRP(100)));
741 env.close();
742
743 // Carol makes an offer that consumes AMM liquidity and
744 // fully consumes Carol's offer.
745 env(offer(carol_, USD(100), EUR(100)));
746 env.close();
747
748 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), USD(10'000), ammAlice.tokens()));
749 BEAST_EXPECT(expectHolding(env, carol_, USD(15'100)));
750 BEAST_EXPECT(expectHolding(env, carol_, EUR(14'900)));
751 BEAST_EXPECT(expectOffers(env, carol_, 0));
752 BEAST_EXPECT(expectOffers(env, bob_, 0));
753 }
754
755 {
756 Env env{*this, features};
757
758 fund(env, gw_, {alice_, bob_, carol_}, {USD(15'000), EUR(15'000)}, Fund::All);
759
760 // The scenario:
761 // o USD/XRP offer is created.
762 // o EUR/XRP AMM is created.
763 // o carol_ has EUR but wants USD.
764 // Note that carol_'s offer must come last. If carol_'s offer is
765 // placed before AMM and alice_'s offer are created, then
766 // autobridging will not occur.
767 env(offer(alice_, XRP(100), USD(100)));
768 env.close();
769 AMM const ammBob(env, bob_, EUR(10'000), XRP(10'100));
770
771 // Carol makes an offer that consumes AMM liquidity and
772 // fully consumes Carol's offer.
773 env(offer(carol_, USD(100), EUR(100)));
774 env.close();
775
776 BEAST_EXPECT(ammBob.expectBalances(XRP(10'000), EUR(10'100), ammBob.tokens()));
777 BEAST_EXPECT(expectHolding(env, carol_, USD(15'100)));
778 BEAST_EXPECT(expectHolding(env, carol_, EUR(14'900)));
779 BEAST_EXPECT(expectOffers(env, carol_, 0));
780 BEAST_EXPECT(expectOffers(env, alice_, 0));
781 }
782 }
783
784 void
786 {
787 // Test a number of different corner cases regarding offer crossing
788 // when both the tfSell flag and tfFillOrKill flags are set.
789 testcase("Combine tfSell with tfFillOrKill");
790
791 using namespace jtx;
792
793 // Code returned if an offer is killed.
794 TER const killedCode{TER{tecKILLED}};
795
796 {
797 Env env{*this, features};
798 fund(env, gw_, {alice_, bob_}, {USD(20'000)}, Fund::All);
799 AMM const ammBob(env, bob_, XRP(20'000), USD(200));
800 // alice_ submits a tfSell | tfFillOrKill offer that does not cross.
801 env(offer(alice_, USD(2.1), XRP(210), tfSell | tfFillOrKill), Ter(killedCode));
802
803 BEAST_EXPECT(ammBob.expectBalances(XRP(20'000), USD(200), ammBob.tokens()));
804 BEAST_EXPECT(expectOffers(env, bob_, 0));
805 }
806 {
807 Env env{*this, features};
808 fund(env, gw_, {alice_, bob_}, {USD(1'000)}, Fund::All);
809 AMM const ammBob(env, bob_, XRP(20'000), USD(200));
810 // alice_ submits a tfSell | tfFillOrKill offer that crosses.
811 // Even though tfSell is present it doesn't matter this time.
812 env(offer(alice_, USD(2), XRP(220), tfSell | tfFillOrKill));
813 env.close();
814 BEAST_EXPECT(ammBob.expectBalances(
815 XRP(20'220), STAmount{USD, UINT64_C(197'8239366963403), -13}, ammBob.tokens()));
816 BEAST_EXPECT(
817 expectHolding(env, alice_, STAmount{USD, UINT64_C(1'002'17606330366), -11}));
818 BEAST_EXPECT(expectOffers(env, alice_, 0));
819 }
820 {
821 // alice_ submits a tfSell | tfFillOrKill offer that crosses and
822 // returns more than was asked for (because of the tfSell flag).
823 Env env{*this, features};
824 fund(env, gw_, {alice_, bob_}, {USD(1'000)}, Fund::All);
825 AMM const ammBob(env, bob_, XRP(20'000), USD(200));
826
827 env(offer(alice_, USD(10), XRP(1'500), tfSell | tfFillOrKill));
828 env.close();
829
830 BEAST_EXPECT(ammBob.expectBalances(
831 XRP(21'500), STAmount{USD, UINT64_C(186'046511627907), -12}, ammBob.tokens()));
832 BEAST_EXPECT(
833 expectHolding(env, alice_, STAmount{USD, UINT64_C(1'013'953488372093), -12}));
834 BEAST_EXPECT(expectOffers(env, alice_, 0));
835 }
836 {
837 // alice_ submits a tfSell | tfFillOrKill offer that doesn't cross.
838 // This would have succeeded with a regular tfSell, but the
839 // fillOrKill prevents the transaction from crossing since not
840 // all of the offer is consumed because AMM generated offer,
841 // which matches alice_'s offer quality is ~ 10XRP/0.01996USD.
842 Env env{*this, features};
843 fund(env, gw_, {alice_, bob_}, {USD(10'000)}, Fund::All);
844 AMM const ammBob(env, bob_, XRP(5000), USD(10));
845
846 env(offer(alice_, USD(1), XRP(501), tfSell | tfFillOrKill), Ter(tecKILLED));
847 env.close();
848 BEAST_EXPECT(expectOffers(env, alice_, 0));
849 BEAST_EXPECT(expectOffers(env, bob_, 0));
850 }
851 }
852
853 void
855 {
856 testcase("Transfer Rate Offer");
857
858 using namespace jtx;
859
860 // AMM XRP/USD. Alice places USD/XRP offer.
861 testAMM(
862 [&](AMM& ammAlice, Env& env) {
863 env(rate(gw_, 1.25));
864 env.close();
865
866 env(offer(carol_, USD(100), XRP(100)));
867 env.close();
868
869 // AMM doesn't pay the transfer fee
870 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), USD(10'000), ammAlice.tokens()));
871 BEAST_EXPECT(expectHolding(env, carol_, USD(30'100)));
872 BEAST_EXPECT(expectOffers(env, carol_, 0));
873 },
874 {{XRP(10'000), USD(10'100)}},
875 0,
876 std::nullopt,
877 {features});
878
879 // Reverse the order, so the offer in the books is to sell XRP
880 // in return for USD.
881 testAMM(
882 [&](AMM& ammAlice, Env& env) {
883 env(rate(gw_, 1.25));
884 env.close();
885
886 env(offer(carol_, XRP(100), USD(100)));
887 env.close();
888
889 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), USD(10'100), ammAlice.tokens()));
890 // Carol pays 25% transfer fee
891 BEAST_EXPECT(expectHolding(env, carol_, USD(29'875)));
892 BEAST_EXPECT(expectOffers(env, carol_, 0));
893 },
894 {{XRP(10'100), USD(10'000)}},
895 0,
896 std::nullopt,
897 {features});
898
899 {
900 // Bridged crossing.
901 Env env{*this, features};
902 fund(env, gw_, {alice_, bob_, carol_}, {USD(15'000), EUR(15'000)}, Fund::All);
903 env(rate(gw_, 1.25));
904
905 // The scenario:
906 // o USD/XRP AMM is created.
907 // o EUR/XRP Offer is created.
908 // o carol_ has EUR but wants USD.
909 // Note that Carol's offer must come last. If Carol's offer is
910 // placed before AMM is created, then autobridging will not occur.
911 AMM const ammAlice(env, alice_, XRP(10'000), USD(10'100));
912 env(offer(bob_, EUR(100), XRP(100)));
913 env.close();
914
915 // Carol makes an offer that consumes AMM liquidity and
916 // fully consumes Bob's offer.
917 env(offer(carol_, USD(100), EUR(100)));
918 env.close();
919
920 // AMM doesn't pay the transfer fee
921 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), USD(10'000), ammAlice.tokens()));
922 BEAST_EXPECT(expectHolding(env, carol_, USD(15'100)));
923 // Carol pays 25% transfer fee.
924 BEAST_EXPECT(expectHolding(env, carol_, EUR(14'875)));
925 BEAST_EXPECT(expectOffers(env, carol_, 0));
926 BEAST_EXPECT(expectOffers(env, bob_, 0));
927 }
928
929 {
930 // Bridged crossing. The transfer fee is paid on the step not
931 // involving AMM as src/dst.
932 Env env{*this, features};
933 fund(env, gw_, {alice_, bob_, carol_}, {USD(15'000), EUR(15'000)}, Fund::All);
934 env(rate(gw_, 1.25));
935
936 // The scenario:
937 // o USD/XRP AMM is created.
938 // o EUR/XRP Offer is created.
939 // o carol_ has EUR but wants USD.
940 // Note that Carol's offer must come last. If Carol's offer is
941 // placed before AMM is created, then autobridging will not occur.
942 AMM const ammAlice(env, alice_, XRP(10'000), USD(10'050));
943 env(offer(bob_, EUR(100), XRP(100)));
944 env.close();
945
946 // Carol makes an offer that consumes AMM liquidity and
947 // partially consumes Bob's offer.
948 env(offer(carol_, USD(50), EUR(50)));
949 env.close();
950 // This test verifies that the amount removed from an offer
951 // accounts for the transfer fee that is removed from the
952 // account but not from the remaining offer.
953
954 // AMM doesn't pay the transfer fee
955 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'050), USD(10'000), ammAlice.tokens()));
956 BEAST_EXPECT(expectHolding(env, carol_, USD(15'050)));
957 // Carol pays 25% transfer fee.
958 BEAST_EXPECT(expectHolding(env, carol_, EUR(14'937.5)));
959 BEAST_EXPECT(expectOffers(env, carol_, 0));
960 BEAST_EXPECT(expectOffers(env, bob_, 1, {{Amounts{EUR(50), XRP(50)}}}));
961 }
962
963 {
964 // A trust line's QualityIn should not affect offer crossing.
965 // Bridged crossing. The transfer fee is paid on the step not
966 // involving AMM as src/dst.
967 Env env{*this, features};
968 fund(env, gw_, {alice_, carol_, bob_}, XRP(30'000));
969 env(rate(gw_, 1.25));
970 env(trust(alice_, USD(15'000)));
971 env(trust(bob_, EUR(15'000)));
972 env(trust(carol_, EUR(15'000)), QualityInPercent(80));
973 env(trust(bob_, USD(15'000)));
974 env(trust(carol_, USD(15'000)));
975 env.close();
976
977 env(pay(gw_, alice_, USD(11'000)));
978 env(pay(gw_, carol_, EUR(1'000)), Sendmax(EUR(10'000)));
979 env.close();
980 // 1000 / 0.8
981 BEAST_EXPECT(expectHolding(env, carol_, EUR(1'250)));
982 // The scenario:
983 // o USD/XRP AMM is created.
984 // o EUR/XRP Offer is created.
985 // o carol_ has EUR but wants USD.
986 // Note that Carol's offer must come last. If Carol's offer is
987 // placed before AMM is created, then autobridging will not occur.
988 AMM const ammAlice(env, alice_, XRP(10'000), USD(10'100));
989 env(offer(bob_, EUR(100), XRP(100)));
990 env.close();
991
992 // Carol makes an offer that consumes AMM liquidity and
993 // fully consumes Bob's offer.
994 env(offer(carol_, USD(100), EUR(100)));
995 env.close();
996
997 // AMM doesn't pay the transfer fee
998 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), USD(10'000), ammAlice.tokens()));
999 BEAST_EXPECT(expectHolding(env, carol_, USD(100)));
1000 // Carol pays 25% transfer fee: 1250 - 100(offer) - 25(transfer fee)
1001 BEAST_EXPECT(expectHolding(env, carol_, EUR(1'125)));
1002 BEAST_EXPECT(expectOffers(env, carol_, 0));
1003 BEAST_EXPECT(expectOffers(env, bob_, 0));
1004 }
1005
1006 {
1007 // A trust line's QualityOut should not affect offer crossing.
1008 // Bridged crossing. The transfer fee is paid on the step not
1009 // involving AMM as src/dst.
1010 Env env{*this, features};
1011 fund(env, gw_, {alice_, carol_, bob_}, XRP(30'000));
1012 env(rate(gw_, 1.25));
1013 env(trust(alice_, USD(15'000)));
1014 env(trust(bob_, EUR(15'000)));
1015 env(trust(carol_, EUR(15'000)), QualityOutPercent(120));
1016 env(trust(bob_, USD(15'000)));
1017 env(trust(carol_, USD(15'000)));
1018 env.close();
1019
1020 env(pay(gw_, alice_, USD(11'000)));
1021 env(pay(gw_, carol_, EUR(1'000)), Sendmax(EUR(10'000)));
1022 env.close();
1023 BEAST_EXPECT(expectHolding(env, carol_, EUR(1'000)));
1024 // The scenario:
1025 // o USD/XRP AMM is created.
1026 // o EUR/XRP Offer is created.
1027 // o carol_ has EUR but wants USD.
1028 // Note that Carol's offer must come last. If Carol's offer is
1029 // placed before AMM is created, then autobridging will not occur.
1030 AMM const ammAlice(env, alice_, XRP(10'000), USD(10'100));
1031 env(offer(bob_, EUR(100), XRP(100)));
1032 env.close();
1033
1034 // Carol makes an offer that consumes AMM liquidity and
1035 // fully consumes Bob's offer.
1036 env(offer(carol_, USD(100), EUR(100)));
1037 env.close();
1038
1039 // AMM pay doesn't transfer fee
1040 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), USD(10'000), ammAlice.tokens()));
1041 BEAST_EXPECT(expectHolding(env, carol_, USD(100)));
1042 // Carol pays 25% transfer fee: 1000 - 100(offer) - 25(transfer fee)
1043 BEAST_EXPECT(expectHolding(env, carol_, EUR(875)));
1044 BEAST_EXPECT(expectOffers(env, carol_, 0));
1045 BEAST_EXPECT(expectOffers(env, bob_, 0));
1046 }
1047 }
1048
1049 void
1051 {
1052 // This test is not the same as corresponding testSelfIssueOffer()
1053 // in the Offer_test. It simply tests AMM with self issue and
1054 // offer crossing.
1055 using namespace jtx;
1056
1057 Env env{*this, features};
1058
1059 auto const usdBob = bob_["USD"];
1060 auto const f = env.current()->fees().base;
1061
1062 env.fund(XRP(30'000) + f, alice_, bob_);
1063 env.close();
1064 AMM const ammBob(env, bob_, XRP(10'000), usdBob(10'100));
1065
1066 env(offer(alice_, usdBob(100), XRP(100)));
1067 env.close();
1068
1069 BEAST_EXPECT(ammBob.expectBalances(XRP(10'100), usdBob(10'000), ammBob.tokens()));
1070 BEAST_EXPECT(expectOffers(env, alice_, 0));
1071 BEAST_EXPECT(expectHolding(env, alice_, usdBob(100)));
1072 }
1073
1074 void
1076 {
1077 // At one point in the past this invalid path caused assert. It
1078 // should not be possible for user-supplied data to cause assert.
1079 // Make sure assert is gone.
1080 testcase("Bad path assert");
1081
1082 using namespace jtx;
1083
1084 Env env{*this, features};
1085
1086 // The fee that's charged for transactions.
1087 auto const fee = env.current()->fees().base;
1088 {
1089 // A trust line's QualityOut should not affect offer crossing.
1090 auto const ann = Account("ann");
1091 auto const aBux = ann["BUX"];
1092 auto const localBob = Account("bob");
1093 auto const cam = Account("cam");
1094 auto const dan = Account("dan");
1095 auto const dBux = dan["BUX"];
1096
1097 // Verify trust line QualityOut affects payments.
1098 env.fund(reserve(env, 4) + (fee * 4), ann, localBob, cam, dan);
1099 env.close();
1100
1101 env(trust(localBob, aBux(400)));
1102 env(trust(localBob, dBux(200)), QualityOutPercent(120));
1103 env(trust(cam, dBux(100)));
1104 env.close();
1105 env(pay(dan, localBob, dBux(100)));
1106 env.close();
1107 BEAST_EXPECT(expectHolding(env, localBob, dBux(100)));
1108
1109 env(pay(ann, cam, dBux(60)), Path(localBob, dan), Sendmax(aBux(200)));
1110 env.close();
1111
1112 BEAST_EXPECT(expectHolding(env, ann, aBux(kNone)));
1113 BEAST_EXPECT(expectHolding(env, ann, dBux(kNone)));
1114 BEAST_EXPECT(expectHolding(env, localBob, aBux(72)));
1115 BEAST_EXPECT(expectHolding(env, localBob, dBux(40)));
1116 BEAST_EXPECT(expectHolding(env, cam, aBux(kNone)));
1117 BEAST_EXPECT(expectHolding(env, cam, dBux(60)));
1118 BEAST_EXPECT(expectHolding(env, dan, aBux(kNone)));
1119 BEAST_EXPECT(expectHolding(env, dan, dBux(kNone)));
1120
1121 AMM const ammBob(env, localBob, aBux(30), dBux(30));
1122
1123 env(trust(ann, dBux(100)));
1124 env.close();
1125
1126 // This payment caused the assert.
1127 env(pay(ann, ann, dBux(30)), Path(aBux, dBux), Sendmax(aBux(30)), Ter(temBAD_PATH));
1128 env.close();
1129
1130 BEAST_EXPECT(ammBob.expectBalances(aBux(30), dBux(30), ammBob.tokens()));
1131 BEAST_EXPECT(expectHolding(env, ann, aBux(kNone)));
1132 BEAST_EXPECT(expectHolding(env, ann, dBux(0)));
1133 BEAST_EXPECT(expectHolding(env, cam, aBux(kNone)));
1134 BEAST_EXPECT(expectHolding(env, cam, dBux(60)));
1135 BEAST_EXPECT(expectHolding(env, dan, aBux(0)));
1136 BEAST_EXPECT(expectHolding(env, dan, dBux(kNone)));
1137 }
1138 }
1139
1140 void
1142 {
1143 // The offer crossing code expects that a DirectStep is always
1144 // preceded by a BookStep. In one instance the default path
1145 // was not matching that assumption. Here we recreate that case
1146 // so we can prove the bug stays fixed.
1147 testcase("Direct to Direct path");
1148
1149 using namespace jtx;
1150
1151 Env env{*this, features};
1152
1153 auto const ann = Account("ann");
1154 auto const localBob = Account("bob");
1155 auto const cam = Account("cam");
1156 auto const carol = Account("carol");
1157 auto const aBux = ann["BUX"];
1158 auto const bBux = localBob["BUX"];
1159
1160 auto const fee = env.current()->fees().base;
1161 env.fund(XRP(1'000), carol);
1162 env.fund(reserve(env, 4) + (fee * 5), ann, localBob, cam);
1163 env.close();
1164
1165 env(trust(ann, bBux(40)));
1166 env(trust(cam, aBux(40)));
1167 env(trust(localBob, aBux(30)));
1168 env(trust(cam, bBux(40)));
1169 env(trust(carol, bBux(400)));
1170 env(trust(carol, aBux(400)));
1171 env.close();
1172
1173 env(pay(ann, cam, aBux(35)));
1174 env(pay(localBob, cam, bBux(35)));
1175 env(pay(localBob, carol, bBux(400)));
1176 env(pay(ann, carol, aBux(400)));
1177
1178 AMM const ammCarol(env, carol, aBux(300), bBux(330));
1179
1180 // cam puts an offer on the books that her upcoming offer could cross.
1181 // But this offer should be deleted, not crossed, by her upcoming
1182 // offer.
1183 env(offer(cam, aBux(29), bBux(30), tfPassive));
1184 env.close();
1185 env.require(Balance(cam, aBux(35)));
1186 env.require(Balance(cam, bBux(35)));
1187 env.require(Offers(cam, 1));
1188
1189 // This offer caused the assert.
1190 env(offer(cam, bBux(30), aBux(30)));
1191
1192 // AMM is consumed up to the first cam Offer quality
1193 BEAST_EXPECT(ammCarol.expectBalances(
1194 STAmount{aBux, UINT64_C(309'3541659651604), -13},
1195 STAmount{bBux, UINT64_C(320'0215509984419), -13},
1196 ammCarol.tokens()));
1197 BEAST_EXPECT(expectOffers(
1198 env,
1199 cam,
1200 1,
1201 {{Amounts{
1202 STAmount{bBux, UINT64_C(20'0215509984419), -13},
1203 STAmount{aBux, UINT64_C(20'0215509984419), -13}}}}));
1204 }
1205
1206 void
1208 {
1209 testcase("lsfRequireAuth");
1210
1211 using namespace jtx;
1212
1213 Env env{*this, features};
1214
1215 auto const aliceUSD = alice_["USD"];
1216 auto const bobUSD = bob_["USD"];
1217
1218 env.fund(XRP(400'000), gw_, alice_, bob_);
1219 env.close();
1220
1221 // GW requires authorization for holders of its IOUs
1222 env(fset(gw_, asfRequireAuth));
1223 env.close();
1224
1225 // Properly set trust and have gw_ authorize bob_ and alice_
1226 env(trust(gw_, bobUSD(100)), Txflags(tfSetfAuth));
1227 env(trust(bob_, USD(100)));
1228 env(trust(gw_, aliceUSD(100)), Txflags(tfSetfAuth));
1229 env(trust(alice_, USD(2'000)));
1230 env(pay(gw_, alice_, USD(1'000)));
1231 env.close();
1232 // Alice is able to create AMM since the GW has authorized her
1233 AMM const ammAlice(env, alice_, USD(1'000), XRP(1'050));
1234
1235 // Set up authorized trust line for AMM.
1236 env(trust(gw_, STAmount{Issue{USD.currency, ammAlice.ammAccount()}, 10}),
1237 Txflags(tfSetfAuth));
1238 env.close();
1239
1240 env(pay(gw_, bob_, USD(50)));
1241 env.close();
1242
1243 BEAST_EXPECT(expectHolding(env, bob_, USD(50)));
1244
1245 // Bob's offer should cross Alice's AMM
1246 env(offer(bob_, XRP(50), USD(50)));
1247 env.close();
1248
1249 BEAST_EXPECT(ammAlice.expectBalances(USD(1'050), XRP(1'000), ammAlice.tokens()));
1250 BEAST_EXPECT(expectOffers(env, bob_, 0));
1251 BEAST_EXPECT(expectHolding(env, bob_, USD(0)));
1252 }
1253
1254 // Same shape as testRequireAuth, except the issuer never authorizes the AMM's own trust line.
1255 // An AMM holds the asset for its liquidity providers and cannot sign a TrustSet for itself, so
1256 // once pseudo-accounts are implicitly authorized the pool keeps trading. Before that the offer
1257 // stream drops it and the taker's offer stays on the book.
1258 void
1260 {
1261 testcase("lsfRequireAuth, unauthorized AMM pseudo-account");
1262
1263 using namespace jtx;
1264
1265 bool const pseudoExempt = features[fixCleanup3_4_0];
1266
1267 Env env{*this, features};
1268
1269 auto const aliceUSD = alice_["USD"];
1270 auto const bobUSD = bob_["USD"];
1271
1272 env.fund(XRP(400'000), gw_, alice_, bob_);
1273 env.close();
1274
1275 env(fset(gw_, asfRequireAuth));
1276 env.close();
1277
1278 env(trust(gw_, bobUSD(100)), Txflags(tfSetfAuth));
1279 env(trust(bob_, USD(100)));
1280 env(trust(gw_, aliceUSD(100)), Txflags(tfSetfAuth));
1281 env(trust(alice_, USD(2'000)));
1282 env(pay(gw_, alice_, USD(1'000)));
1283 env.close();
1284
1285 AMM const ammAlice(env, alice_, USD(1'000), XRP(1'050));
1286
1287 // The pool's own line stays unauthorized: AMMCreate opens it without the flag, and the
1288 // pseudo-account has no key to ask for one.
1289 auto const ammLineAuthorized = [&] -> bool {
1290 auto const line =
1291 env.le(keylet::trustLine(ammAlice.ammAccount(), USD.issue().account, USD.currency));
1292 if (!BEAST_EXPECT(line))
1293 return false;
1294 return line->isFlag(
1295 ammAlice.ammAccount() > USD.issue().account ? lsfLowAuth : lsfHighAuth);
1296 };
1297 BEAST_EXPECT(!ammLineAuthorized());
1298
1299 env(pay(gw_, bob_, USD(50)));
1300 env.close();
1301 BEAST_EXPECT(expectHolding(env, bob_, USD(50)));
1302
1303 // Bob sells USD into the pool, so the pool is the side that has to be authorized to hold
1304 // the asset.
1305 env(offer(bob_, XRP(50), USD(50)));
1306 env.close();
1307
1308 if (pseudoExempt)
1309 {
1310 BEAST_EXPECT(ammAlice.expectBalances(USD(1'050), XRP(1'000), ammAlice.tokens()));
1311 BEAST_EXPECT(expectOffers(env, bob_, 0));
1312 BEAST_EXPECT(expectHolding(env, bob_, USD(0)));
1313 }
1314 else
1315 {
1316 // The pool is skipped, so nothing crosses and the offer rests on the book.
1317 BEAST_EXPECT(ammAlice.expectBalances(USD(1'000), XRP(1'050), ammAlice.tokens()));
1318 BEAST_EXPECT(expectOffers(env, bob_, 1));
1319 BEAST_EXPECT(expectHolding(env, bob_, USD(50)));
1320 }
1321
1322 // Either way the exemption skips the check rather than setting the flag.
1323 BEAST_EXPECT(!ammLineAuthorized());
1324 }
1325
1326 void
1328 {
1329 testcase("Missing Auth");
1330
1331 using namespace jtx;
1332
1333 Env env{*this, features};
1334
1335 env.fund(XRP(400'000), gw_, alice_, bob_);
1336 env.close();
1337
1338 // Alice doesn't have the funds
1339 {
1340 AMM const ammAlice(env, alice_, USD(1'000), XRP(1'000), Ter(tecUNFUNDED_AMM));
1341 }
1342
1343 env(fset(gw_, asfRequireAuth));
1344 env.close();
1345
1346 env(trust(gw_, bob_["USD"](50)), Txflags(tfSetfAuth));
1347 env.close();
1348 env(trust(bob_, USD(50)));
1349 env.close();
1350
1351 env(pay(gw_, bob_, USD(50)));
1352 env.close();
1353 BEAST_EXPECT(expectHolding(env, bob_, USD(50)));
1354
1355 // Alice should not be able to create AMM without authorization.
1356 {
1357 AMM const ammAlice(env, alice_, USD(1'000), XRP(1'000), Ter(tecNO_LINE));
1358 }
1359
1360 // Set up a trust line for Alice, but don't authorize it. Alice
1361 // should still not be able to create AMM for USD/gw_.
1362 env(trust(gw_, alice_["USD"](2'000)));
1363 env.close();
1364
1365 {
1366 AMM const ammAlice(env, alice_, USD(1'000), XRP(1'000), Ter(tecNO_AUTH));
1367 }
1368
1369 // Finally, set up an authorized trust line for Alice. Now Alice's
1370 // AMM create should succeed.
1371 env(trust(gw_, alice_["USD"](100)), Txflags(tfSetfAuth));
1372 env(trust(alice_, USD(2'000)));
1373 env(pay(gw_, alice_, USD(1'000)));
1374 env.close();
1375
1376 AMM const ammAlice(env, alice_, USD(1'000), XRP(1'050));
1377
1378 // Set up authorized trust line for AMM.
1379 env(trust(gw_, STAmount{Issue{USD.currency, ammAlice.ammAccount()}, 10}),
1380 Txflags(tfSetfAuth));
1381 env.close();
1382
1383 // Now bob_ creates his offer again, which crosses with alice_'s AMM.
1384 env(offer(bob_, XRP(50), USD(50)));
1385 env.close();
1386
1387 BEAST_EXPECT(ammAlice.expectBalances(USD(1'050), XRP(1'000), ammAlice.tokens()));
1388 BEAST_EXPECT(expectOffers(env, bob_, 0));
1389 BEAST_EXPECT(expectHolding(env, bob_, USD(0)));
1390 }
1391
1392 void
1426
1427 void
1429 {
1430 testcase("path find consume all");
1431 using namespace jtx;
1432
1433 Env env = pathTestEnv();
1434 env.fund(XRP(100'000'250), alice_);
1435 fund(env, gw_, {carol_, bob_}, {USD(100)}, Fund::All);
1436 fund(env, gw_, {alice_}, {USD(100)}, Fund::TokenOnly);
1437 AMM const ammCarol(env, carol_, XRP(100), USD(100));
1438
1439 STPathSet st;
1440 STAmount sa;
1441 STAmount da;
1442 std::tie(st, sa, da) = findPaths(
1443 env, alice_, bob_, bob_["AUD"](-1), std::optional<STAmount>(XRP(100'000'000)));
1444 BEAST_EXPECT(st.empty());
1445 std::tie(st, sa, da) = findPaths(
1446 env, alice_, bob_, bob_["USD"](-1), std::optional<STAmount>(XRP(100'000'000)));
1447 // Alice sends all requested 100,000,000XRP
1448 BEAST_EXPECT(sa == XRP(100'000'000));
1449 // Bob gets ~99.99USD. This is the amount Bob
1450 // can get out of AMM for 100,000,000XRP.
1451 BEAST_EXPECT(equal(da, STAmount{bob_["USD"], UINT64_C(99'9999000001), -10}));
1452 }
1453
1454 // carol_ holds gateway AUD, sells gateway AUD for XRP
1455 // bob_ will hold gateway AUD
1456 // alice_ pays bob_ gateway AUD using XRP
1457 void
1459 {
1460 testcase("via gateway");
1461 using namespace jtx;
1462
1463 Env env = pathTestEnv();
1464 auto const aud = gw_["AUD"];
1465 env.fund(XRP(10'000), alice_, bob_, carol_, gw_);
1466 env.close();
1467 env(rate(gw_, 1.1));
1468 env.trust(aud(2'000), bob_, carol_);
1469 env(pay(gw_, carol_, aud(51)));
1470 env.close();
1471 AMM const ammCarol(env, carol_, XRP(40), aud(51));
1472 env(pay(alice_, bob_, aud(10)), Sendmax(XRP(100)), Paths(XRP));
1473 env.close();
1474 // AMM offer is 51.282052XRP/11AUD, 11AUD/1.1 = 10AUD to bob_
1475 BEAST_EXPECT(ammCarol.expectBalances(XRP(51), aud(40), ammCarol.tokens()));
1476 BEAST_EXPECT(expectHolding(env, bob_, aud(10)));
1477
1478 auto const result = findPaths(env, alice_, bob_, Account(bob_)["USD"](25));
1479 BEAST_EXPECT(std::get<0>(result).empty());
1480 }
1481
1482 void
1484 {
1485 testcase("Receive max");
1486 using namespace jtx;
1487 auto const charlie = Account("charlie");
1488 {
1489 // XRP -> IOU receive max
1490 Env env = pathTestEnv();
1491 fund(env, gw_, {alice_, bob_, charlie}, {USD(11)}, Fund::All);
1492 AMM const ammCharlie(env, charlie, XRP(10), USD(11));
1493 auto [st, sa, da] = findPaths(env, alice_, bob_, USD(-1), XRP(1).value());
1494 BEAST_EXPECT(sa == XRP(1));
1495 BEAST_EXPECT(equal(da, USD(1)));
1496 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
1497 {
1498 auto const& pathElem = st[0][0];
1499 BEAST_EXPECT(
1500 pathElem.isOffer() && pathElem.getIssuerID() == gw_.id() &&
1501 pathElem.getCurrency() == USD.currency);
1502 }
1503 }
1504 {
1505 // IOU -> XRP receive max
1506 Env env = pathTestEnv();
1507 fund(env, gw_, {alice_, bob_, charlie}, {USD(11)}, Fund::All);
1508 AMM const ammCharlie(env, charlie, XRP(11), USD(10));
1509 env.close();
1510 auto [st, sa, da] = findPaths(env, alice_, bob_, drops(-1), USD(1).value());
1511 BEAST_EXPECT(sa == USD(1));
1512 BEAST_EXPECT(equal(da, XRP(1)));
1513 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
1514 {
1515 auto const& pathElem = st[0][0];
1516 BEAST_EXPECT(
1517 pathElem.isOffer() && pathElem.getIssuerID() == xrpAccount() &&
1518 pathElem.getCurrency() == xrpCurrency());
1519 }
1520 }
1521 }
1522
1523 void
1525 {
1526 testcase("Path Find: XRP -> XRP and XRP -> IOU");
1527 using namespace jtx;
1528 Env env = pathTestEnv();
1529 Account const a1{"A1"};
1530 Account const a2{"A2"};
1531 Account const a3{"A3"};
1532 Account const g1{"G1"};
1533 Account const g2{"G2"};
1534 Account const g3{"G3"};
1535 Account const m1{"M1"};
1536
1537 env.fund(XRP(100'000), a1);
1538 env.fund(XRP(10'000), a2);
1539 env.fund(XRP(1'000), a3, g1, g2, g3);
1540 env.fund(XRP(20'000), m1);
1541 env.close();
1542
1543 env.trust(g1["XYZ"](5'000), a1);
1544 env.trust(g3["ABC"](5'000), a1);
1545 env.trust(g2["XYZ"](5'000), a2);
1546 env.trust(g3["ABC"](5'000), a2);
1547 env.trust(a2["ABC"](1'000), a3);
1548 env.trust(g1["XYZ"](100'000), m1);
1549 env.trust(g2["XYZ"](100'000), m1);
1550 env.trust(g3["ABC"](100'000), m1);
1551 env.close();
1552
1553 env(pay(g1, a1, g1["XYZ"](3'500)));
1554 env(pay(g3, a1, g3["ABC"](1'200)));
1555 env(pay(g1, m1, g1["XYZ"](25'000)));
1556 env(pay(g2, m1, g2["XYZ"](25'000)));
1557 env(pay(g3, m1, g3["ABC"](25'000)));
1558 env.close();
1559
1560 AMM const ammM1G1G2(env, m1, g1["XYZ"](1'000), g2["XYZ"](1'000));
1561 AMM const ammM1XrpG3(env, m1, XRP(10'000), g3["ABC"](1'000));
1562
1563 STPathSet st;
1564 STAmount sa, da;
1565
1566 {
1567 auto const& sendAmt = XRP(10);
1568 std::tie(st, sa, da) = findPaths(env, a1, a2, sendAmt, std::nullopt, xrpCurrency());
1569 BEAST_EXPECT(equal(da, sendAmt));
1570 BEAST_EXPECT(st.empty());
1571 }
1572
1573 {
1574 // no path should exist for this since dest account
1575 // does not exist.
1576 auto const& sendAmt = XRP(200);
1577 std::tie(st, sa, da) =
1578 findPaths(env, a1, Account{"A0"}, sendAmt, std::nullopt, xrpCurrency());
1579 BEAST_EXPECT(equal(da, sendAmt));
1580 BEAST_EXPECT(st.empty());
1581 }
1582
1583 {
1584 auto const& sendAmt = g3["ABC"](10);
1585 std::tie(st, sa, da) = findPaths(env, a2, g3, sendAmt, std::nullopt, xrpCurrency());
1586 BEAST_EXPECT(equal(da, sendAmt));
1587 BEAST_EXPECT(equal(sa, XRPAmount{101'010'102}));
1588 BEAST_EXPECT(same(st, stpath(ipe(g3["ABC"]))));
1589 }
1590
1591 {
1592 auto const& sendAmt = a2["ABC"](1);
1593 std::tie(st, sa, da) = findPaths(env, a1, a2, sendAmt, std::nullopt, xrpCurrency());
1594 BEAST_EXPECT(equal(da, sendAmt));
1595 BEAST_EXPECT(equal(sa, XRPAmount{10'010'011}));
1596 BEAST_EXPECT(same(st, stpath(ipe(g3["ABC"]), g3)));
1597 }
1598
1599 {
1600 auto const& sendAmt = a3["ABC"](1);
1601 std::tie(st, sa, da) = findPaths(env, a1, a3, sendAmt, std::nullopt, xrpCurrency());
1602 BEAST_EXPECT(equal(da, sendAmt));
1603 BEAST_EXPECT(equal(sa, XRPAmount{10'010'011}));
1604 BEAST_EXPECT(same(st, stpath(ipe(g3["ABC"]), g3, a2)));
1605 }
1606 }
1607
1608 void
1610 {
1611 testcase("Path Find: non-XRP -> XRP");
1612 using namespace jtx;
1613 Env env = pathTestEnv();
1614 Account const a1{"A1"};
1615 Account const a2{"A2"};
1616 Account const g3{"G3"};
1617 Account const m1{"M1"};
1618
1619 env.fund(XRP(1'000), a1, a2, g3);
1620 env.fund(XRP(11'000), m1);
1621 env.close();
1622
1623 env.trust(g3["ABC"](1'000), a1, a2);
1624 env.trust(g3["ABC"](100'000), m1);
1625 env.close();
1626
1627 env(pay(g3, a1, g3["ABC"](1'000)));
1628 env(pay(g3, a2, g3["ABC"](1'000)));
1629 env(pay(g3, m1, g3["ABC"](1'200)));
1630 env.close();
1631
1632 AMM const ammM1(env, m1, g3["ABC"](1'000), XRP(10'010));
1633
1634 STPathSet st;
1635 STAmount sa, da;
1636
1637 auto const& sendAmt = XRP(10);
1638 std::tie(st, sa, da) = findPaths(env, a1, a2, sendAmt, std::nullopt, a2["ABC"].currency);
1639 BEAST_EXPECT(equal(da, sendAmt));
1640 BEAST_EXPECT(equal(sa, a1["ABC"](1)));
1641 BEAST_EXPECT(same(st, stpath(g3, ipe(xrpIssue()))));
1642 }
1643
1644 void
1646 {
1647 testcase("Path Find: non-XRP -> non-XRP, same currency");
1648 using namespace jtx;
1649 Env env = pathTestEnv();
1650 Account const a1{"A1"};
1651 Account const a2{"A2"};
1652 Account const a3{"A3"};
1653 Account const a4{"A4"};
1654 Account const g1{"G1"};
1655 Account const g2{"G2"};
1656 Account const g3{"G3"};
1657 Account const g4{"G4"};
1658 Account const m1{"M1"};
1659 Account const m2{"M2"};
1660
1661 env.fund(XRP(1'000), a1, a2, a3, g1, g2, g3, g4);
1662 env.fund(XRP(10'000), a4);
1663 env.fund(XRP(21'000), m1, m2);
1664 env.close();
1665
1666 env.trust(g1["HKD"](2'000), a1);
1667 env.trust(g2["HKD"](2'000), a2);
1668 env.trust(g1["HKD"](2'000), a3);
1669 env.trust(g1["HKD"](100'000), m1);
1670 env.trust(g2["HKD"](100'000), m1);
1671 env.trust(g1["HKD"](100'000), m2);
1672 env.trust(g2["HKD"](100'000), m2);
1673 env.close();
1674
1675 env(pay(g1, a1, g1["HKD"](1'000)));
1676 env(pay(g2, a2, g2["HKD"](1'000)));
1677 env(pay(g1, a3, g1["HKD"](1'000)));
1678 env(pay(g1, m1, g1["HKD"](1'200)));
1679 env(pay(g2, m1, g2["HKD"](5'000)));
1680 env(pay(g1, m2, g1["HKD"](1'200)));
1681 env(pay(g2, m2, g2["HKD"](5'000)));
1682 env.close();
1683
1684 AMM const ammM1(env, m1, g1["HKD"](1'010), g2["HKD"](1'000));
1685 AMM const ammM2XrpG2(env, m2, XRP(10'000), g2["HKD"](1'010));
1686 AMM const ammM2G1Xrp(env, m2, g1["HKD"](1'010), XRP(10'000));
1687
1688 STPathSet st;
1689 STAmount sa, da;
1690
1691 {
1692 // A) Borrow or repay --
1693 // Source -> Destination (repay source issuer)
1694 auto const& sendAmt = g1["HKD"](10);
1695 std::tie(st, sa, da) =
1696 findPaths(env, a1, g1, sendAmt, std::nullopt, g1["HKD"].currency);
1697 BEAST_EXPECT(st.empty());
1698 BEAST_EXPECT(equal(da, sendAmt));
1699 BEAST_EXPECT(equal(sa, a1["HKD"](10)));
1700 }
1701
1702 {
1703 // A2) Borrow or repay --
1704 // Source -> Destination (repay destination issuer)
1705 auto const& sendAmt = a1["HKD"](10);
1706 std::tie(st, sa, da) =
1707 findPaths(env, a1, g1, sendAmt, std::nullopt, g1["HKD"].currency);
1708 BEAST_EXPECT(st.empty());
1709 BEAST_EXPECT(equal(da, sendAmt));
1710 BEAST_EXPECT(equal(sa, a1["HKD"](10)));
1711 }
1712
1713 {
1714 // B) Common gateway --
1715 // Source -> AC -> Destination
1716 auto const& sendAmt = a3["HKD"](10);
1717 std::tie(st, sa, da) =
1718 findPaths(env, a1, a3, sendAmt, std::nullopt, g1["HKD"].currency);
1719 BEAST_EXPECT(equal(da, sendAmt));
1720 BEAST_EXPECT(equal(sa, a1["HKD"](10)));
1721 BEAST_EXPECT(same(st, stpath(g1)));
1722 }
1723
1724 {
1725 // C) Gateway to gateway --
1726 // Source -> OB -> Destination
1727 auto const& sendAmt = g2["HKD"](10);
1728 std::tie(st, sa, da) =
1729 findPaths(env, g1, g2, sendAmt, std::nullopt, g1["HKD"].currency);
1730 BEAST_EXPECT(equal(da, sendAmt));
1731 BEAST_EXPECT(equal(sa, g1["HKD"](10)));
1732 BEAST_EXPECT(same(
1733 st,
1734 stpath(ipe(g2["HKD"])),
1735 stpath(m1),
1736 stpath(m2),
1737 stpath(ipe(xrpIssue()), ipe(g2["HKD"]))));
1738 }
1739
1740 {
1741 // D) User to unlinked gateway via order book --
1742 // Source -> AC -> OB -> Destination
1743 auto const& sendAmt = g2["HKD"](10);
1744 std::tie(st, sa, da) =
1745 findPaths(env, a1, g2, sendAmt, std::nullopt, g1["HKD"].currency);
1746 BEAST_EXPECT(equal(da, sendAmt));
1747 BEAST_EXPECT(equal(sa, a1["HKD"](10)));
1748 BEAST_EXPECT(same(
1749 st,
1750 stpath(g1, m1),
1751 stpath(g1, m2),
1752 stpath(g1, ipe(g2["HKD"])),
1753 stpath(g1, ipe(xrpIssue()), ipe(g2["HKD"]))));
1754 }
1755
1756 {
1757 // I4) XRP bridge" --
1758 // Source -> AC -> OB to XRP -> OB from XRP -> AC ->
1759 // Destination
1760 auto const& sendAmt = a2["HKD"](10);
1761 std::tie(st, sa, da) =
1762 findPaths(env, a1, a2, sendAmt, std::nullopt, g1["HKD"].currency);
1763 BEAST_EXPECT(equal(da, sendAmt));
1764 BEAST_EXPECT(equal(sa, a1["HKD"](10)));
1765 BEAST_EXPECT(same(
1766 st,
1767 stpath(g1, m1, g2),
1768 stpath(g1, m2, g2),
1769 stpath(g1, ipe(g2["HKD"]), g2),
1770 stpath(g1, ipe(xrpIssue()), ipe(g2["HKD"]), g2)));
1771 }
1772 }
1773
1774 void
1776 {
1777 testcase("Path Find: non-XRP -> non-XRP, same currency");
1778 using namespace jtx;
1779 Env env = pathTestEnv();
1780 Account const a1{"A1"};
1781 Account const a2{"A2"};
1782 Account const a3{"A3"};
1783 Account const g1{"G1"};
1784 Account const g2{"G2"};
1785 Account const m1{"M1"};
1786
1787 env.fund(XRP(11'000), m1);
1788 env.fund(XRP(1'000), a1, a2, a3, g1, g2);
1789 env.close();
1790
1791 env.trust(g1["HKD"](2'000), a1);
1792 env.trust(g2["HKD"](2'000), a2);
1793 env.trust(a2["HKD"](2'000), a3);
1794 env.trust(g1["HKD"](100'000), m1);
1795 env.trust(g2["HKD"](100'000), m1);
1796 env.close();
1797
1798 env(pay(g1, a1, g1["HKD"](1'000)));
1799 env(pay(g2, a2, g2["HKD"](1'000)));
1800 env(pay(g1, m1, g1["HKD"](5'000)));
1801 env(pay(g2, m1, g2["HKD"](5'000)));
1802 env.close();
1803
1804 AMM const ammM1(env, m1, g1["HKD"](1'010), g2["HKD"](1'000));
1805
1806 // E) Gateway to user
1807 // Source -> OB -> AC -> Destination
1808 auto const& sendAmt = a2["HKD"](10);
1809 STPathSet st;
1810 STAmount sa, da;
1811 std::tie(st, sa, da) = findPaths(env, g1, a2, sendAmt, std::nullopt, g1["HKD"].currency);
1812 BEAST_EXPECT(equal(da, sendAmt));
1813 BEAST_EXPECT(equal(sa, g1["HKD"](10)));
1814 BEAST_EXPECT(same(st, stpath(m1, g2), stpath(ipe(g2["HKD"]), g2)));
1815 }
1816
1817 void
1819 {
1820 testcase("falseDryChanges");
1821
1822 using namespace jtx;
1823
1824 Env env(*this, features);
1825
1826 env.fund(XRP(10'000), alice_, gw_);
1827 // This removes no ripple for carol_,
1828 // different from the original test
1829 fund(env, gw_, {carol_}, XRP(10'000), {}, Fund::Acct);
1830 auto const ammXrpPool = env.current()->fees().increment * 2;
1831 env.fund(reserve(env, 5) + ammCrtFee(env) + ammXrpPool, bob_);
1832 env.close();
1833 env.trust(USD(1'000), alice_, bob_, carol_);
1834 env.trust(EUR(1'000), alice_, bob_, carol_);
1835
1836 env(pay(gw_, alice_, EUR(50)));
1837 env(pay(gw_, bob_, USD(150)));
1838
1839 // Bob has _just_ slightly less than 50 xrp available
1840 // If his owner count changes, he will have more liquidity.
1841 // This is one error case to test (when Flow is used).
1842 // Computing the incoming xrp to the XRP/USD offer will require two
1843 // recursive calls to the EUR/XRP offer. The second call will return
1844 // tecPATH_DRY, but the entire path should not be marked as dry.
1845 // This is the second error case to test (when flowV1 is used).
1846 env(offer(bob_, EUR(50), XRP(50)));
1847 AMM const ammBob(env, bob_, ammXrpPool, USD(150));
1848
1849 env(pay(alice_, carol_, USD(1'000'000)),
1850 Path(~XRP, ~USD),
1851 Sendmax(EUR(500)),
1852 Txflags(tfNoRippleDirect | tfPartialPayment));
1853
1854 auto const carolUSD = env.balance(carol_, USD).value();
1855 BEAST_EXPECT(carolUSD > USD(0) && carolUSD < USD(50));
1856 }
1857
1858 void
1860 {
1861 testcase("Book Step");
1862
1863 using namespace jtx;
1864
1865 {
1866 // simple IOU/IOU offer
1867 Env env(*this, features);
1868
1869 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000), {BTC(100), USD(150)}, Fund::All);
1870
1871 AMM const ammBob(env, bob_, BTC(100), USD(150));
1872
1873 env(pay(alice_, carol_, USD(50)), Path(~USD), Sendmax(BTC(50)));
1874
1875 BEAST_EXPECT(expectHolding(env, alice_, BTC(50)));
1876 BEAST_EXPECT(expectHolding(env, bob_, BTC(0)));
1877 BEAST_EXPECT(expectHolding(env, bob_, USD(0)));
1878 BEAST_EXPECT(expectHolding(env, carol_, USD(200)));
1879 BEAST_EXPECT(ammBob.expectBalances(BTC(150), USD(100), ammBob.tokens()));
1880 }
1881 {
1882 // simple IOU/XRP XRP/IOU offer
1883 Env env(*this, features);
1884
1885 fund(env, gw_, {alice_, carol_, bob_}, XRP(10'000), {BTC(100), USD(150)}, Fund::All);
1886
1887 AMM const ammBobBtcXrp(env, bob_, BTC(100), XRP(150));
1888 AMM const ammBobXrpUsd(env, bob_, XRP(100), USD(150));
1889
1890 env(pay(alice_, carol_, USD(50)), Path(~XRP, ~USD), Sendmax(BTC(50)));
1891
1892 BEAST_EXPECT(expectHolding(env, alice_, BTC(50)));
1893 BEAST_EXPECT(expectHolding(env, bob_, BTC(0)));
1894 BEAST_EXPECT(expectHolding(env, bob_, USD(0)));
1895 BEAST_EXPECT(expectHolding(env, carol_, USD(200)));
1896 BEAST_EXPECT(ammBobBtcXrp.expectBalances(BTC(150), XRP(100), ammBobBtcXrp.tokens()));
1897 BEAST_EXPECT(ammBobXrpUsd.expectBalances(XRP(150), USD(100), ammBobXrpUsd.tokens()));
1898 }
1899 {
1900 // simple XRP -> USD through offer and sendmax
1901 Env env(*this, features);
1902
1903 fund(env, gw_, {alice_, carol_, bob_}, XRP(10'000), {USD(150)}, Fund::All);
1904
1905 AMM const ammBob(env, bob_, XRP(100), USD(150));
1906
1907 env(pay(alice_, carol_, USD(50)), Path(~USD), Sendmax(XRP(50)));
1908
1909 BEAST_EXPECT(expectLedgerEntryRoot(env, alice_, xrpMinusFee(env, 10'000 - 50)));
1910 BEAST_EXPECT(expectLedgerEntryRoot(env, bob_, XRP(10'000) - XRP(100) - ammCrtFee(env)));
1911 BEAST_EXPECT(expectHolding(env, bob_, USD(0)));
1912 BEAST_EXPECT(expectHolding(env, carol_, USD(200)));
1913 BEAST_EXPECT(ammBob.expectBalances(XRP(150), USD(100), ammBob.tokens()));
1914 }
1915 {
1916 // simple USD -> XRP through offer and sendmax
1917 Env env(*this, features);
1918
1919 fund(env, gw_, {alice_, carol_, bob_}, XRP(10'000), {USD(100)}, Fund::All);
1920
1921 AMM const ammBob(env, bob_, USD(100), XRP(150));
1922
1923 env(pay(alice_, carol_, XRP(50)), Path(~XRP), Sendmax(USD(50)));
1924
1925 BEAST_EXPECT(expectHolding(env, alice_, USD(50)));
1926 BEAST_EXPECT(expectLedgerEntryRoot(env, bob_, XRP(10'000) - XRP(150) - ammCrtFee(env)));
1927 BEAST_EXPECT(expectHolding(env, bob_, USD(0)));
1928 BEAST_EXPECT(expectLedgerEntryRoot(env, carol_, XRP(10'000 + 50)));
1929 BEAST_EXPECT(ammBob.expectBalances(USD(150), XRP(100), ammBob.tokens()));
1930 }
1931 {
1932 // test unfunded offers are removed when payment succeeds
1933 Env env(*this, features);
1934
1935 env.fund(XRP(10'000), alice_, carol_, gw_);
1936 env.fund(XRP(10'000), bob_);
1937 env.close();
1938 env.trust(USD(1'000), alice_, bob_, carol_);
1939 env.trust(BTC(1'000), alice_, bob_, carol_);
1940 env.trust(EUR(1'000), alice_, bob_, carol_);
1941 env.close();
1942
1943 env(pay(gw_, alice_, BTC(60)));
1944 env(pay(gw_, bob_, USD(200)));
1945 env(pay(gw_, bob_, EUR(150)));
1946 env.close();
1947
1948 env(offer(bob_, BTC(50), USD(50)));
1949 env(offer(bob_, BTC(40), EUR(50)));
1950 env.close();
1951 AMM const ammBob(env, bob_, EUR(100), USD(150));
1952
1953 // unfund offer
1954 env(pay(bob_, gw_, EUR(50)));
1955 BEAST_EXPECT(isOffer(env, bob_, BTC(50), USD(50)));
1956 BEAST_EXPECT(isOffer(env, bob_, BTC(40), EUR(50)));
1957
1958 env(pay(alice_, carol_, USD(50)), Path(~USD), Path(~EUR, ~USD), Sendmax(BTC(60)));
1959
1960 env.require(Balance(alice_, BTC(10)));
1961 env.require(Balance(bob_, BTC(50)));
1962 env.require(Balance(bob_, USD(0)));
1963 env.require(Balance(bob_, EUR(0)));
1964 env.require(Balance(carol_, USD(50)));
1965 // used in the payment
1966 BEAST_EXPECT(!isOffer(env, bob_, BTC(50), USD(50)));
1967 // found unfunded
1968 BEAST_EXPECT(!isOffer(env, bob_, BTC(40), EUR(50)));
1969 // unchanged
1970 BEAST_EXPECT(ammBob.expectBalances(EUR(100), USD(150), ammBob.tokens()));
1971 }
1972 {
1973 // test unfunded offers are removed when the payment fails.
1974 // bob_ makes two offers: a funded 50 USD for 50 BTC and an
1975 // unfunded 50 EUR for 60 BTC. alice_ pays carol_ 61 USD with 61
1976 // BTC. alice_ only has 60 BTC, so the payment will fail. The
1977 // payment uses two paths: one through bob_'s funded offer and
1978 // one through his unfunded offer. When the payment fails `flow`
1979 // should return the unfunded offer. This test is intentionally
1980 // similar to the one that removes unfunded offers when the
1981 // payment succeeds.
1982 Env env(*this, features);
1983
1984 env.fund(XRP(10'000), bob_, carol_, gw_);
1985 env.close();
1986 // Sets rippling on, this is different from
1987 // the original test
1988 fund(env, gw_, {alice_}, XRP(10'000), {}, Fund::Acct);
1989 env.trust(USD(1'000), alice_, bob_, carol_);
1990 env.trust(BTC(1'000), alice_, bob_, carol_);
1991 env.trust(EUR(1'000), alice_, bob_, carol_);
1992 env.close();
1993
1994 env(pay(gw_, alice_, BTC(60)));
1995 env(pay(gw_, bob_, BTC(100)));
1996 env(pay(gw_, bob_, USD(100)));
1997 env(pay(gw_, bob_, EUR(50)));
1998 env(pay(gw_, carol_, EUR(1)));
1999 env.close();
2000
2001 // This is multiplath, which generates limited # of offers
2002 AMM const ammBobBtcUsd(env, bob_, BTC(50), USD(50));
2003 env(offer(bob_, BTC(60), EUR(50)));
2004 env(offer(carol_, BTC(1'000), EUR(1)));
2005 env(offer(bob_, EUR(50), USD(50)));
2006
2007 // unfund offer
2008 env(pay(bob_, gw_, EUR(50)));
2009 BEAST_EXPECT(ammBobBtcUsd.expectBalances(BTC(50), USD(50), ammBobBtcUsd.tokens()));
2010 BEAST_EXPECT(isOffer(env, bob_, BTC(60), EUR(50)));
2011 BEAST_EXPECT(isOffer(env, carol_, BTC(1'000), EUR(1)));
2012 BEAST_EXPECT(isOffer(env, bob_, EUR(50), USD(50)));
2013
2014 auto flowJournal = env.app().getJournal("Flow");
2015 auto const flowResult = [&] {
2016 STAmount const deliver(USD(51));
2017 STAmount smax(BTC(61));
2018 PaymentSandbox sb(env.current().get(), TapNone);
2019 STPathSet paths;
2020 auto const ipe = [](Issue const& iss) {
2021 return STPathElement(
2023 xrpAccount(),
2024 iss.currency,
2025 iss.account);
2026 };
2027 {
2028 // BTC -> USD
2029 STPath const p1({ipe(USD)});
2030 paths.pushBack(p1);
2031 // BTC -> EUR -> USD
2032 STPath const p2({ipe(EUR), ipe(USD)});
2033 paths.pushBack(p2);
2034 }
2035
2036 return flow(
2037 sb,
2038 deliver,
2039 alice_,
2040 carol_,
2041 paths,
2042 false,
2043 false,
2044 true,
2046 std::nullopt,
2047 smax,
2048 std::nullopt,
2049 flowJournal);
2050 }();
2051
2052 BEAST_EXPECT(flowResult.removableOffers.size() == 1);
2053 env.app().getOpenLedger().modify([&](OpenView& view, beast::Journal j) {
2054 if (flowResult.removableOffers.empty())
2055 return false;
2056 Sandbox sb(&view, TapNone);
2057 for (auto const& o : flowResult.removableOffers)
2058 {
2059 if (auto const ok = sb.peek(keylet::offer(o)))
2060 offerDelete(sb, ok, flowJournal);
2061 }
2062 sb.apply(view);
2063 return true;
2064 });
2065
2066 // used in payment, but since payment failed should be untouched
2067 BEAST_EXPECT(ammBobBtcUsd.expectBalances(BTC(50), USD(50), ammBobBtcUsd.tokens()));
2068 BEAST_EXPECT(isOffer(env, carol_, BTC(1'000), EUR(1)));
2069 // found unfunded
2070 BEAST_EXPECT(!isOffer(env, bob_, BTC(60), EUR(50)));
2071 }
2072 {
2073 // Do not produce more in the forward pass than the reverse pass
2074 // This test uses a path that whose reverse pass will compute a
2075 // 0.5 USD input required for a 1 EUR output. It sets a sendmax
2076 // of 0.4 USD, so the payment engine will need to do a forward
2077 // pass. Without limits, the 0.4 USD would produce 1000 EUR in
2078 // the forward pass. This test checks that the payment produces
2079 // 1 EUR, as expected.
2080
2081 Env env(*this, features);
2082 env.fund(XRP(10'000), bob_, carol_, gw_);
2083 env.close();
2084 fund(env, gw_, {alice_}, XRP(10'000), {}, Fund::Acct);
2085 env.trust(USD(1'000), alice_, bob_, carol_);
2086 env.trust(EUR(1'000), alice_, bob_, carol_);
2087 env.close();
2088
2089 env(pay(gw_, alice_, USD(1'000)));
2090 env(pay(gw_, bob_, EUR(1'000)));
2091 env(pay(gw_, bob_, USD(1'000)));
2092 env.close();
2093
2094 // env(offer(bob_, USD(1), drops(2)), txflags(tfPassive));
2095 AMM const ammBob(env, bob_, USD(8), XRPAmount{21});
2096 env(offer(bob_, drops(1), EUR(1'000)), Txflags(tfPassive));
2097
2098 env(pay(alice_, carol_, EUR(1)),
2099 Path(~XRP, ~EUR),
2100 Sendmax(USD(0.4)),
2101 Txflags(tfNoRippleDirect | tfPartialPayment));
2102
2103 BEAST_EXPECT(expectHolding(env, carol_, EUR(1)));
2104 BEAST_EXPECT(ammBob.expectBalances(USD(8.4), XRPAmount{20}, ammBob.tokens()));
2105 }
2106 }
2107
2108 void
2110 {
2111 testcase("No Owner Fee");
2112 using namespace jtx;
2113
2114 {
2115 // payment via AMM
2116 Env env(*this, features);
2117
2118 fund(env, gw_, {alice_, bob_, carol_}, XRP(1'000), {USD(1'000), GBP(1'000)});
2119 env(rate(gw_, 1.25));
2120 env.close();
2121
2122 AMM const amm(env, bob_, GBP(1'000), USD(1'000));
2123
2124 env(pay(alice_, carol_, USD(100)),
2125 Path(~USD),
2126 Sendmax(GBP(150)),
2127 Txflags(tfNoRippleDirect | tfPartialPayment));
2128 env.close();
2129
2130 // alice_ buys 107.1428USD with 120GBP and pays 25% tr fee on 120GBP
2131 // 1,000 - 120*1.25 = 850GBP
2132 BEAST_EXPECT(expectHolding(env, alice_, GBP(850)));
2133 BEAST_EXPECT(amm.expectBalances(
2134 GBP(1'120), STAmount{USD, UINT64_C(892'8571428571429), -13}, amm.tokens()));
2135 // 25% of 85.7142USD is paid in tr fee
2136 // 85.7142*1.25 = 107.1428USD
2137 BEAST_EXPECT(
2138 expectHolding(env, carol_, STAmount(USD, UINT64_C(1'085'714285714286), -12)));
2139 }
2140
2141 {
2142 // Payment via offer and AMM
2143 Env env(*this, features);
2144 Account const ed("ed");
2145
2146 fund(
2147 env,
2148 gw_,
2149 {alice_, bob_, carol_, ed},
2150 XRP(1'000),
2151 {USD(1'000), EUR(1'000), GBP(1'000)});
2152 env(rate(gw_, 1.25));
2153 env.close();
2154
2155 env(offer(ed, GBP(1'000), EUR(1'000)), Txflags(tfPassive));
2156 env.close();
2157
2158 AMM const amm(env, bob_, EUR(1'000), USD(1'000));
2159
2160 env(pay(alice_, carol_, USD(100)),
2161 Path(~EUR, ~USD),
2162 Sendmax(GBP(150)),
2163 Txflags(tfNoRippleDirect | tfPartialPayment));
2164 env.close();
2165
2166 // alice_ buys 120EUR with 120GBP via the offer
2167 // and pays 25% tr fee on 120GBP
2168 // 1,000 - 120*1.25 = 850GBP
2169 BEAST_EXPECT(expectHolding(env, alice_, GBP(850)));
2170 // consumed offer is 120GBP/120EUR
2171 // ed doesn't pay tr fee
2172 BEAST_EXPECT(expectHolding(env, ed, EUR(880), GBP(1'120)));
2173 BEAST_EXPECT(expectOffers(env, ed, 1, {Amounts{GBP(880), EUR(880)}}));
2174 // 25% on 96EUR is paid in tr fee 96*1.25 = 120EUR
2175 // 96EUR is swapped in for 87.5912USD
2176 BEAST_EXPECT(amm.expectBalances(
2177 EUR(1'096), STAmount{USD, UINT64_C(912'4087591240876), -13}, amm.tokens()));
2178 // 25% on 70.0729USD is paid in tr fee 70.0729*1.25 = 87.5912USD
2179 BEAST_EXPECT(
2180 expectHolding(env, carol_, STAmount(USD, UINT64_C(1'070'07299270073), -11)));
2181 }
2182 {
2183 // Payment via AMM, AMM
2184 Env env(*this, features);
2185 Account const ed("ed");
2186
2187 fund(
2188 env,
2189 gw_,
2190 {alice_, bob_, carol_, ed},
2191 XRP(1'000),
2192 {USD(1'000), EUR(1'000), GBP(1'000)});
2193 env(rate(gw_, 1.25));
2194 env.close();
2195
2196 AMM const amm1(env, bob_, GBP(1'000), EUR(1'000));
2197 AMM const amm2(env, ed, EUR(1'000), USD(1'000));
2198
2199 env(pay(alice_, carol_, USD(100)),
2200 Path(~EUR, ~USD),
2201 Sendmax(GBP(150)),
2202 Txflags(tfNoRippleDirect | tfPartialPayment));
2203 env.close();
2204
2205 BEAST_EXPECT(expectHolding(env, alice_, GBP(850)));
2206 // alice_ buys 107.1428EUR with 120GBP and pays 25% tr fee on
2207 // 120GBP 1,000 - 120*1.25 = 850GBP 120GBP is swapped in for
2208 // 107.1428EUR
2209 BEAST_EXPECT(amm1.expectBalances(
2210 GBP(1'120), STAmount{EUR, UINT64_C(892'8571428571429), -13}, amm1.tokens()));
2211 // 25% on 85.7142EUR is paid in tr fee 85.7142*1.25 =
2212 // 107.1428EUR 85.7142EUR is swapped in for 78.9473USD
2213 BEAST_EXPECT(amm2.expectBalances(
2214 STAmount(EUR, UINT64_C(1'085'714285714286), -12),
2215 STAmount{USD, UINT64_C(921'052631578948), -12},
2216 amm2.tokens()));
2217 // 25% on 63.1578USD is paid in tr fee 63.1578*1.25 = 78.9473USD
2218 BEAST_EXPECT(
2219 expectHolding(env, carol_, STAmount(USD, UINT64_C(1'063'157894736842), -12)));
2220 }
2221 {
2222 // AMM offer crossing
2223 Env env(*this, features);
2224
2225 fund(env, gw_, {alice_, bob_}, XRP(1'000), {USD(1'100), EUR(1'100)});
2226 env(rate(gw_, 1.25));
2227 env.close();
2228
2229 AMM const amm(env, bob_, USD(1'000), EUR(1'100));
2230 env(offer(alice_, EUR(100), USD(100)));
2231 env.close();
2232
2233 // 100USD is swapped in for 100EUR
2234 BEAST_EXPECT(amm.expectBalances(USD(1'100), EUR(1'000), amm.tokens()));
2235 // alice_ pays 25% tr fee on 100USD 1100-100*1.25 = 975USD
2236 BEAST_EXPECT(expectHolding(env, alice_, USD(975), EUR(1'200)));
2237 BEAST_EXPECT(expectOffers(env, alice_, 0));
2238 }
2239
2240 {
2241 // Payment via AMM with limit quality
2242 Env env(*this, features);
2243
2244 fund(env, gw_, {alice_, bob_, carol_}, XRP(1'000), {USD(1'000), GBP(1'000)});
2245 env(rate(gw_, 1.25));
2246 env.close();
2247
2248 AMM const amm(env, bob_, GBP(1'000), USD(1'000));
2249
2250 // requested quality limit is 100USD/178.58GBP = 0.55997
2251 // trade quality is 100USD/178.5714 = 0.55999
2252 env(pay(alice_, carol_, USD(100)),
2253 Path(~USD),
2254 Sendmax(GBP(178.58)),
2255 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2256 env.close();
2257
2258 // alice_ buys 125USD with 142.8571GBP and pays 25% tr fee
2259 // on 142.8571GBP
2260 // 1,000 - 142.8571*1.25 = 821.4285GBP
2261 BEAST_EXPECT(
2262 expectHolding(env, alice_, STAmount(GBP, UINT64_C(821'4285714285712), -13)));
2263 // 142.8571GBP is swapped in for 125USD
2264 BEAST_EXPECT(amm.expectBalances(
2265 STAmount{GBP, UINT64_C(1'142'857142857143), -12}, USD(875), amm.tokens()));
2266 // 25% on 100USD is paid in tr fee
2267 // 100*1.25 = 125USD
2268 BEAST_EXPECT(expectHolding(env, carol_, USD(1'100)));
2269 }
2270 {
2271 // Payment via AMM with limit quality, deliver less
2272 // than requested
2273 Env env(*this, features);
2274
2275 fund(env, gw_, {alice_, bob_, carol_}, XRP(1'000), {USD(1'200), GBP(1'200)});
2276 env(rate(gw_, 1.25));
2277 env.close();
2278
2279 AMM const amm(env, bob_, GBP(1'000), USD(1'200));
2280
2281 // requested quality limit is 90USD/120GBP = 0.75
2282 // trade quality is 22.5USD/30GBP = 0.75
2283 env(pay(alice_, carol_, USD(90)),
2284 Path(~USD),
2285 Sendmax(GBP(120)),
2286 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2287 env.close();
2288
2289 // alice_ buys 28.125USD with 24GBP and pays 25% tr fee
2290 // on 24GBP
2291 // 1,200 - 24*1.25 =~ 1,170GBP
2292 BEAST_EXPECT(
2293 expectHolding(env, alice_, STAmount{GBP, UINT64_C(1'169'999999999999), -12}));
2294 // 24GBP is swapped in for 28.125USD
2295 BEAST_EXPECT(amm.expectBalances(
2296 STAmount{GBP, UINT64_C(1'024'000000000001), -12}, USD(1'171.875), amm.tokens()));
2297 // 25% on 22.5USD is paid in tr fee
2298 // 22.5*1.25 = 28.125USD
2299 BEAST_EXPECT(expectHolding(env, carol_, USD(1'222.5)));
2300 }
2301 {
2302 // Payment via offer and AMM with limit quality, deliver less
2303 // than requested
2304 Env env(*this, features);
2305 Account const ed("ed");
2306
2307 fund(
2308 env,
2309 gw_,
2310 {alice_, bob_, carol_, ed},
2311 XRP(1'000),
2312 {USD(1'400), EUR(1'400), GBP(1'400)});
2313 env(rate(gw_, 1.25));
2314 env.close();
2315
2316 env(offer(ed, GBP(1'000), EUR(1'000)), Txflags(tfPassive));
2317 env.close();
2318
2319 AMM const amm(env, bob_, EUR(1'000), USD(1'400));
2320
2321 // requested quality limit is 95USD/140GBP = 0.6785
2322 // trade quality is 59.7321USD/88.0262GBP = 0.6785
2323 env(pay(alice_, carol_, USD(95)),
2324 Path(~EUR, ~USD),
2325 Sendmax(GBP(140)),
2326 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2327 env.close();
2328
2329 // alice_ buys 70.4210EUR with 70.4210GBP via the offer
2330 // and pays 25% tr fee on 70.4210GBP
2331 // 1,400 - 70.4210*1.25 = 1400 - 88.0262 = 1311.9736GBP
2332 BEAST_EXPECT(
2333 expectHolding(env, alice_, STAmount{GBP, UINT64_C(1'311'973684210525), -12}));
2334 // ed doesn't pay tr fee, the balances reflect consumed offer
2335 // 70.4210GBP/70.4210EUR
2336 BEAST_EXPECT(expectHolding(
2337 env,
2338 ed,
2339 STAmount{EUR, UINT64_C(1'329'57894736842), -11},
2340 STAmount{GBP, UINT64_C(1'470'42105263158), -11}));
2341 BEAST_EXPECT(expectOffers(
2342 env,
2343 ed,
2344 1,
2345 {Amounts{
2346 STAmount{GBP, UINT64_C(929'57894736842), -11},
2347 STAmount{EUR, UINT64_C(929'57894736842), -11}}}));
2348 // 25% on 56.3368EUR is paid in tr fee 56.3368*1.25 = 70.4210EUR
2349 // 56.3368EUR is swapped in for 74.6651USD
2350 BEAST_EXPECT(amm.expectBalances(
2351 STAmount{EUR, UINT64_C(1'056'336842105264), -12},
2352 STAmount{USD, UINT64_C(1'325'334821428571), -12},
2353 amm.tokens()));
2354 // 25% on 59.7321USD is paid in tr fee 59.7321*1.25 = 74.6651USD
2355 BEAST_EXPECT(
2356 expectHolding(env, carol_, STAmount(USD, UINT64_C(1'459'732142857143), -12)));
2357 }
2358 {
2359 // Payment via AMM and offer with limit quality, deliver less
2360 // than requested
2361 Env env(*this, features);
2362 Account const ed("ed");
2363
2364 fund(
2365 env,
2366 gw_,
2367 {alice_, bob_, carol_, ed},
2368 XRP(1'000),
2369 {USD(1'400), EUR(1'400), GBP(1'400)});
2370 env(rate(gw_, 1.25));
2371 env.close();
2372
2373 AMM const amm(env, bob_, GBP(1'000), EUR(1'000));
2374
2375 env(offer(ed, EUR(1'000), USD(1'400)), Txflags(tfPassive));
2376 env.close();
2377
2378 // requested quality limit is 95USD/140GBP = 0.6785
2379 // trade quality is 47.7857USD/70.4210GBP = 0.6785
2380 env(pay(alice_, carol_, USD(95)),
2381 Path(~EUR, ~USD),
2382 Sendmax(GBP(140)),
2383 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2384 env.close();
2385
2386 // alice_ buys 53.3322EUR with 56.3368GBP via the amm
2387 // and pays 25% tr fee on 56.3368GBP
2388 // 1,400 - 56.3368*1.25 = 1400 - 70.4210 = 1329.5789GBP
2389 BEAST_EXPECT(
2390 expectHolding(env, alice_, STAmount{GBP, UINT64_C(1'329'57894736842), -11}));
2395 // 56.3368GBP is swapped in for 53.3322EUR
2396 BEAST_EXPECT(amm.expectBalances(
2397 STAmount{GBP, UINT64_C(1'056'336842105264), -12},
2398 STAmount{EUR, UINT64_C(946'6677295918366), -13},
2399 amm.tokens()));
2400 // 25% on 42.6658EUR is paid in tr fee 42.6658*1.25 = 53.3322EUR
2401 // 42.6658EUR/59.7321USD
2402 BEAST_EXPECT(expectHolding(
2403 env,
2404 ed,
2405 STAmount{USD, UINT64_C(1'340'267857142857), -12},
2406 STAmount{EUR, UINT64_C(1'442'665816326531), -12}));
2407 BEAST_EXPECT(expectOffers(
2408 env,
2409 ed,
2410 1,
2411 {Amounts{
2412 STAmount{EUR, UINT64_C(957'3341836734693), -13},
2413 STAmount{USD, UINT64_C(1'340'267857142857), -12}}}));
2414 // 25% on 47.7857USD is paid in tr fee 47.7857*1.25 = 59.7321USD
2415 BEAST_EXPECT(
2416 expectHolding(env, carol_, STAmount(USD, UINT64_C(1'447'785714285714), -12)));
2417 }
2418 {
2419 // Payment via AMM, AMM with limit quality, deliver less
2420 // than requested
2421 Env env(*this, features);
2422 Account const ed("ed");
2423
2424 fund(
2425 env,
2426 gw_,
2427 {alice_, bob_, carol_, ed},
2428 XRP(1'000),
2429 {USD(1'400), EUR(1'400), GBP(1'400)});
2430 env(rate(gw_, 1.25));
2431 env.close();
2432
2433 AMM const amm1(env, bob_, GBP(1'000), EUR(1'000));
2434 AMM const amm2(env, ed, EUR(1'000), USD(1'400));
2435
2436 // requested quality limit is 90USD/145GBP = 0.6206
2437 // trade quality is 66.7432USD/107.5308GBP = 0.6206
2438 env(pay(alice_, carol_, USD(90)),
2439 Path(~EUR, ~USD),
2440 Sendmax(GBP(145)),
2441 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2442 env.close();
2443
2444 // alice_ buys 53.3322EUR with 107.5308GBP
2445 // 25% on 86.0246GBP is paid in tr fee
2446 // 1,400 - 86.0246*1.25 = 1400 - 107.5308 = 1229.4691GBP
2447 BEAST_EXPECT(
2448 expectHolding(env, alice_, STAmount{GBP, UINT64_C(1'292'469135802466), -12}));
2449 // 86.0246GBP is swapped in for 79.2106EUR
2450 BEAST_EXPECT(amm1.expectBalances(
2451 STAmount{GBP, UINT64_C(1'086'024691358027), -12},
2452 STAmount{EUR, UINT64_C(920'7893779556188), -13},
2453 amm1.tokens()));
2454 // 25% on 63.3684EUR is paid in tr fee 63.3684*1.25 = 79.2106EUR
2455 // 63.3684EUR is swapped in for 83.4291USD
2456 BEAST_EXPECT(amm2.expectBalances(
2457 STAmount{EUR, UINT64_C(1'063'368497635505), -12},
2458 STAmount{USD, UINT64_C(1'316'570881226053), -12},
2459 amm2.tokens()));
2460 // 25% on 66.7432USD is paid in tr fee 66.7432*1.25 = 83.4291USD
2461 BEAST_EXPECT(
2462 expectHolding(env, carol_, STAmount(USD, UINT64_C(1'466'743295019157), -12)));
2463 }
2464 {
2465 // Payment by the issuer via AMM, AMM with limit quality,
2466 // deliver less than requested
2467 Env env(*this, features);
2468
2469 fund(
2470 env, gw_, {alice_, bob_, carol_}, XRP(1'000), {USD(1'400), EUR(1'400), GBP(1'400)});
2471 env(rate(gw_, 1.25));
2472 env.close();
2473
2474 AMM const amm1(env, alice_, GBP(1'000), EUR(1'000));
2475 AMM const amm2(env, bob_, EUR(1'000), USD(1'400));
2476
2477 // requested quality limit is 90USD/120GBP = 0.75
2478 // trade quality is 81.1111USD/108.1481GBP = 0.75
2479 env(pay(gw_, carol_, USD(90)),
2480 Path(~EUR, ~USD),
2481 Sendmax(GBP(120)),
2482 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2483 env.close();
2484
2485 // 108.1481GBP is swapped in for 97.5935EUR
2486 BEAST_EXPECT(amm1.expectBalances(
2487 STAmount{GBP, UINT64_C(1'108'148148148151), -12},
2488 STAmount{EUR, UINT64_C(902'4064171122975), -13},
2489 amm1.tokens()));
2490 // 25% on 78.0748EUR is paid in tr fee 78.0748*1.25 = 97.5935EUR
2491 // 78.0748EUR is swapped in for 101.3888USD
2492 BEAST_EXPECT(amm2.expectBalances(
2493 STAmount{EUR, UINT64_C(1'078'074866310162), -12},
2494 STAmount{USD, UINT64_C(1'298'611111111111), -12},
2495 amm2.tokens()));
2496 // 25% on 81.1111USD is paid in tr fee 81.1111*1.25 = 101.3888USD
2497 BEAST_EXPECT(
2498 expectHolding(env, carol_, STAmount{USD, UINT64_C(1'481'111111111111), -12}));
2499 }
2500 }
2501
2502 void
2504 {
2505 // Single path with amm, offer, and limit quality. The quality limit
2506 // is such that the first offer should be taken but the second
2507 // should not. The total amount delivered should be the sum of the
2508 // two offers and sendMax should be more than the first offer.
2509 testcase("limitQuality");
2510 using namespace jtx;
2511
2512 {
2513 Env env(*this);
2514
2515 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000), {USD(2'000)});
2516
2517 AMM const ammBob(env, bob_, XRP(1'000), USD(1'050));
2518 env(offer(bob_, XRP(100), USD(50)));
2519
2520 env(pay(alice_, carol_, USD(100)),
2521 Path(~USD),
2522 Sendmax(XRP(100)),
2523 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2524
2525 BEAST_EXPECT(ammBob.expectBalances(XRP(1'050), USD(1'000), ammBob.tokens()));
2526 BEAST_EXPECT(expectHolding(env, carol_, USD(2'050)));
2527 BEAST_EXPECT(expectOffers(env, bob_, 1, {{{XRP(100), USD(50)}}}));
2528 }
2529 }
2530
2531 void
2533 {
2534 testcase("Circular XRP");
2535
2536 using namespace jtx;
2537 {
2538 // Payment path starting with XRP
2539 Env env(*this, testableAmendments());
2540 // Note, if alice_ doesn't have default ripple, then pay
2541 // fails with tecPATH_DRY.
2542 fund(env, gw_, {alice_, bob_}, XRP(10'000), {USD(200), EUR(200)}, Fund::All);
2543
2544 AMM const ammAliceXrpUsd(env, alice_, XRP(100), USD(101));
2545 AMM const ammAliceXrpEur(env, alice_, XRP(100), EUR(101));
2546 env.close();
2547
2548 TER const expectedTer = TER{temBAD_PATH_LOOP};
2549 env(pay(alice_, bob_, EUR(1)),
2550 Path(~USD, ~XRP, ~EUR),
2551 Sendmax(XRP(1)),
2552 Txflags(tfNoRippleDirect),
2553 Ter(expectedTer));
2554 }
2555 {
2556 // Payment path ending with XRP
2557 Env env(*this);
2558 // Note, if alice_ doesn't have default ripple, then pay fails
2559 // with tecPATH_DRY.
2560 fund(env, gw_, {alice_, bob_}, XRP(10'000), {USD(200), EUR(200)}, Fund::All);
2561
2562 AMM const ammAliceXrpUsd(env, alice_, XRP(100), USD(100));
2563 AMM const ammAliceXrpEur(env, alice_, XRP(100), EUR(100));
2564 // EUR -> //XRP -> //USD ->XRP
2565 env(pay(alice_, bob_, XRP(1)),
2566 Path(~XRP, ~USD, ~XRP),
2567 Sendmax(EUR(1)),
2568 Txflags(tfNoRippleDirect),
2570 }
2571 {
2572 // Payment where loop is formed in the middle of the path, not
2573 // on an endpoint
2574 auto const jpy = gw_["JPY"];
2575 Env env(*this);
2576 // Note, if alice_ doesn't have default ripple, then pay fails
2577 // with tecPATH_DRY.
2578 fund(env, gw_, {alice_, bob_}, XRP(10'000), {USD(200), EUR(200), jpy(200)}, Fund::All);
2579
2580 AMM const ammAliceXrpUsd(env, alice_, XRP(100), USD(100));
2581 AMM const ammAliceXrpEur(env, alice_, XRP(100), EUR(100));
2582 AMM const ammAliceXrpJpy(env, alice_, XRP(100), jpy(100));
2583
2584 env(pay(alice_, bob_, jpy(1)),
2585 Path(~XRP, ~EUR, ~XRP, ~jpy),
2586 Sendmax(USD(1)),
2587 Txflags(tfNoRippleDirect),
2589 }
2590 }
2591
2592 void
2594 {
2595 testcase("Step Limit");
2596
2597 using namespace jtx;
2598 Env env(*this, features);
2599 auto const dan = Account("dan");
2600 auto const ed = Account("ed");
2601
2602 fund(env, gw_, {ed}, XRP(100'000'000), {USD(11)});
2603 env.fund(XRP(100'000'000), alice_, bob_, carol_, dan);
2604 env.close();
2605 env.trust(USD(1), bob_);
2606 env(pay(gw_, bob_, USD(1)));
2607 env.trust(USD(1), dan);
2608 env(pay(gw_, dan, USD(1)));
2609 nOffers(env, 2'000, bob_, XRP(1), USD(1));
2610 nOffers(env, 1, dan, XRP(1), USD(1));
2611 AMM const ammEd(env, ed, XRP(9), USD(11));
2612
2613 // Alice offers to buy 1000 XRP for 1000 USD. She takes Bob's first
2614 // offer, removes 999 more as unfunded, then hits the step limit.
2615 env(offer(alice_, USD(1'000), XRP(1'000)));
2616 env.require(Balance(alice_, STAmount{USD, UINT64_C(2'050125257867587), -15}));
2617 env.require(Owners(alice_, 2));
2618 env.require(Balance(bob_, USD(0)));
2619 env.require(Owners(bob_, 1'001));
2620 env.require(Balance(dan, USD(1)));
2621 env.require(Owners(dan, 2));
2622
2623 // Carol offers to buy 1000 XRP for 1000 USD. She removes Bob's next
2624 // 1000 offers as unfunded and hits the step limit.
2625 env(offer(carol_, USD(1'000), XRP(1'000)));
2626 env.require(Balance(carol_, USD(kNone)));
2627 env.require(Owners(carol_, 1));
2628 env.require(Balance(bob_, USD(0)));
2629 env.require(Owners(bob_, 1));
2630 env.require(Balance(dan, USD(1)));
2631 env.require(Owners(dan, 2));
2632 }
2633
2634 void
2636 {
2637 testcase("Convert all of an asset using DeliverMin");
2638
2639 using namespace jtx;
2640
2641 {
2642 Env env(*this, features);
2643 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000));
2644 env.trust(USD(100), alice_, bob_, carol_);
2645 env(pay(alice_, bob_, USD(10)), DeliverMin(USD(10)), Ter(temBAD_AMOUNT));
2646 env(pay(alice_, bob_, USD(10)),
2647 DeliverMin(USD(-5)),
2648 Txflags(tfPartialPayment),
2650 env(pay(alice_, bob_, USD(10)),
2651 DeliverMin(XRP(5)),
2652 Txflags(tfPartialPayment),
2654 env(pay(alice_, bob_, USD(10)),
2655 DeliverMin(Account(carol_)["USD"](5)),
2656 Txflags(tfPartialPayment),
2658 env(pay(alice_, bob_, USD(10)),
2659 DeliverMin(USD(15)),
2660 Txflags(tfPartialPayment),
2662 env(pay(gw_, carol_, USD(50)));
2663 AMM const ammCarol(env, carol_, XRP(10), USD(15));
2664 env(pay(alice_, bob_, USD(10)),
2665 Paths(XRP),
2666 DeliverMin(USD(7)),
2667 Txflags(tfPartialPayment),
2668 Sendmax(XRP(5)),
2670 env.require(
2671 Balance(alice_, drops(10'000'000'000 - env.current()->fees().base.drops())));
2672 env.require(Balance(bob_, XRP(10'000)));
2673 }
2674
2675 {
2676 Env env(*this, features);
2677 fund(env, gw_, {alice_, bob_}, XRP(10'000));
2678 env.trust(USD(1'100), alice_, bob_);
2679 env(pay(gw_, bob_, USD(1'100)));
2680 AMM const ammBob(env, bob_, XRP(1'000), USD(1'100));
2681 env(pay(alice_, alice_, USD(10'000)),
2682 Paths(XRP),
2683 DeliverMin(USD(100)),
2684 Txflags(tfPartialPayment),
2685 Sendmax(XRP(100)));
2686 env.require(Balance(alice_, USD(100)));
2687 }
2688
2689 {
2690 Env env(*this, features);
2691 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000));
2692 env.trust(USD(1'200), bob_, carol_);
2693 env(pay(gw_, bob_, USD(1'200)));
2694 AMM const ammBob(env, bob_, XRP(5'500), USD(1'200));
2695 env(pay(alice_, carol_, USD(10'000)),
2696 Paths(XRP),
2697 DeliverMin(USD(200)),
2698 Txflags(tfPartialPayment),
2699 Sendmax(XRP(1'000)),
2701 env(pay(alice_, carol_, USD(10'000)),
2702 Paths(XRP),
2703 DeliverMin(USD(200)),
2704 Txflags(tfPartialPayment),
2705 Sendmax(XRP(1'100)));
2706 BEAST_EXPECT(ammBob.expectBalances(XRP(6'600), USD(1'000), ammBob.tokens()));
2707 env.require(Balance(carol_, USD(200)));
2708 }
2709
2710 {
2711 auto const dan = Account("dan");
2712 Env env(*this, features);
2713 fund(env, gw_, {alice_, bob_, carol_, dan}, XRP(10'000));
2714 env.close();
2715 env.trust(USD(1'100), bob_, carol_, dan);
2716 env(pay(gw_, bob_, USD(100)));
2717 env(pay(gw_, dan, USD(1'100)));
2718 env(offer(bob_, XRP(100), USD(100)));
2719 env(offer(bob_, XRP(1'000), USD(100)));
2720 AMM const ammDan(env, dan, XRP(1'000), USD(1'100));
2721 env(pay(alice_, carol_, USD(10'000)),
2722 Paths(XRP),
2723 DeliverMin(USD(200)),
2724 Txflags(tfPartialPayment),
2725 Sendmax(XRPAmount(200'000'001)));
2726 env.require(Balance(bob_, USD(0)));
2727 env.require(Balance(carol_, STAmount{USD, UINT64_C(200'00000090909), -11}));
2728 BEAST_EXPECT(ammDan.expectBalances(
2729 XRPAmount{1'100'000'001},
2730 STAmount{USD, UINT64_C(999'99999909091), -11},
2731 ammDan.tokens()));
2732 }
2733 }
2734
2735 void
2737 {
2738 testcase("Payment");
2739
2740 using namespace jtx;
2741 Account const becky{"becky"};
2742
2743 // The initial implementation of DepositAuth had a bug where an
2744 // account with the DepositAuth flag set could not make a payment
2745 // to itself. That bug was fixed in the DepositPreauth amendment.
2746 Env env(*this, features);
2747 fund(env, gw_, {alice_, becky}, XRP(5'000));
2748 env.close();
2749
2750 env.trust(USD(1'000), alice_);
2751 env.trust(USD(1'000), becky);
2752 env.close();
2753
2754 env(pay(gw_, alice_, USD(500)));
2755 env.close();
2756
2757 AMM const ammAlice(env, alice_, XRP(100), USD(140));
2758
2759 // becky pays herself USD (10) by consuming part of alice_'s offer.
2760 // Make sure the payment works if PaymentAuth is not involved.
2761 env(pay(becky, becky, USD(10)), Path(~USD), Sendmax(XRP(10)));
2762 env.close();
2763 BEAST_EXPECT(ammAlice.expectBalances(XRPAmount(107'692'308), USD(130), ammAlice.tokens()));
2764
2765 // becky decides to require authorization for deposits.
2766 env(fset(becky, asfDepositAuth));
2767 env.close();
2768
2769 // becky pays herself again.
2770 env(pay(becky, becky, USD(10)), Path(~USD), Sendmax(XRP(10)), Ter(tesSUCCESS));
2771
2772 env.close();
2773 }
2774
2775 void
2777 {
2778 // Exercise IOU payments and non-direct XRP payments to an account
2779 // that has the lsfDepositAuth flag set.
2780 testcase("Pay IOU");
2781
2782 using namespace jtx;
2783
2784 Env env(*this);
2785
2786 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000));
2787 env.trust(USD(1'000), alice_, bob_, carol_);
2788 env.close();
2789
2790 env(pay(gw_, alice_, USD(150)));
2791 env(pay(gw_, carol_, USD(150)));
2792 AMM const ammCarol(env, carol_, USD(100), XRPAmount(101));
2793
2794 // Make sure bob_'s trust line is all set up so he can receive USD.
2795 env(pay(alice_, bob_, USD(50)));
2796 env.close();
2797
2798 // bob_ sets the lsfDepositAuth flag.
2799 env(fset(bob_, asfDepositAuth), Require(Flags(bob_, asfDepositAuth)));
2800 env.close();
2801
2802 // None of the following payments should succeed.
2803 auto const failedIouPayments = [this, &env] {
2804 env.require(Flags(bob_, asfDepositAuth));
2805
2806 // Capture bob_'s balances before hand to confirm they don't
2807 // change.
2808 PrettyAmount const bobXrpBalance{env.balance(bob_, XRP)};
2809 PrettyAmount const bobUsdBalance{env.balance(bob_, USD)};
2810
2811 env(pay(alice_, bob_, USD(50)), Ter(tecNO_PERMISSION));
2812 env.close();
2813
2814 // Note that even though alice_ is paying bob_ in XRP, the payment
2815 // is still not allowed since the payment passes through an
2816 // offer.
2817 env(pay(alice_, bob_, drops(1)), Sendmax(USD(1)), Ter(tecNO_PERMISSION));
2818 env.close();
2819
2820 BEAST_EXPECT(bobXrpBalance == env.balance(bob_, XRP));
2821 BEAST_EXPECT(bobUsdBalance == env.balance(bob_, USD));
2822 };
2823
2824 // Test when bob_ has an XRP balance > base reserve.
2825 failedIouPayments();
2826
2827 // Set bob_'s XRP balance == base reserve. Also demonstrate that
2828 // bob_ can make payments while his lsfDepositAuth flag is set.
2829 env(pay(bob_, alice_, USD(25)));
2830 env.close();
2831
2832 {
2833 STAmount const bobPaysXRP{env.balance(bob_, XRP) - reserve(env, 1)};
2834 XRPAmount const bobPaysFee{reserve(env, 1) - reserve(env, 0)};
2835 env(pay(bob_, alice_, bobPaysXRP), Fee(bobPaysFee));
2836 env.close();
2837 }
2838
2839 // Test when bob_'s XRP balance == base reserve.
2840 BEAST_EXPECT(env.balance(bob_, XRP) == reserve(env, 0));
2841 BEAST_EXPECT(env.balance(bob_, USD) == USD(25));
2842 failedIouPayments();
2843
2844 // Test when bob_ has an XRP balance == 0.
2845 env(noop(bob_), Fee(reserve(env, 0)));
2846 env.close();
2847
2848 BEAST_EXPECT(env.balance(bob_, XRP) == XRP(0));
2849 failedIouPayments();
2850
2851 // Give bob_ enough XRP for the fee to clear the lsfDepositAuth flag.
2852 env(pay(alice_, bob_, drops(env.current()->fees().base)));
2853
2854 // bob_ clears the lsfDepositAuth and the next payment succeeds.
2855 env(fclear(bob_, asfDepositAuth));
2856 env.close();
2857
2858 env(pay(alice_, bob_, USD(50)));
2859 env.close();
2860
2861 env(pay(alice_, bob_, drops(1)), Sendmax(USD(1)));
2862 env.close();
2863 BEAST_EXPECT(ammCarol.expectBalances(USD(101), XRPAmount(100), ammCarol.tokens()));
2864 }
2865
2866 void
2868 {
2869 testcase("RippleState Freeze");
2870
2871 using namespace test::jtx;
2872 Env env(*this, features);
2873
2874 Account const g1{"G1"};
2875 Account const alice{"alice"};
2876 Account const bob{"bob"};
2877
2878 env.fund(XRP(1'000), g1, alice, bob);
2879 env.close();
2880
2881 env.trust(g1["USD"](100), bob);
2882 env.trust(g1["USD"](205), alice);
2883 env.close();
2884
2885 env(pay(g1, bob, g1["USD"](10)));
2886 env(pay(g1, alice, g1["USD"](205)));
2887 env.close();
2888
2889 AMM const ammAlice(env, alice, XRP(500), g1["USD"](105));
2890
2891 {
2892 auto lines = getAccountLines(env, bob);
2893 if (!BEAST_EXPECT(checkArraySize(lines[jss::lines], 1u)))
2894 return;
2895 BEAST_EXPECT(lines[jss::lines][0u][jss::account] == g1.human());
2896 BEAST_EXPECT(lines[jss::lines][0u][jss::limit] == "100");
2897 BEAST_EXPECT(lines[jss::lines][0u][jss::balance] == "10");
2898 }
2899
2900 {
2901 auto lines = getAccountLines(env, alice, g1["USD"]);
2902 if (!BEAST_EXPECT(checkArraySize(lines[jss::lines], 1u)))
2903 return;
2904 BEAST_EXPECT(lines[jss::lines][0u][jss::account] == g1.human());
2905 BEAST_EXPECT(lines[jss::lines][0u][jss::limit] == "205");
2906 // 105 transferred to AMM
2907 BEAST_EXPECT(lines[jss::lines][0u][jss::balance] == "100");
2908 }
2909
2910 // Account with line unfrozen (proving operations normally work)
2911 // test: can make Payment on that line
2912 env(pay(alice, bob, g1["USD"](1)));
2913
2914 // test: can receive Payment on that line
2915 env(pay(bob, alice, g1["USD"](1)));
2916 env.close();
2917
2918 // Is created via a TrustSet with SetFreeze flag
2919 // test: sets LowFreeze | HighFreeze flags
2920 env(trust(g1, bob["USD"](0), tfSetFreeze));
2921 env.close();
2922
2923 {
2924 // Account with line frozen by issuer
2925 // test: can buy more assets on that line
2926 env(offer(bob, g1["USD"](5), XRP(25)));
2927 env.close();
2928 BEAST_EXPECT(ammAlice.expectBalances(XRP(525), g1["USD"](100), ammAlice.tokens()));
2929 }
2930
2931 {
2932 // test: can not sell assets from that line
2933 env(offer(bob, XRP(1), g1["USD"](5)), Ter(tecUNFUNDED_OFFER));
2934
2935 // test: can receive Payment on that line
2936 env(pay(alice, bob, g1["USD"](1)));
2937
2938 // test: can not make Payment from that line
2939 env(pay(bob, alice, g1["USD"](1)), Ter(tecPATH_DRY));
2940 }
2941
2942 {
2943 // check G1 account lines
2944 // test: shows freeze
2945 auto lines = getAccountLines(env, g1);
2946 json::Value bobLine;
2947 for (auto const& it : lines[jss::lines])
2948 {
2949 if (it[jss::account] == bob.human())
2950 {
2951 bobLine = it;
2952 break;
2953 }
2954 }
2955 if (!BEAST_EXPECT(bobLine))
2956 return;
2957 BEAST_EXPECT(bobLine[jss::freeze] == true);
2958 BEAST_EXPECT(bobLine[jss::balance] == "-16");
2959 }
2960
2961 {
2962 // test: shows freeze peer
2963 auto lines = getAccountLines(env, bob);
2964 json::Value g1Line;
2965 for (auto const& it : lines[jss::lines])
2966 {
2967 if (it[jss::account] == g1.human())
2968 {
2969 g1Line = it;
2970 break;
2971 }
2972 }
2973 if (!BEAST_EXPECT(g1Line))
2974 return;
2975 BEAST_EXPECT(g1Line[jss::freeze_peer] == true);
2976 BEAST_EXPECT(g1Line[jss::balance] == "16");
2977 }
2978
2979 {
2980 // Is cleared via a TrustSet with ClearFreeze flag
2981 // test: sets LowFreeze | HighFreeze flags
2982 env(trust(g1, bob["USD"](0), tfClearFreeze));
2983 auto affected =
2984 env.meta()->getJson(JsonOptions::Values::None)[sfAffectedNodes.fieldName];
2985 if (!BEAST_EXPECT(checkArraySize(affected, 2u)))
2986 return;
2987 auto ff = affected[1u][sfModifiedNode.fieldName][sfFinalFields.fieldName];
2988 BEAST_EXPECT(
2989 ff[sfLowLimit.fieldName] ==
2990 g1["USD"](0).value().getJson(JsonOptions::Values::None));
2991 BEAST_EXPECT(!(ff[jss::Flags].asUInt() & lsfLowFreeze));
2992 BEAST_EXPECT(!(ff[jss::Flags].asUInt() & lsfHighFreeze));
2993 env.close();
2994 }
2995 }
2996
2997 void
2999 {
3000 testcase("Global Freeze");
3001
3002 using namespace test::jtx;
3003 Env env(*this, features);
3004
3005 Account const g1{"G1"};
3006 Account const a1{"A1"};
3007 Account const a2{"A2"};
3008 Account const a3{"A3"};
3009 Account const a4{"A4"};
3010
3011 env.fund(XRP(12'000), g1);
3012 env.fund(XRP(1'000), a1);
3013 env.fund(XRP(20'000), a2, a3, a4);
3014 env.close();
3015
3016 env.trust(g1["USD"](1'200), a1);
3017 env.trust(g1["USD"](200), a2);
3018 env.trust(g1["BTC"](100), a3);
3019 env.trust(g1["BTC"](100), a4);
3020 env.close();
3021
3022 env(pay(g1, a1, g1["USD"](1'000)));
3023 env(pay(g1, a2, g1["USD"](100)));
3024 env(pay(g1, a3, g1["BTC"](100)));
3025 env(pay(g1, a4, g1["BTC"](100)));
3026 env.close();
3027
3028 AMM const ammG1(env, g1, XRP(10'000), g1["USD"](100));
3029 env(offer(a1, XRP(10'000), g1["USD"](100)), Txflags(tfPassive));
3030 env(offer(a2, g1["USD"](100), XRP(10'000)), Txflags(tfPassive));
3031 env.close();
3032
3033 {
3034 // Account without GlobalFreeze (proving operations normally
3035 // work)
3036 // test: visible offers where taker_pays is unfrozen issuer
3037 auto offers = env.rpc(
3038 "book_offers", std::string("USD/") + g1.human(), "XRP")[jss::result][jss::offers];
3039 if (!BEAST_EXPECT(checkArraySize(offers, 1u)))
3040 return;
3041 std::set<std::string> accounts;
3042 for (auto const& offer : offers)
3043 {
3044 accounts.insert(offer[jss::Account].asString());
3045 }
3046 BEAST_EXPECT(accounts.find(a2.human()) != std::end(accounts));
3047
3048 // test: visible offers where taker_gets is unfrozen issuer
3049 offers = env.rpc(
3050 "book_offers", "XRP", std::string("USD/") + g1.human())[jss::result][jss::offers];
3051 if (!BEAST_EXPECT(checkArraySize(offers, 1u)))
3052 return;
3053 accounts.clear();
3054 for (auto const& offer : offers)
3055 {
3056 accounts.insert(offer[jss::Account].asString());
3057 }
3058 BEAST_EXPECT(accounts.find(a1.human()) != std::end(accounts));
3059 }
3060
3061 {
3062 // Offers/Payments
3063 // test: assets can be bought on the market
3064 // env(offer(A3, G1["BTC"](1), XRP(1)));
3065 AMM ammA3(env, a3, g1["BTC"](1), XRP(1));
3066
3067 // test: assets can be sold on the market
3068 // AMM is bidirectional
3069
3070 // test: direct issues can be sent
3071 env(pay(g1, a2, g1["USD"](1)));
3072
3073 // test: direct redemptions can be sent
3074 env(pay(a2, g1, g1["USD"](1)));
3075
3076 // test: via rippling can be sent
3077 env(pay(a2, a1, g1["USD"](1)));
3078
3079 // test: via rippling can be sent back
3080 env(pay(a1, a2, g1["USD"](1)));
3081 ammA3.withdrawAll(std::nullopt);
3082 }
3083
3084 {
3085 // Account with GlobalFreeze
3086 // set GlobalFreeze first
3087 // test: SetFlag GlobalFreeze will toggle back to freeze
3088 env.require(Nflags(g1, asfGlobalFreeze));
3089 env(fset(g1, asfGlobalFreeze));
3090 env.require(Flags(g1, asfGlobalFreeze));
3091 env.require(Nflags(g1, asfNoFreeze));
3092
3093 // test: assets can't be bought on the market
3094 AMM const ammA3(env, a3, g1["BTC"](1), XRP(1), Ter(tecFROZEN));
3095
3096 // test: assets can't be sold on the market
3097 // AMM is bidirectional
3098 }
3099
3100 {
3101 // test: book_offers shows offers
3102 // (should these actually be filtered?)
3103 auto offers = env.rpc(
3104 "book_offers", "XRP", std::string("USD/") + g1.human())[jss::result][jss::offers];
3105 if (!BEAST_EXPECT(checkArraySize(offers, 1u)))
3106 return;
3107
3108 offers = env.rpc(
3109 "book_offers", std::string("USD/") + g1.human(), "XRP")[jss::result][jss::offers];
3110 if (!BEAST_EXPECT(checkArraySize(offers, 1u)))
3111 return;
3112 }
3113
3114 {
3115 // Payments
3116 // test: direct issues can be sent
3117 env(pay(g1, a2, g1["USD"](1)));
3118
3119 // test: direct redemptions can be sent
3120 env(pay(a2, g1, g1["USD"](1)));
3121
3122 // test: via rippling cant be sent
3123 env(pay(a2, a1, g1["USD"](1)), Ter(tecPATH_DRY));
3124 }
3125 }
3126
3127 void
3129 {
3130 testcase("Offers for Frozen Trust Lines");
3131
3132 using namespace test::jtx;
3133 Env env(*this, features);
3134
3135 Account const g1{"G1"};
3136 Account const a2{"A2"};
3137 Account const a3{"A3"};
3138 Account const a4{"A4"};
3139
3140 env.fund(XRP(2'000), g1, a3, a4);
3141 env.fund(XRP(2'000), a2);
3142 env.close();
3143
3144 env.trust(g1["USD"](1'000), a2);
3145 env.trust(g1["USD"](2'000), a3);
3146 env.trust(g1["USD"](2'001), a4);
3147 env.close();
3148
3149 env(pay(g1, a3, g1["USD"](2'000)));
3150 env(pay(g1, a4, g1["USD"](2'001)));
3151 env.close();
3152
3153 AMM const ammA3(env, a3, XRP(1'000), g1["USD"](1'001));
3154
3155 // removal after successful payment
3156 // test: make a payment with partially consuming offer
3157 env(pay(a2, g1, g1["USD"](1)), Paths(g1["USD"]), Sendmax(XRP(1)));
3158 env.close();
3159
3160 BEAST_EXPECT(ammA3.expectBalances(XRP(1'001), g1["USD"](1'000), ammA3.tokens()));
3161
3162 // test: someone else creates an offer providing liquidity
3163 env(offer(a4, XRP(999), g1["USD"](999)));
3164 env.close();
3165 // The offer consumes AMM offer
3166 BEAST_EXPECT(ammA3.expectBalances(XRP(1'000), g1["USD"](1'001), ammA3.tokens()));
3167
3168 // test: AMM line is frozen
3169 auto const a3am = STAmount{Issue{toCurrency("USD"), ammA3.ammAccount()}, 0};
3170 env(trust(g1, a3am, tfSetFreeze));
3171 auto const info = ammA3.ammRpcInfo();
3172 BEAST_EXPECT(info[jss::amm][jss::asset2_frozen].asBool());
3173 env.close();
3174
3175 // test: Can make a payment via the new offer
3176 env(pay(a2, g1, g1["USD"](1)), Paths(g1["USD"]), Sendmax(XRP(1)));
3177 env.close();
3178 // AMM is not consumed
3179 BEAST_EXPECT(ammA3.expectBalances(XRP(1'000), g1["USD"](1'001), ammA3.tokens()));
3180
3181 // removal buy successful OfferCreate
3182 // test: freeze the new offer
3183 env(trust(g1, a4["USD"](0), tfSetFreeze));
3184 env.close();
3185
3186 // test: can no longer create a crossing offer
3187 env(offer(a2, g1["USD"](999), XRP(999)));
3188 env.close();
3189
3190 // test: offer was removed by offer_create
3191 auto const offers = getAccountOffers(env, a4)[jss::offers];
3192 if (!BEAST_EXPECT(checkArraySize(offers, 0u)))
3193 return;
3194 }
3195
3196 void
3198 {
3199 testcase("Multisign AMM Transactions");
3200
3201 using namespace jtx;
3202 Env env{*this, features};
3203 Account const bogie{"bogie", KeyType::Secp256k1};
3204 Account const alice{"alice", KeyType::Secp256k1};
3205 Account const becky{"becky", KeyType::Ed25519};
3206 Account const zelda{"zelda", KeyType::Secp256k1};
3207 fund(env, gw_, {alice, becky, zelda}, XRP(20'000), {USD(20'000)});
3208
3209 // alice_ uses a regular key with the master disabled.
3210 Account const alie{"alie", KeyType::Secp256k1};
3211 env(regkey(alice, alie));
3212 env(fset(alice, asfDisableMaster), Sig(alice));
3213
3214 // Attach signers to alice_.
3215 env(signers(alice, 2, {{becky, 1}, {bogie, 1}}), Sig(alie));
3216 env.close();
3217 env.require(Owners(alice, 2));
3218
3219 Msig const ms{becky, bogie};
3220
3221 // Multisign all AMM transactions
3222 AMM ammAlice(
3223 env,
3224 alice,
3225 XRP(10'000),
3226 USD(10'000),
3227 false,
3228 0,
3229 ammCrtFee(env).drops(),
3230 std::nullopt,
3231 std::nullopt,
3232 ms,
3233 Ter(tesSUCCESS));
3234 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), USD(10'000), ammAlice.tokens()));
3235
3236 ammAlice.deposit(alice, 1'000'000);
3237 BEAST_EXPECT(ammAlice.expectBalances(XRP(11'000), USD(11'000), IOUAmount{11'000'000, 0}));
3238
3239 ammAlice.withdraw(alice, 1'000'000);
3240 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), USD(10'000), ammAlice.tokens()));
3241
3242 ammAlice.vote({}, 1'000);
3243 BEAST_EXPECT(ammAlice.expectTradingFee(1'000));
3244
3245 env(ammAlice.bid({.account = alice, .bidMin = 100}), ms).close();
3246 BEAST_EXPECT(ammAlice.expectAuctionSlot(100, 0, IOUAmount{4'000}));
3247 // 4000 tokens burnt
3248 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), USD(10'000), IOUAmount{9'996'000, 0}));
3249 }
3250
3251 void
3253 {
3254 testcase("To Strand");
3255
3256 using namespace jtx;
3257
3258 // cannot have more than one offer with the same output issue
3259
3260 Env env(*this, features);
3261
3262 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000), {USD(2'000), EUR(1'000)});
3263
3264 AMM const bobXrpUsd(env, bob_, XRP(1'000), USD(1'000));
3265 AMM const bobUsdEur(env, bob_, USD(1'000), EUR(1'000));
3266
3267 // payment path: XRP -> XRP/USD -> USD/EUR -> EUR/USD
3268 env(pay(alice_, carol_, USD(100)),
3269 Path(~USD, ~EUR, ~USD),
3270 Sendmax(XRP(200)),
3271 Txflags(tfNoRippleDirect),
3273 }
3274
3275 void
3277 {
3278 using namespace jtx;
3279 testcase("RIPD1373");
3280
3281 {
3282 Env env(*this, features);
3283 auto const bobUsd = bob_["USD"];
3284 auto const bobEur = bob_["EUR"];
3285 fund(env, gw_, {alice_, bob_}, XRP(10'000));
3286 env.trust(USD(1'000), alice_, bob_);
3287 env.trust(EUR(1'000), alice_, bob_);
3288 env.close();
3289 fund(env, bob_, {alice_, gw_}, {bobUsd(100), bobEur(100)}, Fund::TokenOnly);
3290
3291 AMM const ammBobXrpUsd(env, bob_, XRP(100), bobUsd(100));
3292 env(offer(gw_, XRP(100), USD(100)), Txflags(tfPassive));
3293
3294 AMM const ammBobUsdEur(env, bob_, bobUsd(100), bobEur(100));
3295 env(offer(gw_, USD(100), EUR(100)), Txflags(tfPassive));
3296
3297 TestPath const p = [&] {
3298 TestPath result;
3299 result.pushBack(allPathElements(gw_, bobUsd));
3300 result.pushBack(cpe(EUR.currency));
3301 return result;
3302 }();
3303
3304 PathSet const paths(p);
3305
3306 env(pay(alice_, alice_, EUR(1)),
3307 Json(paths.json()),
3308 Sendmax(XRP(10)),
3309 Txflags(tfNoRippleDirect | tfPartialPayment),
3310 Ter(temBAD_PATH));
3311 }
3312
3313 {
3314 Env env(*this, features);
3315
3316 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000), {USD(100)});
3317
3318 AMM const ammBob(env, bob_, XRP(100), USD(100));
3319
3320 // payment path: XRP -> XRP/USD -> USD/XRP
3321 env(pay(alice_, carol_, XRP(100)),
3322 Path(~USD, ~XRP),
3323 Txflags(tfNoRippleDirect),
3325 }
3326
3327 {
3328 Env env(*this, features);
3329
3330 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000), {USD(100)});
3331
3332 AMM const ammBob(env, bob_, XRP(100), USD(100));
3333
3334 // payment path: XRP -> XRP/USD -> USD/XRP
3335 env(pay(alice_, carol_, XRP(100)),
3336 Path(~USD, ~XRP),
3337 Sendmax(XRP(200)),
3338 Txflags(tfNoRippleDirect),
3340 }
3341 }
3342
3343 void
3345 {
3346 testcase("test loop");
3347 using namespace jtx;
3348
3349 auto const cny = gw_["CNY"];
3350
3351 {
3352 Env env(*this, features);
3353
3354 env.fund(XRP(10'000), alice_, bob_, carol_, gw_);
3355 env.close();
3356 env.trust(USD(10'000), alice_, bob_, carol_);
3357 env.close();
3358 env(pay(gw_, bob_, USD(100)));
3359 env(pay(gw_, alice_, USD(100)));
3360 env.close();
3361
3362 AMM const ammBob(env, bob_, XRP(100), USD(100));
3363
3364 // payment path: USD -> USD/XRP -> XRP/USD
3365 env(pay(alice_, carol_, USD(100)),
3366 Sendmax(USD(100)),
3367 Path(~XRP, ~USD),
3368 Txflags(tfNoRippleDirect),
3370 }
3371
3372 {
3373 Env env(*this, features);
3374
3375 env.fund(XRP(10'000), alice_, bob_, carol_, gw_);
3376 env.close();
3377 env.trust(USD(10'000), alice_, bob_, carol_);
3378 env.trust(EUR(10'000), alice_, bob_, carol_);
3379 env.trust(cny(10'000), alice_, bob_, carol_);
3380
3381 env(pay(gw_, bob_, USD(200)));
3382 env(pay(gw_, bob_, EUR(200)));
3383 env(pay(gw_, bob_, cny(100)));
3384
3385 AMM const ammBobXrpUsd(env, bob_, XRP(100), USD(100));
3386 AMM const ammBobUsdEur(env, bob_, USD(100), EUR(100));
3387 AMM const ammBobEurCny(env, bob_, EUR(100), cny(100));
3388
3389 // payment path: XRP->XRP/USD->USD/EUR->USD/CNY
3390 env(pay(alice_, carol_, cny(100)),
3391 Sendmax(XRP(100)),
3392 Path(~USD, ~EUR, ~USD, ~cny),
3393 Txflags(tfNoRippleDirect),
3395 }
3396 }
3397
3398 void
3400 {
3403 receiveMax();
3404 pathFind01();
3405 pathFind02();
3406 pathFind05();
3407 pathFind06();
3408 }
3409
3410 void
3412 {
3413 using namespace jtx;
3414
3418 testTransferRateNoOwnerFee(all_ - fixAMMv1_3);
3421 }
3422
3423 void
3425 {
3426 using namespace jtx;
3428 testStepLimit(all_ - fixAMMv1_3);
3429 }
3430
3431 void
3433 {
3434 using namespace jtx;
3436 testConvertAllOfAnAsset(all_ - fixAMMv1_3);
3437 }
3438
3439 void
3441 {
3443 testPayIOU();
3444 }
3445
3446 void
3448 {
3449 using namespace test::jtx;
3453 }
3454
3455 void
3460
3461 void
3463 {
3464 auto const all = jtx::testableAmendments();
3465
3466 testToStrand(all);
3467 testRIPD1373(all);
3468 testLoop(all);
3469 }
3470
3471 void
3472 run() override
3473 {
3474 testOffers();
3475 testPaths();
3476 testFlow();
3480 testFreeze();
3481 testMultisign();
3482 testPayStrand();
3483 }
3484};
3485
3486BEAST_DEFINE_TESTSUITE_PRIO(AMMExtended, app, xrpl, 1);
3487
3488} // namespace xrpl::test
A generic endpoint for log messages.
Definition Journal.h:44
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:26
A currency issued by an account.
Definition Issue.h:18
Sets the new scale and restores the old scale when it leaves scope.
Definition Number.h:967
bool modify(ModifyType const &f)
Modify the open ledger.
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
A wrapper which makes credits unavailable to balances.
bool pushBack(STPath const &e)
pushBack adds a path to the set.
Definition STPathSet.h:669
bool empty() const
Definition STPathSet.h:630
Discardable, editable view to a ledger.
Definition Sandbox.h:18
void apply(RawView &to)
Definition Sandbox.h:38
virtual beast::Journal getJournal(std::string const &name)=0
virtual OpenLedger & getOpenLedger()=0
SLE::pointer peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
Tests of AMM that use offers too.
void testRippleState(FeatureBitset features)
void testOfferFeesConsumeFunds(FeatureBitset features)
void testTransferRateOffer(FeatureBitset features)
void testCrossCurrencyBridged(FeatureBitset features)
void testOffersWhenFrozen(FeatureBitset features)
void testDirectToDirectPath(FeatureBitset features)
void testTransferRateNoOwnerFee(FeatureBitset features)
void testFillModes(FeatureBitset features)
void testSellFlagExceedLimit(FeatureBitset features)
void testRmFundedOffer(FeatureBitset features)
void testSelfIssueOffer(FeatureBitset features)
void testStepLimit(FeatureBitset features)
void testSellFlagBasic(FeatureBitset features)
void testTxMultisign(FeatureBitset features)
void testEnforceNoRipple(FeatureBitset features)
void testGlobalFreeze(FeatureBitset features)
void testLoop(FeatureBitset features)
void testBadPathAssert(FeatureBitset features)
void run() override
Runs the suite.
void testBridgedCross(FeatureBitset features)
void testMissingAuth(FeatureBitset features)
void testCurrencyConversionEntire(FeatureBitset features)
void testCurrencyConversionInParts(FeatureBitset features)
void testToStrand(FeatureBitset features)
NumberMantissaScaleGuard const sg_
void testCrossCurrencyEndXRP(FeatureBitset features)
void testFalseDry(FeatureBitset features)
void testOfferCreateThenCross(FeatureBitset features)
void testOfferCrossWithXRP(FeatureBitset features)
void testBookStep(FeatureBitset features)
void testConvertAllOfAnAsset(FeatureBitset features)
void testPseudoAccountRequireAuth(FeatureBitset features)
void testGatewayCrossCurrency(FeatureBitset features)
void testSellWithFillOrKill(FeatureBitset features)
void testRequireAuth(FeatureBitset features)
void testPayment(FeatureBitset features)
void testRIPD1373(FeatureBitset features)
void testOfferCrossWithLimitOverride(FeatureBitset features)
void testCrossCurrencyStartXRP(FeatureBitset features)
json::Value json() const
Definition PathSet.h:187
TestPath & pushBack(Issue const &iss)
Definition PathSet.h:125
jtx::Account const alice_
Definition AMMTest.h:87
void testAMM(std::function< void(jtx::AMM &, jtx::Env &)> const &cb, std::optional< std::pair< STAmount, STAmount > > const &pool=std::nullopt, std::uint16_t tfee=0, std::optional< jtx::Ter > const &ter=std::nullopt, std::vector< FeatureBitset > const &features={testableAmendments()})
testAMM() funds 30,000XRP and 30,000IOU for each non-XRP asset to Alice and Carol
Definition AMMTest.cpp:120
jtx::Account const gw_
Definition AMMTest.h:85
static FeatureBitset testableAmendments()
Definition AMMTest.h:99
jtx::Account const carol_
Definition AMMTest.h:86
jtx::Account const bob_
Definition AMMTest.h:88
static XRPAmount reserve(jtx::Env &env, std::uint32_t count)
Definition AMMTest.cpp:199
static XRPAmount ammCrtFee(jtx::Env &env)
Definition AMMTest.cpp:205
Convenience class to test AMM functionality.
bool expectTradingFee(std::uint16_t fee) const
Definition AMM.cpp:337
json::Value bid(BidArg const &arg)
Definition AMM.cpp:735
IOUAmount withdrawAll(std::optional< Account > const &account, std::optional< STAmount > const &asset1OutDetails=std::nullopt, std::optional< Ter > const &ter=std::nullopt)
bool expectAuctionSlot(std::uint32_t fee, std::optional< std::uint8_t > timeSlot, IOUAmount expectedPrice) const
Definition AMM.cpp:307
IOUAmount tokens() const
IOUAmount deposit(std::optional< Account > const &account, LPToken tokens, std::optional< STAmount > const &asset1InDetails=std::nullopt, std::optional< std::uint32_t > const &flags=std::nullopt, std::optional< Ter > const &ter=std::nullopt)
Definition AMM.cpp:463
IOUAmount withdraw(std::optional< Account > const &account, std::optional< LPToken > const &tokens, std::optional< STAmount > const &asset1OutDetails=std::nullopt, std::optional< std::uint32_t > const &flags=std::nullopt, std::optional< Ter > const &ter=std::nullopt)
Definition AMM.cpp:605
json::Value ammRpcInfo(std::optional< AccountID > const &account=std::nullopt, std::optional< std::string > const &ledgerIndex=std::nullopt, std::optional< Asset > const &asset1=std::nullopt, std::optional< Asset > const &asset2=std::nullopt, std::optional< AccountID > const &ammAccount=std::nullopt, bool ignoreParams=false, unsigned apiVersion=rpc::kApiInvalidVersion) const
Send amm_info RPC command.
Definition AMM.cpp:183
AccountID const & ammAccount() const
void vote(std::optional< Account > const &account, std::uint32_t feeVal, std::optional< std::uint32_t > const &flags=std::nullopt, std::optional< jtx::Seq > const &seq=std::nullopt, std::optional< std::pair< Asset, Asset > > const &assets=std::nullopt, std::optional< Ter > const &ter=std::nullopt)
Definition AMM.cpp:709
bool expectBalances(STAmount const &asset1, STAmount const &asset2, IOUAmount const &lpt, std::optional< AccountID > const &account=std::nullopt) const
Verify the AMM balances.
Definition AMM.cpp:269
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
A balance matches.
Definition balance.h:24
Sets the DeliverMin on a JTx.
Definition delivermin.h:16
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
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
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
std::shared_ptr< STObject const > meta()
Return metadata for the last JTx.
Definition Env.cpp:538
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
Match set account flags.
Definition flags.h:119
Inject raw JSON.
Definition jtx_json.h:16
Set a multisignature on a JTx.
Definition multisign.h:53
Match clear account flags.
Definition flags.h:137
Match the number of items in the account's owner directory.
Definition owners.h:55
Add a path.
Definition paths.h:47
Set Paths, SendMax on a JTx.
Definition paths.h:23
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 regular signature on a JTx.
Definition sig.h:19
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
T clear(T... args)
T end(T... args)
T find(T... args)
T insert(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
void nOffers(Env &env, std::size_t n, Account const &account, STAmount const &in, STAmount const &out)
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
std::vector< STAmount > fund(jtx::Env &env, jtx::Account const &gw, std::vector< jtx::Account > const &accounts, std::vector< STAmount > const &amts, Fund how)
Definition AMMTest.cpp:34
bool expectLedgerEntryRoot(Env &env, Account const &acct, STAmount const &expectedValue)
json::Value regkey(Account const &account, DisabledT)
Disable the regular key.
Definition regkey.cpp:13
PrettyAmount xrpMinusFee(Env const &env, std::int64_t xrpAmount)
STPathElement allPathElements(AccountID const &a, Asset const &asset)
bool expectHolding(Env const &env, AccountID const &account, STAmount const &value, bool defaultLimits)
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
std::tuple< STPathSet, STAmount, STAmount > findPaths(jtx::Env &env, jtx::Account const &src, jtx::Account const &dst, STAmount const &saDstAmount, std::optional< STAmount > const &saSendMax, std::optional< PathAsset > const &srcAsset, std::optional< AccountID > const &srcIssuer, std::optional< UInt256 > const &domain)
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)
bool same(STPathSet const &st1, Args const &... args)
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< ltOFFER > Offers
Match the number of offers in the account's owner directory.
Definition owners.h:137
STPathElement cpe(PathAsset const &pa)
STPathElement ipe(Asset const &asset)
json::Value ledgerEntryState(Env &env, Account const &acctA, Account const &acctB, std::string const &currency)
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
bool equal(STAmount const &sa1, STAmount const &sa2)
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.
STPath stpath(Args const &... args)
json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
Definition rate.cpp:15
json::Value getAccountLines(Env &env, AccountID const &acctId)
json::Value signers(Account const &account, std::uint32_t quorum, std::vector< Signer > const &v)
Definition multisign.cpp:31
bool checkArraySize(json::Value const &val, unsigned int size)
json::Value getAccountOffers(Env &env, AccountID const &acct, bool current)
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)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
TER offerDelete(ApplyView &view, SLE::Ref sle, beast::Journal j)
Delete an offer.
TAmounts< STAmount, STAmount > Amounts
Definition Quality.h:69
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:58
Currency const & xrpCurrency()
XRP currency.
Definition UintTypes.cpp:99
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
StrandResult< TInAmt, TOutAmt > flow(PaymentSandbox const &baseView, Strand const &strand, std::optional< TInAmt > const &maxIn, TOutAmt const &out, beast::Journal j)
Request out amount from a strand.
Definition StrandFlow.h:101
@ TapNone
Definition ApplyView.h:28
@ temBAD_PATH
Definition TER.h:84
@ temBAD_SEND_XRP_PATHS
Definition TER.h:91
@ temBAD_SEND_XRP_MAX
Definition TER.h:88
@ temBAD_PATH_LOOP
Definition TER.h:85
@ temBAD_AMOUNT
Definition TER.h:77
TERSubset< CanCvtToTER > TER
Definition TER.h:657
AccountID const & xrpAccount()
Compute AccountID from public key.
@ tecPATH_PARTIAL
Definition TER.h:293
@ tecPATH_DRY
Definition TER.h:305
@ tecUNFUNDED_AMM
Definition TER.h:339
@ tecNO_AUTH
Definition TER.h:311
@ tecFROZEN
Definition TER.h:314
@ tecUNFUNDED_OFFER
Definition TER.h:295
@ tecNO_LINE
Definition TER.h:312
@ tecKILLED
Definition TER.h:327
@ tecNO_PERMISSION
Definition TER.h:316
@ tesSUCCESS
Definition TER.h:253
Represents an XRP, IOU, or MPT quantity This customizes the string conversion and supports XRP conver...
STAmount const & value() const
T tie(T... args)