xrpld
Loading...
Searching...
No Matches
AMMExtendedMPT_test.cpp
1#include <test/jtx/AMM.h>
2#include <test/jtx/AMMTest.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/PathSet.h>
5#include <test/jtx/TestHelpers.h>
6#include <test/jtx/amount.h>
7#include <test/jtx/balance.h>
8#include <test/jtx/delivermin.h>
9#include <test/jtx/fee.h>
10#include <test/jtx/flags.h>
11#include <test/jtx/jtx_json.h>
12#include <test/jtx/mpt.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/regkey.h>
20#include <test/jtx/require.h>
21#include <test/jtx/sendmax.h>
22#include <test/jtx/sig.h>
23#include <test/jtx/tags.h>
24#include <test/jtx/ter.h>
25#include <test/jtx/txflags.h>
26
27#include <xrpl/beast/unit_test/suite.h>
28#include <xrpl/json/json_value.h>
29#include <xrpl/json/to_string.h>
30#include <xrpl/ledger/ApplyView.h>
31#include <xrpl/ledger/OpenView.h>
32#include <xrpl/ledger/PaymentSandbox.h>
33#include <xrpl/ledger/Sandbox.h>
34#include <xrpl/ledger/helpers/OfferHelpers.h>
35#include <xrpl/protocol/AccountID.h>
36#include <xrpl/protocol/Feature.h>
37#include <xrpl/protocol/Indexes.h>
38#include <xrpl/protocol/Issue.h>
39#include <xrpl/protocol/KeyType.h>
40#include <xrpl/protocol/Quality.h>
41#include <xrpl/protocol/SField.h>
42#include <xrpl/protocol/STPathSet.h>
43#include <xrpl/protocol/Seed.h>
44#include <xrpl/protocol/TER.h>
45#include <xrpl/protocol/TxFlags.h>
46#include <xrpl/protocol/UintTypes.h>
47#include <xrpl/protocol/XRPAmount.h>
48#include <xrpl/protocol/jss.h>
49#include <xrpl/tx/paths/Flow.h>
50#include <xrpl/tx/paths/detail/Steps.h>
51
52#include <cstdint>
53#include <optional>
54#include <tuple>
55
56namespace xrpl::test {
57
62{
63private:
64 void
66 {
67 testcase("Incorrect Removal of Funded Offers");
68
69 // We need at least two paths. One at good quality and one at bad
70 // quality. The bad quality path needs two offer books in a row.
71 // Each offer book should have two offers at the same quality, the
72 // offers should be completely consumed, and the payment should
73 // require both offers to be satisfied. The first offer must
74 // be "taker gets" XRP. Ensure that the payment engine does not remove
75 // the first "taker gets" xrp offer, because the offer is still
76 // funded and not used for the payment.
77
78 using namespace jtx;
79 Env env{*this, features};
80
81 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000));
82
83 MPTTester const eth({
84 .env = env,
85 .issuer = gw_,
86 .holders = {alice_, bob_, carol_},
87 .pay = 200'000'000'000'000'000,
88 .flags = kMptDexFlags,
89 });
90
91 MPTTester const btc({
92 .env = env,
93 .issuer = gw_,
94 .holders = {alice_, bob_, carol_},
95 .pay = 2'000'000'000'000'000,
96 .flags = kMptDexFlags,
97 });
98
99 // Must be two offers at the same quality
100 // "taker gets" must be XRP
101 // (Different amounts so I can distinguish the offers)
102 env(offer(carol_, btc(49'000'000'000'000), XRP(49)));
103 env(offer(carol_, btc(51'000'000'000'000), XRP(51)));
104
105 // Offers for the poor quality path
106 // Must be two offers at the same quality
107 env(offer(carol_, XRP(50), eth(50'000'000'000'000)));
108 env(offer(carol_, XRP(50), eth(50'000'000'000'000)));
109
110 // Good quality path
111 AMM const ammCarol(env, carol_, btc(1'000'000'000'000'000), eth(100'100'000'000'000'000));
112
113 PathSet const paths(TestPath(XRP, MPT(eth)), TestPath(MPT(eth)));
114
115 env(pay(alice_, bob_, eth(100'000'000'000'000)),
116 Json(paths.json()),
117 Sendmax(btc(1'000'000'000'000'000)),
118 Txflags(tfPartialPayment));
119
120 BEAST_EXPECT(ammCarol.expectBalances(
121 btc(1'001'000'000'374'814), eth(100'000'000'000'000'000), ammCarol.tokens()));
122
123 env.require(Balance(bob_, eth(200'100'000'000'000'000)));
124 BEAST_EXPECT(isOffer(env, carol_, btc(49'000'000'000'000), XRP(49)));
125 }
126
127 void
129 {
130 testcase("Fill Modes");
131 using namespace jtx;
132
133 auto const startBalance = XRP(1'000'000);
134
135 // Fill or Kill - unless we fully cross, just charge a fee and don't
136 // place the offer on the books. But also clean up expired offers
137 // that are discovered along the way.
138 testAMM(
139 [&](AMM& ammAlice, Env& env) {
140 auto const& btc = MPT(ammAlice[1]);
141 auto const baseFee = env.current()->fees().base;
142 auto const carolBTC = env.balance(carol_, btc);
143 auto carolXRP = env.balance(carol_, XRP);
144 // Order that can't be filled
145 env(offer(carol_, btc(100), XRP(100)), Txflags(tfFillOrKill), Ter(tecKILLED));
146 env.close();
147 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), btc(10'000), ammAlice.tokens()));
148 // fee = AMM
149 env.require(Balance(carol_, carolXRP - baseFee));
150 env.require(Balance(carol_, carolBTC));
151
152 BEAST_EXPECT(expectOffers(env, carol_, 0));
153 carolXRP = env.balance(carol_, XRP);
154
155 // Order that can be filled
156 env(offer(carol_, XRP(100), btc(100)), Txflags(tfFillOrKill), Ter(tesSUCCESS));
157 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), btc(10'100), ammAlice.tokens()));
158 env.require(Balance(carol_, carolXRP + XRP(100) - baseFee));
159 env.require(Balance(carol_, carolBTC - btc(100)));
160 BEAST_EXPECT(expectOffers(env, carol_, 0));
161 },
162 {{XRP(10'100), gAmmmpt(10'000)}},
163 0,
164 std::nullopt,
165 {features});
166
167 // Immediate or Cancel - cross as much as possible
168 // and add nothing on the books.
169 testAMM(
170 [&](AMM& ammAlice, Env& env) {
171 auto const& btc = MPT(ammAlice[1]);
172 auto const baseFee = env.current()->fees().base;
173 auto const carolBTC = env.balance(carol_, btc);
174 auto const carolXRP = env.balance(carol_, XRP);
175 env(offer(carol_, XRP(200), btc(200)),
176 Txflags(tfImmediateOrCancel),
177 Ter(tesSUCCESS));
178
179 // AMM generates a synthetic offer of 100BTC/100XRP
180 // to match the CLOB offer quality.
181 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), btc(10'100), ammAlice.tokens()));
182 // +AMM - offer * fee
183 env.require(Balance(carol_, carolXRP + XRP(100) - baseFee));
184 env.require(Balance(carol_, carolBTC - btc(100)));
185 BEAST_EXPECT(expectOffers(env, carol_, 0));
186 },
187 {{XRP(10'100), gAmmmpt(10'000)}},
188 0,
189 std::nullopt,
190 {features});
191
192 // tfPassive -- place the offer without crossing it.
193 {
194 Env env{*this, features};
195 fund(env, gw_, {alice_, carol_}, XRP(30'000'000));
196
197 MPTTester const btc({
198 .env = env,
199 .issuer = gw_,
200 .holders = {alice_, carol_},
201 .pay = 30'000'000,
202 .flags = kMptDexFlags,
203 });
204
205 AMM const ammAlice(env, alice_, XRP(10'100'000), btc(10'000'000));
206
207 // Scale the exact-quality fixture up so the visual relationship
208 // stays clear: the passive CLOB offer has the same 1:1 quality as
209 // the generated AMM offer, so it should not cross.
210 env(offer(carol_, XRP(100'000), btc(100'000), tfPassive));
211 env.close();
212 BEAST_EXPECT(
213 ammAlice.expectBalances(XRP(10'100'000), btc(10'000'000), ammAlice.tokens()));
214 BEAST_EXPECT(expectOffers(env, carol_, 1, {{{XRP(100'000), btc(100'000)}}}));
215 }
216
217 // tfPassive -- cross only offers of better quality.
218 testAMM(
219 [&](AMM& ammAlice, Env& env) {
220 auto const& btc = MPT(ammAlice[1]);
221 env(offer(alice_, btc(110), XRP(100)));
222 env.close();
223
224 // Carol creates a passive offer. That offer should cross
225 // AMM and leave Alice's offer untouched.
226 env(offer(carol_, XRP(100), btc(100), tfPassive));
227 env.close();
228 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'900), btc(9083), ammAlice.tokens()));
229 BEAST_EXPECT(expectOffers(env, carol_, 0));
230 BEAST_EXPECT(expectOffers(env, alice_, 1));
231 },
232 {{XRP(11'000), gAmmmpt(9'000)}},
233 0,
234 std::nullopt,
235 {features});
236 }
237
238 void
240 {
241 testcase("Offer Crossing with XRP, Normal order");
242
243 using namespace jtx;
244
245 Env env{*this, features};
246
247 fund(env, gw_, {bob_, alice_}, XRP(300'000));
248
249 MPTTester const btc({
250 .env = env,
251 .issuer = gw_,
252 .holders = {alice_, bob_},
253 .pay = 100'000'000,
254 .flags = kMptDexFlags,
255 });
256
257 AMM const ammAlice(env, alice_, XRP(150'000), btc(50'000'000));
258
259 // Existing offer pays better than this wants.
260 // Partially consume existing offer.
261 // Pay 1'000'000 BTC, get 3061224490 Drops.
262 auto const xrpTransferred = XRPAmount{3'061'224'490};
263 env(offer(bob_, btc(1'000'000), XRP(4'000)));
264
265 BEAST_EXPECT(ammAlice.expectBalances(
266 XRP(150'000) + xrpTransferred, btc(49'000'000), IOUAmount{273'861'278752583, -5}));
267
268 env.require(Balance(bob_, btc(101'000'000)));
269 BEAST_EXPECT(
270 expectLedgerEntryRoot(env, bob_, XRP(300'000) - xrpTransferred - 2 * txFee(env, 1)));
271 BEAST_EXPECT(expectOffers(env, bob_, 0));
272 }
273
274 void
276 {
277 testcase("Offer Crossing with Limit Override");
278
279 using namespace jtx;
280
281 Env env{*this, features};
282
283 env.fund(XRP(200'000), gw_, alice_, bob_);
284 env.close();
285
286 MPTTester const btc(
287 {.env = env, .issuer = gw_, .holders = {alice_, bob_}, .flags = kMptDexFlags});
288 env(pay(gw_, alice_, btc(500'000'000)));
289
290 AMM const ammAlice(env, alice_, XRP(150'000), btc(51'000'000));
291 env(offer(bob_, btc(1'000'000), XRP(3'000)));
292
293 BEAST_EXPECT(ammAlice.expectBalances(XRP(153'000), btc(50'000'000), ammAlice.tokens()));
294
295 env.require(Balance(bob_, btc(1'000'000)));
296 env.require(Balance(bob_, XRP(200'000) - XRP(3'000) - env.current()->fees().base * 2));
297 }
298
299 void
301 {
302 testcase("Currency Conversion: Entire Offer");
303
304 using namespace jtx;
305
306 Env env{*this, features};
307
308 fund(env, gw_, {alice_, bob_}, XRP(10'000));
309 env.require(Owners(bob_, 0));
310
311 MPTTester const btc(
312 {.env = env, .issuer = gw_, .holders = {alice_, bob_}, .flags = kMptDexFlags});
313 env(pay(gw_, bob_, btc(1'000'000'000)));
314
315 env.require(Owners(alice_, 1), Owners(bob_, 1));
316
317 env(pay(gw_, alice_, btc(100'000'000)));
318 AMM const ammBob(env, bob_, btc(200'000'000), XRP(1'500));
319
320 env(pay(alice_, alice_, XRP(500)), Sendmax(btc(100'000'000)));
321
322 BEAST_EXPECT(ammBob.expectBalances(btc(300'000'000), XRP(1'000), ammBob.tokens()));
323 env.require(Balance(alice_, btc(0)));
324
325 auto const jrr = ledgerEntryRoot(env, alice_);
326 env.require(Balance(alice_, XRP(10'000) + XRP(500) - env.current()->fees().base * 2));
327 }
328
329 void
331 {
332 testcase("Currency Conversion: In Parts");
333
334 using namespace jtx;
335
336 Env env{*this, features};
337 env.fund(XRP(30'000), gw_, bob_);
338 env.fund(XRP(40'000), alice_);
339
340 MPTTester const btc({
341 .env = env,
342 .issuer = gw_,
343 .holders = {alice_, bob_},
344 .pay = 30'000'000'000,
345 .flags = kMptDexFlags,
346 });
347 env(pay(gw_, alice_, btc(10'000'000'000)));
348
349 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'000'000'000));
350 env.close();
351
352 // Alice converts BTC to XRP which should fail
353 // due to PartialPayment.
354 env(pay(alice_, alice_, XRP(100)), Sendmax(btc(100'000'000)), Ter(tecPATH_PARTIAL));
355
356 // Alice converts BTC to XRP, should succeed because
357 // we permit partial payment
358 env(pay(alice_, alice_, XRP(100)), Sendmax(btc(100'000'000)), Txflags(tfPartialPayment));
359 env.close();
360 BEAST_EXPECT(ammAlice.expectBalances(
361 XRPAmount{9'900'990'100}, btc(10'100'000'000), ammAlice.tokens()));
362 // initial 40,000'000'000 - 10,000'000'000AMM - 100'000'000pay
363 env.require(Balance(alice_, btc(29'900'000'000)));
364 // initial 40,000 - 10,0000AMM + 99.009900pay - fee*3
365 BEAST_EXPECT(expectLedgerEntryRoot(
366 env,
367 alice_,
368 XRP(40'000) - XRP(10'000) + XRPAmount{99'009'900} - ammCrtFee(env) - txFee(env, 3)));
369 }
370
371 void
373 {
374 testcase("Cross Currency Payment: Start with XRP");
375
376 using namespace jtx;
377
378 Env env{*this, features};
379 env.fund(XRP(30'000), gw_);
380 env.fund(XRP(40'000), alice_);
381 env.fund(XRP(1'000), bob_);
382
383 MPTTester const btc(
384 {.env = env, .issuer = gw_, .holders = {alice_, bob_}, .flags = kMptDexFlags});
385 env(pay(gw_, alice_, btc(10'100'000'000)));
386
387 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'100'000'000));
388 env.close();
389
390 env(pay(alice_, bob_, btc(100'000'000)), Sendmax(XRP(100)));
391 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), btc(10'000'000'000), ammAlice.tokens()));
392 env.require(Balance(bob_, btc(100'000'000)));
393 }
394
395 void
397 {
398 testcase("Cross Currency Payment: End with XRP");
399
400 using namespace jtx;
401
402 Env env{*this, features};
403 env.fund(XRP(30'000), gw_);
404 env.fund(XRP(40'100), alice_);
405 env.fund(XRP(1'000), bob_);
406
407 MPTTester const btc(
408 {.env = env, .issuer = gw_, .holders = {alice_, bob_}, .flags = kMptDexFlags});
409 env(pay(gw_, alice_, btc(40'000'000'000)));
410
411 AMM const ammAlice(env, alice_, XRP(10'100), btc(10'000'000'000));
412 env.close();
413
414 env(pay(alice_, bob_, XRP(100)), Sendmax(btc(100'000'000)));
415 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), btc(10'100'000'000), ammAlice.tokens()));
416 BEAST_EXPECT(expectLedgerEntryRoot(env, bob_, XRP(1'000) + XRP(100) - txFee(env, 1)));
417 }
418
419 void
421 {
422 testcase("Cross Currency Payment: Bridged");
423
424 using namespace jtx;
425
426 auto const test = [&](auto&& issue1, auto&& issue2) {
427 Env env(*this);
428 auto const dan = Account{"dan"};
429 env.fund(XRP(60'000), alice_, bob_, carol_, gw_, dan);
430 env.close();
431 auto const eth = issue1({
432 .env = env,
433 .token = "ETH",
434 .issuer = gw_,
435 .holders = {alice_, bob_, carol_, dan},
436 .limit = 10'000'000'000'000'000,
437 });
438 auto const btc = issue2({
439 .env = env,
440 .token = "BTC",
441 .issuer = gw_,
442 .holders = {alice_, bob_, carol_, dan},
443 .limit = 10'000'000'000'000'000,
444 });
445 env(pay(gw_, alice_, btc(500'000'000'000'000)));
446 env(pay(gw_, carol_, btc(6'000'000'000'000'000)));
447 env(pay(gw_, dan, eth(400'000'000'000'000)));
448 env.close();
449 env.close();
450 AMM const ammCarol(env, carol_, btc(5'000'000'000'000'000), XRP(50'000));
451
452 env(offer(dan, XRP(500), eth(50'000'000'000'000)));
453 env.close();
454
456 jtp[0u][0u][jss::currency] = "XRP";
457 env(pay(alice_, bob_, eth(30'000'000'000'000)),
458 Json(jss::Paths, jtp),
459 Sendmax(btc(333'000'000'000'000)));
460 env.close();
461 BEAST_EXPECT(ammCarol.expectBalances(
462 XRP(49'700), btc(5'030'181'086'519'115), ammCarol.tokens()));
463 BEAST_EXPECT(expectOffers(env, dan, 1, {{Amounts{XRP(200), eth(20'000'000'000'000)}}}));
464 env.require(Balance(bob_, eth(30'000'000'000'000)));
465 };
467 }
468
469 void
471 {
472 testcase("Offer Fees Consume Funds");
473
474 using namespace jtx;
475
476 Env env{*this, features};
477
478 // Provide micro amounts to compensate for fees to make results round
479 // nice.
480 auto const startingXrp =
481 XRP(100) + env.current()->fees().accountReserve(2, 1) + env.current()->fees().base * 3;
482
483 env.fund(startingXrp, gw_, alice_);
484 env.fund(XRP(2'000), bob_);
485 env.close();
486
487 MPTTester const btc(
488 {.env = env, .issuer = gw_, .holders = {alice_, bob_}, .flags = kMptDexFlags});
489
490 // Created only to increase one reserve count for alice
491 MPTTester const eth(
492 {.env = env, .issuer = gw_, .holders = {alice_}, .flags = kMptDexFlags});
493
494 env(pay(gw_, bob_, btc(1'200'000'000'000'000)));
495
496 AMM const ammBob(env, bob_, XRP(1'000), btc(1'200'000'000'000'000));
497 // Alice has 400 - (2 reserve of 50 = 300 reserve) = 100 available.
498 // Ask for more than available to prove reserve works.
499 env(offer(alice_, btc(200'000'000'000'000), XRP(200)));
500
501 // The pool gets only 100XRP for ~109.09e12BTC, even though
502 // it can exchange more.
503 BEAST_EXPECT(
504 ammBob.expectBalances(XRP(1'100), btc(1'090'909'090'909'091), ammBob.tokens()));
505
506 env.require(Balance(alice_, btc(109'090'909'090'909)));
507 env.require(Balance(alice_, XRP(300)));
508 }
509
510 void
512 {
513 testcase("Offer Create, then Cross");
514
515 using namespace jtx;
516
517 Env env{*this, features};
518
519 fund(env, gw_, {alice_, bob_}, XRP(200'000));
520
521 MPTTester const btc({
522 .env = env,
523 .issuer = gw_,
524 .holders = {alice_, bob_},
525 .transferFee = 500,
526 .flags = kMptDexFlags,
527 });
528
529 env(pay(gw_, bob_, btc(1'000'000'000'000)));
530 env(pay(gw_, alice_, btc(200'000'000'000'000)));
531
532 AMM const ammAlice(env, alice_, btc(150'000'000'000'000), XRP(150'100));
533 env(offer(bob_, XRP(100), btc(100'000'000'000)));
534
535 BEAST_EXPECT(
536 ammAlice.expectBalances(btc(150'100'000'000'000), XRP(150'000), ammAlice.tokens()));
537
538 // Bob pays 0.005 transfer fee.
539 env.require(Balance(bob_, btc(899'500'000'000)));
540 }
541
542 void
544 {
545 testcase("Offer tfSell: Basic Sell");
546
547 using namespace jtx;
548
549 Env env{*this, features};
550 env.fund(XRP(30'000), gw_, bob_, carol_);
551 env.fund(XRP(39'900), alice_);
552
553 MPTTester const btc({
554 .env = env,
555 .issuer = gw_,
556 .holders = {alice_, bob_, carol_},
557 .pay = 30'000,
558 .flags = kMptDexFlags,
559 });
560 env(pay(gw_, alice_, btc(10'100)));
561
562 AMM const ammAlice(env, alice_, XRP(9'900), btc(10'100));
563
564 env(offer(carol_, btc(100), XRP(100)), Json(jss::Flags, tfSell));
565 env.close();
566 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), btc(9'999), ammAlice.tokens()));
567 BEAST_EXPECT(expectOffers(env, carol_, 0));
568 env.require(Balance(carol_, btc(30'101)));
569 BEAST_EXPECT(
570 expectLedgerEntryRoot(env, carol_, XRP(30'000) - XRP(100) - 2 * txFee(env, 1)));
571 }
572
573 void
575 {
576 testcase("Offer tfSell: 2x Sell Exceed Limit");
577
578 using namespace jtx;
579
580 Env env{*this, features};
581
582 auto const startingXrp = XRP(100) + reserve(env, 1) + env.current()->fees().base * 2;
583
584 env.fund(startingXrp, gw_, alice_);
585 env.fund(XRP(2'000), bob_);
586 env.close();
587
588 MPTTester const btc(
589 {.env = env, .issuer = gw_, .holders = {alice_, bob_}, .flags = kMptDexFlags});
590 env(pay(gw_, bob_, btc(2'200'000'000)));
591
592 AMM const ammBob(env, bob_, XRP(1'000), btc(2'200'000'000));
593 // Alice has 350 fees - a reserve of 50 = 250 reserve = 100 available.
594 // Ask for more than available to prove reserve works.
595 // Taker pays 100'000'000 BTC for 100 XRP.
596 // Selling XRP.
597 // Will sell all 100 XRP and get more BTC than asked for.
598 env(offer(alice_, btc(100'000'000), XRP(200)), Json(jss::Flags, tfSell));
599 BEAST_EXPECT(ammBob.expectBalances(XRP(1'100), btc(2'000'000'000), ammBob.tokens()));
600 env.require(Balance(alice_, btc(200'000'000)));
601 BEAST_EXPECT(expectLedgerEntryRoot(env, alice_, XRP(250)));
602 BEAST_EXPECT(expectOffers(env, alice_, 0));
603 }
604
605 void
607 {
608 testcase("Client Issue: Gateway Cross Currency");
609
610 using namespace jtx;
611
612 Env env{*this, features};
613
614 auto const startingXrp = XRP(100.1) + reserve(env, 1) + env.current()->fees().base * 2;
615 env.fund(startingXrp, gw_, alice_, bob_);
616
617 MPTTester const xts({
618 .env = env,
619 .issuer = gw_,
620 .holders = {alice_, bob_},
621 .pay = 1'000'000'000'000'000,
622 .flags = kMptDexFlags,
623 });
624 MPTTester const xxx({
625 .env = env,
626 .issuer = gw_,
627 .holders = {alice_, bob_},
628 .pay = 1'000'000'000'000'000,
629 .flags = kMptDexFlags,
630 });
631
632 AMM const ammAlice(env, alice_, xts(1'000'000'000'000'000), xxx(1'000'000'000'000'000));
633
634 json::Value payment;
635 payment[jss::secret] = toBase58(generateSeed("bob"));
636 payment[jss::id] = env.seq(bob_);
637 payment[jss::build_path] = true;
638 payment[jss::tx_json] = pay(bob_, bob_, xxx(10'000'000'000'000));
639 payment[jss::tx_json][jss::Sequence] =
640 env.current()->read(keylet::account(bob_.id()))->getFieldU32(sfSequence);
641 payment[jss::tx_json][jss::Fee] = to_string(env.current()->fees().base);
642 payment[jss::tx_json][jss::SendMax] =
643 xts(15'000'000'000'000).value().getJson(JsonOptions::Values::None);
644 payment[jss::tx_json][jss::Flags] = tfPartialPayment;
645 auto const jrr = env.rpc("json", "submit", to_string(payment));
646 BEAST_EXPECT(jrr[jss::result][jss::status] == "success");
647 BEAST_EXPECT(jrr[jss::result][jss::engine_result] == "tesSUCCESS");
648
649 BEAST_EXPECT(ammAlice.expectBalances(
650 xts(1'010'101'010'101'011), xxx(990'000'000'000'000), ammAlice.tokens()));
651 env.require(Balance(bob_, xts(989'898'989'898'989)));
652 env.require(Balance(bob_, xxx(1'010'000'000'000'000)));
653 }
654
655 void
657 {
658 testcase("Bridged Crossing");
659
660 using namespace jtx;
661
662 {
663 Env env{*this, features};
664 env.fund(XRP(30'000), gw_, alice_, bob_, carol_);
665
666 MPTTester const btc({
667 .env = env,
668 .issuer = gw_,
669 .holders = {alice_, bob_, carol_},
670 .pay = 15'000'000'000,
671 .flags = kMptDexFlags,
672 });
673 MPTTester const eth({
674 .env = env,
675 .issuer = gw_,
676 .holders = {alice_, bob_, carol_},
677 .pay = 15'000'000'000,
678 .flags = kMptDexFlags,
679 });
680
681 // The scenario:
682 // o BTC/XRP AMM is created.
683 // o ETH/XRP AMM is created.
684 // o carol has ETH but wants BTC.
685 // Note that carol's offer must come last. If carol's offer is
686 // placed before AMM is created, then autobridging will not occur.
687 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'100'000'000));
688 AMM const ammBob(env, bob_, eth(10'000'000'000), XRP(10'100));
689
690 // Carol makes an offer that consumes AMM liquidity and
691 // fully consumes Carol's offer.
692 env(offer(carol_, btc(100'000'000), eth(100'000'000)));
693 env.close();
694
695 BEAST_EXPECT(
696 ammAlice.expectBalances(XRP(10'100), btc(10'000'000'000), ammAlice.tokens()));
697 BEAST_EXPECT(ammBob.expectBalances(XRP(10'000), eth(10'100'000'000), ammBob.tokens()));
698 env.require(Balance(carol_, btc(15'100'000'000)));
699 env.require(Balance(carol_, eth(14'900'000'000)));
700 BEAST_EXPECT(expectOffers(env, carol_, 0));
701 }
702
703 {
704 Env env{*this, features};
705 env.fund(XRP(30'000), gw_, alice_, bob_, carol_);
706
707 MPTTester const btc({
708 .env = env,
709 .issuer = gw_,
710 .holders = {alice_, bob_, carol_},
711 .pay = 15'000'000'000,
712 .flags = kMptDexFlags,
713 });
714 MPTTester const eth({
715 .env = env,
716 .issuer = gw_,
717 .holders = {alice_, bob_, carol_},
718 .pay = 15'000'000'000,
719 .flags = kMptDexFlags,
720 });
721
722 // The scenario:
723 // o BTC/XRP AMM is created.
724 // o ETH/XRP offer is created.
725 // o carol has ETH but wants BTC.
726 // Note that carol's offer must come last. If carol's offer is
727 // placed before AMM and bob's offer are created, then autobridging
728 // will not occur.
729 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'100'000'000));
730 env(offer(bob_, eth(100'000'000), XRP(100)));
731 env.close();
732
733 // Carol makes an offer that consumes AMM liquidity and
734 // fully consumes Carol's offer.
735 env(offer(carol_, btc(100'000'000), eth(100'000'000)));
736 env.close();
737
738 BEAST_EXPECT(
739 ammAlice.expectBalances(XRP(10'100), btc(10'000'000'000), ammAlice.tokens()));
740 env.require(Balance(carol_, btc(15'100'000'000)));
741 env.require(Balance(carol_, eth(14'900'000'000)));
742 BEAST_EXPECT(expectOffers(env, carol_, 0));
743 BEAST_EXPECT(expectOffers(env, bob_, 0));
744 }
745
746 {
747 Env env{*this, features};
748 env.fund(XRP(30'000), gw_, alice_, bob_, carol_);
749
750 MPTTester const btc({
751 .env = env,
752 .issuer = gw_,
753 .holders = {alice_, bob_, carol_},
754 .pay = 15'000'000'000,
755 .flags = kMptDexFlags,
756 });
757 MPTTester const eth({
758 .env = env,
759 .issuer = gw_,
760 .holders = {alice_, bob_, carol_},
761 .pay = 15'000'000'000,
762 .flags = kMptDexFlags,
763 });
764
765 // The scenario:
766 // o BTC/XRP offer is created.
767 // o ETH/XRP AMM is created.
768 // o carol has ETH but wants BTC.
769 // Note that carol's offer must come last. If carol's offer is
770 // placed before AMM and alice's offer are created, then
771 // autobridging will not occur.
772 env(offer(alice_, XRP(100), btc(100'000'000)));
773 env.close();
774 AMM const ammBob(env, bob_, eth(10'000'000'000), XRP(10'100));
775
776 // Carol makes an offer that consumes AMM liquidity and
777 // fully consumes Carol's offer.
778 env(offer(carol_, btc(100'000'000), eth(100'000'000)));
779 env.close();
780
781 BEAST_EXPECT(ammBob.expectBalances(XRP(10'000), eth(10'100'000'000), ammBob.tokens()));
782 env.require(Balance(carol_, btc(15'100'000'000)));
783 env.require(Balance(carol_, eth(14'900'000'000)));
784 BEAST_EXPECT(expectOffers(env, carol_, 0));
785 BEAST_EXPECT(expectOffers(env, alice_, 0));
786 }
787 }
788
789 void
791 {
792 // Test a number of different corner cases regarding offer crossing
793 // when both the tfSell flag and tfFillOrKill flags are set.
794 testcase("Combine tfSell with tfFillOrKill");
795
796 using namespace jtx;
797
798 {
799 Env env{*this, features};
800 env.fund(XRP(30'000), gw_, alice_, bob_);
801
802 MPTTester const btc({
803 .env = env,
804 .issuer = gw_,
805 .holders = {alice_, bob_},
806 .pay = 20'000'000'000,
807 .flags = kMptDexFlags,
808 });
809 AMM const ammBob(env, bob_, XRP(20'000), btc(200'000'000));
810 // alice submits a tfSell | tfFillOrKill offer that does not cross.
811 env(offer(alice_, btc(2'100'000), XRP(210), tfSell | tfFillOrKill), Ter(tecKILLED));
812
813 BEAST_EXPECT(ammBob.expectBalances(XRP(20'000), btc(200'000'000), ammBob.tokens()));
814 BEAST_EXPECT(expectOffers(env, bob_, 0));
815 }
816 {
817 Env env{*this, features};
818 env.fund(XRP(30'000), gw_, alice_, bob_);
819
820 MPTTester const btc({
821 .env = env,
822 .issuer = gw_,
823 .holders = {alice_, bob_},
824 .pay = 1'000'000'000'000'000,
825 .flags = kMptDexFlags,
826 });
827 AMM const ammBob(env, bob_, XRP(20'000), btc(200'000'000'000'000));
828 // alice submits a tfSell | tfFillOrKill offer that crosses.
829 // Even though tfSell is present it doesn't matter this time.
830 env(offer(alice_, btc(2'000'000'000'000), XRP(220), tfSell | tfFillOrKill));
831 env.close();
832 BEAST_EXPECT(
833 ammBob.expectBalances(XRP(20'220), btc(197'823'936'696'341), ammBob.tokens()));
834 env.require(Balance(alice_, btc(1'002'176'063'303'659)));
835 BEAST_EXPECT(expectOffers(env, alice_, 0));
836 }
837 {
838 // alice submits a tfSell | tfFillOrKill offer that crosses and
839 // returns more than was asked for (because of the tfSell flag).
840 Env env{*this, features};
841 env.fund(XRP(30'000), gw_, alice_, bob_);
842
843 MPTTester const btc({
844 .env = env,
845 .issuer = gw_,
846 .holders = {alice_, bob_},
847 .pay = 1'000'000'000'000'000,
848 .flags = kMptDexFlags,
849 });
850 AMM const ammBob(env, bob_, XRP(20'000), btc(200'000'000'000'000));
851
852 env(offer(alice_, btc(10'000'000'000'000), XRP(1'500), tfSell | tfFillOrKill));
853 env.close();
854
855 BEAST_EXPECT(
856 ammBob.expectBalances(XRP(21'500), btc(186'046'511'627'907), ammBob.tokens()));
857 env.require(Balance(alice_, btc(1'013'953'488'372'093)));
858 BEAST_EXPECT(expectOffers(env, alice_, 0));
859 }
860 {
861 // alice submits a tfSell | tfFillOrKill offer that doesn't cross.
862 // This would have succeeded with a regular tfSell, but the
863 // fillOrKill prevents the transaction from crossing since not
864 // all of the offer is consumed because AMM generated offer,
865 // which matches alice's offer quality is ~ 10XRP/0.01996e3BTC.
866 Env env{*this, features};
867 env.fund(XRP(30'000), gw_, alice_, bob_);
868
869 MPTTester const btc({
870 .env = env,
871 .issuer = gw_,
872 .holders = {alice_, bob_},
873 .pay = 10'000'000'000,
874 .flags = kMptDexFlags,
875 });
876 AMM const ammBob(env, bob_, XRP(5000), btc(10'000'000));
877
878 env(offer(alice_, btc(1'000'000), XRP(501), tfSell | tfFillOrKill), Ter(tecKILLED));
879 env.close();
880 BEAST_EXPECT(expectOffers(env, alice_, 0));
881 BEAST_EXPECT(expectOffers(env, bob_, 0));
882 }
883 }
884
885 void
887 {
888 testcase("Transfer Rate Offer");
889
890 using namespace jtx;
891
892 // AMM XRP/BTC. Alice places BTC/XRP offer.
893 {
894 Env env(*this, features);
895 env.fund(XRP(30'000), gw_, bob_, carol_);
896 env.fund(XRP(40'000), alice_);
897
898 MPTTester const btc({
899 .env = env,
900 .issuer = gw_,
901 .holders = {alice_, bob_, carol_},
902 .transferFee = 25'000,
903 .pay = 30'000'000,
904 .flags = kMptDexFlags,
905 });
906 env(pay(gw_, alice_, btc(10'100'000)));
907
908 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'100'000));
909 env.close();
910
911 env(offer(carol_, btc(100'000), XRP(100)));
912 env.close();
913
914 // AMM doesn't pay the transfer fee
915 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), btc(10'000'000), ammAlice.tokens()));
916 env.require(Balance(carol_, btc(30'100'000)));
917 BEAST_EXPECT(expectOffers(env, carol_, 0));
918 }
919
920 {
921 Env env(*this, features);
922 env.fund(XRP(30'000), gw_, bob_, carol_);
923 env.fund(XRP(40'100), alice_);
924
925 MPTTester const btc({
926 .env = env,
927 .issuer = gw_,
928 .holders = {alice_, bob_, carol_},
929 .transferFee = 25'000,
930 .pay = 30'000'000,
931 .flags = kMptDexFlags,
932 });
933 env(pay(gw_, alice_, btc(10'000'000)));
934
935 AMM const ammAlice(env, alice_, XRP(10'100), btc(10'000'000));
936 env.close();
937
938 env(offer(carol_, XRP(100), btc(100'000)));
939 env.close();
940
941 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), btc(10'100'000), ammAlice.tokens()));
942 // Carol pays 25% transfer fee
943 env.require(Balance(carol_, btc(29'875'000)));
944 BEAST_EXPECT(expectOffers(env, carol_, 0));
945 }
946
947 {
948 // Bridged crossing.
949 Env env{*this, features};
950 env.fund(XRP(30'000), gw_, alice_, bob_, carol_);
951
952 MPTTester const btc({
953 .env = env,
954 .issuer = gw_,
955 .holders = {alice_, bob_, carol_},
956 .transferFee = 25'000,
957 .pay = 15'000'000,
958 .flags = kMptDexFlags,
959 });
960 MPTTester const eth({
961 .env = env,
962 .issuer = gw_,
963 .holders = {alice_, bob_, carol_},
964 .transferFee = 25'000,
965 .pay = 15'000'000,
966 .flags = kMptDexFlags,
967 });
968
969 // The scenario:
970 // o BTC/XRP AMM is created.
971 // o ETH/XRP Offer is created.
972 // o carol has ETH but wants BTC.
973 // Note that Carol's offer must come last. If Carol's offer is
974 // placed before AMM is created, then autobridging will not occur.
975 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'100'000));
976 env(offer(bob_, eth(100'000), XRP(100)));
977 env.close();
978
979 // Carol makes an offer that consumes AMM liquidity and
980 // fully consumes Bob's offer.
981 env(offer(carol_, btc(100'000), eth(100'000)));
982 env.close();
983
984 // AMM doesn't pay the transfer fee
985 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'100), btc(10'000'000), ammAlice.tokens()));
986 env.require(Balance(carol_, btc(15'100'000)));
987 // Carol pays 25% transfer fee.
988 env.require(Balance(carol_, eth(14'875'000)));
989 BEAST_EXPECT(expectOffers(env, carol_, 0));
990 BEAST_EXPECT(expectOffers(env, bob_, 0));
991 }
992
993 {
994 // Bridged crossing. The transfer fee is paid on the step not
995 // involving AMM as src/dst.
996 Env env{*this, features};
997 env.fund(XRP(30'000), gw_, alice_, bob_, carol_);
998
999 MPTTester const btc({
1000 .env = env,
1001 .issuer = gw_,
1002 .holders = {alice_, bob_, carol_},
1003 .transferFee = 25'000,
1004 .pay = 15'000'000,
1005 .flags = kMptDexFlags,
1006 });
1007 MPTTester const eth({
1008 .env = env,
1009 .issuer = gw_,
1010 .holders = {alice_, bob_, carol_},
1011 .transferFee = 25'000,
1012 .pay = 15'000'000,
1013 .flags = kMptDexFlags,
1014 });
1015
1016 // The scenario:
1017 // o BTC/XRP AMM is created.
1018 // o ETH/XRP Offer is created.
1019 // o carol has ETH but wants BTC.
1020 // Note that Carol's offer must come last. If Carol's offer is
1021 // placed before AMM is created, then autobridging will not occur.
1022 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'050'000));
1023 env(offer(bob_, eth(100'000), XRP(100)));
1024 env.close();
1025
1026 // Carol makes an offer that consumes AMM liquidity and
1027 // partially consumes Bob's offer.
1028 env(offer(carol_, btc(50'000), eth(50'000)));
1029 env.close();
1030 // This test verifies that the amount removed from an offer
1031 // accounts for the transfer fee that is removed from the
1032 // account but not from the remaining offer.
1033
1034 // AMM doesn't pay the transfer fee
1035 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'050), btc(10'000'000), ammAlice.tokens()));
1036 env.require(Balance(carol_, btc(15'050'000)));
1037 // Carol pays 25% transfer fee.
1038 env.require(Balance(carol_, eth(14'937'500)));
1039 BEAST_EXPECT(expectOffers(env, carol_, 0));
1040 BEAST_EXPECT(expectOffers(env, bob_, 1, {{Amounts{eth(50'000), XRP(50)}}}));
1041 }
1042 }
1043
1044 void
1046 {
1047 // This test is not the same as corresponding testSelfIssueOffer()
1048 // in the Offer_test. It simply tests AMM with self issue and
1049 // offer crossing.
1050 using namespace jtx;
1051
1052 Env env{*this, features};
1053
1054 auto const f = env.current()->fees().base;
1055
1056 env.fund(XRP(30'000) + f, alice_, bob_);
1057 env.close();
1058
1059 MPTTester const btc(
1060 {.env = env, .issuer = bob_, .holders = {alice_}, .flags = kMptDexFlags});
1061
1062 AMM const ammBob(env, bob_, XRP(10'000), btc(10'100));
1063
1064 env(offer(alice_, btc(100), XRP(100)));
1065 env.close();
1066
1067 BEAST_EXPECT(ammBob.expectBalances(XRP(10'100), btc(10'000), ammBob.tokens()));
1068 BEAST_EXPECT(expectOffers(env, alice_, 0));
1069 env.require(Balance(alice_, btc(100)));
1070 }
1071
1072 void
1074 {
1075 // The offer crossing code expects that a DirectStep is always
1076 // preceded by a BookStep. In one instance the default path
1077 // was not matching that assumption. Here we recreate that case
1078 // so we can prove the bug stays fixed.
1079 testcase("Direct to Direct path");
1080
1081 using namespace jtx;
1082
1083 Env env{*this, features};
1084
1085 auto const ann = Account("ann");
1086 auto const bob = Account("bob");
1087 auto const cam = Account("cam");
1088 auto const carol = Account("carol");
1089
1090 auto const fee = env.current()->fees().base;
1091 env.fund(XRP(1'000), carol);
1092 env.fund(reserve(env, 4) + (fee * 5), ann, bob, cam);
1093 env.close();
1094
1095 MPTTester const aBux(
1096 {.env = env, .issuer = ann, .holders = {bob, cam, carol}, .flags = kMptDexFlags});
1097
1098 MPTTester const bBux(
1099 {.env = env, .issuer = bob, .holders = {ann, cam, carol}, .flags = kMptDexFlags});
1100
1101 env(pay(ann, cam, aBux(350'000'000'000'000)));
1102 env(pay(bob, cam, bBux(350'000'000'000'000)));
1103 env(pay(bob, carol, bBux(4'000'000'000'000'000)));
1104 env(pay(ann, carol, aBux(4'000'000'000'000'000)));
1105
1106 AMM const ammCarol(env, carol, aBux(3'000'000'000'000'000), bBux(3'300'000'000'000'000));
1107
1108 // cam puts an offer on the books that her upcoming offer could cross.
1109 // But this offer should be deleted, not crossed, by her upcoming
1110 // offer.
1111 env(offer(cam, aBux(290'000'000'000'000), bBux(300'000'000'000'000), tfPassive));
1112 env.close();
1113 env.require(Balance(cam, aBux(350'000'000'000'000)));
1114 env.require(Balance(cam, bBux(350'000'000'000'000)));
1115 env.require(Offers(cam, 1));
1116
1117 // This offer caused the assert.
1118 env(offer(cam, bBux(300'000'000'000'000), aBux(300'000'000'000'000)));
1119
1120 // AMM is consumed up to the first cam Offer quality
1121 BEAST_EXPECT(ammCarol.expectBalances(
1122 aBux(3'093'541'659'651'603), bBux(3'200'215'509'984'419), ammCarol.tokens()));
1123 BEAST_EXPECT(expectOffers(
1124 env, cam, 1, {{Amounts{bBux(200'215'509'984'419), aBux(200'215'509'984'419)}}}));
1125 }
1126
1127 void
1129 {
1130 testcase("RequireAuth");
1131
1132 using namespace jtx;
1133
1134 Env env{*this, features};
1135 env.fund(XRP(400'000), gw_, alice_, bob_);
1136
1137 MPTTester btc({
1138 .env = env,
1139 .issuer = gw_,
1140 .holders = {alice_, bob_},
1141 .flags = tfMPTRequireAuth | kMptDexFlags,
1142 });
1143
1144 // Authorize bob and alice
1145 btc.authorize({.holder = alice_});
1146 btc.authorize({.holder = bob_});
1147
1148 env(pay(gw_, alice_, btc(1'000)));
1149 env.close();
1150
1151 // Alice is able to create AMM since the GW has authorized her
1152 AMM const ammAlice(env, alice_, btc(1'000), XRP(1'050));
1153
1154 env(pay(gw_, bob_, btc(50)));
1155 env.close();
1156
1157 env.require(Balance(bob_, btc(50)));
1158
1159 // Bob's offer should cross Alice's AMM
1160 env(offer(bob_, XRP(50), btc(50)));
1161 env.close();
1162
1163 BEAST_EXPECT(ammAlice.expectBalances(btc(1'050), XRP(1'000), ammAlice.tokens()));
1164 BEAST_EXPECT(expectOffers(env, bob_, 0));
1165 env.require(Balance(bob_, btc(0)));
1166 }
1167
1168 void
1170 {
1171 testcase("Missing Auth");
1172
1173 using namespace jtx;
1174
1175 Env env{*this, features};
1176
1177 env.fund(XRP(400'000), gw_, alice_, bob_);
1178 env.close();
1179
1180 MPTTester btc({
1181 .env = env,
1182 .issuer = gw_,
1183 .holders = {alice_, bob_},
1184 .flags = tfMPTRequireAuth | kMptDexFlags,
1185 });
1186
1187 // Alice doesn't have the funds
1188 {
1189 AMM const ammAlice(env, alice_, btc(1'000), XRP(1'000), Ter(tecNO_AUTH));
1190 }
1191
1192 btc.authorize({.holder = bob_});
1193 env(pay(gw_, bob_, btc(50)));
1194 env.close();
1195 env.require(Balance(bob_, btc(50)));
1196
1197 // Alice should not be able to create AMM without authorization.
1198 {
1199 AMM const ammAlice(env, alice_, btc(1'000), XRP(1'000), Ter(tecNO_AUTH));
1200 }
1201
1202 // Finally, authorize alice. Now alice's AMM create should succeed.
1203 btc.authorize({.holder = alice_});
1204 env(pay(gw_, alice_, btc(1'000)));
1205 env.close();
1206
1207 AMM const ammAlice(env, alice_, btc(1'000), XRP(1'050));
1208
1209 // Authorize AMM.
1210 // BTC.authorize({.account = ammAlice.ammAccount()});
1211 // env.close();
1212
1213 // Now bob creates his offer again, which crosses with alice's AMM.
1214 env(offer(bob_, XRP(50), btc(50)));
1215 env.close();
1216
1217 BEAST_EXPECT(ammAlice.expectBalances(btc(1'050), XRP(1'000), ammAlice.tokens()));
1218 BEAST_EXPECT(expectOffers(env, bob_, 0));
1219 env.require(Balance(bob_, btc(0)));
1220 }
1221
1222 void
1249
1250 void
1252 {
1253 testcase("path find consume all");
1254 using namespace jtx;
1255
1256 Env env = pathTestEnv();
1257 env.fund(XRP(100'000'260), alice_);
1258 env.fund(XRP(30'000), gw_, bob_, carol_);
1259
1260 MPTTester const eth({
1261 .env = env,
1262 .issuer = gw_,
1263 .holders = {alice_, bob_, carol_},
1264 .pay = 100'000'000'000'000,
1265 .flags = kMptDexFlags,
1266 });
1267
1268 AMM const ammCarol(env, carol_, XRP(100), eth(100'000'000'000'000));
1269
1270 STPathSet st;
1271 STAmount sa;
1272 STAmount da;
1273 std::tie(st, sa, da) = findPaths(
1274 env, alice_, bob_, bob_["AUD"](-1), std::optional<STAmount>(XRP(100'000'000)));
1275 BEAST_EXPECT(st.empty());
1276 std::tie(st, sa, da) =
1277 findPaths(env, alice_, bob_, eth(-1), std::optional<STAmount>(XRP(100'000'000)));
1278 // Alice sends all requested 100,000,000XRP
1279 BEAST_EXPECT(sa == XRP(100'000'000));
1280 // Bob gets ~99.99e12ETH. This is the amount Bob
1281 // can get out of AMM for 100,000,000XRP.
1282 BEAST_EXPECT(equal(da, eth(99'999'900'000'099)));
1283 }
1284
1285 // carol holds ETH, sells ETH for XRP
1286 // bob will hold ETH
1287 // alice pays bob ETH using XRP
1288 void
1290 {
1291 testcase("via gateway");
1292 using namespace jtx;
1293
1294 Env env = pathTestEnv();
1295 env.fund(XRP(10'000), alice_, bob_, carol_, gw_);
1296 env.close();
1297
1298 MPTTester const eth({
1299 .env = env,
1300 .issuer = gw_,
1301 .holders = {alice_, bob_, carol_},
1302 .transferFee = 10'000,
1303 .flags = kMptDexFlags,
1304 });
1305
1306 MPTTester const btc({
1307 .env = env,
1308 .issuer = gw_,
1309 .holders = {alice_, bob_, carol_},
1310 .transferFee = 10'000,
1311 .flags = kMptDexFlags,
1312 });
1313
1314 env(pay(gw_, carol_, eth(51)));
1315 env.close();
1316 AMM const ammCarol(env, carol_, XRP(40), eth(51));
1317 env(pay(alice_, bob_, eth(10)), Sendmax(XRP(100)), Paths(XRP));
1318 env.close();
1319 // AMM offer is 51.282052XRP/11ETH, 11ETH/1.1 = 10ETH to bob
1320 BEAST_EXPECT(ammCarol.expectBalances(XRP(51), eth(40), ammCarol.tokens()));
1321 env.require(Balance(bob_, eth(10)));
1322
1323 auto const result = findPaths(env, alice_, bob_, btc(25));
1324 BEAST_EXPECT(std::get<0>(result).empty());
1325 }
1326
1327 void
1329 {
1330 testcase("Receive max");
1331 using namespace jtx;
1332 auto const charlie = Account("charlie");
1333 {
1334 // XRP -> MPT receive max
1335 Env env = pathTestEnv();
1336 env.fund(XRP(30'000), alice_, bob_, charlie, gw_);
1337
1338 MPTTester const eth({
1339 .env = env,
1340 .issuer = gw_,
1341 .holders = {alice_, bob_, charlie},
1342 .pay = 11'000'000'000'000,
1343 .flags = kMptDexFlags,
1344 });
1345
1346 AMM const ammCharlie(env, charlie, XRP(10), eth(11'000'000'000'000));
1347 auto [st, sa, da] = findPaths(env, alice_, bob_, eth(-1), XRP(1).value());
1348 BEAST_EXPECT(sa == XRP(1));
1349 BEAST_EXPECT(equal(da, eth(1'000'000'000'000)));
1350 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
1351 {
1352 auto const& pathElem = st[0][0];
1353 BEAST_EXPECT(
1354 pathElem.isOffer() && pathElem.getIssuerID() == gw_.id() &&
1355 pathElem.getMPTID() == eth.issuanceID());
1356 }
1357 }
1358 {
1359 // MPT -> XRP receive max
1360 Env env = pathTestEnv();
1361 env.fund(XRP(30'000), alice_, bob_, charlie, gw_);
1362
1363 MPTTester const eth({
1364 .env = env,
1365 .issuer = gw_,
1366 .holders = {alice_, bob_, charlie},
1367 .pay = 11'000'000'000'000,
1368 .flags = kMptDexFlags,
1369 });
1370
1371 AMM const ammCharlie(env, charlie, XRP(11), eth(10'000'000'000'000));
1372 env.close();
1373 auto [st, sa, da] =
1374 findPaths(env, alice_, bob_, drops(-1), eth(1'000'000'000'000).value());
1375 BEAST_EXPECT(sa == eth(1'000'000'000'000));
1376 BEAST_EXPECT(equal(da, XRP(1)));
1377 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
1378 {
1379 auto const& pathElem = st[0][0];
1380 BEAST_EXPECT(
1381 pathElem.isOffer() && pathElem.getIssuerID() == xrpAccount() &&
1382 pathElem.getCurrency() == xrpCurrency());
1383 }
1384 }
1385 }
1386
1387 void
1389 {
1390 testcase("Path Find: XRP -> XRP and XRP -> MPT");
1391 using namespace jtx;
1392 Env env = pathTestEnv();
1393 Account a1{"A1"};
1394 Account a2{"A2"};
1395 Account a3{"A3"};
1396 Account const g1{"G1"};
1397 Account const g2{"G2"};
1398 Account g3{"G3"};
1399 Account m1{"M1"};
1400
1401 env.fund(XRP(100'000), a1);
1402 env.fund(XRP(10'000), a2);
1403 env.fund(XRP(1'000), a3, g1, g2, g3);
1404 env.fund(XRP(20'000), m1);
1405 env.close();
1406
1407 MPTTester const xyzG1(
1408 {.env = env, .issuer = g1, .holders = {a1, m1, a2}, .flags = kMptDexFlags});
1409
1410 MPTTester const xyzG2(
1411 {.env = env, .issuer = g2, .holders = {a2, m1, a1}, .flags = kMptDexFlags});
1412
1413 MPTTester const abcG3(
1414 {.env = env, .issuer = g3, .holders = {a1, a2, m1, a3}, .flags = kMptDexFlags});
1415
1416 MPTTester const abcA2(
1417 {.env = env, .issuer = a2, .holders = {g3, a1}, .flags = kMptDexFlags});
1418
1419 env(pay(g1, a1, xyzG1(3'500'000'000)));
1420 env(pay(g3, a1, abcG3(1'200'000'000)));
1421 env(pay(g1, m1, xyzG1(25'000'000'000)));
1422 env(pay(g2, m1, xyzG2(25'000'000'000)));
1423 env(pay(g3, m1, abcG3(25'000'000'000)));
1424 env(pay(a2, g3, abcA2(101'000'000)));
1425 env.close();
1426
1427 AMM const ammM1XyzG1XyzG2(env, m1, xyzG1(1'000'000'000), xyzG2(1'000'000'000));
1428 AMM const ammM1XrpAbcG3(env, m1, XRP(10'000), abcG3(1'000'000'000));
1429 AMM const ammG3AbcG3AbcA2(env, g3, abcG3(100'000'000), abcA2(101'000'000));
1430 env.close();
1431
1432 STPathSet st;
1433 STAmount sa, da;
1434
1435 {
1436 auto const& sendAmt = XRP(10);
1437 std::tie(st, sa, da) = findPaths(env, a1, a2, sendAmt, std::nullopt, xrpCurrency());
1438 BEAST_EXPECT(equal(da, sendAmt));
1439 BEAST_EXPECT(st.empty());
1440 }
1441
1442 {
1443 // no path should exist for this since dest account
1444 // does not exist.
1445 auto const& sendAmt = XRP(200);
1446 std::tie(st, sa, da) =
1447 findPaths(env, a1, Account{"A0"}, sendAmt, std::nullopt, xrpCurrency());
1448 BEAST_EXPECT(equal(da, sendAmt));
1449 BEAST_EXPECT(st.empty());
1450 }
1451
1452 {
1453 auto const& sendAmt = abcG3(10'000'000);
1454 std::tie(st, sa, da) = findPaths(env, a2, g3, sendAmt, std::nullopt, xrpCurrency());
1455 BEAST_EXPECT(equal(da, sendAmt));
1456 BEAST_EXPECT(equal(sa, XRPAmount{101'010'102}));
1457 BEAST_EXPECT(same(st, stpath(ipe(MPT(abcG3)))));
1458 }
1459
1460 {
1461 auto const& sendAmt = abcA2(1'000'000);
1462 std::tie(st, sa, da) = findPaths(env, a1, a2, sendAmt, std::nullopt, xrpCurrency());
1463 BEAST_EXPECT(equal(da, sendAmt));
1464 BEAST_EXPECT(equal(sa, XRPAmount{10'010'011}));
1465 BEAST_EXPECT(same(st, stpath(ipe(MPT(abcG3)), ipe(MPT(abcA2)))));
1466 }
1467 }
1468
1469 void
1471 {
1472 testcase("Path Find: non-XRP -> XRP");
1473 using namespace jtx;
1474 Env env = pathTestEnv();
1475 Account a1{"A1"};
1476 Account a2{"A2"};
1477 Account const g3{"G3"};
1478 Account m1{"M1"};
1479
1480 env.fund(XRP(1'000), a1, a2, g3);
1481 env.fund(XRP(11'000), m1);
1482 env.close();
1483
1484 MPTTester const eth({
1485 .env = env,
1486 .issuer = g3,
1487 .holders = {a1, a2, m1},
1488 .pay = 1'000'000'000,
1489 .flags = kMptDexFlags,
1490 });
1491
1492 AMM const ammM1(env, m1, eth(1'000'000'000), XRP(10'010));
1493
1494 STPathSet st;
1495 STAmount sa, da;
1496
1497 auto const& sendAmt = XRP(10);
1498
1499 std::tie(st, sa, da) =
1500 findPathsByElement(env, a1, a2, sendAmt, std::nullopt, ipe(MPT(eth)));
1501 BEAST_EXPECT(equal(da, sendAmt));
1502 BEAST_EXPECT(equal(sa, eth(1'000'000)));
1503 BEAST_EXPECT(same(st, stpath(ipe(xrpIssue()))));
1504 }
1505
1506 void
1508 {
1509 testcase("Path Find: non-XRP -> non-XRP, same issuanceID");
1510 using namespace jtx;
1511 {
1512 Env env = pathTestEnv();
1513 Account a1{"A1"};
1514 Account a2{"A2"};
1515 Account const a3{"A3"};
1516 Account const g1{"G1"};
1517 Account const g2{"G2"};
1518 Account m1{"M1"};
1519
1520 env.fund(XRP(11'000), m1);
1521 env.fund(XRP(1'000), a1, a2, a3, g1, g2);
1522 env.close();
1523
1524 MPTTester const hkdG1({
1525 .env = env,
1526 .issuer = g1,
1527 .holders = {a1, m1},
1528 .pay = 5'000'000'000,
1529 .flags = kMptDexFlags,
1530 });
1531
1532 MPTTester const hkdG2({
1533 .env = env,
1534 .issuer = g2,
1535 .holders = {a2, m1},
1536 .pay = 5'000'000'000,
1537 .flags = kMptDexFlags,
1538 });
1539
1540 AMM const ammM1(env, m1, hkdG1(1'000'000'000), hkdG2(1'010'000'000));
1541
1542 auto const& sendAmt = hkdG2(10'000'000);
1543 STPathSet st;
1544 STAmount sa, da;
1545 std::tie(st, sa, da) = jtx::findPaths(
1546 env, g1, a2, sendAmt, std::nullopt, hkdG1.issuanceID(), std::nullopt, std::nullopt);
1547 BEAST_EXPECT(equal(da, sendAmt));
1548 BEAST_EXPECT(equal(sa, hkdG1(10'000'000)));
1549 BEAST_EXPECT(same(st, stpath(ipe(MPT(hkdG2)))));
1550 }
1551 }
1552
1553 void
1555 {
1556 testcase("Path Find: MPT AMM source amount is executable");
1557 using namespace jtx;
1558
1559 auto const checkQuote = [&](std::int64_t usdPool,
1560 std::int64_t eurPool,
1561 std::int64_t deliverAmount,
1562 std::int64_t expectedSourceAmount) {
1563 Env env = pathTestEnv();
1564 env.fund(XRP(30'000), gw_, alice_, bob_, carol_);
1565 env.close();
1566
1567 MPTTester const usd({
1568 .env = env,
1569 .issuer = gw_,
1570 .holders = {alice_, bob_, carol_},
1571 .pay = usdPool,
1572 .flags = kMptDexFlags,
1573 });
1574
1575 MPTTester const eur({
1576 .env = env,
1577 .issuer = gw_,
1578 .holders = {alice_, bob_, carol_},
1579 .pay = eurPool,
1580 .flags = kMptDexFlags,
1581 });
1582
1583 AMM const ammCarol(env, carol_, usd(usdPool), eur(eurPool));
1584 env.close();
1585
1586 STPathSet st;
1587 STAmount sa, da;
1588 auto const deliver = eur(deliverAmount);
1589 std::tie(st, sa, da) = findPaths(
1590 env,
1591 alice_,
1592 bob_,
1593 deliver,
1594 std::nullopt,
1595 usd.issuanceID(),
1596 std::nullopt,
1597 std::nullopt);
1598
1599 // Each quote must execute when used as an exact-output SendMax.
1600 BEAST_EXPECT(equal(da, deliver));
1601 BEAST_EXPECT(equal(sa, usd(expectedSourceAmount)));
1602 BEAST_EXPECT(!st.empty());
1603
1604 auto const before = eur.getBalance(bob_);
1605 env(pay(alice_, bob_, deliver),
1606 Json(jss::Paths, st.getJson(JsonOptions::Values::None)),
1607 Sendmax(sa),
1608 Txflags(tfNoRippleDirect));
1609 BEAST_EXPECT(eur.getBalance(bob_) == before + deliverAmount);
1610 };
1611
1612 struct TestCase
1613 {
1614 std::int64_t usdPool;
1615 std::int64_t eurPool;
1616 std::int64_t deliverAmount;
1617 std::int64_t expectedSourceAmount;
1618 };
1619
1620 // Cover the original 2:1 pool and the same pool scaled down by 1000.
1621 // clang-format off
1622 TestCase const testCases[] = {
1623 {.usdPool = 2'000'000, .eurPool = 1'000'000, .deliverAmount = 1, .expectedSourceAmount = 3},
1624 {.usdPool = 2'000'000, .eurPool = 1'000'000, .deliverAmount = 2, .expectedSourceAmount = 5},
1625 {.usdPool = 2'000'000, .eurPool = 1'000'000, .deliverAmount = 10, .expectedSourceAmount = 21},
1626 {.usdPool = 2'000'000, .eurPool = 1'000'000, .deliverAmount = 100, .expectedSourceAmount = 201},
1627 {.usdPool = 2'000'000, .eurPool = 1'000'000, .deliverAmount = 1'000, .expectedSourceAmount = 2'003},
1628 {.usdPool = 2'000, .eurPool = 1'000, .deliverAmount = 1, .expectedSourceAmount = 3},
1629 {.usdPool = 2'000, .eurPool = 1'000, .deliverAmount = 2, .expectedSourceAmount = 5},
1630 {.usdPool = 2'000, .eurPool = 1'000, .deliverAmount = 10, .expectedSourceAmount = 21},
1631 {.usdPool = 2'000, .eurPool = 1'000, .deliverAmount = 100, .expectedSourceAmount = 223},
1632 };
1633 // clang-format on
1634
1635 for (auto const& testCase : testCases)
1636 {
1637 checkQuote(
1638 testCase.usdPool,
1639 testCase.eurPool,
1640 testCase.deliverAmount,
1641 testCase.expectedSourceAmount);
1642 }
1643 }
1644
1645 void
1647 {
1648 testcase("falseDryChanges");
1649
1650 using namespace jtx;
1651
1652 Env env(*this, features);
1653 env.memoize(bob_);
1654
1655 env.fund(XRP(10'000), alice_, gw_);
1656 fund(env, gw_, {carol_}, XRP(10'000), {}, Fund::Acct);
1657 auto const ammxrpPool = env.current()->fees().increment * 2;
1658 env.fund(reserve(env, 5) + ammCrtFee(env) + ammxrpPool, bob_);
1659 env.close();
1660
1661 MPTTester const eth(
1662 {.env = env, .issuer = gw_, .holders = {alice_, bob_, carol_}, .flags = kMptDexFlags});
1663
1664 MPTTester const btc(
1665 {.env = env, .issuer = gw_, .holders = {alice_, bob_, carol_}, .flags = kMptDexFlags});
1666
1667 env(pay(gw_, alice_, eth(50'000)));
1668 env(pay(gw_, bob_, btc(150'000)));
1669
1670 // Bob has _just_ slightly less than 50 xrp available
1671 // If his owner count changes, he will have more liquidity.
1672 // This is one error case to test (when Flow is used).
1673 // Computing the incoming xrp to the XRP/BTC offer will require two
1674 // recursive calls to the ETH/XRP offer. The second call will return
1675 // tecPATH_DRY, but the entire path should not be marked as dry.
1676 // This is the second error case to test (when flowV1 is used).
1677 env(offer(bob_, eth(50'000), XRP(50)));
1678 AMM const ammBob(env, bob_, ammxrpPool, btc(150'000));
1679
1680 env(pay(alice_, carol_, btc(1'000'000'000)),
1681 Path(~XRP, ~MPT(btc)),
1682 Sendmax(eth(500'000)),
1683 Txflags(tfNoRippleDirect | tfPartialPayment));
1684
1685 auto const carolBTC = env.balance(carol_, MPT(btc));
1686 BEAST_EXPECT(carolBTC > btc(0) && carolBTC < btc(50'000));
1687 }
1688
1689 void
1691 {
1692 testcase("Book Step");
1693
1694 using namespace jtx;
1695
1696 // simple MPT/IOU mix offer
1697 {
1698 auto const test = [&](auto&& issue1, auto&& issue2) {
1699 Env env(*this);
1700 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
1701 env.close();
1702 auto const eth = issue1({
1703 .env = env,
1704 .token = "ETH",
1705 .issuer = gw_,
1706 .holders = {alice_, bob_, carol_},
1707 .limit = 100'000'000,
1708 });
1709 auto const btc = issue2({
1710 .env = env,
1711 .token = "BTC",
1712 .issuer = gw_,
1713 .holders = {alice_, bob_, carol_},
1714 .limit = 100'000'000,
1715 });
1716 env(pay(gw_, alice_, btc(500000)));
1717 env(pay(gw_, bob_, btc(500000)));
1718 env(pay(gw_, carol_, btc(500000)));
1719 env(pay(gw_, alice_, eth(500000)));
1720 env(pay(gw_, bob_, eth(500000)));
1721 env(pay(gw_, carol_, eth(500000)));
1722 env.close();
1723 AMM const ammBob(env, bob_, btc(100'000), eth(150'000));
1724
1725 env(pay(alice_, carol_, eth(50'000)), Path(~eth), Sendmax(btc(50'000)));
1726
1727 env.require(Balance(alice_, btc(450'000)));
1728 env.require(Balance(bob_, btc(400'000)));
1729 env.require(Balance(bob_, eth(350'000)));
1730 env.require(Balance(carol_, eth(550'000)));
1731 BEAST_EXPECT(ammBob.expectBalances(btc(150'000), eth(100'000), ammBob.tokens()));
1732 };
1734 }
1735
1736 {
1737 // simple MPT/XRP XRP/MPT offer
1738 Env env(*this, features);
1739 env.fund(XRP(10'000), gw_, alice_, bob_, carol_);
1740
1741 MPTTester const btc({
1742 .env = env,
1743 .issuer = gw_,
1744 .holders = {alice_, bob_, carol_},
1745 .pay = 100'000,
1746 .flags = kMptDexFlags,
1747 });
1748
1749 MPTTester const eth({
1750 .env = env,
1751 .issuer = gw_,
1752 .holders = {alice_, bob_, carol_},
1753 .pay = 150'000,
1754 .flags = kMptDexFlags,
1755 });
1756
1757 AMM const ammBobBtcXrp(env, bob_, btc(100'000), XRP(150));
1758 AMM const ammBobXrpEth(env, bob_, XRP(100), eth(150'000));
1759
1760 env(pay(alice_, carol_, eth(50'000)), Path(~XRP, ~MPT(eth)), Sendmax(btc(50'000)));
1761
1762 env.require(Balance(alice_, btc(50'000)));
1763 env.require(Balance(bob_, btc(0)));
1764 env.require(Balance(bob_, eth(0)));
1765 env.require(Balance(carol_, eth(200'000)));
1766 BEAST_EXPECT(
1767 ammBobBtcXrp.expectBalances(btc(150'000), XRP(100), ammBobBtcXrp.tokens()));
1768 BEAST_EXPECT(
1769 ammBobXrpEth.expectBalances(XRP(150), eth(100'000), ammBobXrpEth.tokens()));
1770 }
1771 {
1772 // simple XRP -> MPT through offer and sendmax
1773 Env env(*this, features);
1774 XRPAmount const baseFee{env.current()->fees().base};
1775 env.fund(XRP(10'000), gw_, alice_, bob_, carol_);
1776
1777 MPTTester const eth({
1778 .env = env,
1779 .issuer = gw_,
1780 .holders = {alice_, bob_, carol_},
1781 .pay = 150'000,
1782 .flags = kMptDexFlags,
1783 });
1784
1785 AMM const ammBob(env, bob_, XRP(100), eth(150'000));
1786
1787 env(pay(alice_, carol_, eth(50'000)), Path(~MPT(eth)), Sendmax(XRP(50)));
1788 BEAST_EXPECT(expectLedgerEntryRoot(env, alice_, XRP(10'000) - XRP(50) - 2 * baseFee));
1789 BEAST_EXPECT(expectLedgerEntryRoot(
1790 env, bob_, XRP(10'000) - XRP(100) - ammCrtFee(env) - baseFee));
1791 env.require(Balance(bob_, eth(0)));
1792 env.require(Balance(carol_, eth(200'000)));
1793 BEAST_EXPECT(ammBob.expectBalances(XRP(150), eth(100'000), ammBob.tokens()));
1794 }
1795 {
1796 // simple MPT -> XRP through offer and sendmax
1797 Env env(*this, features);
1798 XRPAmount const baseFee{env.current()->fees().base};
1799 env.fund(XRP(10'000), gw_, alice_, bob_, carol_);
1800
1801 MPTTester const eth({
1802 .env = env,
1803 .issuer = gw_,
1804 .holders = {alice_, bob_, carol_},
1805 .pay = 100'000,
1806 .flags = kMptDexFlags,
1807 });
1808
1809 AMM const ammBob(env, bob_, eth(100'000), XRP(150));
1810
1811 env(pay(alice_, carol_, XRP(50)), Path(~XRP), Sendmax(eth(50'000)));
1812
1813 env.require(Balance(alice_, eth(50'000)));
1814 BEAST_EXPECT(expectLedgerEntryRoot(
1815 env, bob_, XRP(10'000) - XRP(150) - ammCrtFee(env) - baseFee));
1816 env.require(Balance(bob_, eth(0)));
1817 BEAST_EXPECT(expectLedgerEntryRoot(env, carol_, XRP(10'000 + 50) - baseFee));
1818 BEAST_EXPECT(ammBob.expectBalances(eth(150'000), XRP(100), ammBob.tokens()));
1819 }
1820
1821 // test unfunded offers are removed when payment succeeds
1822 {
1823 auto const test = [&](auto&& issue1, auto&& issue2, auto&& issue3) {
1824 Env env(*this, features);
1825 env.fund(XRP(10'000), alice_, bob_, carol_, gw_);
1826 env.close();
1827 auto const btc = issue1({
1828 .env = env,
1829 .token = "BTC",
1830 .issuer = gw_,
1831 .holders = {alice_, bob_, carol_},
1832 .limit = 1000'000000,
1833 });
1834 auto const eth = issue2({
1835 .env = env,
1836 .token = "ETH",
1837 .issuer = gw_,
1838 .holders = {alice_, bob_, carol_},
1839 .limit = 1000'000000,
1840 });
1841 auto const gbp = issue3({
1842 .env = env,
1843 .token = "GBP",
1844 .issuer = gw_,
1845 .holders = {alice_, bob_, carol_},
1846 .limit = 1000'000000,
1847 });
1848
1849 env(pay(gw_, alice_, btc(60'000)));
1850 env(pay(gw_, bob_, eth(200'000)));
1851 env(pay(gw_, bob_, gbp(150'000)));
1852 env(offer(bob_, btc(50'000), eth(50'000)));
1853 env(offer(bob_, btc(40'000), gbp(50'000)));
1854 env.close();
1855 AMM const ammBob(env, bob_, gbp(100'000), eth(150'000));
1856
1857 // unfund offer
1858 env(pay(bob_, gw_, gbp(50'000)));
1859 BEAST_EXPECT(isOffer(env, bob_, btc(50'000), eth(50'000)));
1860 BEAST_EXPECT(isOffer(env, bob_, btc(40'000), gbp(50'000)));
1861 env(pay(alice_, carol_, eth(50'000)),
1862 Path(~eth),
1863 Path(~gbp, ~eth),
1864 Sendmax(btc(60'000)));
1865 env.require(Balance(alice_, btc(10'000)));
1866 env.require(Balance(bob_, btc(50'000)));
1867 env.require(Balance(bob_, eth(0)));
1868 env.require(Balance(bob_, gbp(0)));
1869 env.require(Balance(carol_, eth(50'000)));
1870 // used in the payment
1871 BEAST_EXPECT(!isOffer(env, bob_, btc(50'000), eth(50'000)));
1872 // found unfunded
1873 BEAST_EXPECT(!isOffer(env, bob_, btc(40'000), gbp(50'000)));
1874 // unchanged
1875 BEAST_EXPECT(ammBob.expectBalances(gbp(100'000), eth(150'000), ammBob.tokens()));
1876 };
1878 }
1879
1880 {
1881 // test unfunded offers are removed when the payment fails.
1882 // bob makes two offers: a funded 50'000'000 ETH for 50'000'000 BTC
1883 // and an unfunded 50'000'000 GBP for 60'000'000 BTC. alice pays
1884 // carol 61'000'000 ETH with 61'000'000 BTC. alice only has
1885 // 60'000'000 BTC, so the payment will fail. The payment uses two
1886 // paths: one through bob's funded offer and one through his
1887 // unfunded offer. When the payment fails `flow` should return the
1888 // unfunded offer. This test is intentionally similar to the one
1889 // that removes unfunded offers when the payment succeeds.
1890 Env env(*this, features);
1891
1892 env.fund(XRP(10'000), bob_, carol_, gw_);
1893 env.close();
1894 // Sets rippling on, this is different from
1895 // the original test
1896 fund(env, gw_, {alice_}, XRP(10'000), {}, Fund::Acct);
1897
1898 MPTTester btc({
1899 .env = env,
1900 .issuer = gw_,
1901 .holders = {alice_, bob_, carol_},
1902 .flags = kMptDexFlags,
1903 });
1904
1905 MPTTester eth({
1906 .env = env,
1907 .issuer = gw_,
1908 .holders = {alice_, bob_, carol_},
1909 .flags = kMptDexFlags,
1910 });
1911
1912 MPTTester gbp({
1913 .env = env,
1914 .issuer = gw_,
1915 .holders = {alice_, bob_, carol_},
1916 .flags = kMptDexFlags,
1917 });
1918
1919 env(pay(gw_, alice_, btc(60'000'000)));
1920 env(pay(gw_, bob_, btc(100'000'000)));
1921 env(pay(gw_, bob_, eth(100'000'000)));
1922 env(pay(gw_, bob_, gbp(50'000'000)));
1923 env(pay(gw_, carol_, gbp(1'000'000)));
1924 env.close();
1925
1926 // This is multiplath, which generates limited # of offers
1927 AMM const ammBobBtcEth(env, bob_, btc(50'000'000), eth(50'000'000));
1928 env(offer(bob_, btc(60'000'000), gbp(50'000'000)));
1929 env(offer(carol_, btc(1'000'000'000), gbp(1'000'000)));
1930 env(offer(bob_, gbp(50'000'000), eth(50'000'000)));
1931
1932 // unfund offer
1933 env(pay(bob_, gw_, gbp(50'000'000)));
1934 BEAST_EXPECT(ammBobBtcEth.expectBalances(
1935 btc(50'000'000), eth(50'000'000), ammBobBtcEth.tokens()));
1936 BEAST_EXPECT(isOffer(env, bob_, btc(60'000'000), gbp(50'000'000)));
1937 BEAST_EXPECT(isOffer(env, carol_, btc(1'000'000'000), gbp(1'000'000)));
1938 BEAST_EXPECT(isOffer(env, bob_, gbp(50'000'000), eth(50'000'000)));
1939
1940 auto flowJournal = env.app().getLogs().journal("Flow");
1941 auto const flowResult = [&] {
1942 STAmount const deliver(eth(51'000'000));
1943 STAmount smax(btc(61'000'000));
1944 PaymentSandbox sb(env.current().get(), TapNone);
1945 STPathSet paths;
1946 auto const ipe = [](MPTTester const& iss) {
1947 return STPathElement(
1949 xrpAccount(),
1950 PathAsset{iss.issuanceID()},
1951 iss.issuer());
1952 };
1953 {
1954 // BTC -> ETH
1955 STPath const p1({ipe(eth)});
1956 paths.pushBack(p1);
1957 // BTC -> GBP -> ETH
1958 STPath const p2({ipe(gbp), ipe(eth)});
1959 paths.pushBack(p2);
1960 }
1961
1962 return flow(
1963 sb,
1964 deliver,
1965 alice_,
1966 carol_,
1967 paths,
1968 false,
1969 false,
1970 true,
1972 std::nullopt,
1973 smax,
1974 std::nullopt,
1975 flowJournal);
1976 }();
1977
1978 BEAST_EXPECT(flowResult.removableOffers.size() == 1);
1979 env.app().getOpenLedger().modify([&](OpenView& view, beast::Journal j) {
1980 if (flowResult.removableOffers.empty())
1981 return false;
1982 Sandbox sb(&view, TapNone);
1983 for (auto const& o : flowResult.removableOffers)
1984 {
1985 if (auto const ok = sb.peek(keylet::offer(o)))
1986 {
1987 offerDelete(sb, ok, flowJournal);
1988 }
1989 }
1990 sb.apply(view);
1991 return true;
1992 });
1993
1994 // used in payment, but since payment failed should be untouched
1995 BEAST_EXPECT(ammBobBtcEth.expectBalances(
1996 btc(50'000'000), eth(50'000'000), ammBobBtcEth.tokens()));
1997 BEAST_EXPECT(isOffer(env, carol_, btc(1'000'000'000), gbp(1'000'000)));
1998 // found unfunded
1999 BEAST_EXPECT(!isOffer(env, bob_, btc(60'000'000), gbp(50'000'000)));
2000 }
2001 {
2002 // Do not produce more in the forward pass than the reverse pass
2003 // This test uses a path that whose reverse pass will compute a
2004 // 500 ETH input required for a 1'000 BTC output. It sets a sendmax
2005 // of 400 ETH, so the payment engine will need to do a forward
2006 // pass. Without limits, the 400 ETH would produce 1'000 BTC in
2007 // the forward pass. This test checks that the payment produces
2008 // 1'000 BTC, as expected.
2009
2010 auto const test = [&](auto&& issue1, auto&& issue2) {
2011 Env env(*this);
2012 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
2013 env.close();
2014 auto const eth = issue1({
2015 .env = env,
2016 .token = "ETH",
2017 .issuer = gw_,
2018 .holders = {alice_, bob_, carol_},
2019 .limit = 10'000'000,
2020 });
2021 auto const btc = issue2({
2022 .env = env,
2023 .token = "BTC",
2024 .issuer = gw_,
2025 .holders = {alice_, bob_, carol_},
2026 .limit = 10'000'000,
2027 });
2028
2029 env(pay(gw_, alice_, eth(1'000'000)));
2030 env(pay(gw_, bob_, btc(1'000'000)));
2031 env(pay(gw_, bob_, eth(1'000'000)));
2032 env.close();
2033
2034 AMM const ammBob(env, bob_, eth(8'000), XRPAmount{21});
2035 env(offer(bob_, drops(1), btc(1'000'000)), Txflags(tfPassive));
2036
2037 env(pay(alice_, carol_, btc(1'000)),
2038 Path(~XRP, ~btc),
2039 Sendmax(eth(400)),
2040 Txflags(tfNoRippleDirect | tfPartialPayment));
2041
2042 env.require(Balance(carol_, btc(1'000)));
2043 BEAST_EXPECT(ammBob.expectBalances(eth(8400), XRPAmount{20}, ammBob.tokens()));
2044 };
2046 }
2047 }
2048
2049 void
2051 {
2052 testcase("No Owner Fee");
2053 using namespace jtx;
2054
2055 {
2056 // payment via AMM
2057 Env env(*this, features);
2058 env.fund(XRP(1'000), gw_, alice_, bob_, carol_);
2059
2060 MPTTester const gbp({
2061 .env = env,
2062 .issuer = gw_,
2063 .holders = {alice_, bob_, carol_},
2064 .transferFee = 25'000,
2065 .pay = 1'000'000'000'000'000,
2066 .flags = kMptDexFlags,
2067 });
2068
2069 MPTTester const btc({
2070 .env = env,
2071 .issuer = gw_,
2072 .holders = {alice_, bob_, carol_},
2073 .transferFee = 25'000,
2074 .pay = 1'000'000'000'000'000,
2075 .flags = kMptDexFlags,
2076 });
2077
2078 AMM const amm(env, bob_, gbp(1'000'000'000'000'000), btc(1'000'000'000'000'000));
2079
2080 env(pay(alice_, carol_, btc(100'000'000'000'000)),
2081 Path(~MPT(btc)),
2082 Sendmax(gbp(150'000'000'000'000)),
2083 Txflags(tfNoRippleDirect | tfPartialPayment));
2084 env.close();
2085
2086 // alice buys 107.1428e12BTC with 120e12GBP and pays 25% tr fee on
2087 // 120e12GBP 1,000e12 - 120e12*1.25 = 850e12GBP
2088 env.require(Balance(alice_, gbp(850'000'000'000'000)));
2089
2090 BEAST_EXPECT(amm.expectBalances(
2091 gbp(1'120'000'000'000'000), btc(892'857'142'857'143), amm.tokens()));
2092
2093 // 25% of 85.7142e12BTC is paid in tr fee
2094 // 85.7142e12*1.25 = 107.1428e12BTC
2095 env.require(Balance(carol_, btc(1'085'714'285'714'285)));
2096 }
2097 {
2098 // Payment via offer and AMM
2099 Env env(*this, features);
2100 Account const ed("ed");
2101
2102 env.fund(XRP(1'000), gw_, alice_, bob_, carol_, ed);
2103
2104 MPTTester const gbp({
2105 .env = env,
2106 .issuer = gw_,
2107 .holders = {alice_, bob_, carol_, ed},
2108 .transferFee = 25'000,
2109 .pay = 1'000'000'000'000'000,
2110 .flags = kMptDexFlags,
2111 });
2112
2113 MPTTester const btc({
2114 .env = env,
2115 .issuer = gw_,
2116 .holders = {alice_, bob_, carol_, ed},
2117 .transferFee = 25'000,
2118 .pay = 1'000'000'000'000'000,
2119 .flags = kMptDexFlags,
2120 });
2121
2122 MPTTester const eth({
2123 .env = env,
2124 .issuer = gw_,
2125 .holders = {alice_, bob_, carol_, ed},
2126 .transferFee = 25'000,
2127 .pay = 1'000'000'000'000'000,
2128 .flags = kMptDexFlags,
2129 });
2130
2131 env(offer(ed, gbp(1'000'000'000'000'000), eth(1'000'000'000'000'000)),
2132 Txflags(tfPassive));
2133 env.close();
2134
2135 AMM const amm(env, bob_, eth(1'000'000'000'000'000), btc(1'000'000'000'000'000));
2136
2137 env(pay(alice_, carol_, btc(100'000'000'000'000)),
2138 Path(~MPT(eth), ~MPT(btc)),
2139 Sendmax(gbp(150'000'000'000'000)),
2140 Txflags(tfNoRippleDirect | tfPartialPayment));
2141 env.close();
2142
2143 // alice buys 120e12ETH with 120e12GBP via the offer
2144 // and pays 25% tr fee on 120e12GBP
2145 // 1,000e12 - 120e12*1.25 = 850e12GBP
2146 env.require(Balance(alice_, gbp(850'000'000'000'000)));
2147 // consumed offer is 120e12GBP/120e12ETH
2148 // ed doesn't pay tr fee
2149 env.require(Balance(ed, eth(880'000'000'000'000)));
2150 env.require(Balance(ed, gbp(1'120'000'000'000'000)));
2151 BEAST_EXPECT(expectOffers(
2152 env, ed, 1, {Amounts{gbp(880'000'000'000'000), eth(880'000'000'000'000)}}));
2153 // 25% on 96e12ETH is paid in tr fee 96e12*1.25 = 120e12ETH
2154 // 96e12ETH is swapped in for 87.5912e12BTC
2155 BEAST_EXPECT(amm.expectBalances(
2156 eth(1'096'000'000'000'000), btc(912'408'759'124'088), amm.tokens()));
2157 // 25% on 70.0729e12BTC is paid in tr fee 70.0729e12*1.25
2158 // = 87.5912e12BTC
2159 env.require(Balance(carol_, btc(1'070'072'992'700'729)));
2160 }
2161 {
2162 // Payment via AMM, AMM
2163 Env env(*this, features);
2164 Account const ed("ed");
2165
2166 env.fund(XRP(1'000), gw_, alice_, bob_, carol_, ed);
2167
2168 MPTTester const gbp({
2169 .env = env,
2170 .issuer = gw_,
2171 .holders = {alice_, bob_, carol_, ed},
2172 .transferFee = 25'000,
2173 .pay = 1'000'000'000'000'000,
2174 .flags = kMptDexFlags,
2175 });
2176
2177 MPTTester const btc({
2178 .env = env,
2179 .issuer = gw_,
2180 .holders = {alice_, bob_, carol_, ed},
2181 .transferFee = 25'000,
2182 .pay = 1'000'000'000'000'000,
2183 .flags = kMptDexFlags,
2184 });
2185
2186 MPTTester const eth({
2187 .env = env,
2188 .issuer = gw_,
2189 .holders = {alice_, bob_, carol_, ed},
2190 .transferFee = 25'000,
2191 .pay = 1'000'000'000'000'000,
2192 .flags = kMptDexFlags,
2193 });
2194
2195 AMM const amm1(env, bob_, gbp(1'000'000'000'000'000), eth(1'000'000'000'000'000));
2196 AMM const amm2(env, ed, eth(1'000'000'000'000'000), btc(1'000'000'000'000'000));
2197
2198 env(pay(alice_, carol_, btc(100'000'000'000'000)),
2199 Path(~MPT(eth), ~MPT(btc)),
2200 Sendmax(gbp(150'000'000'000'000)),
2201 Txflags(tfNoRippleDirect | tfPartialPayment));
2202 env.close();
2203
2204 env.require(Balance(alice_, gbp(850'000'000'000'000)));
2205
2206 // alice buys 107.1428e12ETH with 120e12GBP and pays 25% tr fee on
2207 // 120e12GBP 1,000e12 - 120e12*1.25 = 850e12GBP 120e12GBP is swapped
2208 // in for 107.1428e12ETH
2209 BEAST_EXPECT(amm1.expectBalances(
2210 gbp(1'120'000'000'000'000), eth(892'857'142'857'143), amm1.tokens()));
2211 // 25% on 85.7142e12ETH is paid in tr fee 85.7142e12*1.25 =
2212 // 107.1428e12ETH 85.7142e12ETH is swapped in for 78.9473e12BTC
2213 BEAST_EXPECT(amm2.expectBalances(
2214 eth(1'085'714'285'714'285), btc(921'052'631'578'948), amm2.tokens()));
2215
2216 // 25% on 63.1578e12BTC is paid in tr fee 63.1578e12*1.25
2217 // = 78.9473e12BTC
2218 env.require(Balance(carol_, btc(1'063'157'894'736'841)));
2219 }
2220 {
2221 // AMM offer crossing
2222 Env env(*this, features);
2223
2224 env.fund(XRP(1'000), gw_, alice_, bob_);
2225
2226 MPTTester const btc({
2227 .env = env,
2228 .issuer = gw_,
2229 .holders = {alice_, bob_},
2230 .transferFee = 25'000,
2231 .pay = 1'100'000,
2232 .flags = kMptDexFlags,
2233 });
2234
2235 MPTTester const eth({
2236 .env = env,
2237 .issuer = gw_,
2238 .holders = {alice_, bob_},
2239 .transferFee = 25'000,
2240 .pay = 1'100'000,
2241 .flags = kMptDexFlags,
2242 });
2243
2244 AMM const amm(env, bob_, btc(1'000'000), eth(1'100'000));
2245 env(offer(alice_, eth(100'000), btc(100'000)));
2246 env.close();
2247
2248 // 100e3BTC is swapped in for 100e3ETH
2249 BEAST_EXPECT(amm.expectBalances(btc(1'100'000), eth(1'000'000), amm.tokens()));
2250 // alice pays 25% tr fee on 100e3BTC 1100e3-100e3*1.25 = 975e3BTC
2251 env.require(Balance(alice_, btc(975'000)));
2252 env.require(Balance(alice_, eth(1'200'000)));
2253 BEAST_EXPECT(expectOffers(env, alice_, 0));
2254 }
2255 {
2256 // Payment via AMM with limit quality
2257 Env env(*this, features);
2258
2259 env.fund(XRP(1'000), gw_, alice_, bob_, carol_);
2260
2261 MPTTester const btc({
2262 .env = env,
2263 .issuer = gw_,
2264 .holders = {alice_, bob_, carol_},
2265 .transferFee = 25'000,
2266 .pay = 1'000'000'000'000'000,
2267 .flags = kMptDexFlags,
2268 });
2269
2270 MPTTester const gbp({
2271 .env = env,
2272 .issuer = gw_,
2273 .holders = {alice_, bob_, carol_},
2274 .transferFee = 25'000,
2275 .pay = 1'000'000'000'000'000,
2276 .flags = kMptDexFlags,
2277 });
2278
2279 AMM const amm(env, bob_, gbp(1'000'000'000'000'000), btc(1'000'000'000'000'000));
2280
2281 // requested quality limit is 100e12BTC/178.58e12GBP = 0.55997
2282 // trade quality is 100e12BTC/178.5714 = 0.55999e12
2283 env(pay(alice_, carol_, btc(100'000'000'000'000)),
2284 Path(~MPT(btc)),
2285 Sendmax(gbp(178'580'000'000'000)),
2286 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2287 env.close();
2288
2289 // alice buys 125e12BTC with 142.8571e12GBP and pays 25% tr fee
2290 // on 142.8571e12GBP
2291 // 1,000e12 - 142.8571e12*1.25 = 821.4285e12GBP
2292 env.require(Balance(alice_, gbp(821'428'571'428'571)));
2293 // 142.8571e12GBP is swapped in for 125e12BTC
2294 BEAST_EXPECT(amm.expectBalances(
2295 gbp(1'142'857'142'857'143), btc(875'000'000'000'000), amm.tokens()));
2296 // 25% on 100e12BTC is paid in tr fee
2297 // 100e12*1.25 = 125e12BTC
2298 env.require(Balance(carol_, btc(1'100'000'000'000'000)));
2299 }
2300 {
2301 // Payment via AMM with limit quality, deliver less
2302 // than requested
2303 Env env(*this, features);
2304
2305 env.fund(XRP(1'000), gw_, alice_, bob_, carol_);
2306
2307 MPTTester const btc({
2308 .env = env,
2309 .issuer = gw_,
2310 .holders = {alice_, bob_, carol_},
2311 .transferFee = 25'000,
2312 .pay = 1'200'000'000'000'000,
2313 .flags = kMptDexFlags,
2314 });
2315
2316 MPTTester const gbp({
2317 .env = env,
2318 .issuer = gw_,
2319 .holders = {alice_, bob_, carol_},
2320 .transferFee = 25'000,
2321 .pay = 1'200'000'000'000'000,
2322 .flags = kMptDexFlags,
2323 });
2324
2325 AMM const amm(env, bob_, gbp(1'000'000'000'000'000), btc(1'200'000'000'000'000));
2326
2327 // requested quality limit is 90e12BTC/120e12GBP = 0.75
2328 // trade quality is 22.5e12BTC/30e12GBP = 0.75
2329 env(pay(alice_, carol_, btc(90'000'000'000'000)),
2330 Path(~MPT(btc)),
2331 Sendmax(gbp(120'000'000'000'000)),
2332 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2333 env.close();
2334
2335 // alice buys 28.125e12BTC with 24e12GBP and pays 25% tr fee
2336 // on 24e12GBP
2337 // 1,200e12 - 24e12*1.25 =~ 1,170e12GBP
2338 env.require(Balance(alice_, gbp(1'170'000'000'000'000)));
2339 // 24e12GBP is swapped in for 28.125e12BTC, less one unit: the
2340 // limit quality 120/90 is rounded to nearest, so a trade at
2341 // exactly 4/3 is just over the limit
2342 BEAST_EXPECT(amm.expectBalances(
2343 gbp(1'024'000'000'000'000), btc(1'171'875'000'000'001), amm.tokens()));
2344
2345 // 25% on 22.5e12BTC is paid in tr fee
2346 // 22.5*1.25 = 28.125e12BTC
2347 env.require(Balance(carol_, btc(1'222'499'999'999'999)));
2348 }
2349 {
2350 // Payment via offer and AMM with limit quality, deliver less
2351 // than requested
2352 Env env(*this, features);
2353 Account const ed("ed");
2354
2355 env.fund(XRP(1'000), gw_, alice_, bob_, carol_, ed);
2356
2357 MPTTester const btc({
2358 .env = env,
2359 .issuer = gw_,
2360 .holders = {alice_, bob_, carol_, ed},
2361 .transferFee = 25'000,
2362 .pay = 1'400'000'000'000'000,
2363 .flags = kMptDexFlags,
2364 });
2365
2366 MPTTester const gbp({
2367 .env = env,
2368 .issuer = gw_,
2369 .holders = {alice_, bob_, carol_, ed},
2370 .transferFee = 25'000,
2371 .pay = 1'400'000'000'000'000,
2372 .flags = kMptDexFlags,
2373 });
2374
2375 MPTTester const eth({
2376 .env = env,
2377 .issuer = gw_,
2378 .holders = {alice_, bob_, carol_, ed},
2379 .transferFee = 25'000,
2380 .pay = 1'400'000'000'000'000,
2381 .flags = kMptDexFlags,
2382 });
2383
2384 env(offer(ed, gbp(1'000'000'000'000'000), eth(1'000'000'000'000'000)),
2385 Txflags(tfPassive));
2386 env.close();
2387
2388 AMM const amm(env, bob_, eth(1'000'000'000'000'000), btc(1'400'000'000'000'000));
2389
2390 // requested quality limit is 95e12BTC/140e12GBP = 0.6785
2391 // trade quality is 59.7321e12BTC/88.0262e12GBP = 0.6785
2392 env(pay(alice_, carol_, btc(95'000'000'000'000)),
2393 Path(~MPT(eth), ~MPT(btc)),
2394 Sendmax(gbp(140'000'000'000'000)),
2395 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2396 env.close();
2397
2398 // alice buys 70.4210e12ETH with 70.4210e12GBP via the offer
2399 // and pays 25% tr fee on 70.4210e12GBP
2400 // 1,400e12 - 70.4210e12*1.25 = 1400e12 - 88.0262e12 =
2401 // 1311.9736e12GBP
2402 env.require(Balance(alice_, gbp(1'311'973'684'210'525)));
2403 // ed doesn't pay tr fee, the balances reflect consumed offer
2404 // 70.4210e12GBP/70.4210e12ETH
2405 env.require(Balance(ed, eth(1'329'578'947'368'420)));
2406 env.require(Balance(ed, gbp(1'470'421'052'631'580)));
2407 BEAST_EXPECT(expectOffers(
2408 env, ed, 1, {Amounts{gbp(929'578'947'368'420), eth(929'578'947'368'420)}}));
2409 // 25% on 56.3368e12ETH is paid in tr fee 56.3368e12*1.25
2410 // = 70.4210e12ETH 56.3368e12ETH is swapped in for 74.6651e12BTC
2411 BEAST_EXPECT(amm.expectBalances(
2412 eth(1'056'336'842'105'264), btc(1'325'334'821'428'571), amm.tokens()));
2413
2414 // 25% on 59.7321e12BTC is paid in tr fee 59.7321e12*1.25
2415 // = 74.6651e12BTC
2416 env.require(Balance(carol_, btc(1'459'732'142'857'143)));
2417 }
2418 {
2419 // Payment via AMM and offer with limit quality, deliver less
2420 // than requested
2421 Env env(*this, features);
2422 Account const ed("ed");
2423
2424 env.fund(XRP(1'000), gw_, alice_, bob_, carol_, ed);
2425
2426 MPTTester const btc({
2427 .env = env,
2428 .issuer = gw_,
2429 .holders = {alice_, bob_, carol_, ed},
2430 .transferFee = 25'000,
2431 .pay = 1'400'000'000'000'000,
2432 .flags = kMptDexFlags,
2433 });
2434
2435 MPTTester const gbp({
2436 .env = env,
2437 .issuer = gw_,
2438 .holders = {alice_, bob_, carol_, ed},
2439 .transferFee = 25'000,
2440 .pay = 1'400'000'000'000'000,
2441 .flags = kMptDexFlags,
2442 });
2443
2444 MPTTester const eth({
2445 .env = env,
2446 .issuer = gw_,
2447 .holders = {alice_, bob_, carol_, ed},
2448 .transferFee = 25'000,
2449 .pay = 1'400'000'000'000'000,
2450 .flags = kMptDexFlags,
2451 });
2452
2453 AMM const amm(env, bob_, gbp(1'000'000'000'000'000), eth(1'000'000'000'000'000));
2454
2455 env(offer(ed, eth(1'000'000'000'000'000), btc(1'400'000'000'000'000)),
2456 Txflags(tfPassive));
2457 env.close();
2458
2459 // requested quality limit is 95e12BTC/140e12GBP = 0.6785
2460 // trade quality is 47.7857e12BTC/70.4210e12GBP = 0.6785
2461 env(pay(alice_, carol_, btc(95'000'000'000'000)),
2462 Path(~MPT(eth), ~MPT(btc)),
2463 Sendmax(gbp(140'000'000'000'000)),
2464 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2465 env.close();
2466
2467 // alice buys 53.3322e12ETH with 56.3368e12GBP via the amm
2468 // and pays 25% tr fee on 56.3368e12GBP
2469 // 1,400e12 - 56.3368e12*1.25 = 1400e12 - 70.4210e12 =
2470 // 1329.5789e12GBP
2471 env.require(Balance(alice_, gbp(1'329'578'947'368'420)));
2476 // 56.3368e12GBP is swapped in for 53.3322e12ETH
2477 BEAST_EXPECT(amm.expectBalances(
2478 gbp(1'056'336'842'105'264), eth(946'667'729'591'836), amm.tokens()));
2479
2480 // 25% on 42.6658e12ETH is paid in tr fee 42.6658e12*1.25
2481 // = 53.3322e12ETH 42.6658e12ETH/59.7321e12BTC
2482 env.require(Balance(ed, btc(1'340'267'857'142'857)));
2483 env.require(Balance(ed, eth(1'442'665'816'326'531)));
2484 BEAST_EXPECT(expectOffers(
2485 env, ed, 1, {Amounts{eth(957'334'183'673'469), btc(1'340'267'857'142'857)}}));
2486 // 25% on 47.7857e12BTC is paid in tr fee 47.7857e12*1.25
2487 // = 59.7321e12BTC
2488 env.require(Balance(carol_, btc(1'447'785714285714)));
2489 }
2490 {
2491 // Payment via AMM, AMM with limit quality, deliver less
2492 // than requested
2493 Env env(*this, features);
2494 Account const ed("ed");
2495
2496 env.fund(XRP(1'000), gw_, alice_, bob_, carol_, ed);
2497
2498 MPTTester const btc({
2499 .env = env,
2500 .issuer = gw_,
2501 .holders = {alice_, bob_, carol_, ed},
2502 .transferFee = 25'000,
2503 .pay = 1'400'000'000'000'000,
2504 .flags = kMptDexFlags,
2505 });
2506
2507 MPTTester const gbp({
2508 .env = env,
2509 .issuer = gw_,
2510 .holders = {alice_, bob_, carol_, ed},
2511 .transferFee = 25'000,
2512 .pay = 1'400'000'000'000'000,
2513 .flags = kMptDexFlags,
2514 });
2515
2516 MPTTester const eth({
2517 .env = env,
2518 .issuer = gw_,
2519 .holders = {alice_, bob_, carol_, ed},
2520 .transferFee = 25'000,
2521 .pay = 1'400'000'000'000'000,
2522 .flags = kMptDexFlags,
2523 });
2524
2525 AMM const amm1(env, bob_, gbp(1'000'000'000'000'000), eth(1'000'000'000'000'000));
2526 AMM const amm2(env, ed, eth(1'000'000'000'000'000), btc(1'400'000'000'000'000));
2527
2528 // requested quality limit is 90e12BTC/145e12GBP = 0.6206
2529 // trade quality is 66.7432e12BTC/107.5308e12GBP = 0.6206
2530 env(pay(alice_, carol_, btc(90'000'000'000'000)),
2531 Path(~MPT(eth), ~MPT(btc)),
2532 Sendmax(gbp(145'000'000'000'000)),
2533 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2534 env.close();
2535
2536 // alice buys 53.3322e12ETH with 107.5308e12GBP
2537 // 25% on 86.0246e12GBP is paid in tr fee
2538 // 1,400e12 - 86.0246e12*1.25 = 1400e12 - 107.5308e12 =
2539 // 1229.4691e12GBP
2540 env.require(Balance(alice_, gbp(1'292'469'135'802'465)));
2541 // 86.0246e12GBP is swapped in for 79.2106e12ETH
2542 BEAST_EXPECT(amm1.expectBalances(
2543 gbp(1'086'024'691'358'028), eth(920'789'377'955'618), amm1.tokens()));
2544 // 25% on 63.3684e12ETH is paid in tr fee 63.3684e12*1.25
2545 // = 79.2106e12ETH 63.3684e12ETH is swapped in for 83.4291e12BTC
2546 BEAST_EXPECT(amm2.expectBalances(
2547 eth(1'063'368'497'635'505), btc(1'316'570'881'226'053), amm2.tokens()));
2548
2549 // 25% on 66.7432e12BTC is paid in tr fee 66.7432e12*1.25
2550 // = 83.4291e12BTC
2551 env.require(Balance(carol_, btc(1'466'743'295'019'157)));
2552 }
2553 {
2554 // Payment by the issuer via AMM, AMM with limit quality,
2555 // deliver less than requested
2556 Env env(*this, features);
2557
2558 env.fund(XRP(1'000), gw_, alice_, bob_, carol_);
2559
2560 MPTTester const btc({
2561 .env = env,
2562 .issuer = gw_,
2563 .holders = {alice_, bob_, carol_},
2564 .transferFee = 25'000,
2565 .pay = 1'400'000'000'000'000,
2566 .flags = kMptDexFlags,
2567 });
2568
2569 MPTTester const gbp({
2570 .env = env,
2571 .issuer = gw_,
2572 .holders = {alice_, bob_, carol_},
2573 .transferFee = 25'000,
2574 .pay = 1'400'000'000'000'000,
2575 .flags = kMptDexFlags,
2576 });
2577
2578 MPTTester const eth({
2579 .env = env,
2580 .issuer = gw_,
2581 .holders = {alice_, bob_, carol_},
2582 .transferFee = 25'000,
2583 .pay = 1'400'000'000'000'000,
2584 .flags = kMptDexFlags,
2585 });
2586
2587 AMM const amm1(env, alice_, gbp(1'000'000'000'000'000), eth(1'000'000'000'000'000));
2588 AMM const amm2(env, bob_, eth(1'000'000'000'000'000), btc(1'400'000'000'000'000));
2589
2590 // requested quality limit is 90e12BTC/120e12GBP = 0.75
2591 // trade quality is 81.1111e12BTC/108.1481e12GBP = 0.75
2592 env(pay(gw_, carol_, btc(90'000'000'000'000)),
2593 Path(~MPT(eth), ~MPT(btc)),
2594 Sendmax(gbp(120'000'000'000'000)),
2595 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2596 env.close();
2597
2598 // 108.1481e12GBP is swapped in for 97.5935e12ETH
2599 BEAST_EXPECT(amm1.expectBalances(
2600 gbp(1'108'148'148'148'150), eth(902'406'417'112'298), amm1.tokens()));
2601 // 25% on 78.0748e12ETH is paid in tr fee 78.0748e12*1.25
2602 // = 97.5935e12ETH 78.0748e12ETH is swapped in for 101.3888e12BTC
2603 BEAST_EXPECT(amm2.expectBalances(
2604 eth(1'078'074'866'310'161), btc(1'298'611'111'111'111), amm2.tokens()));
2605
2606 // 25% on 81.1111e12BTC is paid in tr fee 81.1111e12*1.25 =
2607 // 101.3888e12BTC
2608 env.require(Balance(carol_, btc(1'481'111'111'111'111)));
2609 }
2610 }
2611
2612 void
2614 {
2615 // Single path with amm, offer, and limit quality. The quality limit
2616 // is such that the first offer should be taken but the second
2617 // should not. The total amount delivered should be the sum of the
2618 // two offers and sendMax should be more than the first offer.
2619 testcase("limitQuality");
2620 using namespace jtx;
2621
2622 {
2623 Env env(*this);
2624 env.fund(XRP(10'000), gw_, alice_, bob_, carol_);
2625
2626 MPTTester const eth({
2627 .env = env,
2628 .issuer = gw_,
2629 .holders = {alice_, bob_, carol_},
2630 .pay = 2'000'000,
2631 .flags = kMptDexFlags,
2632 });
2633
2634 AMM const ammBob(env, bob_, XRP(1'000), eth(1'050'000));
2635 env(offer(bob_, XRP(100), eth(50'000)));
2636
2637 env(pay(alice_, carol_, eth(100'000)),
2638 Path(~MPT(eth)),
2639 Sendmax(XRP(100)),
2640 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2641
2642 BEAST_EXPECT(ammBob.expectBalances(XRP(1'050), eth(1'000'000), ammBob.tokens()));
2643 env.require(Balance(carol_, eth(2'050'000)));
2644 BEAST_EXPECT(expectOffers(env, bob_, 1, {{{XRP(100), eth(50'000)}}}));
2645 }
2646 }
2647
2648 void
2650 {
2651 testcase("Circular XRP");
2652
2653 using namespace jtx;
2654
2655 // Payment path starting with XRP
2656 {
2657 auto const test = [&](auto&& issue1, auto&& issue2) {
2658 Env env(*this);
2659 env.fund(XRP(30'000), alice_, bob_, gw_);
2660 env.close();
2661 auto const eth = issue1({
2662 .env = env,
2663 .token = "ETH",
2664 .issuer = gw_,
2665 .holders = {alice_, bob_},
2666 .limit = 2000'000,
2667 });
2668 auto const btc = issue2({
2669 .env = env,
2670 .token = "BTC",
2671 .issuer = gw_,
2672 .holders = {alice_, bob_},
2673 .limit = 2000'000,
2674 });
2675
2676 env(pay(gw_, alice_, btc(200'000)));
2677 env(pay(gw_, bob_, btc(200'000)));
2678 env(pay(gw_, alice_, eth(200'000)));
2679 env(pay(gw_, bob_, eth(200'000)));
2680 env.close();
2681
2682 AMM const ammAliceXrpBtc(env, alice_, XRP(100), btc(101'000));
2683 AMM const ammAliceXrpEth(env, alice_, XRP(100), eth(101'000));
2684 env(pay(alice_, bob_, eth(1'000)),
2685 Path(~btc, ~XRP, ~eth),
2686 Sendmax(XRP(1)),
2687 Txflags(tfNoRippleDirect),
2689 };
2691 }
2692
2693 // Payment path ending with XRP
2694 {
2695 auto const test = [&](auto&& issue1, auto&& issue2) {
2696 Env env(*this);
2697 env.fund(XRP(30'000), alice_, bob_, gw_);
2698 env.close();
2699 auto const eth = issue1({
2700 .env = env,
2701 .token = "ETH",
2702 .issuer = gw_,
2703 .holders = {alice_, bob_},
2704 .limit = 2000'000,
2705 });
2706 auto const btc = issue2({
2707 .env = env,
2708 .token = "BTC",
2709 .issuer = gw_,
2710 .holders = {alice_, bob_},
2711 .limit = 2000'000,
2712 });
2713
2714 env(pay(gw_, alice_, btc(200'000)));
2715 env(pay(gw_, bob_, btc(200'000)));
2716 env(pay(gw_, alice_, eth(200'000)));
2717 env(pay(gw_, bob_, eth(200'000)));
2718 env.close();
2719
2720 AMM const ammAliceXrpBtc(env, alice_, XRP(100), btc(100'000));
2721 AMM const ammAliceXrpEth(env, alice_, XRP(100), eth(100'000));
2722 // ETH -> //XRP -> //BTC ->XRP
2723 env(pay(alice_, bob_, XRP(1)),
2724 Path(~XRP, ~btc, ~XRP),
2725 Sendmax(eth(1'000)),
2726 Txflags(tfNoRippleDirect),
2728 };
2730 }
2731
2732 // Payment where loop is formed in the middle of the path, not
2733 // on an endpoint
2734 {
2735 auto const test = [&](auto&& issue1, auto&& issue2, auto&& issue3) {
2736 Env env(*this);
2737 env.fund(XRP(10'000), gw_, alice_, bob_);
2738 env.close();
2739 auto const eth = issue1({
2740 .env = env,
2741 .token = "ETH",
2742 .issuer = gw_,
2743 .holders = {alice_, bob_},
2744 .limit = 2000'000,
2745 });
2746 auto const btc = issue2({
2747 .env = env,
2748 .token = "BTC",
2749 .issuer = gw_,
2750 .holders = {alice_, bob_},
2751 .limit = 2000'000,
2752 });
2753 auto const jpy = issue2({
2754 .env = env,
2755 .token = "JPY",
2756 .issuer = gw_,
2757 .holders = {alice_, bob_},
2758 .limit = 2000'000,
2759 });
2760
2761 env(pay(gw_, alice_, btc(200'000)));
2762 env(pay(gw_, bob_, btc(200'000)));
2763 env(pay(gw_, alice_, eth(200'000)));
2764 env(pay(gw_, bob_, eth(200'000)));
2765 env(pay(gw_, alice_, jpy(200'000)));
2766 env(pay(gw_, bob_, jpy(200'000)));
2767 env.close();
2768
2769 AMM const ammAliceXrpBtc(env, alice_, XRP(100), btc(100'000));
2770 AMM const ammAliceXrpEth(env, alice_, XRP(100), eth(100'000));
2771 AMM const ammAliceXrpJpy(env, alice_, XRP(100), jpy(100'000));
2772
2773 env(pay(alice_, bob_, jpy(1'000)),
2774 Path(~XRP, ~eth, ~XRP, ~jpy),
2775 Sendmax(btc(1'000)),
2776 Txflags(tfNoRippleDirect),
2778 };
2780 }
2781 }
2782
2783 void
2785 {
2786 testcase("Step Limit");
2787
2788 using namespace jtx;
2789 {
2790 Env env(*this, features);
2791 auto const dan = Account("dan");
2792 auto const ed = Account("ed");
2793
2794 env.fund(XRP(100'000'000), gw_, alice_, bob_, carol_, dan, ed);
2795
2796 MPTTester const btc(
2797 {.env = env, .issuer = gw_, .holders = {bob_, dan, ed}, .flags = kMptDexFlags});
2798
2799 env(pay(gw_, ed, btc(11'000'000'000'000)));
2800 env(pay(gw_, bob_, btc(1'000'000'000'000)));
2801 env(pay(gw_, dan, btc(1'000'000'000'000)));
2802
2803 nOffers(env, 2'000, bob_, XRP(1), btc(1'000'000'000'000));
2804 nOffers(env, 1, dan, XRP(1), btc(1'000'000'000'000));
2805 AMM const ammEd(env, ed, XRP(9), btc(11'000'000'000'000));
2806
2807 // Alice offers to buy 1000 XRP for 1000e12 BTC. She takes Bob's
2808 // first offer, removes 999 more as unfunded, then hits the step
2809 // limit.
2810 env(offer(alice_, btc(1'000'000'000'000'000), XRP(1'000)));
2811 env.require(Balance(alice_, btc(2'050'125'257'867)));
2812 env.require(Owners(alice_, 2));
2813 env.require(Balance(bob_, btc(0)));
2814 env.require(Owners(bob_, 1'001));
2815 env.require(Balance(dan, btc(1'000'000'000'000)));
2816 env.require(Owners(dan, 2));
2817
2818 // Carol offers to buy 1000 XRP for 1000e12 BTC. She removes Bob's
2819 // next 1000 offers as unfunded and hits the step limit.
2820 env(offer(carol_, btc(1'000'000'000'000'000), XRP(1'000)));
2821 env.require(Balance(carol_, MPT(btc)(kNone)));
2822 env.require(Owners(carol_, 1));
2823 env.require(Balance(bob_, btc(0)));
2824 env.require(Owners(bob_, 1));
2825 env.require(Balance(dan, btc(1'000'000'000'000)));
2826 env.require(Owners(dan, 2));
2827 }
2828
2829 // MPT/IOU, similar to the case above
2830 {
2831 Env env(*this, features);
2832 auto const dan = Account("dan");
2833 auto const ed = Account("ed");
2834
2835 env.fund(XRP(100'000), gw_, alice_, bob_, carol_, dan, ed);
2836 env.close();
2837
2838 MPTTester const usd({
2839 .env = env,
2840 .issuer = gw_,
2841 .holders = {alice_, bob_, carol_, dan, ed},
2842 .pay = 10000'000'000,
2843 .flags = kMptDexFlags,
2844 });
2845
2846 env.trust(BTC(11'000'000'000'000), ed);
2847 env(pay(gw_, ed, BTC(11'000'000'000'000)));
2848 env.trust(BTC(1'000'000'000'000), bob_);
2849 env(pay(gw_, bob_, BTC(1'000'000'000'000)));
2850 env.trust(BTC(1'000'000'000'000), dan);
2851 env(pay(gw_, dan, BTC(1'000'000'000'000)));
2852 env.close();
2853
2854 nOffers(env, 2'000, bob_, usd(1000000), BTC(1'000'000'000'000));
2855 nOffers(env, 1, dan, usd(1000000), BTC(1'000'000'000'000));
2856 AMM const ammEd(env, ed, usd(9000000), BTC(11'000'000'000'000));
2857 env(offer(alice_, BTC(1'000'000'000'000'000), usd(1'000000000)));
2858
2859 env.require(Balance(alice_, STAmount{BTC, UINT64_C(2050125257867'587), -3}));
2860 env.require(Owners(alice_, 3));
2861 env.require(Balance(bob_, BTC(0)));
2862 env.require(Owners(bob_, 1'002));
2863 env.require(Balance(dan, BTC(1000000000000)));
2864 env.require(Owners(dan, 3));
2865 }
2866
2867 // IOU/MPT, similar to the case above
2868 {
2869 Env env(*this, features);
2870 auto const dan = Account("dan");
2871 auto const ed = Account("ed");
2872
2873 env.fund(XRP(100'000), gw_, alice_, bob_, carol_, dan, ed);
2874 env.close();
2875
2876 env.trust(USD(10000'000'000), alice_);
2877 env(pay(gw_, alice_, USD(10000'000'000)));
2878 env.trust(USD(10000'000'000), bob_);
2879 env(pay(gw_, bob_, USD(10000'000'000)));
2880 env.trust(USD(10000'000'000), carol_);
2881 env(pay(gw_, carol_, USD(10000'000'000)));
2882 env.trust(USD(10000'000'000), dan);
2883 env(pay(gw_, dan, USD(10000'000'000)));
2884 env.trust(USD(10000'000'000), ed);
2885 env(pay(gw_, ed, USD(10000'000'000)));
2886 env.close();
2887
2888 MPTTester const btc(
2889 {.env = env, .issuer = gw_, .holders = {bob_, dan, ed}, .flags = kMptDexFlags});
2890
2891 env(pay(gw_, ed, btc(11'000'000'000'000)));
2892 env(pay(gw_, bob_, btc(1'000'000'000'000)));
2893 env(pay(gw_, dan, btc(1'000'000'000'000)));
2894 env.close();
2895
2896 nOffers(env, 2'000, bob_, USD(1000000), btc(1'000'000'000'000));
2897 nOffers(env, 1, dan, USD(1000000), btc(1'000'000'000'000));
2898 AMM const ammEd(env, ed, USD(9000000), btc(11'000'000'000'000));
2899 env(offer(alice_, btc(1'000'000'000'000'000), USD(1'000000000)));
2900
2901 env.require(Balance(alice_, btc(2050125628933)));
2902 env.require(Owners(alice_, 3));
2903 env.require(Balance(bob_, btc(0)));
2904 env.require(Owners(bob_, 1'002));
2905 env.require(Balance(dan, btc(1000000000000)));
2906 env.require(Owners(dan, 3));
2907 }
2908
2909 // MPT/MPT, similar to the case above
2910 {
2911 Env env(*this, features);
2912 auto const dan = Account("dan");
2913 auto const ed = Account("ed");
2914
2915 env.fund(XRP(100'000), gw_, alice_, bob_, carol_, dan, ed);
2916 env.close();
2917
2918 MPTTester const btc(
2919 {.env = env, .issuer = gw_, .holders = {bob_, dan, ed}, .flags = kMptDexFlags});
2920 MPTTester const usd({
2921 .env = env,
2922 .issuer = gw_,
2923 .holders = {alice_, bob_, carol_, dan, ed},
2924 .pay = 10000'000'000,
2925 .flags = kMptDexFlags,
2926 });
2927
2928 env(pay(gw_, ed, btc(11'000'000'000'000)));
2929 env(pay(gw_, bob_, btc(1'000'000'000'000)));
2930 env(pay(gw_, dan, btc(1'000'000'000'000)));
2931 env.close();
2932
2933 nOffers(env, 2'000, bob_, usd(1000000), btc(1'000'000'000'000));
2934 nOffers(env, 1, dan, usd(1000000), btc(1'000'000'000'000));
2935 AMM const ammEd(env, ed, usd(9000000), btc(11'000'000'000'000));
2936 env(offer(alice_, btc(1'000'000'000'000'000), usd(1'000000000)));
2937
2938 env.require(Balance(alice_, btc(2050125257867)));
2939 env.require(Owners(alice_, 3));
2940 env.require(Balance(bob_, btc(0)));
2941 env.require(Owners(bob_, 1'002));
2942 env.require(Balance(dan, btc(1000000000000)));
2943 env.require(Owners(dan, 3));
2944 }
2945 }
2946
2947 void
2949 {
2950 testcase("Convert all of an asset using DeliverMin");
2951
2952 using namespace jtx;
2953
2954 {
2955 Env env(*this, features);
2956 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000));
2957
2958 MPTTester const btc({
2959 .env = env,
2960 .issuer = gw_,
2961 .holders = {alice_, bob_, carol_},
2962 .flags = kMptDexFlags,
2963 });
2964
2965 env(pay(alice_, bob_, btc(10'000)), DeliverMin(btc(10'000)), Ter(temBAD_AMOUNT));
2966 env(pay(alice_, bob_, btc(10'000)),
2967 DeliverMin(btc(-5'000)),
2968 Txflags(tfPartialPayment),
2970 env(pay(alice_, bob_, btc(10'000)),
2971 DeliverMin(XRP(5)),
2972 Txflags(tfPartialPayment),
2974 env(pay(alice_, bob_, btc(10'000)),
2975 DeliverMin(btc(5'000)),
2976 Txflags(tfPartialPayment),
2977 Ter(tecPATH_DRY));
2978 env(pay(alice_, bob_, btc(10'000)),
2979 DeliverMin(btc(15'000)),
2980 Txflags(tfPartialPayment),
2982 env(pay(gw_, carol_, btc(50'000)));
2983 AMM const ammCarol(env, carol_, XRP(10), btc(15'000));
2984 env(pay(alice_, bob_, btc(10'000)),
2985 Paths(XRP),
2986 DeliverMin(btc(7'000)),
2987 Txflags(tfPartialPayment),
2988 Sendmax(XRP(5)),
2990 env.require(
2991 Balance(alice_, drops(10'000'000'000 - (3 * env.current()->fees().base.drops()))));
2992 env.require(Balance(bob_, drops(10'000'000'000 - env.current()->fees().base.drops())));
2993 }
2994
2995 {
2996 Env env(*this, features);
2997 fund(env, gw_, {alice_, bob_}, XRP(10'000));
2998
2999 MPTTester const btc(
3000 {.env = env, .issuer = gw_, .holders = {alice_, bob_}, .flags = kMptDexFlags});
3001
3002 env(pay(gw_, bob_, btc(1'100'000)));
3003 AMM const ammBob(env, bob_, XRP(1'000), btc(1'100'000));
3004 env(pay(alice_, alice_, btc(10'000'000)),
3005 Paths(XRP),
3006 DeliverMin(btc(100'000)),
3007 Txflags(tfPartialPayment),
3008 Sendmax(XRP(100)));
3009 env.require(Balance(alice_, btc(100'000)));
3010 }
3011
3012 // IOU/MPT mix, similar to the above case
3013 {
3014 auto const test = [&](auto&& issue1, auto&& issue2) {
3015 Env env(*this);
3016 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
3017 env.close();
3018 auto const usd = issue1({
3019 .env = env,
3020 .token = "USD",
3021 .issuer = gw_,
3022 .holders = {alice_, bob_, carol_},
3023 .limit = 3000'000,
3024 });
3025 auto const btc = issue2({
3026 .env = env,
3027 .token = "BTC",
3028 .issuer = gw_,
3029 .holders = {alice_, bob_, carol_},
3030 .limit = 1'000'000,
3031 });
3032
3033 env(pay(gw_, alice_, usd(10'000)));
3034 env(pay(gw_, bob_, usd(10'000)));
3035 env(pay(gw_, bob_, btc(1'200)));
3036 env.close();
3037
3038 AMM const ammBob(env, bob_, usd(1'000), btc(1'100));
3039 env(pay(alice_, alice_, btc(10'000)),
3040 Paths(usd),
3041 DeliverMin(btc(100)),
3042 Txflags(tfPartialPayment),
3043 Sendmax(usd(100)));
3044 env.require(Balance(alice_, btc(100)));
3045 };
3047 }
3048
3049 {
3050 Env env(*this, features);
3051 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000));
3052
3053 MPTTester const btc(
3054 {.env = env, .issuer = gw_, .holders = {bob_, carol_}, .flags = kMptDexFlags});
3055
3056 env(pay(gw_, bob_, btc(1'200'000)));
3057 AMM const ammBob(env, bob_, XRP(5'500), btc(1'200'000));
3058 env(pay(alice_, carol_, btc(10'000'000)),
3059 Paths(XRP),
3060 DeliverMin(btc(200'000)),
3061 Txflags(tfPartialPayment),
3062 Sendmax(XRP(1'000)),
3064 env(pay(alice_, carol_, btc(10'000'000)),
3065 Paths(XRP),
3066 DeliverMin(btc(200'000)),
3067 Txflags(tfPartialPayment),
3068 Sendmax(XRP(1'100)));
3069 BEAST_EXPECT(ammBob.expectBalances(XRP(6'600), btc(1'000'000), ammBob.tokens()));
3070 env.require(Balance(carol_, btc(200'000)));
3071 }
3072
3073 // IOU/MPT mix, similar to the above case
3074 {
3075 auto const test = [&](auto&& issue1, auto&& issue2) {
3076 Env env(*this);
3077 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
3078 env.close();
3079 auto const usd = issue1({
3080 .env = env,
3081 .token = "USD",
3082 .issuer = gw_,
3083 .holders = {alice_, bob_, carol_},
3084 .limit = 3000'000,
3085 });
3086 auto const btc = issue2({
3087 .env = env,
3088 .token = "BTC",
3089 .issuer = gw_,
3090 .holders = {alice_, bob_, carol_},
3091 .limit = 1'000'000,
3092 });
3093
3094 env(pay(gw_, alice_, usd(100'000)));
3095 env(pay(gw_, bob_, usd(100'000)));
3096 env(pay(gw_, carol_, usd(100'000)));
3097
3098 env(pay(gw_, bob_, btc(1'200)));
3099 env.close();
3100
3101 AMM const ammBob(env, bob_, usd(5'500), btc(1'200));
3102 env(pay(alice_, carol_, btc(10'000)),
3103 Paths(usd),
3104 DeliverMin(btc(200)),
3105 Txflags(tfPartialPayment),
3106 Sendmax(usd(1'000)),
3108 env(pay(alice_, carol_, btc(10'000)),
3109 Paths(usd),
3110 DeliverMin(btc(200)),
3111 Txflags(tfPartialPayment),
3112 Sendmax(usd(1'100)));
3113 BEAST_EXPECT(ammBob.expectBalances(usd(6'600), btc(1'000), ammBob.tokens()));
3114 env.require(Balance(carol_, btc(200)));
3115 };
3117 }
3118
3119 {
3120 auto const dan = Account("dan");
3121 Env env(*this, features);
3122 fund(env, gw_, {alice_, bob_, carol_, dan}, XRP(10'000));
3123
3124 MPTTester const btc(
3125 {.env = env, .issuer = gw_, .holders = {bob_, carol_, dan}, .flags = kMptDexFlags});
3126
3127 env(pay(gw_, bob_, btc(100'000'000)));
3128 env(pay(gw_, dan, btc(1'100'000'000)));
3129 env(offer(bob_, XRP(100), btc(100'000'000)));
3130 env(offer(bob_, XRP(1'000), btc(100'000'000)));
3131 AMM const ammDan(env, dan, XRP(1'000), btc(1'100'000'000));
3132
3133 env(pay(alice_, carol_, btc(10'000'000'000)),
3134 Paths(XRP),
3135 DeliverMin(btc(200'000'000)),
3136 Txflags(tfPartialPayment),
3137 Sendmax(XRPAmount(200'000'001)));
3138 env.require(Balance(bob_, btc(0)));
3139 env.require(Balance(carol_, btc(200'000'000)));
3140 BEAST_EXPECT(
3141 ammDan.expectBalances(XRPAmount{1'100'000'001}, btc(1000'000000), ammDan.tokens()));
3142 }
3143 }
3144
3145 void
3147 {
3148 testcase("Payment");
3149
3150 using namespace jtx;
3151 Account const becky{"becky"};
3152
3153 Env env(*this, features);
3154 fund(env, gw_, {alice_, becky}, XRP(5'000));
3155
3156 MPTTester const btc(
3157 {.env = env, .issuer = gw_, .holders = {alice_, becky}, .flags = kMptDexFlags});
3158
3159 env(pay(gw_, alice_, btc(500'000)));
3160 env.close();
3161
3162 AMM const ammAlice(env, alice_, XRP(100), btc(140'000));
3163
3164 // becky pays herself BTC (10'000) by consuming part of alice's offer.
3165 // Make sure the payment works if PaymentAuth is not involved.
3166 env(pay(becky, becky, btc(10'000)), Path(~MPT(btc)), Sendmax(XRP(10)));
3167 env.close();
3168 BEAST_EXPECT(
3169 ammAlice.expectBalances(XRPAmount(107'692'308), btc(130'000), ammAlice.tokens()));
3170
3171 // becky decides to require authorization for deposits.
3172 env(fset(becky, asfDepositAuth));
3173 env.close();
3174
3175 // becky pays herself again.
3176 env(pay(becky, becky, btc(10'000)), Path(~MPT(btc)), Sendmax(XRP(10)), Ter(tesSUCCESS));
3177
3178 env.close();
3179 }
3180
3181 void
3183 {
3184 // Exercise MPT payments and non-direct XRP payments to an account
3185 // that has the lsfDepositAuth flag set.
3186 testcase("Pay MPT");
3187
3188 using namespace jtx;
3189
3190 Env env(*this);
3191
3192 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000));
3193
3194 MPTTester btc(
3195 {.env = env, .issuer = gw_, .holders = {alice_, bob_, carol_}, .flags = kMptDexFlags});
3196
3197 env(pay(gw_, alice_, btc(150'000)));
3198 env(pay(gw_, carol_, btc(150'000)));
3199 AMM const ammCarol(env, carol_, btc(100'000), XRPAmount(101));
3200
3201 env(pay(alice_, bob_, btc(50'000)));
3202 env.close();
3203
3204 // bob sets the lsfDepositAuth flag.
3205 env(fset(bob_, asfDepositAuth), Require(Flags(bob_, asfDepositAuth)));
3206 env.close();
3207
3208 // None of the following payments should succeed.
3209 auto const failedMptPayments = [this, &env, &btc] {
3210 env.require(Flags(bob_, asfDepositAuth));
3211
3212 // Capture bob's balances before hand to confirm they don't
3213 // change.
3214 PrettyAmount const bobXrpBalance{env.balance(bob_, XRP)};
3215 PrettyAmount const bobBTCBalance{env.balance(bob_, MPT(btc))};
3216
3217 env(pay(alice_, bob_, btc(50'000)), Ter(tecNO_PERMISSION));
3218 env.close();
3219
3220 // Note that even though alice is paying bob in XRP, the payment
3221 // is still not allowed since the payment passes through an
3222 // offer.
3223 env(pay(alice_, bob_, drops(1)), Sendmax(btc(1'000)), Ter(tecNO_PERMISSION));
3224 env.close();
3225
3226 BEAST_EXPECT(bobXrpBalance == env.balance(bob_, XRP));
3227 BEAST_EXPECT(bobBTCBalance == env.balance(bob_, MPT(btc)));
3228 };
3229
3230 // Test when bob has an XRP balance > base reserve.
3231 failedMptPayments();
3232
3233 // Set bob's XRP balance == base reserve. Also demonstrate that
3234 // bob can make payments while his lsfDepositAuth flag is set.
3235 env(pay(bob_, alice_, btc(25'000)));
3236 env.close();
3237
3238 {
3239 STAmount const bobPaysXRP{env.balance(bob_, XRP) - reserve(env, 1)};
3240 XRPAmount const bobPaysFee{reserve(env, 1) - reserve(env, 0)};
3241 env(pay(bob_, alice_, bobPaysXRP), Fee(bobPaysFee));
3242 env.close();
3243 }
3244
3245 // Test when bob's XRP balance == base reserve.
3246 BEAST_EXPECT(env.balance(bob_, XRP) == reserve(env, 0));
3247 BEAST_EXPECT(env.balance(bob_, MPT(btc)) == btc(25'000));
3248 failedMptPayments();
3249
3250 // Test when bob has an XRP balance == 0.
3251 env(noop(bob_), Fee(reserve(env, 0)));
3252 env.close();
3253
3254 BEAST_EXPECT(env.balance(bob_, XRP) == XRP(0));
3255 failedMptPayments();
3256
3257 // Give bob enough XRP for the fee to clear the lsfDepositAuth flag.
3258 env(pay(alice_, bob_, drops(env.current()->fees().base)));
3259
3260 // bob clears the lsfDepositAuth and the next payment succeeds.
3261 env(fclear(bob_, asfDepositAuth));
3262 env.close();
3263
3264 env(pay(alice_, bob_, btc(50'000)));
3265 env.close();
3266
3267 env(pay(alice_, bob_, drops(1)), Sendmax(btc(1'000)));
3268 env.close();
3269 BEAST_EXPECT(ammCarol.expectBalances(btc(101'000), XRPAmount(100), ammCarol.tokens()));
3270 }
3271
3272 void
3274 {
3275 testcase("Individual Lock");
3276
3277 using namespace test::jtx;
3278 Env env(*this, features);
3279
3280 Account const g1{"G1"};
3281 Account const alice{"alice"};
3282 Account const bob{"bob"};
3283
3284 env.fund(XRP(1'000), g1, alice, bob);
3285
3286 MPTTester btc({
3287 .env = env,
3288 .issuer = g1,
3289 .holders = {alice, bob},
3290 .flags = tfMPTCanLock | kMptDexFlags,
3291 });
3292
3293 env(pay(g1, bob, btc(10)));
3294 env(pay(g1, alice, btc(205)));
3295 env.close();
3296
3297 AMM const ammAlice(env, alice, XRP(500), btc(105));
3298
3299 env.require(Balance(bob, btc(10)));
3300 env.require(Balance(alice, btc(100)));
3301
3302 // Account with MPT unlocked (proving operations normally work)
3303 // can make Payment
3304 env(pay(alice, bob, btc(1)));
3305
3306 // can receive Payment
3307 env(pay(bob, alice, btc(1)));
3308 env.close();
3309
3310 // Lock MPT for bob
3311 btc.set({.holder = bob, .flags = tfMPTLock});
3312
3313 {
3314 env(offer(bob, btc(5), XRP(25)), Ter(tecLOCKED));
3315 env.close();
3316 BEAST_EXPECT(ammAlice.expectBalances(XRP(500), btc(105), ammAlice.tokens()));
3317 }
3318
3319 {
3320 // can not sell assets
3321 env(offer(bob, XRP(1), btc(5)), Ter(tecUNFUNDED_OFFER));
3322
3323 // different from IOU
3324 // can not receive Payment when locked
3325 env(pay(alice, bob, btc(1)), Ter(tecPATH_DRY));
3326
3327 // can not make Payment when locked
3328 env(pay(bob, alice, btc(1)), Ter(tecPATH_DRY));
3329
3330 env.require(Balance(bob, btc(10)));
3331 }
3332
3333 {
3334 // Unlock
3335 btc.set({.holder = bob, .flags = tfMPTUnlock});
3336 env(offer(bob, XRP(1), btc(5)));
3337 env(pay(bob, alice, btc(1)));
3338 env(pay(alice, bob, btc(1)));
3339 env.close();
3340 }
3341 }
3342
3343 void
3345 {
3346 testcase("Global Lock");
3347
3348 using namespace test::jtx;
3349 Env env(*this, features);
3350
3351 Account const g1{"G1"};
3352 Account a1{"A1"};
3353 Account a2{"A2"};
3354 Account a3{"A3"};
3355 Account a4{"A4"};
3356
3357 env.fund(XRP(12'000), g1);
3358 env.fund(XRP(1'000), a1);
3359 env.fund(XRP(20'000), a2, a3, a4);
3360
3361 MPTTester const eth({
3362 .env = env,
3363 .issuer = g1,
3364 .holders = {a1, a2, a3, a4},
3365 .flags = tfMPTCanLock | kMptDexFlags,
3366 });
3367
3368 MPTTester btc({
3369 .env = env,
3370 .issuer = g1,
3371 .holders = {a1, a2, a3, a4},
3372 .flags = tfMPTCanLock | kMptDexFlags,
3373 });
3374
3375 env(pay(g1, a1, eth(1'000)));
3376 env(pay(g1, a2, eth(100)));
3377 env(pay(g1, a3, btc(100)));
3378 env(pay(g1, a4, btc(100)));
3379 env.close();
3380
3381 AMM const ammG1(env, g1, XRP(10'000), eth(100));
3382 env(offer(a1, XRP(10'000), eth(100)), Txflags(tfPassive));
3383 env(offer(a2, eth(100), XRP(10'000)), Txflags(tfPassive));
3384 env.close();
3385
3386 {
3387 // Account without Global Lock (proving operations normally
3388 // work)
3389 // visible offers where taker_pays is unlocked issuer
3390 auto offers = getAccountOffers(env, a2)[jss::offers];
3391 if (!BEAST_EXPECT(checkArraySize(offers, 1u)))
3392 return;
3393
3394 // visible offers where taker_gets is unlocked issuer
3395 offers = getAccountOffers(env, a1)[jss::offers];
3396 if (!BEAST_EXPECT(checkArraySize(offers, 1u)))
3397 return;
3398 }
3399
3400 {
3401 // Offers/Payments
3402 // assets can be bought on the market
3403 AMM ammA3(env, a3, btc(1), XRP(1));
3404
3405 // assets can be sold on the market
3406 // AMM is bidirectional
3407 env(pay(g1, a2, eth(1)));
3408 env(pay(a2, g1, eth(1)));
3409 env(pay(a2, a1, eth(1)));
3410 env(pay(a1, a2, eth(1)));
3411 ammA3.withdrawAll(std::nullopt);
3412 }
3413
3414 {
3415 // Account with Global Lock
3416 // set Global Lock first
3417 btc.set({.flags = tfMPTLock});
3418
3419 // assets can't be bought on the market
3420 AMM const ammA3(env, a3, btc(1), XRP(1), Ter(tecLOCKED));
3421
3422 // direct issues can be sent
3423 env(pay(g1, a2, btc(1)));
3424 env(pay(a2, g1, btc(1)));
3425 // locked
3426 env(pay(a2, a1, btc(1)), Ter(tecPATH_DRY));
3427 env(pay(a1, a2, btc(1)), Ter(tecPATH_DRY));
3428 }
3429
3430 {
3431 auto offers = getAccountOffers(env, a2)[jss::offers];
3432 if (!BEAST_EXPECT(checkArraySize(offers, 1u)))
3433 return;
3434
3435 offers = getAccountOffers(env, a1)[jss::offers];
3436 if (!BEAST_EXPECT(checkArraySize(offers, 1u)))
3437 return;
3438 }
3439 }
3440
3441 void
3443 {
3444 testcase("Offers for Locked MPTs");
3445
3446 using namespace test::jtx;
3447 Env env(*this, features);
3448
3449 Account const g1{"G1"};
3450 Account a2{"A2"};
3451 Account a3{"A3"};
3452 Account a4{"A4"};
3453
3454 env.fund(XRP(2'000), g1, a3, a4);
3455 env.fund(XRP(2'000), a2);
3456 env.close();
3457
3458 MPTTester btc({
3459 .env = env,
3460 .issuer = g1,
3461 .holders = {a2, a3, a4},
3462 .flags = tfMPTCanLock | kMptDexFlags,
3463 });
3464
3465 env(pay(g1, a3, btc(2'000)));
3466 env(pay(g1, a4, btc(2'001)));
3467 env.close();
3468
3469 AMM const ammA3(env, a3, XRP(1'000), btc(1'001));
3470
3471 // removal after successful payment
3472 // test: make a payment with partially consuming offer
3473 env(pay(a2, g1, btc(1)), Paths(MPT(btc)), Sendmax(XRP(1)));
3474 env.close();
3475
3476 BEAST_EXPECT(ammA3.expectBalances(XRP(1'001), btc(1'000), ammA3.tokens()));
3477
3478 // test: someone else creates an offer providing liquidity
3479 env(offer(a4, XRP(999), btc(999)));
3480 env.close();
3481 // The offer consumes AMM offer
3482 BEAST_EXPECT(ammA3.expectBalances(XRP(1'000), btc(1'001), ammA3.tokens()));
3483
3484 // test: AMM is Locked
3485 btc.set({.holder = ammA3.ammAccount(), .flags = tfMPTLock});
3486 auto const info = ammA3.ammRpcInfo();
3487 BEAST_EXPECT(info[jss::amm][jss::asset2_frozen].asBool());
3488 env.close();
3489
3490 // test: Can make a payment via the new offer
3491 env(pay(a2, g1, btc(1)), Paths(MPT(btc)), Sendmax(XRP(1)));
3492 env.close();
3493 // AMM is not consumed
3494 BEAST_EXPECT(ammA3.expectBalances(XRP(1'000), btc(1'001), ammA3.tokens()));
3495
3496 // removal buy successful OfferCreate
3497 // test: lock the new offer
3498 btc.set({.holder = a4, .flags = tfMPTUnlock});
3499 env.close();
3500
3501 // test: can no longer create a crossing offer
3502 env(offer(a2, btc(999), XRP(999)));
3503 env.close();
3504
3505 // test: offer was removed by offer_create
3506 auto const offers = getAccountOffers(env, a4)[jss::offers];
3507 if (!BEAST_EXPECT(checkArraySize(offers, 0u)))
3508 return;
3509 }
3510
3511 void
3513 {
3514 testcase("Multisign AMM Transactions");
3515
3516 using namespace jtx;
3517 Env env{*this, features};
3518 Account const bogie{"bogie", KeyType::Secp256k1};
3519 Account const alice{"alice", KeyType::Secp256k1};
3520 Account const becky{"becky", KeyType::Ed25519};
3521 Account const zelda{"zelda", KeyType::Secp256k1};
3522 fund(env, gw_, {alice, becky, zelda}, XRP(20'000));
3523
3524 MPTTester const btc({
3525 .env = env,
3526 .issuer = gw_,
3527 .holders = {alice, becky, zelda},
3528 .pay = 20'000'000'000,
3529 .flags = kMptDexFlags,
3530 });
3531
3532 // alice uses a regular key with the master disabled.
3533 Account const alie{"alie", KeyType::Secp256k1};
3534 env(regkey(alice, alie));
3535 env(fset(alice, asfDisableMaster), Sig(alice));
3536
3537 // Attach signers to alice.
3538 env(signers(alice, 2, {{becky, 1}, {bogie, 1}}), Sig(alie));
3539 env.close();
3540 static constexpr int kSignerListOwners{2};
3541 env.require(Owners(alice, kSignerListOwners + 0));
3542
3543 Msig const ms{becky, bogie};
3544
3545 // Multisign all AMM transactions
3546 AMM ammAlice(
3547 env,
3548 alice,
3549 XRP(10'000),
3550 btc(10'000),
3551 false,
3552 0,
3553 ammCrtFee(env).drops(),
3554 std::nullopt,
3555 std::nullopt,
3556 ms,
3557 Ter(tesSUCCESS));
3558 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), btc(10'000), ammAlice.tokens()));
3559
3560 ammAlice.deposit(alice, 1'000'000);
3561 BEAST_EXPECT(ammAlice.expectBalances(XRP(11'000), btc(11'000), IOUAmount{11'000'000, 0}));
3562
3563 ammAlice.withdraw(alice, 1'000'000);
3564 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), btc(10'000), ammAlice.tokens()));
3565
3566 ammAlice.vote({}, 1'000);
3567 BEAST_EXPECT(ammAlice.expectTradingFee(1'000));
3568
3569 env(ammAlice.bid({.account = alice, .bidMin = 100}), ms).close();
3570 BEAST_EXPECT(ammAlice.expectAuctionSlot(100, 0, IOUAmount{4'000}));
3571 // 4000 tokens burnt
3572 BEAST_EXPECT(ammAlice.expectBalances(XRP(10'000), btc(10'000), IOUAmount{9'996'000, 0}));
3573 }
3574
3575 void
3577 {
3578 testcase("To Strand");
3579
3580 using namespace jtx;
3581
3582 // cannot have more than one offer with the same output issue
3583 {
3584 auto const test = [&](auto&& issue1, auto&& issue2) {
3585 Env env(*this);
3586 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
3587 env.close();
3588 auto const eth = issue1({
3589 .env = env,
3590 .token = "ETH",
3591 .issuer = gw_,
3592 .holders = {alice_, bob_, carol_},
3593 .limit = 1'000'000,
3594 });
3595 auto const btc = issue2({
3596 .env = env,
3597 .token = "BTC",
3598 .issuer = gw_,
3599 .holders = {alice_, bob_, carol_},
3600 .limit = 1'000'000,
3601 });
3602 env(pay(gw_, alice_, btc(50000)));
3603 env(pay(gw_, bob_, btc(50000)));
3604 env(pay(gw_, carol_, btc(50000)));
3605 env(pay(gw_, alice_, eth(50000)));
3606 env(pay(gw_, bob_, eth(50000)));
3607 env(pay(gw_, carol_, eth(50000)));
3608 env.close();
3609 AMM const bobXrpBtc(env, bob_, XRP(1'000), btc(1'000));
3610 AMM const bobBtcEth(env, bob_, btc(1'000), eth(1'000));
3611
3612 // payment path: XRP -> XRP/BTC -> BTC/ETH -> ETH/BTC
3613 env(pay(alice_, carol_, btc(100)),
3614 Path(~btc, ~eth, ~btc),
3615 Sendmax(XRP(200)),
3616 Txflags(tfNoRippleDirect),
3618 };
3620 }
3621 }
3622
3623 void
3625 {
3626 using namespace jtx;
3627 testcase("RIPD1373");
3628
3629 {
3630 Env env(*this, features);
3631 fund(env, gw_, {alice_, bob_}, XRP(10'000));
3632
3633 MPTTester btc({
3634 .env = env,
3635 .issuer = bob_,
3636 .holders = {alice_, gw_},
3637 .pay = 100'000'000,
3638 .flags = kMptDexFlags,
3639 });
3640
3641 MPTTester eth({
3642 .env = env,
3643 .issuer = bob_,
3644 .holders = {alice_, gw_},
3645 .pay = 100'000'000,
3646 .flags = kMptDexFlags,
3647 });
3648
3649 AMM const ammXrpBtc(env, bob_, XRP(100), btc(100'000));
3650 env(offer(gw_, XRP(100), btc(100'000)), Txflags(tfPassive));
3651
3652 AMM const ammBtcEth(env, bob_, btc(100'000), eth(100'000));
3653 env(offer(gw_, btc(100'000), eth(100'000)), Txflags(tfPassive));
3654
3655 TestPath const p = [&] {
3656 TestPath result;
3657 result.pushBack(allPathElements(gw_, MPT(btc)));
3658 result.pushBack(cpe(eth.issuanceID()));
3659 return result;
3660 }();
3661
3662 PathSet const paths(p);
3663
3664 env(pay(alice_, alice_, eth(1'000)),
3665 Json(paths.json()),
3666 Sendmax(XRP(10)),
3667 Txflags(tfNoRippleDirect | tfPartialPayment),
3668 Ter(temBAD_PATH));
3669 }
3670
3671 {
3672 Env env(*this, features);
3673
3674 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000));
3675
3676 MPTTester const btc({
3677 .env = env,
3678 .issuer = gw_,
3679 .holders = {alice_, bob_, carol_},
3680 .pay = 100'000,
3681 .flags = kMptDexFlags,
3682 });
3683
3684 AMM const ammBob(env, bob_, XRP(100), btc(100));
3685
3686 // payment path: XRP -> XRP/BTC -> BTC/XRP
3687 env(pay(alice_, carol_, XRP(100)),
3688 Path(~MPT(btc), ~XRP),
3689 Txflags(tfNoRippleDirect),
3691 }
3692
3693 {
3694 Env env(*this, features);
3695
3696 fund(env, gw_, {alice_, bob_, carol_}, XRP(10'000));
3697
3698 MPTTester const btc({
3699 .env = env,
3700 .issuer = gw_,
3701 .holders = {alice_, bob_, carol_},
3702 .pay = 100'000,
3703 .flags = kMptDexFlags,
3704 });
3705
3706 AMM const ammBob(env, bob_, XRP(100), btc(100));
3707
3708 // payment path: XRP -> XRP/BTC -> BTC/XRP
3709 env(pay(alice_, carol_, XRP(100)),
3710 Path(~MPT(btc), ~XRP),
3711 Sendmax(XRP(200)),
3712 Txflags(tfNoRippleDirect),
3714 }
3715 }
3716
3717 void
3719 {
3720 testcase("test loop");
3721 using namespace jtx;
3722
3723 {
3724 Env env(*this, features);
3725
3726 env.fund(XRP(10'000), alice_, bob_, carol_, gw_);
3727
3728 MPTTester const btc({
3729 .env = env,
3730 .issuer = gw_,
3731 .holders = {alice_, bob_, carol_},
3732 .flags = kMptDexFlags,
3733 });
3734
3735 env(pay(gw_, bob_, btc(100'000'000)));
3736 env(pay(gw_, alice_, btc(100'000'000)));
3737 env.close();
3738
3739 AMM const ammBob(env, bob_, XRP(100), btc(100'000'000));
3740
3741 // payment path: BTC -> BTC/XRP -> XRP/BTC
3742 env(pay(alice_, carol_, btc(100'000'000)),
3743 Sendmax(btc(100'000'000)),
3744 Path(~XRP, ~MPT(btc)),
3745 Txflags(tfNoRippleDirect),
3747 }
3748
3749 {
3750 auto const test = [&](auto&& issue1, auto&& issue2, auto&& issue3) {
3751 Env env(*this, features);
3752
3753 env.fund(XRP(10'000), alice_, bob_, carol_, gw_);
3754 env.close();
3755
3756 auto const btc = issue1(
3757 {.env = env, .token = "BTC", .issuer = gw_, .holders = {alice_, bob_, carol_}});
3758 auto const eth = issue2(
3759 {.env = env, .token = "ETH", .issuer = gw_, .holders = {alice_, bob_, carol_}});
3760 auto const cny = issue3(
3761 {.env = env, .token = "CNY", .issuer = gw_, .holders = {alice_, bob_, carol_}});
3762
3763 env(pay(gw_, bob_, btc(200)));
3764 env(pay(gw_, bob_, eth(200)));
3765 env(pay(gw_, bob_, cny(100)));
3766 env.close();
3767
3768 AMM const ammBobXrpBtc(env, bob_, XRP(100), btc(100));
3769 AMM const ammBobBtcEth(env, bob_, btc(100), eth(100));
3770 AMM const ammBobEthCny(env, bob_, eth(100), cny(100));
3771
3772 // payment path: XRP->XRP/BTC->BTC/ETH->BTC/CNY
3773 env(pay(alice_, carol_, cny(100)),
3774 Sendmax(XRP(100)),
3775 Path(~btc, ~eth, ~btc, ~cny),
3776 Txflags(tfNoRippleDirect),
3778 };
3780 }
3781 }
3782
3783 void
3794
3795 void
3797 {
3798 using namespace jtx;
3799 FeatureBitset const all{testableAmendments()};
3800
3801 testFalseDry(all);
3802 testBookStep(all);
3806 }
3807
3808 void
3810 {
3811 using namespace jtx;
3812 FeatureBitset const all{testableAmendments()};
3813 testStepLimit(all);
3814 }
3815
3816 void
3818 {
3819 using namespace jtx;
3820 FeatureBitset const all{testableAmendments()};
3822 }
3823
3824 void
3826 {
3827 auto const supported{jtx::testableAmendments()};
3828 testPayment(supported);
3829 testPayMPT();
3830 }
3831
3832 void
3834 {
3835 using namespace test::jtx;
3836 auto const sa = testableAmendments();
3838 testGlobalLock(sa);
3840 }
3841
3842 void
3844 {
3845 using namespace jtx;
3846 auto const all = testableAmendments();
3847
3848 testTxMultisign(all);
3849 }
3850
3851 void
3853 {
3854 using namespace jtx;
3855 auto const all = testableAmendments();
3856
3857 testToStrand(all);
3858 testRIPD1373(all);
3859 testLoop(all);
3860 }
3861
3862 void
3863 run() override
3864 {
3865 testOffers();
3866 testPaths();
3867 testFlow();
3871 testLock();
3872 testMultisign();
3873 testPayStrand();
3874 }
3875};
3876
3877BEAST_DEFINE_TESTSUITE_PRIO(AMMExtendedMPT, app, xrpl, 1);
3878
3879} // 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
beast::Journal journal(std::string const &name)
Definition Log.cpp:137
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.
json::Value getJson(JsonOptions) const override
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 OpenLedger & getOpenLedger()=0
virtual Logs & getLogs()=0
SLE::pointer peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
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
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
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
void memoize(Account const &account)
Associate AccountID with account.
Definition Env.cpp:174
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
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Definition mpt.h:512
void authorize(MPTAuthorize const &arg=MPTAuthorize{}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:459
Converts to MPT Issue or STAmount.
Set a multisignature on a JTx.
Definition multisign.h:53
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
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
@ 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
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
auto const kMptDexFlags
Definition mpt.h:47
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
STPathElement allPathElements(AccountID const &a, Asset const &asset)
void testHelper2TokensMix(TTester &&tester)
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
void testHelper3TokensMix(TTester &&tester)
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)
static auto gAmmmpt
Definition AMMTest.h:46
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)
std::tuple< STPathSet, STAmount, STAmount > findPathsByElement(jtx::Env &env, jtx::Account const &src, jtx::Account const &dst, STAmount const &saDstAmount, std::optional< STAmount > const &saSendMax, std::optional< STPathElement > const &srcElement, std::optional< AccountID > const &srcIssuer, std::optional< UInt256 > const &domain)
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
STPath stpath(Args const &... args)
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
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
AccountID const & xrpAccount()
Compute AccountID from public key.
@ tecLOCKED
Definition TER.h:369
@ tecPATH_PARTIAL
Definition TER.h:293
@ tecPATH_DRY
Definition TER.h:305
@ tecNO_AUTH
Definition TER.h:311
@ tecUNFUNDED_OFFER
Definition TER.h:295
@ tecKILLED
Definition TER.h:327
@ tecNO_PERMISSION
Definition TER.h:316
@ tesSUCCESS
Definition TER.h:253
Tests of AMM MPT that use offers.
void testOfferCrossWithXRP(FeatureBitset features)
void testSellFlagBasic(FeatureBitset features)
void testSellWithFillOrKill(FeatureBitset features)
void testBridgedCross(FeatureBitset features)
void testCrossCurrencyEndXRP(FeatureBitset features)
void testCurrencyConversionEntire(FeatureBitset features)
void testTxMultisign(FeatureBitset features)
void testDirectToDirectPath(FeatureBitset features)
void testOffersWhenLocked(FeatureBitset features)
void testFalseDry(FeatureBitset features)
void testCrossCurrencyBridged(FeatureBitset features)
void testOfferFeesConsumeFunds(FeatureBitset features)
void testRequireAuth(FeatureBitset features)
void testCurrencyConversionInParts(FeatureBitset features)
void testOfferCreateThenCross(FeatureBitset features)
void run() override
Runs the suite.
void testStepLimit(FeatureBitset features)
void testSellFlagExceedLimit(FeatureBitset features)
void testFillModes(FeatureBitset features)
void testPayment(FeatureBitset features)
void testSelfIssueOffer(FeatureBitset features)
void testOfferCrossWithLimitOverride(FeatureBitset features)
void testTransferRateOffer(FeatureBitset features)
void testRmFundedOffer(FeatureBitset features)
void testLoop(FeatureBitset features)
void testConvertAllOfAnAsset(FeatureBitset features)
void testCrossCurrencyStartXRP(FeatureBitset features)
void testToStrand(FeatureBitset features)
void testGlobalLock(FeatureBitset features)
void testTransferRateNoOwnerFee(FeatureBitset features)
void testMissingAuth(FeatureBitset features)
void testGatewayCrossCurrency(FeatureBitset features)
void testRIPD1373(FeatureBitset features)
void testIndividualLock(FeatureBitset features)
void testBookStep(FeatureBitset features)
Represents an XRP, IOU, or MPT quantity This customizes the string conversion and supports XRP conver...
T tie(T... args)