xrpld
Loading...
Searching...
No Matches
AMMMPT_test.cpp
1#include <test/jtx/AMM.h>
2#include <test/jtx/AMMTest.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/balance.h>
7#include <test/jtx/envconfig.h>
8#include <test/jtx/escrow.h>
9#include <test/jtx/fee.h>
10#include <test/jtx/flags.h>
11#include <test/jtx/mpt.h>
12#include <test/jtx/offer.h>
13#include <test/jtx/paths.h>
14#include <test/jtx/pay.h>
15#include <test/jtx/paychan.h>
16#include <test/jtx/rate.h>
17#include <test/jtx/sendmax.h>
18#include <test/jtx/seq.h>
19#include <test/jtx/tags.h>
20#include <test/jtx/ter.h>
21#include <test/jtx/trust.h>
22#include <test/jtx/txflags.h>
23#include <test/jtx/vault.h>
24
25#include <xrpl/basics/base_uint.h>
26#include <xrpl/basics/chrono.h>
27#include <xrpl/beast/unit_test/suite.h>
28#include <xrpl/beast/utility/Journal.h>
29#include <xrpl/json/json_value.h>
30#include <xrpl/ledger/ApplyView.h>
31#include <xrpl/ledger/helpers/AMMHelpers.h>
32#include <xrpl/ledger/helpers/MPTokenHelpers.h>
33#include <xrpl/ledger/helpers/TokenHelpers.h>
34#include <xrpl/protocol/AMMCore.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/MPTAmount.h>
40#include <xrpl/protocol/MPTIssue.h>
41#include <xrpl/protocol/Protocol.h>
42#include <xrpl/protocol/Quality.h>
43#include <xrpl/protocol/SField.h>
44#include <xrpl/protocol/STAmount.h>
45#include <xrpl/protocol/SeqProxy.h>
46#include <xrpl/protocol/TER.h>
47#include <xrpl/protocol/TxFlags.h>
48#include <xrpl/protocol/UintTypes.h>
49#include <xrpl/protocol/XRPAmount.h>
50#include <xrpl/protocol/jss.h>
51#include <xrpl/tx/Transactor.h>
52#include <xrpl/tx/transactors/dex/AMMBid.h>
53
54#include <array>
55#include <chrono>
56#include <cstdint>
57#include <functional>
58#include <initializer_list>
59#include <memory>
60#include <optional>
61#include <string>
62#include <type_traits>
63#include <utility>
64#include <vector>
65
66namespace xrpl::test {
67
73{
74private:
75 void
77 {
78 testcase("Instance Create");
79
80 using namespace jtx;
81
82 // XRP to MPT
83 testAMM(
84 [&](AMM& ammAlice, Env&) {
85 BEAST_EXPECT(ammAlice.expectBalances(
86 XRP(10'000), MPT(ammAlice[1])(10'000), IOUAmount{10'000'000}));
87 },
88 {{XRP(10'000), gAmmmpt(10'000)}});
89
90 // IOU to MPT
91 testAMM(
92 [&](AMM& ammAlice, Env&) {
93 BEAST_EXPECT(ammAlice.expectBalances(
94 USD(20'000), MPT(ammAlice[1])(20'000), IOUAmount{20'000}));
95 },
96 {{USD(20'000), gAmmmpt(20'000)}});
97
98 // MPT to MPT
99 testAMM(
100 [&](AMM& ammAlice, Env&) {
101 BEAST_EXPECT(ammAlice.expectBalances(
102 MPT(ammAlice[0])(20'000), MPT(ammAlice[1])(20'000), IOUAmount{20'000}));
103 },
104 {{gAmmmpt(20'000), gAmmmpt(20'000)}});
105
106 // IOU to MPT + transfer fee
107 {
108 Env env{*this};
109 fund(env, gw_, {alice_}, {USD(20'000)}, Fund::All);
110 env(rate(gw_, 1.25));
111 env.close();
112 MPT const btc = MPTTester({
113 .env = env,
114 .issuer = gw_,
115 .holders = {alice_},
116 .transferFee = 1'500,
117 .pay = 30'000,
118 });
119 // no transfer fee on create
120 AMM const ammAlice(env, alice_, USD(20'000), btc(20'000));
121 BEAST_EXPECT(ammAlice.expectBalances(USD(20'000), btc(20'000), IOUAmount{20'000, 0}));
122 BEAST_EXPECT(expectHolding(env, alice_, USD(0)));
123 // alice initially had 30'000
124 BEAST_EXPECT(expectMPT(env, alice_, btc(10'000)));
125 }
126
127 // Require authorization is set, account is authorized
128 {
129 Env env{*this};
130 env.fund(XRP(30'000), gw_, alice_);
131 env.close();
132 env(fset(gw_, asfRequireAuth));
133 env(trust(alice_, gw_["USD"](30'000), 0));
134 env(trust(gw_, alice_["USD"](0), tfSetfAuth));
135 env(pay(gw_, alice_, USD(10'000)));
136 env.close();
137 MPT const btc = MPTTester({
138 .env = env,
139 .issuer = gw_,
140 .holders = {alice_},
141 .pay = 30'000,
142 .flags = tfMPTRequireAuth | kMptDexFlags,
143 .authHolder = true,
144 });
145 AMM const ammAlice(env, alice_, USD(10'000), btc(10'000));
146 }
147
148 // Cleared global freeze
149 {
150 Env env{*this};
151 env.fund(XRP(30'000), gw_, alice_);
152 MPTTester usd({
153 .env = env,
154 .issuer = gw_,
155 .holders = {alice_},
156 .pay = 30'000,
157 .flags = tfMPTCanLock | kMptDexFlags,
158 });
159 usd.set({.flags = tfMPTLock});
160 AMM const ammAliceFail(env, alice_, XRP(10'000), usd(10'000), Ter(tecLOCKED));
161 usd.set({.flags = tfMPTUnlock});
162 AMM const ammAlice(env, alice_, XRP(10'000), usd(10'000));
163 }
164 }
165
166 void
168 {
169 testcase("Invalid Instance");
170
171 using namespace jtx;
172
173 // Can't have both tokens the same MPT
174 {
175 Env env{*this};
176 env.fund(XRP(30'000), alice_, gw_);
177 env.close();
178 MPT const btc = MPTTester({
179 .env = env,
180 .issuer = gw_,
181 .holders = {alice_},
182 .transferFee = 1'500,
183 .pay = 40'000,
184 });
185 AMM const ammAlice(env, alice_, btc(20'000), btc(10'000), Ter(temBAD_AMM_TOKENS));
186 }
187
188 // MPTCanTrade is not set and AMM creator is not the issuer of MPT
189 {
190 Env env{*this};
191 env.fund(XRP(30'000), alice_, gw_);
192 env.close();
193 MPT const btc = MPTTester({
194 .env = env,
195 .issuer = gw_,
196 .holders = {alice_},
197 .pay = 40'000,
198 .flags = tfMPTCanLock,
199 });
200 AMM const ammAlice(env, alice_, btc(20'000), XRP(10'000), Ter(tecNO_PERMISSION));
201 }
202
203 // MPTokenIssuance doesn't exist
204 {
205 Env env{*this};
206 env.fund(XRP(30'000), alice_, gw_);
207 env.close();
208 AMM const ammAlice(
209 env, alice_, MPT(gw_, 1'000)(20'000), XRP(10'000), Ter(tecOBJECT_NOT_FOUND));
210 }
211
212 // MPToken doesn't exist and amm creator is not the issuer
213 {
214 Env env{*this};
215 env.fund(XRP(30'000), alice_, gw_);
216 env.close();
217 MPT const btc = MPTTester({
218 .env = env,
219 .issuer = gw_,
220 });
221 AMM const ammAlice(env, alice_, btc(20'000), XRP(10'000), Ter(tecNO_AUTH));
222 }
223
224 // Can't have zero or negative amounts
225 {
226 Env env{*this};
227 fund(env, gw_, {alice_}, {USD(20'000)}, Fund::All);
228 env(rate(gw_, 1.25));
229 env.close();
230 MPT const btc = MPTTester({
231 .env = env,
232 .issuer = gw_,
233 .holders = {alice_},
234 .transferFee = 1'500,
235 .pay = 30'000,
236 });
237
238 AMM const ammAlice(env, alice_, XRP(0), btc(10'000), Ter(temBAD_AMOUNT));
239 BEAST_EXPECT(!ammAlice.ammExists());
240 AMM const ammAlice1(env, alice_, XRP(10'000), btc(0), Ter(temBAD_AMOUNT));
241 BEAST_EXPECT(!ammAlice1.ammExists());
242 AMM const ammAlice2(env, alice_, USD(0), btc(10'000), Ter(temBAD_AMOUNT));
243 BEAST_EXPECT(!ammAlice2.ammExists());
244 AMM const ammAlice3(env, alice_, USD(10'000), btc(0), Ter(temBAD_AMOUNT));
245 BEAST_EXPECT(!ammAlice3.ammExists());
246 AMM const ammAlice4(env, alice_, XRP(-10'0000), btc(10'000), Ter(temBAD_AMOUNT));
247 BEAST_EXPECT(!ammAlice4.ammExists());
248 AMM const ammAlice5(env, alice_, XRP(10'000), btc(-10'000), Ter(temBAD_AMOUNT));
249 BEAST_EXPECT(!ammAlice5.ammExists());
250 AMM const ammAlice6(env, alice_, USD(-10'000), btc(10'000), Ter(temBAD_AMOUNT));
251 BEAST_EXPECT(!ammAlice6.ammExists());
252 AMM const ammAlice7(env, alice_, USD(10'000), btc(-10'000), Ter(temBAD_AMOUNT));
253 BEAST_EXPECT(!ammAlice7.ammExists());
254 }
255
256 // Bad MPT
257 {
258 Env env{*this};
259 fund(env, gw_, {alice_}, {USD(20'000)}, Fund::All);
260
261 AMM const ammAlice(env, alice_, XRP(10'000), MPT(badMPT())(100), Ter(temBAD_MPT));
262
263 BEAST_EXPECT(!ammAlice.ammExists());
264 }
265
266 // Insufficient MPT balance
267 {
268 Env env{*this};
269 fund(env, gw_, {alice_}, {USD(30'000)}, Fund::All);
270 MPT const btc = MPTTester({
271 .env = env,
272 .issuer = gw_,
273 .holders = {alice_},
274 .transferFee = 1'500,
275 .pay = 30'000,
276 });
277 AMM const ammAlice(env, alice_, XRP(10'000), btc(40'000), Ter(tecUNFUNDED_AMM));
278 BEAST_EXPECT(!ammAlice.ammExists());
279 }
280
281 // Invalid trading fee
282 {
283 Env env{*this};
284 fund(env, gw_, {alice_}, {USD(20'000)}, Fund::All);
285 MPT const btc = MPTTester({
286 .env = env,
287 .issuer = gw_,
288 .holders = {alice_},
289 .transferFee = 1'500,
290 .pay = 30'000,
291 });
292 AMM const ammAlice(
293 env,
294 alice_,
295 USD(10'000),
296 btc(10'000),
297 false,
298 65'001,
299 10,
300 std::nullopt,
301 std::nullopt,
302 std::nullopt,
303 Ter(temBAD_FEE));
304 BEAST_EXPECT(!ammAlice.ammExists());
305 }
306
307 // AMM already exists XRP/MPT
308 testAMM(
309 [&](AMM& ammAlice, Env& env) {
310 // Account bob1("bob1");
311 env.fund(XRP(30'000), bob_);
312 env.close();
313 AMM const ammBob(env, bob_, XRP(1'000), MPT(ammAlice[1])(1'000), Ter(tecDUPLICATE));
314 },
315 {{XRP(10'000), gAmmmpt(10'000)}});
316
317 // AMM already exists IOU/MPT
318 testAMM(
319 [&](AMM& ammAlice, Env& env) {
320 env.fund(XRP(30'000), bob_);
321 env.close();
322 env.trust(USD(10000), bob_);
323 env(pay(gw_, bob_, USD(100)));
324
325 AMM const ammBob(env, bob_, USD(1'000), MPT(ammAlice[1])(1'000), Ter(tecDUPLICATE));
326 },
327 {{USD(10'000), gAmmmpt(10'000)}});
328
329 // AMM already exists MPT/MPT
330 testAMM(
331 [&](AMM& ammAlice, Env& env) {
332 AMM const ammCarol(
333 env,
334 carol_,
335 MPT(ammAlice[0])(1'000),
336 MPT(ammAlice[1])(2'000),
338 },
339 {{gAmmmpt(20'000), gAmmmpt(10'000)}});
340
341 // MPTRequireAuth flag is set and AMM creator is not authorized
342 {
343 Env env{*this};
344 env.fund(XRP(30'000), gw_, alice_);
345 env.close();
346 env(fset(gw_, asfRequireAuth));
347 env(trust(alice_, gw_["USD"](30'000), 0));
348 env(trust(gw_, alice_["USD"](0), tfSetfAuth));
349 env.close();
350 env(pay(gw_, alice_, USD(10'000)));
351 env.close();
352 MPT const btc = MPTTester(
353 {.env = env, .issuer = gw_, .holders = {alice_}, .flags = tfMPTRequireAuth});
354 AMM const ammAlice(env, alice_, USD(10'000), btc(10'000), Ter(tecNO_AUTH));
355 BEAST_EXPECT(!ammAlice.ammExists());
356 }
357
358 // MPTLocked flag is set
359 {
360 Env env{*this};
361 fund(env, gw_, {alice_}, {USD(20'000)}, Fund::All);
362 MPTTester btc({
363 .env = env,
364 .issuer = gw_,
365 .holders = {alice_},
366 .pay = 30'000,
367 .flags = tfMPTCanLock | kMptDexFlags,
368 });
369 btc.set({.flags = tfMPTLock});
370 AMM const ammAlice(env, alice_, USD(10'000), btc(10'000), Ter(tecLOCKED));
371 BEAST_EXPECT(!ammAlice.ammExists());
372 }
373
374 // MPT individually locked
375 {
376 Env env{*this};
377 fund(env, gw_, {alice_, bob_}, {USD(20'000)}, Fund::All);
378 MPTTester btc({
379 .env = env,
380 .issuer = gw_,
381 .holders = {alice_, bob_},
382 .pay = 30'000,
383 .flags = tfMPTCanLock | kMptDexFlags,
384 });
385 btc.set({.holder = alice_, .flags = tfMPTLock});
386
387 // alice's token is locked
388 AMM const ammAlice(env, alice_, USD(10'000), btc(10'000), Ter(tecLOCKED));
389 BEAST_EXPECT(!ammAlice.ammExists());
390
391 // bob can create
392 AMM const ammBob(env, bob_, USD(10'000), btc(10'000));
393 BEAST_EXPECT(ammBob.ammExists());
394 }
395
396 // OutstandingAmount > MaximumAmount
397 {
398 Env env{*this};
399 env.fund(XRP(1'000), gw_, alice_);
400
401 MPTTester const btc({.env = env, .issuer = gw_, .holders = {alice_}, .maxAmt = 100});
402
403 // OutstandingAmount is 0, issuer issues 10 over MaximumAmount
404 AMM const amm(env, gw_, XRP(100), btc(110), Ter(tecUNFUNDED_AMM));
405
406 env(pay(gw_, alice_, btc(100)));
407
408 // OutstandingAmount is 100, issuer issues 100 over MaximumAmount
409 AMM const amm1(env, gw_, XRP(100), btc(100), Ter(tecUNFUNDED_AMM));
410 // This is fine - alice transfers 100 to AMM. OutstandingAmount
411 // is 100.
412 AMM const ammAlice(env, alice_, XRP(100), btc(100));
413 }
414 }
415
416 void
418 {
419 testcase("Invalid Deposit");
420
421 using namespace jtx;
422
423 testAMM(
424 [&](AMM& ammAlice, Env& env) {
425 // LPTokenOut can not be zero
426 ammAlice.deposit(alice_, 0, std::nullopt, std::nullopt, Ter(temBAD_AMM_TOKENS));
427
428 // LPTokenOut can not be negative
429 ammAlice.deposit(
430 alice_, IOUAmount{-1}, std::nullopt, std::nullopt, Ter(temBAD_AMM_TOKENS));
431
432 // LPTokenOut can not be MPT
433 {
435 jv[jss::Account] = alice_.human();
436 jv[jss::TransactionType] = jss::AMMDeposit;
437 jv[jss::Asset] = STIssue(sfAsset, XRP).getJson(JsonOptions::Values::None);
438 jv[jss::Asset2] =
439 STIssue(sfAsset, MPT(ammAlice[1])).getJson(JsonOptions::Values::None);
440 jv[jss::LPTokenOut] =
441 MPT(ammAlice[1])(100).value().getJson(JsonOptions::Values::None);
442 jv[jss::Flags] = tfLPToken;
443 env(jv, Ter(telENV_RPC_FAILED));
444 }
445
446 // Provided LPTokenOut does not match AMM pool's LPToken
447 // asset
448 {
450 jv[jss::Account] = alice_.human();
451 jv[jss::TransactionType] = jss::AMMDeposit;
452 jv[jss::Asset] = STIssue(sfAsset, XRP).getJson(JsonOptions::Values::None);
453 jv[jss::Asset2] =
454 STIssue(sfAsset, MPT(ammAlice[1])).getJson(JsonOptions::Values::None);
455 jv[jss::LPTokenOut] = USD(100).value().getJson(JsonOptions::Values::None);
456 jv[jss::Flags] = tfLPToken;
457 env(jv, Ter(temBAD_AMM_TOKENS));
458 }
459
460 // Invalid trading fee
461 ammAlice.deposit(
462 carol_,
463 std::nullopt,
464 XRP(200),
465 MPT(ammAlice[1])(200),
466 std::nullopt,
467 tfTwoAssetIfEmpty,
468 std::nullopt,
469 std::nullopt,
470 10'000,
471 Ter(temBAD_FEE));
472
473 // Invalid tokens
474 {
475 auto const mpt1 = MPTIssue{MPTID(0xabc)};
476 auto const mpt2 = MPTIssue{MPTID(0xdef)};
477 ammAlice.deposit(
478 alice_,
479 1'000,
480 std::nullopt,
481 std::nullopt,
482 std::nullopt,
483 std::nullopt,
484 {{mpt1, mpt2}},
485 std::nullopt,
486 std::nullopt,
487 Ter(terNO_AMM));
488 }
489
490 // invalid MPT
491 ammAlice.deposit(
492 alice_, badMPT(), std::nullopt, std::nullopt, std::nullopt, Ter(temBAD_MPT));
493 ammAlice.deposit(
494 alice_, XRP(100), badMPT(), std::nullopt, std::nullopt, Ter(temBAD_MPT));
495
496 // MPTokenIssuance object doesn't exist
497 ammAlice.deposit(
498 alice_,
499 XRP(100),
500 MPT(gw_, 1'000)(100),
501 std::nullopt,
502 tfTwoAsset,
504
505 // MPToken object doesn't exist
506 env.fund(XRP(1'000), bob_);
507 ammAlice.deposit(
508 bob_,
509 XRP(100),
510 MPT(ammAlice[1])(200),
511 std::nullopt,
512 tfTwoAsset,
513 Ter(tecNO_AUTH));
514
515 MPTTester const btc({
516 .env = env,
517 .issuer = gw_,
518 .holders = {alice_, carol_},
519 .flags = tfMPTCanLock | kMptDexFlags,
520 .authHolder = true,
521 });
522
523 // Depositing mismatched token, invalid Asset1In.issue
524 ammAlice.deposit(
525 alice_,
526 btc(100),
527 std::nullopt,
528 std::nullopt,
529 std::nullopt,
531
532 // Depositing mismatched token, invalid Asset2In.issue
533 ammAlice.deposit(
534 alice_, XRP(100), btc(100), std::nullopt, std::nullopt, Ter(temBAD_AMM_TOKENS));
535
536 // Assets can not be the same
537 ammAlice.deposit(
538 alice_,
539 MPT(ammAlice[1])(100),
540 MPT(ammAlice[1])(200),
541 std::nullopt,
542 tfTwoAsset,
544
545 // Invalid amount value
546 ammAlice.deposit(
547 alice_,
548 MPT(ammAlice[1])(0),
549 std::nullopt,
550 std::nullopt,
551 std::nullopt,
553 ammAlice.deposit(
554 alice_,
555 MPT(ammAlice[1])(-1'000),
556 std::nullopt,
557 std::nullopt,
558 std::nullopt,
560
561 // Invalid Account
562 {
563 Account bad("bad");
564 env.memoize(bad);
565 ammAlice.deposit(
566 bad,
567 std::nullopt,
568 MPT(ammAlice[1])(1'000),
569 std::nullopt,
570 std::nullopt,
571 std::nullopt,
572 std::nullopt,
573 Seq(1),
574 std::nullopt,
576 }
577
578 // Invalid AMM
579 ammAlice.deposit(
580 alice_,
581 1'000,
582 std::nullopt,
583 std::nullopt,
584 std::nullopt,
585 std::nullopt,
586 {{MPT(btc), MPT(ammAlice[1])}},
587 std::nullopt,
588 std::nullopt,
589 Ter(terNO_AMM));
590
591 // Single deposit: 100000 tokens worth of MPT
592 // Amount to deposit exceeds Max
593 ammAlice.deposit(
594 carol_,
595 100'000,
596 MPT(ammAlice[1])(200),
597 std::nullopt,
598 std::nullopt,
599 std::nullopt,
600 std::nullopt,
601 std::nullopt,
602 std::nullopt,
604
605 // Single deposit: 100000 tokens worth of XRP
606 // Amount to deposit exceeds Max
607 ammAlice.deposit(
608 carol_,
609 100'000,
610 XRP(200),
611 std::nullopt,
612 std::nullopt,
613 std::nullopt,
614 std::nullopt,
615 std::nullopt,
616 std::nullopt,
618
619 // Deposit amount is invalid
620 ammAlice.deposit(
621 alice_,
622 MPT(ammAlice[1])(0),
623 std::nullopt,
624 STAmount{ammAlice.lptIssue(), 1, -1},
625 std::nullopt,
627 // Calculated amount is 0
628 ammAlice.deposit(
629 alice_,
630 MPT(ammAlice[1])(0),
631 std::nullopt,
632 STAmount{ammAlice.lptIssue(), 2'000, -6},
633 std::nullopt,
635
636 // Tiny deposit
637 ammAlice.deposit(
638 carol_,
639 IOUAmount{1, -10},
640 std::nullopt,
641 std::nullopt,
643
644 // Deposit non-empty AMM
645 ammAlice.deposit(
646 carol_,
647 XRP(100),
648 MPT(ammAlice[1])(100),
649 std::nullopt,
650 tfTwoAssetIfEmpty,
652 },
653 {{XRP(10'000), gAmmmpt(10'000)}});
654
655 // Tiny deposit
656 testAMM(
657 [&](AMM& ammAlice, Env& env) {
658 auto const enabledV13 = env.current()->rules().enabled(fixAMMv1_3);
659 auto const err = !enabledV13 ? Ter(temBAD_AMOUNT) : Ter(tesSUCCESS);
660 // Pre-amendment XRP deposit side is rounded to 0
661 // and deposit fails.
662 ammAlice.deposit(carol_, IOUAmount{1, -1}, std::nullopt, std::nullopt, err);
663 },
664 {{XRP(10'000), gAmmmpt(10'000)}},
665 0,
666 std::nullopt,
667 {features, features - fixAMMv1_3});
668
669 // Invalid AMM
670 testAMM(
671 [&](AMM& ammAlice, Env& env) {
672 ammAlice.withdrawAll(alice_);
673 ammAlice.deposit(alice_, 10'000, std::nullopt, std::nullopt, Ter(terNO_AMM));
674 },
675 {{XRP(10'000), gAmmmpt(10'000)}});
676
677 // Single deposit MPT with eprice
678 // the calculated amount to deposit is negative.
679 testAMM(
680 [&](AMM& ammAlice, Env& env) {
681 ammAlice.deposit(
682 carol_,
683 MPT(ammAlice[1])(100),
684 std::nullopt,
685 STAmount{ammAlice.lptIssue(), 1, -1},
686 tfLimitLPToken,
688
689 // although we should use lptoken unit for eprice,
690 // we don't check the currency any more, we just use
691 // the value
692 ammAlice.deposit(
693 carol_,
694 MPT(ammAlice[1])(100),
695 std::nullopt,
696 STAmount{USD, 1, -1},
697 tfLimitLPToken,
699 },
700 {{USD(10'000), gAmmmpt(10'000)}});
701
702 // Globally locked MPT
703 {
704 Env env{*this};
705 fund(env, gw_, {alice_, carol_}, {USD(20'000)}, Fund::All);
706 MPTTester btc({
707 .env = env,
708 .issuer = gw_,
709 .holders = {alice_, carol_},
710 .pay = 30'000,
711 .flags = tfMPTCanLock | kMptDexFlags,
712 });
713
714 AMM ammAlice(env, alice_, USD(10'000), btc(10'000));
715 btc.set({.flags = tfMPTLock});
716
717 ammAlice.deposit(
718 carol_, btc(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecLOCKED));
719
720 ammAlice.deposit(
721 carol_, USD(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecLOCKED));
722
723 ammAlice.deposit(carol_, 1'000, std::nullopt, std::nullopt, Ter(tecLOCKED));
724
725 ammAlice.deposit(
726 carol_, USD(100), btc(100), std::nullopt, std::nullopt, Ter(tecLOCKED));
727 }
728
729 // Individually lock MPT or freeze IOU (AMM) with IOU/MPT AMM
730 {
731 Env env{*this, features};
732 fund(env, gw_, {alice_, carol_}, {USD(20'000)}, Fund::All);
733 MPTTester btc({
734 .env = env,
735 .issuer = gw_,
736 .holders = {alice_, carol_},
737 .pay = 30'000,
738 .flags = tfMPTCanLock | kMptDexFlags,
739 });
740
741 AMM ammAlice(env, alice_, USD(10'000), btc(10'000));
742
743 // Carol's mpt is locked
744 btc.set({.holder = carol_, .flags = tfMPTLock});
745
746 // Carol can not deposit locked mpt
747 ammAlice.deposit(
748 carol_, btc(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecLOCKED));
749
750 ammAlice.deposit(carol_, 1'000, std::nullopt, std::nullopt, Ter(tecLOCKED));
751
752 // Post-fixCleanup3_3_0 a locked holder cannot deposit the other
753 // (non-locked) token either, matching featureAMMClawback.
754 if (!features[featureAMMClawback] && !features[fixCleanup3_3_0])
755 {
756 ammAlice.deposit(carol_, USD(100), std::nullopt, std::nullopt, std::nullopt);
757 }
758 else
759 {
760 ammAlice.deposit(
761 carol_, USD(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecLOCKED));
762 }
763
764 // Alice can deposit because she's not individually locked
765 ammAlice.deposit(alice_, btc(100), std::nullopt, std::nullopt, std::nullopt);
766 ammAlice.deposit(alice_, 1'000, std::nullopt, std::nullopt);
767 ammAlice.deposit(alice_, USD(100), std::nullopt, std::nullopt, std::nullopt);
768
769 // Unlock
770 btc.set({.holder = carol_, .flags = tfMPTUnlock});
771 // Carol can deposit after unlock
772 ammAlice.deposit(carol_, btc(100), std::nullopt, std::nullopt, std::nullopt);
773 ammAlice.deposit(carol_, 1'000, std::nullopt, std::nullopt);
774
775 // Individually frozen AMM
776 env(trust(
777 gw_, STAmount{Issue{gw_["USD"].currency, ammAlice.ammAccount()}, 0}, tfSetFreeze));
778 env.close();
779
780 // Post-fixCleanup3_3_0 the deposit checks both pool assets, so the
781 // non-frozen token cannot be deposited while the AMM's USD is frozen.
782 ammAlice.deposit(
783 carol_,
784 btc(100),
785 std::nullopt,
786 std::nullopt,
787 std::nullopt,
788 features[fixCleanup3_3_0] ? Ter(tecFROZEN) : Ter(tesSUCCESS));
789
790 // Cannot deposit frozen token
791 ammAlice.deposit(carol_, 1'000'000, std::nullopt, std::nullopt, Ter(tecFROZEN));
792 ammAlice.deposit(
793 carol_, USD(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecFROZEN));
794
795 // unfreeze IOU
796 env(trust(
797 gw_,
798 STAmount{Issue{gw_["USD"].currency, ammAlice.ammAccount()}, 0},
799 tfClearFreeze));
800 env.close();
801 // Can deposit
802 ammAlice.deposit(carol_, 1'000, std::nullopt, std::nullopt);
803
804 // Individually lock AMM
805 btc.set({.holder = ammAlice.ammAccount(), .flags = tfMPTLock});
806
807 // Post-fixCleanup3_3_0 the non-locked token cannot be deposited
808 // while the AMM's BTC is locked.
809 ammAlice.deposit(
810 carol_,
811 USD(100),
812 std::nullopt,
813 std::nullopt,
814 std::nullopt,
815 features[fixCleanup3_3_0] ? Ter(tecLOCKED) : Ter(tesSUCCESS));
816
817 // Can not deposit locked token
818 ammAlice.deposit(carol_, 1'000, std::nullopt, std::nullopt, Ter(tecLOCKED));
819 ammAlice.deposit(
820 carol_, btc(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecLOCKED));
821
822 // Unlock AMM MPT
823 btc.set({.holder = ammAlice.ammAccount(), .flags = tfMPTUnlock});
824 // can deposit
825 ammAlice.deposit(carol_, 1'000, std::nullopt, std::nullopt);
826 ammAlice.deposit(carol_, btc(100), std::nullopt, std::nullopt, std::nullopt);
827 }
828
829 // Individually lock MPT (AMM) account with MPT/MPT AMM.
830 // This block always runs with all amendments (incl. fixCleanup3_3_0),
831 // so the deposit checks both pool assets unconditionally.
832 {
833 Env env{*this};
834 env.fund(XRP(10'000), gw_, alice_, carol_);
835 env.close();
836 MPTTester btc({
837 .env = env,
838 .issuer = gw_,
839 .holders = {alice_, carol_},
840 .pay = 30'000,
841 .flags = tfMPTCanLock | kMptDexFlags,
842 });
843 MPTTester usd({
844 .env = env,
845 .issuer = gw_,
846 .holders = {alice_, carol_},
847 .pay = 40'000,
848 .flags = tfMPTCanLock | kMptDexFlags,
849 });
850
851 AMM ammAlice(env, alice_, usd(10'000), btc(10'000));
852
853 // Carol's BTC is locked
854 btc.set({.holder = carol_, .flags = tfMPTLock});
855 ammAlice.deposit(
856 carol_, usd(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecLOCKED));
857
858 ammAlice.deposit(
859 carol_, btc(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecLOCKED));
860
861 // Unlock carol's BTC
862 btc.set({.holder = carol_, .flags = tfMPTUnlock});
863
864 // Can deposit
865 ammAlice.deposit(carol_, usd(100), std::nullopt, std::nullopt, std::nullopt);
866 ammAlice.deposit(carol_, btc(100), std::nullopt, std::nullopt, std::nullopt);
867
868 // Individually lock MPT BTC (AMM) account
869 btc.set({.holder = ammAlice.ammAccount(), .flags = tfMPTLock});
870
871 // Post-fixCleanup3_3_0 the non-locked token USD cannot be deposited
872 // while the AMM's BTC is locked.
873 ammAlice.deposit(
874 carol_, usd(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecLOCKED));
875
876 // Can not deposit locked token BTC
877 ammAlice.deposit(carol_, 1'000, std::nullopt, std::nullopt, Ter(tecLOCKED));
878 ammAlice.deposit(
879 carol_, btc(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecLOCKED));
880
881 // Unlock AMM MPT BTC
882 btc.set({.holder = ammAlice.ammAccount(), .flags = tfMPTUnlock});
883 // Can deposit BTC
884 ammAlice.deposit(carol_, 1'000, std::nullopt, std::nullopt);
885 ammAlice.deposit(carol_, btc(100), std::nullopt, std::nullopt, std::nullopt);
886
887 // Individually Lock MPT USD (AMM) account
888 usd.set({.holder = ammAlice.ammAccount(), .flags = tfMPTLock});
889
890 // Post-fixCleanup3_3_0 the non-locked token BTC cannot be deposited
891 // while the AMM's USD is locked.
892 ammAlice.deposit(
893 carol_, btc(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecLOCKED));
894
895 // Can not deposit locked token USD
896 ammAlice.deposit(carol_, 1'000, std::nullopt, std::nullopt, Ter(tecLOCKED));
897 ammAlice.deposit(
898 carol_, usd(100), std::nullopt, std::nullopt, std::nullopt, Ter(tecLOCKED));
899
900 // Unlock AMM MPT USD
901 usd.set({.holder = ammAlice.ammAccount(), .flags = tfMPTUnlock});
902 // Can deposit USD
903 ammAlice.deposit(carol_, 1'000, std::nullopt, std::nullopt);
904 ammAlice.deposit(carol_, usd(100), std::nullopt, std::nullopt, std::nullopt);
905 }
906
907 // Deposit unauthorized token
908 {
909 Env env{*this, features};
910 Account const gw("gateway"), alice{"alice"}, carol{"carol"};
911 env.fund(XRP(30'000), alice, carol, gw);
912 env.close();
913
914 MPTTester btc(env, gw, {.holders = {alice, carol}, .fund = false});
915 btc.create({
916 .maxAmt = 1'000'000,
917 .authorize = {{alice}},
918 .pay = {{{alice}, 10'000}},
919 .flags = tfMPTRequireAuth | kMptDexFlags,
920 .authHolder = true,
921 });
922
923 AMM amm(env, alice, XRP(10'000), btc(10'000));
924 env.close();
925
926 // Post-fixCleanup3_3_0 the deposit requires authorization for both
927 // pool assets, so the unauthorized MPT blocks the XRP deposit too.
928 if (!features[featureAMMClawback] && !features[fixCleanup3_3_0])
929 {
930 amm.deposit(carol, XRP(10), std::nullopt, std::nullopt, std::nullopt);
931 }
932 else
933 {
934 amm.deposit(
935 carol, XRP(10), std::nullopt, std::nullopt, std::nullopt, Ter(tecNO_AUTH));
936 }
937 }
938
939 // MPTCanTransfer is not set and the account is not the issuer of MPT
940 {
941 Env env{*this, features};
942 Account const gw("gateway"), alice{"alice"}, carol{"carol"};
943 env.fund(XRP(30'000), alice, carol, gw);
944 env.close();
945
946 MPTTester const btc({
947 .env = env,
948 .issuer = gw,
949 .holders = {alice},
950 .pay = 1'000,
951 .flags = tfMPTCanTrade,
952 });
953
954 AMM amm(env, gw, XRP(10'000), btc(10'000));
955
956 amm.deposit({.account = alice, .asset1In = btc(10), .err = Ter(tecNO_AUTH)});
957 }
958
959 // Insufficient XRP balance
960 testAMM(
961 [&](AMM& ammAlice, Env& env) {
962 env.fund(XRP(1'000), bob_);
963 env.close();
964 ammAlice.deposit(bob_, XRP(10));
965 ammAlice.deposit(
966 bob_,
967 XRP(1'000),
968 std::nullopt,
969 std::nullopt,
970 std::nullopt,
972 },
973 {{XRP(10'000), gAmmmpt(10'000)}});
974
975 // Insufficient MPT balance
976 testAMM(
977 [&](AMM& ammAlice, Env& env) {
978 ammAlice.deposit(
979 carol_,
980 MPT(ammAlice[1])(450'000),
981 std::nullopt,
982 std::nullopt,
983 std::nullopt,
985 },
986 {{XRP(10'000), gAmmmpt(10'000)}});
987
988 // Insufficient IOU balance
989 testAMM(
990 [&](AMM& ammAlice, Env& env) {
991 fund(env, gw_, {bob_}, {USD(1'000)}, Fund::Acct);
992 ammAlice.deposit(
993 bob_,
994 USD(1'001),
995 std::nullopt,
996 std::nullopt,
997 std::nullopt,
999 },
1000 {{USD(1000), gAmmmpt(1000)}});
1001
1002 // Insufficient MPT balance by tokens
1003 {
1004 Env env{*this};
1005 env.fund(XRP(30'000), alice_, bob_, gw_);
1006 env.close();
1007 MPT const btc = MPTTester({
1008 .env = env,
1009 .issuer = gw_,
1010 .holders = {alice_, bob_},
1011 .transferFee = 1'500,
1012 .pay = 1000,
1013 });
1014 AMM ammAlice(env, alice_, XRP(20'000), btc(1000));
1015 ammAlice.deposit(
1016 bob_,
1017 10'000'000,
1018 std::nullopt,
1019 std::nullopt,
1020 std::nullopt,
1021 std::nullopt,
1022 std::nullopt,
1023 std::nullopt,
1024 std::nullopt,
1026 }
1027
1028 // Insufficient reserve, XRP/MPT
1029 {
1030 Env env(*this, features);
1031 auto const startingXrp = reserve(env, 4) + env.current()->fees().base * 4;
1032 env.fund(XRP(10'000), gw_);
1033 env.fund(XRP(10'000), alice_);
1034 env.fund(startingXrp, carol_);
1035
1036 MPT const btc = MPTTester({
1037 .env = env,
1038 .issuer = gw_,
1039 .holders = {alice_, carol_},
1040 .transferFee = 1'500,
1041 .pay = 40'000,
1042 });
1043
1044 env(offer(carol_, XRP(100), btc(101)));
1045 AMM ammAlice(env, alice_, XRP(1000), btc(1000));
1046 ammAlice.deposit(
1047 carol_,
1048 XRP(100),
1049 std::nullopt,
1050 std::nullopt,
1051 std::nullopt,
1053
1054 env(offer(carol_, XRP(100), btc(102)));
1055 ammAlice.deposit(
1056 carol_,
1057 btc(100),
1058 std::nullopt,
1059 std::nullopt,
1060 std::nullopt,
1062 }
1063
1064 // Invalid min
1065 testAMM(
1066 [&](AMM& ammAlice, Env& env) {
1067 // min tokens can't be <= zero
1068 ammAlice.deposit(carol_, 0, XRP(100), tfSingleAsset, Ter(temBAD_AMM_TOKENS));
1069 ammAlice.deposit(carol_, -1, XRP(100), tfSingleAsset, Ter(temBAD_AMM_TOKENS));
1070 ammAlice.deposit(
1071 carol_,
1072 0,
1073 XRP(100),
1074 MPT(ammAlice[1])(100),
1075 std::nullopt,
1076 tfTwoAsset,
1077 std::nullopt,
1078 std::nullopt,
1079 std::nullopt,
1081
1082 // min amounts can't be <= zero
1083 ammAlice.deposit(
1084 carol_,
1085 1'000,
1086 XRP(0),
1087 MPT(ammAlice[1])(100),
1088 std::nullopt,
1089 tfTwoAsset,
1090 std::nullopt,
1091 std::nullopt,
1092 std::nullopt,
1094 ammAlice.deposit(
1095 carol_,
1096 1'000,
1097 XRP(100),
1098 MPT(ammAlice[1])(-1),
1099 std::nullopt,
1100 tfTwoAsset,
1101 std::nullopt,
1102 std::nullopt,
1103 std::nullopt,
1105 ammAlice.deposit(
1106 carol_,
1107 1'000,
1108 XRP(100),
1109 badMPT(),
1110 std::nullopt,
1111 tfTwoAsset,
1112 std::nullopt,
1113 std::nullopt,
1114 std::nullopt,
1115 Ter(temBAD_MPT));
1116 },
1117 {{XRP(10'000), gAmmmpt(10'000)}});
1118
1119 // Min deposit
1120 testAMM(
1121 [&](AMM& ammAlice, Env& env) {
1122 // Equal deposit by tokens
1123 ammAlice.deposit(
1124 carol_,
1125 1'000'000,
1126 XRP(1'000),
1127 MPT(ammAlice[1])(1'001),
1128 std::nullopt,
1129 tfLPToken,
1130 std::nullopt,
1131 std::nullopt,
1132 std::nullopt,
1134 ammAlice.deposit(
1135 carol_,
1136 1'000'000,
1137 XRP(1'001),
1138 MPT(ammAlice[1])(1'000),
1139 std::nullopt,
1140 tfLPToken,
1141 std::nullopt,
1142 std::nullopt,
1143 std::nullopt,
1145 // Equal deposit by asset
1146 ammAlice.deposit(
1147 carol_,
1148 100'001,
1149 XRP(100),
1150 MPT(ammAlice[1])(100),
1151 std::nullopt,
1152 tfTwoAsset,
1153 std::nullopt,
1154 std::nullopt,
1155 std::nullopt,
1157 // Single deposit by asset
1158 ammAlice.deposit(
1159 carol_,
1160 488'090,
1161 XRP(1'000),
1162 std::nullopt,
1163 std::nullopt,
1164 tfSingleAsset,
1165 std::nullopt,
1166 std::nullopt,
1167 std::nullopt,
1169 },
1170 {{XRP(1000), gAmmmpt(1000)}});
1171
1172 // OutstandingAmount > MaximumAmount
1173 {
1174 Env env{*this};
1175 env.fund(XRP(1'000), gw_, alice_);
1176
1177 MPTTester const btc({.env = env, .issuer = gw_, .holders = {alice_}, .maxAmt = 100});
1178
1179 AMM amm(env, gw_, XRP(100), btc(90));
1180 // OutstandingAmount is 90, issuer issues 1 over MaximumAmount
1181 amm.deposit(
1182 DepositArg{.account = gw_, .asset1In = btc(11), .err = Ter(tecUNFUNDED_AMM)});
1183
1184 env(pay(gw_, alice_, btc(10)));
1185
1186 // OutstandingAmount is 100, issuer issues 10 over MaximumAmount
1187 amm.deposit(
1188 DepositArg{.account = gw_, .asset1In = btc(10), .err = Ter(tecUNFUNDED_AMM)});
1189 // This is fine - alice transfers 10 to AMM. OutstandingAmount
1190 // is 100.
1191 amm.deposit(DepositArg{.account = alice_, .asset1In = btc(10)});
1192 }
1193 }
1194
1195 void
1197 {
1198 testcase("Deposit");
1199
1200 using namespace jtx;
1201
1202 // Equal deposit: 1000000 tokens. XRP/MPT AMM.
1203 testAMM(
1204 [&](AMM& ammAlice, Env& env) {
1205 XRPAmount const baseFee{env.current()->fees().base};
1206 auto const carolXRP = env.balance(carol_, XRP);
1207 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
1208
1209 ammAlice.deposit(carol_, 1'000'000);
1210 BEAST_EXPECT(ammAlice.expectBalances(
1211 XRP(11'000), MPT(ammAlice[1])(11'000), IOUAmount{11'000'000, 0}));
1212
1213 // carol deposited 1000 XRP and pays the transaction fee
1214 env.require(Balance(carol_, carolXRP - XRP(1000) - drops(baseFee)));
1215 // carol deposited 1000 MPT
1216 env.require(Balance(carol_, carolMPT - MPT(ammAlice[1])(1000)));
1217 },
1218 {{XRP(10'000), gAmmmpt(10'000)}});
1219
1220 // single deposit MPT with eprice
1221 testAMM(
1222 [&](AMM& ammAlice, Env& env) {
1223 ammAlice.deposit(
1224 carol_,
1225 MPT(ammAlice[1])(100),
1226 std::nullopt,
1227 STAmount{ammAlice.lptIssue(), 1, -1},
1228 tfLimitLPToken);
1229
1230 // although we should use lptoken unit for eprice,
1231 // we don't check the currency any more, we just use
1232 // the value
1233 ammAlice.deposit(
1234 carol_,
1235 MPT(ammAlice[1])(100),
1236 std::nullopt,
1237 STAmount{USD, 1, -1},
1238 tfLimitLPToken);
1239 },
1240 {{USD(10'000'000), gAmmmpt(10'000)}});
1241
1242 // Equal deposit: 1000000 tokens. IOU/MPT combination
1243 {
1244 auto const test = [&](auto&& issue1, auto&& issue2) {
1245 Env env(*this);
1246 env.fund(XRP(30'000), alice_, carol_, gw_);
1247 env.close();
1248 auto const usd = issue1({
1249 .env = env,
1250 .token = "USD",
1251 .issuer = gw_,
1252 .holders = {alice_, carol_},
1253 .limit = 1'000'000,
1254 });
1255 auto const btc = issue2({
1256 .env = env,
1257 .token = "BTC",
1258 .issuer = gw_,
1259 .holders = {alice_, carol_},
1260 .limit = 1'000'000,
1261 });
1262 env(pay(gw_, alice_, btc(50000)));
1263 env(pay(gw_, carol_, btc(50000)));
1264 env(pay(gw_, alice_, usd(50000)));
1265 env(pay(gw_, carol_, usd(50000)));
1266 env.close();
1267
1268 auto ammAlice = AMM(env, alice_, usd(10000), btc(10000));
1269 auto const carolBTC = env.balance(carol_, btc);
1270 auto const carolUSD = env.balance(carol_, usd);
1271
1272 ammAlice.deposit(carol_, 1'000);
1273 BEAST_EXPECT(ammAlice.expectBalances(btc(11'000), usd(11'000), IOUAmount(11'000)));
1274
1275 env.require(Balance(carol_, carolBTC - btc(1000)));
1276 env.require(Balance(carol_, carolUSD - usd(1000)));
1277 };
1279 }
1280
1281 // Deposit 100MPT/100XRP. XRP/MPT AMM.
1282 testAMM(
1283 [&](AMM& ammAlice, Env& env) {
1284 XRPAmount const baseFee{env.current()->fees().base};
1285 auto const carolXRP = env.balance(carol_, XRP);
1286 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
1287
1288 ammAlice.deposit(carol_, MPT(ammAlice[1])(100), XRP(100));
1289 BEAST_EXPECT(ammAlice.expectBalances(
1290 XRP(10'100), MPT(ammAlice[1])(10'100), IOUAmount{10'100'000, 0}));
1291
1292 env.require(Balance(carol_, carolXRP - XRP(100) - drops(baseFee)));
1293 env.require(Balance(carol_, carolMPT - MPT(ammAlice[1])(100)));
1294 },
1295 {{XRP(10'000), gAmmmpt(10'000)}});
1296
1297 // Deposit MPT/IOU combination
1298 {
1299 auto const test = [&](auto&& issue1, auto&& issue2) {
1300 Env env(*this);
1301 env.fund(XRP(30'000), alice_, carol_, gw_);
1302 env.close();
1303 auto const usd = issue1({
1304 .env = env,
1305 .token = "USD",
1306 .issuer = gw_,
1307 .holders = {alice_, carol_},
1308 .limit = 1'000'000,
1309 });
1310 auto const btc = issue2({
1311 .env = env,
1312 .token = "BTC",
1313 .issuer = gw_,
1314 .holders = {alice_, carol_},
1315 .limit = 1'000'000,
1316 });
1317 env(pay(gw_, alice_, btc(50000)));
1318 env(pay(gw_, carol_, btc(50000)));
1319 env(pay(gw_, alice_, usd(50000)));
1320 env(pay(gw_, carol_, usd(50000)));
1321 env.close();
1322
1323 auto ammAlice = AMM(env, alice_, usd(10000), btc(10000));
1324 auto const carolBTC = env.balance(carol_, btc);
1325 auto const carolUSD = env.balance(carol_, usd);
1326 ammAlice.deposit(carol_, btc(100), usd(100));
1327
1328 BEAST_EXPECT(ammAlice.expectBalances(btc(10'100), usd(10'100), IOUAmount(10'100)));
1329
1330 env.require(Balance(carol_, carolBTC - btc(100)));
1331 env.require(Balance(carol_, carolUSD - usd(100)));
1332 };
1334 }
1335
1336 // Equal limit deposit.
1337 // Try to deposit 200MPT/100XRP. Is truncated to 100MPT/100XRP.
1338 testAMM(
1339 [&](AMM& ammAlice, Env& env) {
1340 XRPAmount const baseFee{env.current()->fees().base};
1341 auto const carolXRP = env.balance(carol_, XRP);
1342 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
1343
1344 ammAlice.deposit(carol_, MPT(ammAlice[1])(200), XRP(100));
1345 BEAST_EXPECT(ammAlice.expectBalances(
1346 XRP(10'100), MPT(ammAlice[1])(10'100), IOUAmount{10'100'000, 0}));
1347
1348 env.require(Balance(carol_, carolXRP - XRP(100) - drops(baseFee)));
1349 env.require(Balance(carol_, carolMPT - MPT(ammAlice[1])(100)));
1350 },
1351 {{XRP(10'000), gAmmmpt(10'000)}});
1352
1353 // Equal limit deposit. MPT/IOU combination.
1354 {
1355 auto const test = [&](auto&& issue1, auto&& issue2) {
1356 Env env(*this);
1357 env.fund(XRP(30'000), alice_, carol_, gw_);
1358 env.close();
1359 auto const usd = issue1({
1360 .env = env,
1361 .token = "USD",
1362 .issuer = gw_,
1363 .holders = {alice_, carol_},
1364 .limit = 1'000'000,
1365 });
1366 auto const btc = issue2({
1367 .env = env,
1368 .token = "BTC",
1369 .issuer = gw_,
1370 .holders = {alice_, carol_},
1371 .limit = 1'000'000,
1372 });
1373 env(pay(gw_, alice_, btc(50000)));
1374 env(pay(gw_, carol_, btc(50000)));
1375 env(pay(gw_, alice_, usd(50000)));
1376 env(pay(gw_, carol_, usd(50000)));
1377 env.close();
1378
1379 auto ammAlice = AMM(env, alice_, usd(10000), btc(10000));
1380 auto const carolBTC = env.balance(carol_, btc);
1381 auto const carolUSD = env.balance(carol_, usd);
1382 ammAlice.deposit(carol_, btc(200), usd(100));
1383 BEAST_EXPECT(ammAlice.expectBalances(btc(10'100), usd(10'100), IOUAmount(10'100)));
1384
1385 env.require(Balance(carol_, carolBTC - btc(100)));
1386 env.require(Balance(carol_, carolUSD - usd(100)));
1387 };
1389 }
1390
1391 // Single deposit: 1000 MPT into MPT/XRP
1392 testAMM(
1393 [&](AMM& ammAlice, Env& env) {
1394 XRPAmount const baseFee{env.current()->fees().base};
1395 auto const carolXRP = env.balance(carol_, XRP);
1396 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
1397
1398 ammAlice.deposit(carol_, MPT(ammAlice[1])(1000));
1399 BEAST_EXPECT(ammAlice.expectBalances(
1400 XRP(10'000), MPT(ammAlice[1])(11'000), IOUAmount{10'488'088'48170151, -8}));
1401
1402 env.require(Balance(carol_, carolXRP - drops(baseFee)));
1403 env.require(Balance(carol_, carolMPT - MPT(ammAlice[1])(1000)));
1404 },
1405 {{XRP(10'000), gAmmmpt(10'000)}});
1406
1407 // Single deposit: 1000 XRP into MPT/XRP
1408 testAMM(
1409 [&](AMM& ammAlice, Env& env) {
1410 XRPAmount const baseFee{env.current()->fees().base};
1411 auto const carolXRP = env.balance(carol_, XRP);
1412 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
1413
1414 ammAlice.deposit(carol_, XRP(1000));
1415 BEAST_EXPECT(ammAlice.expectBalances(
1416 XRP(11'000), MPT(ammAlice[1])(10'000), IOUAmount{10'488'088'48170151, -8}));
1417
1418 env.require(Balance(carol_, carolXRP - XRP(1000) - drops(baseFee)));
1419 env.require(Balance(carol_, carolMPT));
1420 },
1421 {{XRP(10'000), gAmmmpt(10'000)}});
1422
1423 // Single deposit: 1000 MPT0 into MPT/MPT
1424 testAMM(
1425 [&](AMM& ammAlice, Env& env) {
1426 auto const carolMPT0 = env.balance(carol_, MPT(ammAlice[0]));
1427 auto const carolMPT1 = env.balance(carol_, MPT(ammAlice[1]));
1428
1429 ammAlice.deposit(carol_, MPT(ammAlice[0])(1000));
1430 BEAST_EXPECT(ammAlice.expectBalances(
1431 MPT(ammAlice[0])(11'000),
1432 MPT(ammAlice[1])(10'000),
1433 IOUAmount{10'488'08848170151, -11}));
1434
1435 env.require(Balance(carol_, carolMPT0 - MPT(ammAlice[0])(1000)));
1436 env.require(Balance(carol_, carolMPT1));
1437 },
1438 {{gAmmmpt(10'000), gAmmmpt(10'000)}});
1439
1440 // Single deposit: 1000 MPT into MPT/IOU
1441 testAMM(
1442 [&](AMM& ammAlice, Env& env) {
1443 auto const carolMPT = env.balance(carol_, MPT(ammAlice[0]));
1444 auto const carolUSD = env.balance(carol_, USD);
1445
1446 ammAlice.deposit(carol_, MPT(ammAlice[0])(1000));
1447 BEAST_EXPECT(ammAlice.expectBalances(
1448 MPT(ammAlice[0])(11'000), USD(10'000), IOUAmount{10'488'08848170151, -11}));
1449
1450 env.require(Balance(carol_, carolMPT - MPT(ammAlice[0])(1000)));
1451 env.require(Balance(carol_, carolUSD));
1452 },
1453 {{gAmmmpt(10'000), USD(10'000)}});
1454
1455 // Single deposit: 1000 IOU into MPT/IOU
1456 testAMM(
1457 [&](AMM& ammAlice, Env& env) {
1458 auto const carolMPT = env.balance(carol_, MPT(ammAlice[0]));
1459 auto const carolUSD = env.balance(carol_, USD);
1460
1461 ammAlice.deposit(carol_, USD(1000));
1462 BEAST_EXPECT(ammAlice.expectBalances(
1463 MPT(ammAlice[0])(10'000),
1464 STAmount{USD, UINT64_C(10999'99999999999), -11},
1465 IOUAmount{10'488'08848170151, -11}));
1466
1467 env.require(Balance(carol_, carolMPT));
1468 env.require(
1469 Balance(carol_, carolUSD - STAmount{USD, UINT64_C(999'99999999999), -11}));
1470 },
1471 {{gAmmmpt(10'000), USD(10'000)}});
1472
1473 // Single deposit: 100000 tokens worth of MPT into XRP/MPT
1474 testAMM(
1475 [&](AMM& ammAlice, Env& env) {
1476 XRPAmount const baseFee{env.current()->fees().base};
1477 auto const carolXRP = env.balance(carol_, XRP);
1478 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
1479
1480 // The exact deposit is 201; rounding in the pool's favor at
1481 // the large mantissa takes one more unit.
1482 ammAlice.deposit(carol_, 100'000, MPT(ammAlice[1])(205));
1483 BEAST_EXPECT(ammAlice.expectBalances(
1484 XRP(10'000), MPT(ammAlice[1])(10'202), IOUAmount{10'100'000, 0}));
1485
1486 env.require(Balance(carol_, carolXRP - drops(baseFee)));
1487 env.require(Balance(carol_, carolMPT - MPT(ammAlice[1])(202)));
1488 },
1489 {{XRP(10'000), gAmmmpt(10'000)}});
1490
1491 // Single deposit: 100000 tokens worth of XRP into XRP/MPT
1492 testAMM(
1493 [&](AMM& ammAlice, Env& env) {
1494 XRPAmount const baseFee{env.current()->fees().base};
1495 auto const carolXRP = env.balance(carol_, XRP);
1496 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
1497
1498 // The exact deposit is 201 XRP; rounding in the pool's favor
1499 // at the large mantissa takes one more drop.
1500 ammAlice.deposit(carol_, 100'000, XRP(205));
1501 BEAST_EXPECT(ammAlice.expectBalances(
1502 XRPAmount{10'201'000'001}, MPT(ammAlice[1])(10'000), IOUAmount{10'100'000, 0}));
1503
1504 env.require(Balance(carol_, carolXRP - XRPAmount{201'000'001} - drops(baseFee)));
1505 env.require(Balance(carol_, carolMPT));
1506 },
1507 {{XRP(10'000), gAmmmpt(10'000)}});
1508
1509 // Single deposit: 100 tokens worth of MPT/IOU into pool of MPT/IOU
1510 // combination
1511 {
1512 auto const test = [&](auto&& issue1, auto&& issue2) {
1513 Env env(*this);
1514 env.fund(XRP(30'000), alice_, carol_, gw_);
1515 env.close();
1516 auto const usd = issue1({
1517 .env = env,
1518 .token = "USD",
1519 .issuer = gw_,
1520 .holders = {alice_, carol_},
1521 .limit = 1'000'000,
1522 });
1523 auto const btc = issue2({
1524 .env = env,
1525 .token = "BTC",
1526 .issuer = gw_,
1527 .holders = {alice_, carol_},
1528 .limit = 1'000'000,
1529 });
1530 env(pay(gw_, alice_, btc(50000)));
1531 env(pay(gw_, carol_, btc(50000)));
1532 env(pay(gw_, alice_, usd(50000)));
1533 env(pay(gw_, carol_, usd(50000)));
1534 env.close();
1535
1536 auto ammAlice = AMM(env, alice_, usd(10000), btc(10000));
1537 auto const carolBTC = env.balance(carol_, btc);
1538 auto const carolUSD = env.balance(carol_, usd);
1539
1540 ammAlice.deposit(carol_, 100, usd(205));
1541 auto const deltaUSD = [&] {
1542 if constexpr (std::is_same_v<MPT, std::decay_t<decltype(usd)>>)
1543 return usd(202);
1544 return usd(201);
1545 }();
1546 BEAST_EXPECT(ammAlice.expectBalances(
1547 btc(10'000), usd(10'000) + deltaUSD, IOUAmount{10'100, 0}));
1548
1549 env.require(Balance(carol_, carolBTC));
1550 env.require(Balance(carol_, carolUSD - deltaUSD));
1551 };
1553 }
1554
1555 // Single deposit with EP not exceeding specified:
1556 // 100 MPT with EP not to exceed 0.1 (AssetIn/TokensOut)
1557 // for XRP/MPT
1558 testAMM(
1559 [&](AMM& ammAlice, Env& env) {
1560 ammAlice.deposit(
1561 carol_,
1562 MPT(ammAlice[1])(100),
1563 std::nullopt,
1564 STAmount{ammAlice.lptIssue(), 1, -1});
1565
1566 BEAST_EXPECT(ammAlice.expectBalances(
1567 XRP(10'000), MPT(ammAlice[1])(10100), IOUAmount{10'049'875'62112089, -8}));
1568 },
1569 {{XRP(10'000), gAmmmpt(10'000)}});
1570
1571 // Single deposit with EP not exceeding specified:
1572 // 100 MPT with EP not to exceed 0.002004 (AssetIn/TokensOut)
1573 testAMM(
1574 [&](AMM& ammAlice, Env& env) {
1575 XRPAmount const baseFee{env.current()->fees().base};
1576 auto const carolXRP = env.balance(carol_, XRP);
1577 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
1578
1579 ammAlice.deposit(
1580 carol_,
1581 MPT(ammAlice[1])(100),
1582 std::nullopt,
1583 STAmount{ammAlice.lptIssue(), 2004, -6});
1584
1585 BEAST_EXPECT(ammAlice.expectBalances(
1586 XRP(10'000), MPT(ammAlice[1])(10'080), IOUAmount{10'039'920'3184089, -7}));
1587
1588 env.require(Balance(carol_, carolXRP - drops(baseFee)));
1589 env.require(Balance(carol_, carolMPT - MPT(ammAlice[1])(80)));
1590 },
1591 {{XRP(10'000), gAmmmpt(10'000)}});
1592
1593 // Single deposit with EP not exceeding specified:
1594 // 0 MPT with EP not to exceed 0.002004 (AssetIn/TokensOut)
1595 testAMM(
1596 [&](AMM& ammAlice, Env& env) {
1597 XRPAmount const baseFee{env.current()->fees().base};
1598 auto const carolXRP = env.balance(carol_, XRP);
1599 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
1600
1601 ammAlice.deposit(
1602 carol_,
1603 MPT(ammAlice[1])(0),
1604 std::nullopt,
1605 STAmount{ammAlice.lptIssue(), 2004, -6});
1606
1607 BEAST_EXPECT(ammAlice.expectBalances(
1608 XRP(10'000), MPT(ammAlice[1])(10'080), IOUAmount{10'039'920'3184089, -7}));
1609
1610 env.require(Balance(carol_, carolXRP - drops(baseFee)));
1611 env.require(Balance(carol_, carolMPT - MPT(ammAlice[1])(80)));
1612 },
1613 {{XRP(10'000), gAmmmpt(10'000)}});
1614
1615 // Single deposit with EP not exceeding specified:
1616 // 100 MPT with EP not to exceed 0.1 (AssetIn/TokensOut)
1617 // for IOU/MPT
1618 testAMM(
1619 [&](AMM& ammAlice, Env& env) {
1620 ammAlice.deposit(
1621 carol_,
1622 MPT(ammAlice[1])(100),
1623 std::nullopt,
1624 STAmount{ammAlice.lptIssue(), 1, -1});
1625
1626 BEAST_EXPECT(ammAlice.expectBalances(
1627 USD(10'000'000'000),
1628 MPT(ammAlice[1])(10100),
1629 IOUAmount{10'049'875'62112089, -8}));
1630 },
1631 {{USD(10'000'000'000), gAmmmpt(10'000)}});
1632
1633 // Single deposit with EP not exceeding specified:
1634 // 100 IOU with EP not to exceed 0.1 (AssetIn/TokensOut)
1635 // for IOU/MPT
1636 testAMM(
1637 [&](AMM& ammAlice, Env& env) {
1638 ammAlice.deposit(carol_, USD(100), std::nullopt, STAmount{USD, 1, -1});
1639
1640 BEAST_EXPECT(ammAlice.expectBalances(
1641 MPT(ammAlice[1])(10'000'000'000),
1642 USD(10100),
1643 IOUAmount{10'049'875'62112089, -8}));
1644 },
1645 {{USD(10'000), gAmmmpt(10'000'000'000)}});
1646
1647 // Single deposit with EP not exceeding specified:
1648 // 100 IOU with EP not to exceed 0.1 (AssetIn/TokensOut)
1649 // for MPT/MPT
1650 testAMM(
1651 [&](AMM& ammAlice, Env& env) {
1652 ammAlice.deposit(
1653 carol_,
1654 MPT(ammAlice[0])(100),
1655 std::nullopt,
1656 STAmount{ammAlice.lptIssue(), 1, -1});
1657
1658 BEAST_EXPECT(ammAlice.expectBalances(
1659 MPT(ammAlice[1])(10'000'000'000),
1660 MPT(ammAlice[0])(10100),
1661 IOUAmount{10'049'875'62112089, -8}));
1662 },
1663 {{gAmmmpt(10'000), gAmmmpt(10'000'000'000)}});
1664
1665 // MPT/MPT with transfer fee
1666 {
1667 Env env(*this);
1668 env.fund(XRP(30'000), gw_, alice_, carol_);
1669 env.close();
1670
1671 MPT const btc = MPTTester({
1672 .env = env,
1673 .issuer = gw_,
1674 .holders = {alice_, carol_},
1675 .transferFee = 25'000,
1676 .pay = 400'000,
1677 });
1678
1679 MPT const usd = MPTTester({
1680 .env = env,
1681 .issuer = gw_,
1682 .holders = {alice_, carol_},
1683 .transferFee = 25'000,
1684 .pay = 400'000,
1685 });
1686
1687 AMM ammAlice(env, alice_, usd(200'000), btc(5));
1688 BEAST_EXPECT(ammAlice.expectBalances(usd(200'000), btc(5), IOUAmount{1000, 0}));
1689
1690 ammAlice.deposit(carol_, 100, std::nullopt, std::nullopt);
1691 BEAST_EXPECT(ammAlice.expectBalances(usd(220'000), btc(6), IOUAmount{1100, 0}));
1692 }
1693
1694 // IOU/MPT with transfer fee
1695 {
1696 Env env(*this);
1697 env.fund(XRP(30'000), gw_, alice_, bob_, carol_);
1698 env.close();
1699
1700 env(rate(gw_, 1.25));
1701 env.close();
1702
1703 MPT const btc = MPTTester({
1704 .env = env,
1705 .issuer = gw_,
1706 .holders = {alice_, bob_, carol_},
1707 .transferFee = 25'000,
1708 .pay = 400'000,
1709 });
1710
1711 auto const usd = gw_["USD"];
1712 env.trust(usd(1000000), alice_);
1713 env(pay(gw_, alice_, usd(1000000)));
1714 env.trust(usd(1000000), bob_);
1715 env(pay(gw_, bob_, usd(1000000)));
1716 env.trust(usd(1000000), carol_);
1717 env(pay(gw_, carol_, usd(1000000)));
1718 env.close();
1719
1720 // IOU/MPT
1721 AMM ammAlice(env, alice_, usd(200'000), btc(5));
1722 BEAST_EXPECT(ammAlice.expectBalances(usd(200'000), btc(5), IOUAmount{1000, 0}));
1723 ammAlice.deposit(carol_, 100, std::nullopt, std::nullopt);
1724 BEAST_EXPECT(ammAlice.expectBalances(usd(220'000), btc(6), IOUAmount{1100, 0}));
1725
1726 MPT const eth = MPTTester({
1727 .env = env,
1728 .issuer = gw_,
1729 .holders = {alice_, bob_, carol_},
1730 .transferFee = 25'000,
1731 .pay = 400'000,
1732 });
1733
1734 // MPT/IOU
1735 AMM ammBob(env, bob_, eth(20'000), usd(0.5));
1736 BEAST_EXPECT(ammBob.expectBalances(eth(20'000), usd(0.5), IOUAmount{100, 0}));
1737 ammBob.deposit(carol_, 10);
1738 BEAST_EXPECT(ammBob.expectBalances(eth(22'000), usd(0.55), IOUAmount{110, 0}));
1739 }
1740
1741 // Tiny deposits for IOU/MPT
1742 testAMM(
1743 [&](AMM& ammAlice, Env& env) {
1744 // tiny amount causes MPT to deposit rounded to 0
1745 ammAlice.deposit(carol_, IOUAmount{1, -3}, std::nullopt, std::nullopt);
1746 BEAST_EXPECT(ammAlice.expectBalances(
1747 STAmount{USD, UINT64_C(10'000'001), -3},
1748 MPT(ammAlice[1])(10'001),
1749 IOUAmount{10'000'001, -3}));
1750
1751 ammAlice.deposit(carol_, IOUAmount{1});
1752 BEAST_EXPECT(ammAlice.expectBalances(
1753 STAmount{USD, UINT64_C(10'001'001), -3},
1754 MPT(ammAlice[1])(10'003),
1755 IOUAmount{10'001'001, -3}));
1756 },
1757 {{USD(10'000), gAmmmpt(10'000)}});
1758 testAMM(
1759 [&](AMM& ammAlice, Env& env) {
1760 ammAlice.deposit(carol_, STAmount{USD, 1, -10});
1761 BEAST_EXPECT(ammAlice.expectBalances(
1762 STAmount{USD, UINT64_C(10'000'00000000008), -11},
1763 MPT(ammAlice[1])(10'000),
1764 IOUAmount{1'000'000'000000004, -11}));
1765
1766 ammAlice.deposit(carol_, MPT(ammAlice[1])(1));
1767 BEAST_EXPECT(ammAlice.expectBalances(
1768 STAmount{USD, UINT64_C(10'000'00000000008), -11},
1769 MPT(ammAlice[1])(10'001),
1770 IOUAmount{10'000'49998750066, -11}));
1771 },
1772 {{USD(10'000), gAmmmpt(10'000)}});
1773
1774 // Tiny deposits for XRP/MPT
1775 testAMM(
1776 [&](AMM& ammAlice, Env& env) {
1777 ammAlice.deposit(carol_, XRPAmount{1});
1778 BEAST_EXPECT(ammAlice.expectBalances(
1779 XRPAmount{10'000'000'001},
1780 MPT(ammAlice[1])(10'000),
1781 IOUAmount{1'000'000'000049999, -8}));
1782 },
1783 {{XRP(10'000), gAmmmpt(10'000)}});
1784 testAMM(
1785 [&](AMM& ammAlice, Env& env) {
1786 ammAlice.deposit(carol_, MPT(ammAlice[1])(1));
1787 BEAST_EXPECT(ammAlice.expectBalances(
1788 XRP(10'000), MPT(ammAlice[1])(10'001), IOUAmount{10'000'499'98750062, -8}));
1789 },
1790 {{XRP(10'000), gAmmmpt(10'000)}});
1791
1792 // Tiny deposits for MPT/MPT
1793 testAMM(
1794 [&](AMM& ammAlice, Env& env) {
1795 ammAlice.deposit(carol_, MPT(ammAlice[1])(1));
1796
1797 BEAST_EXPECT(ammAlice.expectBalances(
1798 MPT(ammAlice[0])(10'000),
1799 MPT(ammAlice[1])(10'001),
1800 IOUAmount{1'000'049'998750062, -11}));
1801 },
1802 {{gAmmmpt(10'000), gAmmmpt(10'000)}});
1803
1804 // MPT Issuer create/deposit
1805 {
1806 Env env(*this);
1807 env.fund(XRP(30'000), gw_);
1808 env.close();
1809
1810 MPT const btc = MPTTester({.env = env, .issuer = gw_, .holders = {}});
1811
1812 AMM ammGw(env, gw_, XRP(10'000), btc(10'000'000'000));
1813 BEAST_EXPECT(
1814 ammGw.expectBalances(XRP(10'000), btc(10'000'000'000), IOUAmount{10'000'000'000}));
1815
1816 ammGw.deposit(gw_, 1'000'000);
1817 BEAST_EXPECT(
1818 ammGw.expectBalances(XRP(10'001), btc(10'001000000), IOUAmount{10'001000000}));
1819
1820 ammGw.deposit(gw_, btc(1'000000000));
1821 BEAST_EXPECT(ammGw.expectBalances(
1822 XRP(10'001), btc(11'001000000), IOUAmount{1048'908'961731188, -5}));
1823 }
1824
1825 // Issuer deposit in MPT/MPT pool
1826 testAMM(
1827 [&](AMM& ammAlice, Env& env) {
1828 ammAlice.deposit(gw_, 1'000'000);
1829 BEAST_EXPECT(ammAlice.expectBalances(
1830 MPT(ammAlice[0])(1'010'000),
1831 MPT(ammAlice[1])(1'010'000),
1832 IOUAmount{1'010'000}));
1833
1834 ammAlice.deposit(gw_, MPT(ammAlice[0])(1000));
1835 BEAST_EXPECT(ammAlice.expectBalances(
1836 MPT(ammAlice[0])(1'011'000),
1837 MPT(ammAlice[1])(1'010'000),
1838 IOUAmount{1'010'499'876298854, -9}));
1839 },
1840 {{gAmmmpt(10'000), gAmmmpt(10'000)}});
1841
1842 // Issuer deposit in MPT/XRP pool
1843 testAMM(
1844 [&](AMM& ammAlice, Env& env) {
1845 ammAlice.deposit(gw_, 1'000'000);
1846 BEAST_EXPECT(ammAlice.expectBalances(
1847 XRP(11'000), MPT(ammAlice[1])(11'000), IOUAmount{11'000'000}));
1848 ammAlice.deposit(gw_, MPT(ammAlice[1])(1'000));
1849 BEAST_EXPECT(ammAlice.expectBalances(
1850 XRP(11'000), MPT(ammAlice[1])(12'000), IOUAmount{11'489'125'29307605, -8}));
1851 },
1852 {{XRP(10'000), gAmmmpt(10'000)}});
1853
1854 // Equal deposit by tokens MPT/XRP
1855 testAMM(
1856 [&](AMM& ammAlice, Env& env) {
1857 ammAlice.deposit(
1858 carol_,
1859 1'000'000,
1860 XRP(1'000),
1861 MPT(ammAlice[1])(1'000),
1862 std::nullopt,
1863 tfLPToken,
1864 std::nullopt,
1865 std::nullopt);
1866 BEAST_EXPECT(ammAlice.expectBalances(
1867 XRP(11'000), MPT(ammAlice[1])(11'000), IOUAmount{11'000'000, 0}));
1868 },
1869 {{XRP(10'000), gAmmmpt(10'000)}});
1870
1871 // Equal deposit by tokens MPT/IOU combination
1872 {
1873 auto const test = [&](auto&& issue1, auto&& issue2) {
1874 Env env(*this);
1875 env.fund(XRP(30'000), alice_, carol_, gw_);
1876 env.close();
1877 auto const usd = issue1({
1878 .env = env,
1879 .token = "USD",
1880 .issuer = gw_,
1881 .holders = {alice_, carol_},
1882 .limit = 1'000'000,
1883 });
1884 auto const btc = issue2({
1885 .env = env,
1886 .token = "BTC",
1887 .issuer = gw_,
1888 .holders = {alice_, carol_},
1889 .limit = 1'000'000,
1890 });
1891 env(pay(gw_, alice_, btc(50000)));
1892 env(pay(gw_, carol_, btc(50000)));
1893 env(pay(gw_, alice_, usd(50000)));
1894 env(pay(gw_, carol_, usd(50000)));
1895 env.close();
1896
1897 auto ammAlice = AMM(env, alice_, usd(10000), btc(10000));
1898 auto const carolBTC = env.balance(carol_, btc);
1899 auto const carolUSD = env.balance(carol_, usd);
1900
1901 ammAlice.deposit(
1902 carol_,
1903 1'000,
1904 usd(1'000),
1905 btc(1'000),
1906 std::nullopt,
1907 tfLPToken,
1908 std::nullopt,
1909 std::nullopt);
1910 BEAST_EXPECT(ammAlice.expectBalances(usd(11'000), btc(11'000), IOUAmount{11'000}));
1911
1912 env.require(Balance(carol_, carolBTC - btc(1000)));
1913 env.require(Balance(carol_, carolUSD - usd(1000)));
1914 };
1916 }
1917
1918 // Equal deposit by asset XRP/MPT
1919 testAMM(
1920 [&](AMM& ammAlice, Env& env) {
1921 ammAlice.deposit(
1922 carol_,
1923 1'000'000,
1924 XRP(1'000),
1925 MPT(ammAlice[1])(1'000),
1926 std::nullopt,
1927 tfTwoAsset,
1928 std::nullopt,
1929 std::nullopt);
1930 BEAST_EXPECT(ammAlice.expectBalances(
1931 XRP(11'000), MPT(ammAlice[1])(11'000), IOUAmount{11'000'000, 0}));
1932 },
1933 {{XRP(10'000), gAmmmpt(10'000)}});
1934
1935 // Equal deposit by asset IOU/MPT combination
1936 {
1937 auto const test = [&](auto&& issue1, auto&& issue2) {
1938 Env env(*this);
1939 env.fund(XRP(30'000), alice_, carol_, gw_);
1940 env.close();
1941 auto const usd = issue1({
1942 .env = env,
1943 .token = "USD",
1944 .issuer = gw_,
1945 .holders = {alice_, carol_},
1946 .limit = 1'000'000,
1947 });
1948 auto const btc = issue2({
1949 .env = env,
1950 .token = "BTC",
1951 .issuer = gw_,
1952 .holders = {alice_, carol_},
1953 .limit = 1'000'000,
1954 });
1955 env(pay(gw_, alice_, btc(50000)));
1956 env(pay(gw_, carol_, btc(50000)));
1957 env(pay(gw_, alice_, usd(50000)));
1958 env(pay(gw_, carol_, usd(50000)));
1959 env.close();
1960 auto ammAlice = AMM(env, alice_, usd(10000), btc(10000));
1961 auto const carolBTC = env.balance(carol_, btc);
1962 auto const carolUSD = env.balance(carol_, usd);
1963
1964 ammAlice.deposit(
1965 carol_,
1966 1'000,
1967 usd(1'000),
1968 btc(1'000),
1969 std::nullopt,
1970 tfTwoAsset,
1971 std::nullopt,
1972 std::nullopt);
1973 BEAST_EXPECT(
1974 ammAlice.expectBalances(usd(11'000), btc(11'000), IOUAmount{11'000, 0}));
1975
1976 env.require(Balance(carol_, carolBTC - btc(1000)));
1977 env.require(Balance(carol_, carolUSD - usd(1000)));
1978 };
1980 }
1981
1982 // Single deposit XRP by asset MPT/XRP
1983 testAMM(
1984 [&](AMM& ammAlice, Env& env) {
1985 ammAlice.deposit(
1986 carol_,
1987 488'088,
1988 XRP(1'000),
1989 std::nullopt,
1990 std::nullopt,
1991 tfSingleAsset,
1992 std::nullopt,
1993 std::nullopt);
1994 BEAST_EXPECT(ammAlice.expectBalances(
1995 XRP(11'000), MPT(ammAlice[1])(10'000), IOUAmount{10'488'088'48170151, -8}));
1996 },
1997 {{XRP(10'000), gAmmmpt(10'000)}});
1998
1999 // Single deposit MPT by asset MPT/XRP
2000 testAMM(
2001 [&](AMM& ammAlice, Env& env) {
2002 ammAlice.deposit(
2003 carol_,
2004 488'088,
2005 MPT(ammAlice[1])(1'000),
2006 std::nullopt,
2007 std::nullopt,
2008 tfSingleAsset,
2009 std::nullopt,
2010 std::nullopt);
2011 BEAST_EXPECT(ammAlice.expectBalances(
2012 XRP(10'000), MPT(ammAlice[1])(11'000), IOUAmount{10'488'088'48170151, -8}));
2013 },
2014 {{XRP(10'000), gAmmmpt(10'000)}});
2015
2016 // Single deposit IOU by asset MPT/IOU
2017 testAMM(
2018 [&](AMM& ammAlice, Env& env) {
2019 ammAlice.deposit(
2020 carol_,
2021 488,
2022 USD(1'000),
2023 std::nullopt,
2024 std::nullopt,
2025 tfSingleAsset,
2026 std::nullopt,
2027 std::nullopt);
2028 BEAST_EXPECT(ammAlice.expectBalances(
2029 STAmount{USD, UINT64_C(10'999'99999999999), -11},
2030 MPT(ammAlice[1])(10'000),
2031 IOUAmount{10'488'08848170151, -11}));
2032 },
2033 {{USD(10'000), gAmmmpt(10'000)}});
2034
2035 // Single deposit MPT by asset MPT/IOU
2036 testAMM(
2037 [&](AMM& ammAlice, Env& env) {
2038 ammAlice.deposit(
2039 carol_,
2040 488,
2041 MPT(ammAlice[1])(1'000),
2042 std::nullopt,
2043 std::nullopt,
2044 tfSingleAsset,
2045 std::nullopt,
2046 std::nullopt);
2047 BEAST_EXPECT(ammAlice.expectBalances(
2048 USD(10'000), MPT(ammAlice[1])(11'000), IOUAmount{10'488'088'48170151, -11}));
2049 },
2050 {{USD(10'000), gAmmmpt(10'000)}});
2051
2052 // Single deposit MPT by asset MPT/MPT
2053 testAMM(
2054 [&](AMM& ammAlice, Env& env) {
2055 ammAlice.deposit(
2056 carol_,
2057 488,
2058 MPT(ammAlice[1])(1'000),
2059 std::nullopt,
2060 std::nullopt,
2061 tfSingleAsset,
2062 std::nullopt,
2063 std::nullopt);
2064 BEAST_EXPECT(ammAlice.expectBalances(
2065 MPT(ammAlice[0])(10'000),
2066 MPT(ammAlice[1])(11'000),
2067 IOUAmount{10'488'088'48170151, -11}));
2068 },
2069 {{gAmmmpt(10'000), gAmmmpt(10'000)}});
2070 }
2071
2072 void
2074 {
2075 testcase("Invalid AMMWithdraw");
2076
2077 using namespace jtx;
2078 auto const all = testableAmendments();
2079
2080 testAMM(
2081 [&](AMM& ammAlice, Env& env) {
2082 WithdrawArg const args{
2083 .asset1Out = XRP(100),
2084 .err = Ter(tecAMM_BALANCE),
2085 };
2086 ammAlice.withdraw(args);
2087 },
2088 {{XRP(99), gAmmmpt(99)}});
2089
2090 testAMM(
2091 [&](AMM& ammAlice, Env& env) {
2092 WithdrawArg const args{
2093 .asset1Out = MPT(ammAlice[1])(100),
2094 .err = Ter(tecAMM_BALANCE),
2095 };
2096 ammAlice.withdraw(args);
2097 },
2098 {{XRP(99), gAmmmpt(99)}});
2099
2100 {
2101 Env env{*this};
2102 env.fund(XRP(30'000), gw_, alice_, bob_);
2103 env.close();
2104 // alice is authorized to hold gw MPT, bob is not authorized
2105 MPTTester const btc({
2106 .env = env,
2107 .issuer = gw_,
2108 .holders = {alice_},
2109 .pay = 30'000,
2110 .flags = tfMPTRequireAuth | kMptDexFlags,
2111 .authHolder = true,
2112 });
2113 AMM ammAlice(env, alice_, XRP(10'000), btc(10'000));
2114 WithdrawArg const args{
2115 .account = bob_,
2116 .asset1Out = btc(100),
2117 .err = Ter(tecNO_AUTH),
2118 };
2119 ammAlice.withdraw(args);
2120 }
2121
2122 testAMM(
2123 [&](AMM& ammAlice, Env& env) {
2124 MPTTester const btc({
2125 .env = env,
2126 .issuer = gw_,
2127 .holders = {alice_, carol_},
2128 .pay = 2'000,
2129 .flags = tfMPTCanLock | kMptDexFlags,
2130 });
2131
2132 // Invalid tokens
2133 ammAlice.withdraw(alice_, 0, std::nullopt, std::nullopt, Ter(temBAD_AMM_TOKENS));
2134 ammAlice.withdraw(
2135 alice_, IOUAmount{-1}, std::nullopt, std::nullopt, Ter(temBAD_AMM_TOKENS));
2136
2137 // Mismatched token, invalid Asset1Out issue
2138 ammAlice.withdraw(
2139 alice_, btc(100), std::nullopt, std::nullopt, Ter(temBAD_AMM_TOKENS));
2140 ammAlice.withdraw(
2141 alice_, MPT(badMPT())(100), std::nullopt, std::nullopt, Ter(temBAD_MPT));
2142
2143 // Mismatched token, invalid Asset2Out issue
2144 ammAlice.withdraw(alice_, XRP(100), btc(100), std::nullopt, Ter(temBAD_AMM_TOKENS));
2145
2146 // Mismatched token, Asset1Out.issue == Asset2Out.issue
2147 ammAlice.withdraw(
2148 alice_,
2149 MPT(ammAlice[1])(100),
2150 MPT(ammAlice[1])(100),
2151 std::nullopt,
2153
2154 // Invalid amount value
2155 ammAlice.withdraw(
2156 alice_, MPT(ammAlice[1])(0), std::nullopt, std::nullopt, Ter(temBAD_AMOUNT));
2157 ammAlice.withdraw(
2158 alice_, MPT(ammAlice[1])(-100), std::nullopt, std::nullopt, Ter(temBAD_AMOUNT));
2159 ammAlice.withdraw(
2160 alice_, MPT(ammAlice[1])(10), std::nullopt, IOUAmount{-1}, Ter(temBAD_AMOUNT));
2161
2162 // Invalid amount/token value, withdraw all tokens from one side
2163 // of the pool.
2164 ammAlice.withdraw(
2165 alice_,
2166 MPT(ammAlice[1])(10'000),
2167 std::nullopt,
2168 std::nullopt,
2170 ammAlice.withdraw(
2171 alice_, XRP(10'000), std::nullopt, std::nullopt, Ter(tecAMM_BALANCE));
2172 ammAlice.withdraw(
2173 alice_,
2174 std::nullopt,
2175 MPT(ammAlice[1])(0),
2176 std::nullopt,
2177 std::nullopt,
2178 tfOneAssetWithdrawAll,
2179 std::nullopt,
2180 std::nullopt,
2182
2183 // Bad MPT
2184 ammAlice.withdraw(
2185 alice_, XRP(100), MPT(badMPT())(100), std::nullopt, Ter(temBAD_MPT));
2186
2187 // Specified MPToken doesn't match the pool assets
2188 ammAlice.withdraw(
2189 alice_, XRP(100), MPT(noMPT())(100), std::nullopt, Ter(temBAD_AMM_TOKENS));
2190
2191 // Invalid Account
2192 Account bad("bad");
2193 env.memoize(bad);
2194 ammAlice.withdraw(
2195 bad,
2196 1'000'000,
2197 std::nullopt,
2198 std::nullopt,
2199 std::nullopt,
2200 std::nullopt,
2201 std::nullopt,
2202 Seq(1),
2204
2205 // Invalid AMM
2206 ammAlice.withdraw(
2207 alice_,
2208 1'000,
2209 std::nullopt,
2210 std::nullopt,
2211 std::nullopt,
2212 std::nullopt,
2213 {{MPT(ammAlice[1]), GBP}},
2214 std::nullopt,
2215 Ter(terNO_AMM));
2216
2217 // Carol is not a Liquidity Provider
2218 ammAlice.withdraw(carol_, 10'000, std::nullopt, std::nullopt, Ter(tecAMM_BALANCE));
2219 },
2220 {{XRP(10'000), gAmmmpt(10'000)}});
2221
2222 testAMM(
2223 [&](AMM& ammAlice, Env& env) {
2224 // Withdraw entire one side of the pool.
2225 // Pre-fixAMMv1_3:
2226 // Equal withdraw but due to MPT precision limit,
2227 // this results in full withdraw of MPT pool only,
2228 // while leaving a tiny amount in USD pool.
2229 // Post-fixAMMv1_3:
2230 // Most of the pool is withdrawn with remaining tiny amounts
2231 auto err = env.enabled(fixAMMv1_3) ? Ter(tesSUCCESS) : Ter(tecAMM_BALANCE);
2232 ammAlice.withdraw(
2233 alice_, IOUAmount{9'999'999'9999, -4}, std::nullopt, std::nullopt, err);
2234 if (env.enabled(fixAMMv1_3))
2235 {
2236 BEAST_EXPECT(ammAlice.expectBalances(
2237 MPT(ammAlice[0])(1), STAmount{USD, 1, -7}, IOUAmount{1, -4}));
2238 }
2239 },
2240 {{gAmmmpt(10'000'000'000), USD(10'000)}},
2241 0,
2242 std::nullopt,
2243 {all, all - fixAMMv1_3});
2244
2245 testAMM(
2246 [&](AMM& ammAlice, Env& env) {
2247 // Similar to above with even smaller remaining amount
2248 // Pre-fixAMMv1_3: results in full withdraw of MPT pool only,
2249 // returning tecAMM_BALANCE. Post-fixAMMv1_3: most of the pool
2250 // is withdrawn with remaining tiny amounts
2251 auto err = env.enabled(fixAMMv1_3) ? Ter(tesSUCCESS) : Ter(tecAMM_BALANCE);
2252 ammAlice.withdraw(
2253 alice_, IOUAmount{9'999'999'999999999, -9}, std::nullopt, std::nullopt, err);
2254 if (env.enabled(fixAMMv1_3))
2255 {
2256 BEAST_EXPECT(ammAlice.expectBalances(
2257 MPT(ammAlice[0])(1), STAmount{USD, 1, -12}, IOUAmount{1, -9}));
2258 }
2259 },
2260 {{gAmmmpt(10'000'000'000), USD(10'000)}},
2261 0,
2262 std::nullopt,
2263 {all, all - fixAMMv1_3});
2264
2265 // Invalid AMM
2266 testAMM(
2267 [&](AMM& ammAlice, Env& env) {
2268 ammAlice.withdrawAll(alice_);
2269 ammAlice.withdraw(alice_, 10'000, std::nullopt, std::nullopt, Ter(terNO_AMM));
2270 },
2271 {{XRP(10'000), gAmmmpt(10'000)}});
2272
2273 // MPTokenIssuance object doesn't exist
2274 {
2275 Env env{*this};
2276 env.fund(XRP(30'000), gw_, alice_);
2277 env.close();
2278 MPT const btc =
2279 MPTTester({.env = env, .issuer = gw_, .holders = {alice_}, .pay = 30'000});
2280
2281 AMM ammAlice(env, alice_, XRP(10'000), btc(10'000));
2282
2283 ammAlice.withdraw(
2285 .account = alice_,
2286 .asset1Out = MPT(gw_, 1'000)(10),
2287 .assets = {{XRP, MPT(gw_, 1'000)}},
2288 .err = Ter(terNO_AMM),
2289 });
2290 }
2291
2292 // MPTRequireAuth flag is set and the account is not authorized
2293 {
2294 Env env{*this};
2295 env.fund(XRP(30'000), gw_, alice_);
2296 env.close();
2297 auto btcm = MPTTester({
2298 .env = env,
2299 .issuer = gw_,
2300 .holders = {alice_},
2301 .pay = 30'000,
2302 .flags = tfMPTRequireAuth | kMptDexFlags,
2303 .authHolder = true,
2304 });
2305 MPT const btc = btcm;
2306
2307 AMM amm(env, alice_, XRP(10'000), btc(10'000));
2308
2309 btcm.authorize({.account = gw_, .holder = alice_, .flags = tfMPTUnauthorize});
2310
2311 amm.withdraw(
2313 .account = alice_,
2314 .asset1Out = btc(100),
2315 .assets = {{XRP, btc}},
2316 .err = Ter(tecNO_AUTH),
2317 });
2318 }
2319
2320 // MPTCanTransfer is not set and the account is not the issuer of MPT.
2321 // The issuer can create the AMM, and an existing LP token holder can
2322 // still withdraw.
2323 {
2324 Env env{*this};
2325 env.fund(XRP(30'000), gw_, alice_);
2326 env.close();
2327 auto btc = MPTTester({
2328 .env = env,
2329 .issuer = gw_,
2330 .holders = {alice_},
2331 .pay = 30'000,
2332 .flags = tfMPTCanTrade,
2333 .authHolder = true,
2334 });
2335
2336 AMM amm(env, gw_, XRP(10'000), btc(10'000));
2337
2338 auto const lpIssue = amm.lptIssue();
2339 env.trust(STAmount{lpIssue, 20'000'000}, alice_);
2340 env.close();
2341 env(pay(gw_, alice_, LPToken(1'000'000).tokens(lpIssue)));
2342 env.close();
2343
2344 amm.withdraw(
2345 WithdrawArg{.account = alice_, .asset1Out = btc(100), .assets = {{XRP, btc}}});
2346 }
2347
2348 // Globally locked MPT
2349 // MPTLocked flag is set and the account is not the issuer of MPT
2350 {
2351 Env env{*this};
2352 env.fund(XRP(30'000), gw_, alice_);
2353 env.close();
2354 MPTTester btc({
2355 .env = env,
2356 .issuer = gw_,
2357 .holders = {alice_},
2358 .pay = 30'000,
2359 .flags = tfMPTCanLock | kMptDexFlags,
2360 .authHolder = true,
2361 });
2362
2363 AMM ammAlice(env, alice_, XRP(10'000), btc(10'000));
2364 btc.set({.flags = tfMPTLock});
2365
2366 ammAlice.withdraw(
2367 alice_, MPT(ammAlice[1])(100), std::nullopt, std::nullopt, Ter(tecLOCKED));
2368 ammAlice.withdraw(alice_, 1'000, std::nullopt, std::nullopt, Ter(tecLOCKED));
2369
2370 // can single withdraw the other asset
2371 ammAlice.withdraw({.account = alice_, .asset1Out = XRP(100)});
2372 }
2373
2374 // Individually frozen (AMM) account with MPT/MPT AMM
2375 {
2376 Env env{*this};
2377 env.fund(XRP(10'000), gw_, alice_);
2378 env.close();
2379 MPTTester btc({
2380 .env = env,
2381 .issuer = gw_,
2382 .holders = {alice_},
2383 .pay = 30'000,
2384 .flags = tfMPTCanLock | kMptDexFlags,
2385 });
2386 MPTTester const usd({
2387 .env = env,
2388 .issuer = gw_,
2389 .holders = {alice_},
2390 .pay = 40'000,
2391 .flags = tfMPTCanLock | kMptDexFlags,
2392 });
2393
2394 AMM ammAlice(env, alice_, usd(10'000), btc(10'000));
2395
2396 // Alice's BTC is locked
2397 btc.set({.holder = alice_, .flags = tfMPTLock});
2398 ammAlice.withdraw(alice_, 1000, std::nullopt, std::nullopt, Ter(tecLOCKED));
2399 ammAlice.withdraw(alice_, btc(100), std::nullopt, std::nullopt, Ter(tecLOCKED));
2400
2401 // can withdraw the other asset
2402 ammAlice.withdraw(alice_, usd(100), std::nullopt, std::nullopt);
2403
2404 // Unlock and then alice can withdraw
2405 btc.set({.holder = alice_, .flags = tfMPTUnlock});
2406 ammAlice.withdraw(alice_, 1000, std::nullopt, std::nullopt);
2407 ammAlice.withdraw(alice_, btc(100), std::nullopt, std::nullopt);
2408 ammAlice.withdraw(alice_, usd(100), std::nullopt, std::nullopt);
2409 }
2410
2411 // Individually lock MPT or freeze IOU (AMM)
2412 {
2413 Env env{*this};
2414 fund(env, gw_, {alice_}, {USD(20'000)}, Fund::All);
2415 MPTTester btc({
2416 .env = env,
2417 .issuer = gw_,
2418 .holders = {alice_},
2419 .pay = 30'000,
2420 .flags = tfMPTCanLock | kMptDexFlags,
2421 });
2422
2423 AMM ammAlice(env, alice_, USD(10'000), btc(10'000));
2424
2425 // Alice's BTC is locked
2426 btc.set({.holder = alice_, .flags = tfMPTLock});
2427
2428 ammAlice.withdraw(alice_, 1'000, std::nullopt, std::nullopt, Ter(tecLOCKED));
2429 ammAlice.withdraw(alice_, btc(100), std::nullopt, std::nullopt, Ter(tecLOCKED));
2430 // can still single withdraw the unlocked other asset
2431 ammAlice.withdraw(alice_, USD(100), std::nullopt, std::nullopt);
2432
2433 // Unlock alice's BTC
2434 btc.set({.holder = alice_, .flags = tfMPTUnlock});
2435
2436 // Now alice can withdraw
2437 ammAlice.withdraw(alice_, USD(100), std::nullopt, std::nullopt);
2438 ammAlice.withdraw(alice_, 1'000, std::nullopt, std::nullopt);
2439 ammAlice.withdraw(alice_, btc(100), std::nullopt, std::nullopt);
2440
2441 // Individually lock MPT BTC (AMM) account
2442 btc.set({.holder = ammAlice.ammAccount(), .flags = tfMPTLock});
2443
2444 // Can withdraw non-frozen token USD
2445 ammAlice.withdraw(alice_, USD(100), std::nullopt, std::nullopt);
2446
2447 // Can not withdraw locked token BTC
2448 ammAlice.withdraw(alice_, 1'000, std::nullopt, std::nullopt, Ter(tecLOCKED));
2449 ammAlice.withdraw(alice_, btc(100), std::nullopt, std::nullopt, Ter(tecLOCKED));
2450
2451 // Unlock AMM MPT
2452 btc.set({.holder = ammAlice.ammAccount(), .flags = tfMPTUnlock});
2453 // Can withdraw
2454 ammAlice.withdraw(alice_, 1'000, std::nullopt, std::nullopt);
2455 ammAlice.withdraw(alice_, btc(100), std::nullopt, std::nullopt);
2456
2457 // Individually frozen AMM
2458 env(trust(
2459 gw_, STAmount{Issue{gw_["USD"].currency, ammAlice.ammAccount()}, 0}, tfSetFreeze));
2460 env.close();
2461
2462 // Can withdraw non-locked token BTC
2463 ammAlice.withdraw(alice_, btc(100), std::nullopt, std::nullopt);
2464
2465 // Can not withdraw frozen token USD
2466 ammAlice.withdraw(alice_, 1'000, std::nullopt, std::nullopt, Ter(tecFROZEN));
2467 ammAlice.withdraw(alice_, USD(100), std::nullopt, std::nullopt, Ter(tecFROZEN));
2468
2469 // Unfreeze
2470 env(trust(
2471 gw_,
2472 STAmount{Issue{gw_["USD"].currency, ammAlice.ammAccount()}, 0},
2473 tfClearFreeze));
2474 env.close();
2475
2476 // Can withdraw
2477 ammAlice.withdraw(alice_, 1'000, std::nullopt, std::nullopt);
2478 ammAlice.withdraw(alice_, USD(100), std::nullopt, std::nullopt);
2479 }
2480
2481 // Carol withdraws more than she owns
2482 testAMM(
2483 [&](AMM& ammAlice, Env&) {
2484 // Single deposit of 100000 worth of tokens
2485 // which is 10% of the pool. Carol is LP now.
2486 ammAlice.deposit(carol_, 1'000'000);
2487 BEAST_EXPECT(ammAlice.expectBalances(
2488 XRP(11'000), MPT(ammAlice[1])(11'000), IOUAmount{11'000'000, 0}));
2489
2490 ammAlice.withdraw(
2491 carol_, 2'000'000, std::nullopt, std::nullopt, Ter(tecAMM_INVALID_TOKENS));
2492 BEAST_EXPECT(ammAlice.expectBalances(
2493 XRP(11'000), MPT(ammAlice[1])(11'000), IOUAmount{11'000'000, 0}));
2494 },
2495 {{XRP(10'000), gAmmmpt(10'000)}});
2496
2497 // Withdraw with EPrice limit. Fails to withdraw, calculated tokens
2498 // to withdraw are 0.
2499 testAMM(
2500 [&](AMM& ammAlice, Env& env) {
2501 ammAlice.deposit(carol_, 1'000'000);
2502 auto const err =
2503 env.enabled(fixAMMv1_3) ? Ter(tecAMM_INVALID_TOKENS) : Ter(tecAMM_FAILED);
2504 ammAlice.withdraw(
2505 carol_, MPT(ammAlice[1])(100), std::nullopt, IOUAmount{500, 0}, err);
2506 },
2507 {{XRP(10'000), gAmmmpt(10'000)}},
2508 0,
2509 std::nullopt,
2510 {all, all - fixAMMv1_3});
2511
2512 // Withdraw with EPrice limit. Fails to withdraw, calculated tokens
2513 // to withdraw are greater than the LP shares.
2514 testAMM(
2515 [&](AMM& ammAlice, Env&) {
2516 ammAlice.deposit(carol_, 1'000'000);
2517 ammAlice.withdraw(
2518 carol_,
2519 MPT(ammAlice[1])(100),
2520 std::nullopt,
2521 IOUAmount{600, 0},
2523 },
2524 {{XRP(10'000), gAmmmpt(10'000)}});
2525
2526 // Withdraw with EPrice limit. Fails to withdraw, amount1
2527 // to withdraw is less than 1700 MPT.
2528 testAMM(
2529 [&](AMM& ammAlice, Env&) {
2530 ammAlice.deposit(carol_, 1'000'000);
2531 ammAlice.withdraw(
2532 carol_,
2533 MPT(ammAlice[1])(1'700),
2534 std::nullopt,
2535 IOUAmount{520, 0},
2537 },
2538 {{XRP(10'000), gAmmmpt(10'000)}});
2539
2540 // Deposit/Withdraw the same amount with the trading fee
2541 testAMM(
2542 [&](AMM& ammAlice, Env&) {
2543 ammAlice.deposit(carol_, MPT(ammAlice[1])(1'000));
2544 ammAlice.withdraw(
2545 carol_,
2546 MPT(ammAlice[1])(1'000),
2547 std::nullopt,
2548 std::nullopt,
2550 },
2551 {{XRP(10'000), gAmmmpt(10'000)}},
2552 1'000);
2553 testAMM(
2554 [&](AMM& ammAlice, Env&) {
2555 ammAlice.deposit(carol_, XRP(1'000));
2556 ammAlice.withdraw(
2557 carol_, XRP(1'000), std::nullopt, std::nullopt, Ter(tecAMM_INVALID_TOKENS));
2558 },
2559 {{XRP(10'000), gAmmmpt(10'000)}},
2560 1'000);
2561
2562 // Deposit/Withdraw the same amount fails due to the tokens adjustment
2563 testAMM(
2564 [&](AMM& ammAlice, Env&) {
2565 ammAlice.deposit(carol_, STAmount{USD, 1, -6});
2566 ammAlice.withdraw(
2567 carol_,
2568 STAmount{USD, 1, -6},
2569 std::nullopt,
2570 std::nullopt,
2572 },
2573 {{gAmmmpt(10'000'000'000), USD(10'000)}});
2574
2575 // Withdraw close to one side of the pool. Account's LP tokens
2576 // are rounded to all LP tokens.
2577 testAMM(
2578 [&](AMM& ammAlice, Env&) {
2579 ammAlice.withdraw(
2580 alice_,
2581 STAmount{MPT(ammAlice[1]), UINT64_C(9'999'999999999999), -12},
2582 std::nullopt,
2583 std::nullopt,
2585 },
2586 {{XRP(10'000), gAmmmpt(10'000)}});
2587
2588 // Tiny withdraw
2589 testAMM(
2590 [&](AMM& ammAlice, Env&) {
2591 // XRP amount to withdraw is 0
2592 ammAlice.withdraw(
2593 alice_, IOUAmount{1, -5}, std::nullopt, std::nullopt, Ter(tecAMM_FAILED));
2594 // Calculated tokens to withdraw are 0: 1e-16 of 10,000 USD is
2595 // below the large Number mantissa's precision
2596 ammAlice.withdraw(
2597 alice_,
2598 std::nullopt,
2599 STAmount{USD, 1, -16},
2600 std::nullopt,
2602 ammAlice.deposit(carol_, STAmount{USD, 1, -10});
2603 ammAlice.withdraw(
2604 carol_,
2605 std::nullopt,
2606 STAmount{USD, 1, -9},
2607 std::nullopt,
2609 ammAlice.withdraw(
2610 carol_,
2611 std::nullopt,
2612 MPT(ammAlice[0])(1),
2613 std::nullopt,
2615 },
2616 {{gAmmmpt(10'000'000'000), USD(10'000)}});
2617 }
2618
2619 void
2621 {
2622 testcase("Withdraw");
2623
2624 using namespace jtx;
2625
2626 // Equal withdrawal by Carol: 1'000'000 of tokens, 10% of the current
2627 // pool
2628 testAMM(
2629 [&](AMM& ammAlice, Env& env) {
2630 // XRP/MPT
2631 XRPAmount const baseFee{env.current()->fees().base};
2632 auto const carolXRP = env.balance(carol_, XRP);
2633 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
2634
2635 // Single deposit of 1'000'000 worth of tokens,
2636 // which is 10% of the pool. Carol is LP now.
2637 ammAlice.deposit(carol_, 1'000'000);
2638 BEAST_EXPECT(ammAlice.expectBalances(
2639 XRP(11'000), MPT(ammAlice[1])(11'000), IOUAmount{11'000'000, 0}));
2640 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{1'000'000, 0}));
2641 env.require(Balance(carol_, carolMPT - MPT(ammAlice[1])(1'000)));
2642 env.require(Balance(carol_, carolXRP - XRP(1'000) - drops(baseFee)));
2643
2644 // Carol withdraws all tokens
2645 ammAlice.withdraw(carol_, 1'000'000);
2646 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount(beast::Zero())));
2647 env.require(Balance(carol_, carolMPT));
2648 env.require(Balance(carol_, carolXRP - drops(2 * baseFee)));
2649 },
2650 {{XRP(10'000), gAmmmpt(10'000)}});
2651
2652 // Equal withdrawal by tokens 1000000, 10%
2653 // of the current pool, XRP/MPT
2654 testAMM(
2655 [&](AMM& ammAlice, Env&) {
2656 // XRP/MPT
2657 ammAlice.withdraw(alice_, 1'000'000);
2658 BEAST_EXPECT(ammAlice.expectBalances(
2659 XRP(9'000), MPT(ammAlice[1])(9'000), IOUAmount{9'000'000, 0}));
2660 },
2661 {{XRP(10'000), gAmmmpt(10'000)}});
2662
2663 // Equal withdrawal by tokens, 10% of the current pool, IOU/MPT
2664 // combination
2665 {
2666 auto const test = [&](auto&& issue1, auto&& issue2) {
2667 Env env(*this);
2668 env.fund(XRP(30'000), alice_, gw_);
2669 env.close();
2670 auto const usd = issue1({
2671 .env = env,
2672 .token = "USD",
2673 .issuer = gw_,
2674 .holders = {alice_},
2675 .limit = 1'000'000,
2676 });
2677 auto const btc = issue2({
2678 .env = env,
2679 .token = "BTC",
2680 .issuer = gw_,
2681 .holders = {alice_},
2682 .limit = 1'000'000,
2683 });
2684 env(pay(gw_, alice_, btc(50000)));
2685 env(pay(gw_, alice_, usd(50000)));
2686 env.close();
2687 auto ammAlice = AMM(env, alice_, usd(10000), btc(10000));
2688 auto const aliceBTC = env.balance(alice_, btc);
2689 auto const aliceUSD = env.balance(alice_, usd);
2690 ammAlice.withdraw(alice_, 1'000);
2691 BEAST_EXPECT(ammAlice.expectBalances(btc(9'000), usd(9'000), IOUAmount(9'000)));
2692 env.require(Balance(alice_, aliceBTC + btc(1000)));
2693 env.require(Balance(alice_, aliceUSD + usd(1000)));
2694 };
2696 }
2697
2698 // Equal withdrawal with a limit. Withdraw XRP200.
2699 // If proportional withdraw of MPT is less than 100
2700 // then withdraw that amount, otherwise withdraw MPT100
2701 // and proportionally withdraw XRP. It's the latter
2702 // in this case - XRP100/MPT100.
2703 testAMM(
2704 [&](AMM& ammAlice, Env&) {
2705 // XRP/MPT
2706 ammAlice.withdraw(alice_, XRP(200), MPT(ammAlice[1])(100));
2707 BEAST_EXPECT(ammAlice.expectBalances(
2708 XRP(9'900), MPT(ammAlice[1])(9'900), IOUAmount{9'900'000, 0}));
2709 },
2710 {{XRP(10'000), gAmmmpt(10'000)}});
2711
2712 // Equal withdrawal with a limit. XRP100/MPT200 truncated to
2713 // XRP100/MPT100
2714 testAMM(
2715 [&](AMM& ammAlice, Env&) {
2716 // XRP/MPT
2717 ammAlice.withdraw(alice_, XRP(100), MPT(ammAlice[1])(200));
2718 BEAST_EXPECT(ammAlice.expectBalances(
2719 XRP(9'900), MPT(ammAlice[1])(9'900), IOUAmount{9'900'000, 0}));
2720 },
2721 {{XRP(10'000), gAmmmpt(10'000)}});
2722
2723 // Equal withdrawal with a limit. IOU/MPT combination.
2724 {
2725 auto const test = [&](auto&& issue1, auto&& issue2) {
2726 Env env(*this);
2727 env.fund(XRP(30'000), alice_, gw_);
2728 env.close();
2729 auto const usd = issue1({
2730 .env = env,
2731 .token = "USD",
2732 .issuer = gw_,
2733 .holders = {alice_},
2734 .limit = 1'000'000,
2735 });
2736 auto const btc = issue2({
2737 .env = env,
2738 .token = "BTC",
2739 .issuer = gw_,
2740 .holders = {alice_},
2741 .limit = 1'000'000,
2742 });
2743 env(pay(gw_, alice_, btc(50000)));
2744 env(pay(gw_, alice_, usd(50000)));
2745 env.close();
2746 auto ammAlice = AMM(env, alice_, usd(10000), btc(10000));
2747 auto const aliceBTC = env.balance(alice_, btc);
2748 auto const aliceUSD = env.balance(alice_, usd);
2749 ammAlice.withdraw(alice_, btc(200), usd(100));
2750 BEAST_EXPECT(ammAlice.expectBalances(btc(9'900), usd(9'900), IOUAmount(9'900)));
2751 env.require(Balance(alice_, aliceBTC + btc(100)));
2752 env.require(Balance(alice_, aliceUSD + usd(100)));
2753 };
2755 }
2756
2757 // Single withdrawal by amount
2758 testAMM(
2759 [&](AMM& ammAlice, Env&) {
2760 // single withdraw XRP from XRP/MPT
2761 ammAlice.withdraw(alice_, XRP(1'000));
2762 BEAST_EXPECT(ammAlice.expectBalances(
2763 XRPAmount(9000'000001),
2764 MPT(ammAlice[1])(10'000),
2765 IOUAmount{9'486'832'980505138, -9}));
2766 },
2767 {{XRP(10'000), gAmmmpt(10'000)}});
2768 testAMM(
2769 [&](AMM& ammAlice, Env&) {
2770 // single withdraw MPT from XRP/MPT
2771 ammAlice.withdraw(alice_, MPT(ammAlice[1])(1'000));
2772 BEAST_EXPECT(ammAlice.expectBalances(
2773 XRP(10000), MPT(ammAlice[1])(9001), IOUAmount{9'486'832'980505138, -9}));
2774 },
2775 {{XRP(10'000), gAmmmpt(10'000)}});
2776 testAMM(
2777 [&](AMM& ammAlice, Env&) {
2778 // single withdraw IOU from IOU/MPT
2779 ammAlice.withdraw(alice_, USD(1'000));
2780 BEAST_EXPECT(ammAlice.expectBalances(
2781 USD(9'000), MPT(ammAlice[1])(10'000), IOUAmount{9486'832980505138, -12}));
2782 },
2783 {{USD(10'000), gAmmmpt(10'000)}});
2784 testAMM(
2785 [&](AMM& ammAlice, Env&) {
2786 // single withdraw MPT from IOU/MPT
2787 ammAlice.withdraw(alice_, MPT(ammAlice[1])(1'000));
2788 BEAST_EXPECT(ammAlice.expectBalances(
2789 USD(10'000), MPT(ammAlice[1])(9001), IOUAmount{9486'832980505138, -12}));
2790 },
2791 {{USD(10'000), gAmmmpt(10'000)}});
2792 testAMM(
2793 [&](AMM& ammAlice, Env&) {
2794 // single withdraw MPT from MPT/MPT
2795 ammAlice.withdraw(alice_, MPT(ammAlice[0])(1'000));
2796 BEAST_EXPECT(ammAlice.expectBalances(
2797 MPT(ammAlice[0])(9001),
2798 MPT(ammAlice[1])(10'000),
2799 IOUAmount{9486'832980505138, -12}));
2800 },
2801 {{gAmmmpt(10'000), gAmmmpt(10'000)}});
2802
2803 // Single withdrawal MPT by tokens 10000. XRP/MPT
2804 testAMM(
2805 [&](AMM& ammAlice, Env&) {
2806 ammAlice.withdraw(alice_, 10'000, MPT(ammAlice[1])(0));
2807 BEAST_EXPECT(ammAlice.expectBalances(
2808 XRP(10'000), MPT(ammAlice[1])(9981), IOUAmount{9'990'000, 0}));
2809 },
2810 {{XRP(10'000), gAmmmpt(10'000)}});
2811
2812 // Single withdrawal XRP by tokens 10000. XRP/MPT
2813 testAMM(
2814 [&](AMM& ammAlice, Env&) {
2815 ammAlice.withdraw(alice_, 10'000, XRP(0));
2816 BEAST_EXPECT(ammAlice.expectBalances(
2817 XRPAmount(9980010000), MPT(ammAlice[1])(10'000), IOUAmount{9'990'000, 0}));
2818 },
2819 {{XRP(10'000), gAmmmpt(10'000)}});
2820
2821 // Single withdrawal by tokens 10000. MPT/IOU combination.
2822 {
2823 auto const test = [&](auto&& issue1, auto&& issue2) {
2824 Env env(*this);
2825 env.fund(XRP(30'000), alice_, gw_);
2826 env.close();
2827 auto const usd = issue1({
2828 .env = env,
2829 .token = "USD",
2830 .issuer = gw_,
2831 .holders = {alice_},
2832 .limit = 1'000'000,
2833 });
2834 auto const btc = issue2({
2835 .env = env,
2836 .token = "BTC",
2837 .issuer = gw_,
2838 .holders = {alice_},
2839 .limit = 1'000'000,
2840 });
2841 env(pay(gw_, alice_, btc(50000)));
2842 env(pay(gw_, alice_, usd(50000)));
2843 env.close();
2844 auto ammAlice = AMM(env, alice_, usd(10000), btc(10000));
2845 auto const aliceBTC = env.balance(alice_, btc);
2846 auto const aliceUSD = env.balance(alice_, usd);
2847 ammAlice.withdraw(alice_, 1000, btc(0));
2848 BEAST_EXPECT(ammAlice.expectBalances(usd(10'000), btc(8100), IOUAmount{9000, 0}));
2849 env.require(Balance(alice_, aliceBTC + btc(1900)));
2850 env.require(Balance(alice_, aliceUSD));
2851 };
2853 }
2854
2855 // Withdraw all tokens.
2856 testAMM(
2857 [&](AMM& ammAlice, Env& env) {
2858 env(trust(carol_, STAmount{ammAlice.lptIssue(), 10'000}));
2859 // Can SetTrust only for AMM LP tokens
2860 env(trust(carol_, STAmount{Issue{EUR.currency, ammAlice.ammAccount()}, 10'000}),
2862 env.close();
2863 ammAlice.withdrawAll(alice_);
2864 BEAST_EXPECT(!ammAlice.ammExists());
2865
2866 BEAST_EXPECT(!env.le(keylet::ownerDir(ammAlice.ammAccount())));
2867
2868 // Can create AMM for the XRP/MPT pair
2869 AMM const ammCarol(env, carol_, XRP(10'000), MPT(ammAlice[1])(10'000));
2870 BEAST_EXPECT(ammCarol.expectBalances(
2871 XRP(10'000), MPT(ammAlice[1])(10'000), IOUAmount{10'000'000, 0}));
2872 },
2873 {{XRP(10'000), gAmmmpt(10'000)}});
2874
2875 // Single deposit 1000MPT, withdraw all tokens in MPT from XRP/MPT
2876 testAMM(
2877 [&](AMM& ammAlice, Env& env) {
2878 ammAlice.deposit(carol_, MPT(ammAlice[1])(1'000));
2879 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(0));
2880 BEAST_EXPECT(ammAlice.expectBalances(
2881 XRP(10'000), MPT(ammAlice[1])(10'001), IOUAmount{10'000'000, 0}));
2882 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount(beast::Zero())));
2883 },
2884 {{XRP(10'000), gAmmmpt(10'000)}});
2885
2886 // Single deposit 1000MPT, withdraw all tokens in XRP from XRP/MPT
2887 testAMM(
2888 [&](AMM& ammAlice, Env&) {
2889 ammAlice.deposit(carol_, MPT(ammAlice[1])(1'000));
2890 ammAlice.withdrawAll(carol_, XRP(0));
2891 BEAST_EXPECT(ammAlice.expectBalances(
2892 XRPAmount(9'090'909'091), MPT(ammAlice[1])(11000), IOUAmount{10'000'000, 0}));
2893 },
2894 {{XRP(10'000), gAmmmpt(10'000)}});
2895
2896 // Single deposit 1000MPT, withdraw all tokens in MPT from USD/MPT
2897 testAMM(
2898 [&](AMM& ammAlice, Env& env) {
2899 // USD/MPT
2900 ammAlice.deposit(carol_, MPT(ammAlice[1])(1'000));
2901 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(0));
2902 BEAST_EXPECT(ammAlice.expectBalances(
2903 USD(10'000), MPT(ammAlice[1])(10'001), IOUAmount{10'000, 0}));
2904 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount(beast::Zero())));
2905 },
2906 {{USD(10'000), gAmmmpt(10'000)}});
2907
2908 // Single deposit 1000USD, withdraw all tokens in USD from USD/MPT
2909 testAMM(
2910 [&](AMM& ammAlice, Env& env) {
2911 // USD/MPT
2912 ammAlice.deposit(carol_, USD(1'000));
2913 ammAlice.withdrawAll(carol_, USD(0));
2914 BEAST_EXPECT(ammAlice.expectBalances(
2915 USD(10'000), MPT(ammAlice[1])(10'000), IOUAmount{10'000, 0}));
2916 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount(beast::Zero())));
2917 },
2918 {{USD(10'000), gAmmmpt(10'000)}});
2919
2920 // Single deposit 1000MPT, withdraw all tokens in MPT from MPT/MPT
2921 testAMM(
2922 [&](AMM& ammAlice, Env& env) {
2923 // MPT/MPT
2924 ammAlice.deposit(carol_, MPT(ammAlice[1])(1'000));
2925 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(0));
2926 BEAST_EXPECT(ammAlice.expectBalances(
2927 MPT(ammAlice[0])(10'000), MPT(ammAlice[1])(10'001), IOUAmount{10'000, 0}));
2928 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount(beast::Zero())));
2929 },
2930 {{gAmmmpt(10'000), gAmmmpt(10'000)}});
2931
2932 // Single deposit 1000MPT, withdraw all tokens in MPT
2933 testAMM(
2934 [&](AMM& ammAlice, Env&) {
2935 ammAlice.deposit(carol_, MPT(ammAlice[1])(1'000));
2936 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(0));
2937 BEAST_EXPECT(ammAlice.expectBalances(
2938 XRP(10'000), MPT(ammAlice[1])(10001), IOUAmount{10'000'000, 0}));
2939 },
2940 {{XRP(10'000), gAmmmpt(10'000)}});
2941
2942 // Single deposit 1000MPT, withdraw all tokens in USD
2943 testAMM(
2944 [&](AMM& ammAlice, Env&) {
2945 ammAlice.deposit(carol_, MPT(ammAlice[1])(1'000));
2946 ammAlice.withdrawAll(carol_, USD(0));
2947 BEAST_EXPECT(ammAlice.expectBalances(
2948 STAmount{USD, UINT64_C(9'090'9090909091), -10},
2949 MPT(ammAlice[1])(11000),
2950 IOUAmount{10'000, 0}));
2951 },
2952 {{USD(10'000), gAmmmpt(10'000)}});
2953
2954 // Single deposit 1000USD, withdraw all tokens in MPT
2955 testAMM(
2956 [&](AMM& ammAlice, Env&) {
2957 ammAlice.deposit(carol_, USD(1'000));
2958 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(0));
2959 BEAST_EXPECT(ammAlice.expectBalances(
2960 STAmount{USD, UINT64_C(10'999'99999999999), -11},
2961 MPT(ammAlice[1])(9091),
2962 IOUAmount{10'000, 0}));
2963 },
2964 {{USD(10'000), gAmmmpt(10'000)}});
2965
2966 // Single deposit/withdraw by the same account
2967 testAMM(
2968 [&](AMM& ammAlice, Env&) {
2969 auto lpTokens = ammAlice.deposit(carol_, USD(1'000));
2970 ammAlice.withdraw(carol_, lpTokens, USD(0));
2971 lpTokens = ammAlice.deposit(carol_, STAmount(USD, 1, -6));
2972 ammAlice.withdraw(carol_, lpTokens, USD(0));
2973 lpTokens = ammAlice.deposit(carol_, MPT(ammAlice[0])(1));
2974 ammAlice.withdraw(carol_, lpTokens, MPT(ammAlice[0])(0));
2975 BEAST_EXPECT(ammAlice.expectBalances(
2976 MPT(ammAlice[0])(10'000'000'001), USD(10'000), ammAlice.tokens()));
2977 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{0}));
2978 },
2979 {{gAmmmpt(10'000'000'000), USD(10'000)}});
2980 testAMM(
2981 [&](AMM& ammAlice, Env&) {
2982 auto const& btc = MPT(ammAlice[1]);
2983 auto lpTokens = ammAlice.deposit(carol_, btc(1'000));
2984 ammAlice.withdraw(carol_, lpTokens, btc(0));
2985 lpTokens = ammAlice.deposit(carol_, btc(1));
2986 ammAlice.withdraw(carol_, lpTokens, btc(0));
2987 lpTokens = ammAlice.deposit(carol_, btc(1));
2988 ammAlice.withdraw(carol_, lpTokens, btc(0));
2989 BEAST_EXPECT(ammAlice.expectBalances(btc(10'003), XRP(10'000), ammAlice.tokens()));
2990 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{0}));
2991 },
2992 {{XRP(10'000), gAmmmpt(10'000)}});
2993
2994 // Single deposit by different accounts and then withdraw
2995 // in reverse.
2996 testAMM(
2997 [&](AMM& ammAlice, Env&) {
2998 auto const carolTokens = ammAlice.deposit(carol_, MPT(ammAlice[1])(1'000));
2999 auto const aliceTokens = ammAlice.deposit(alice_, MPT(ammAlice[1])(1'000));
3000 ammAlice.withdraw(alice_, aliceTokens, MPT(ammAlice[1])(0));
3001 ammAlice.withdraw(carol_, carolTokens, MPT(ammAlice[1])(0));
3002 BEAST_EXPECT(ammAlice.expectBalances(
3003 XRP(10'000), MPT(ammAlice[1])(10'001), ammAlice.tokens()));
3004 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{0}));
3005 BEAST_EXPECT(ammAlice.expectLPTokens(alice_, ammAlice.tokens()));
3006 },
3007 {{XRP(10'000), gAmmmpt(10'000)}});
3008
3009 // Equal deposit 10%, withdraw all tokens. XRP/MPT
3010 testAMM(
3011 [&](AMM& ammAlice, Env&) {
3012 ammAlice.deposit(carol_, 1'000'000);
3013 ammAlice.withdrawAll(carol_);
3014 BEAST_EXPECT(ammAlice.expectBalances(
3015 XRP(10'000), MPT(ammAlice[1])(10'000), IOUAmount{10'000'000, 0}));
3016 },
3017 {{XRP(10'000), gAmmmpt(10'000)}});
3018 // Equal deposit 10%, withdraw all tokens. IOU/MPT combination.
3019 {
3020 auto const test = [&](auto&& issue1, auto&& issue2) {
3021 Env env(*this);
3022 env.fund(XRP(30'000), alice_, carol_, gw_);
3023 env.close();
3024 auto const usd = issue1({
3025 .env = env,
3026 .token = "USD",
3027 .issuer = gw_,
3028 .holders = {alice_, carol_},
3029 .limit = 1'000'000,
3030 });
3031 auto const btc = issue2({
3032 .env = env,
3033 .token = "BTC",
3034 .issuer = gw_,
3035 .holders = {alice_, carol_},
3036 .limit = 1'000'000,
3037 });
3038 env(pay(gw_, alice_, btc(50000)));
3039 env(pay(gw_, carol_, btc(50000)));
3040 env(pay(gw_, alice_, usd(50000)));
3041 env(pay(gw_, carol_, usd(50000)));
3042 env.close();
3043 auto ammAlice = AMM(env, alice_, usd(10000), btc(10000));
3044 auto const carolBTC = env.balance(carol_, btc);
3045 auto const carolUSD = env.balance(carol_, usd);
3046 ammAlice.deposit(carol_, 1'000);
3047 ammAlice.withdrawAll(carol_);
3048 BEAST_EXPECT(
3049 ammAlice.expectBalances(usd(10'000), btc(10'000), IOUAmount{10'000, 0}));
3050 env.require(Balance(carol_, carolBTC));
3051 env.require(Balance(carol_, carolUSD));
3052 };
3054 }
3055
3056 // Equal deposit 10%, withdraw all tokens in MPT from XRP/MPT
3057 testAMM(
3058 [&](AMM& ammAlice, Env&) {
3059 ammAlice.deposit(carol_, 1'000'000);
3060 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(0));
3061 BEAST_EXPECT(ammAlice.expectBalances(
3062 XRP(11'000),
3063 STAmount{MPT(ammAlice[1]), UINT64_C(9'090'909090909092), -12},
3064 IOUAmount{10'000'000, 0}));
3065 },
3066 {{XRP(10'000), gAmmmpt(10'000)}});
3067
3068 // Equal deposit 10%, withdraw all tokens in XRP from XRP/MPT
3069 testAMM(
3070 [&](AMM& ammAlice, Env&) {
3071 ammAlice.deposit(carol_, 1'000'000);
3072 ammAlice.withdrawAll(carol_, XRP(0));
3073 BEAST_EXPECT(ammAlice.expectBalances(
3074 XRPAmount(9'090'909'091), MPT(ammAlice[1])(11'000), IOUAmount{10'000'000, 0}));
3075 },
3076 {{XRP(10'000), gAmmmpt(10'000)}});
3077
3078 // Equal deposit 10%, withdraw all tokens in USD from USD/MPT
3079 testAMM(
3080 [&](AMM& ammAlice, Env&) {
3081 ammAlice.deposit(carol_, 1'000);
3082 ammAlice.withdrawAll(carol_, USD(0));
3083 BEAST_EXPECT(ammAlice.expectBalances(
3084 STAmount{USD, UINT64_C(9'090'909090909091), -12},
3085 MPT(ammAlice[1])(11'000),
3086 IOUAmount{10'000}));
3087 },
3088 {{USD(10'000), gAmmmpt(10'000)}});
3089 // Equal deposit 10%, withdraw all tokens in MPT from MPT/MPT
3090 testAMM(
3091 [&](AMM& ammAlice, Env&) {
3092 ammAlice.deposit(carol_, 1'000);
3093 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(0));
3094 BEAST_EXPECT(ammAlice.expectBalances(
3095 MPT(ammAlice[0])(11'000), MPT(ammAlice[1])(9'091), IOUAmount{10'000}));
3096 },
3097 {{gAmmmpt(10'000), gAmmmpt(10'000)}});
3098
3099 auto const all = testableAmendments();
3100
3101 // Withdraw with EPrice limit.
3102 testAMM(
3103 [&](AMM& ammAlice, Env& env) {
3104 ammAlice.deposit(carol_, 1'000'000'000'000);
3105 ammAlice.withdraw(
3106 carol_, MPT(ammAlice[1])(100'000000), std::nullopt, IOUAmount{520, 0});
3107 if (!env.enabled(fixAMMv1_3))
3108 {
3109 BEAST_EXPECT(
3110 ammAlice.expectBalances(
3111 XRP(11'000'000000),
3112 MPT(ammAlice[1])(9372781065),
3113 IOUAmount{10'153'846'15384616, -2}) &&
3114 ammAlice.expectLPTokens(carol_, IOUAmount{153'846'15384616, -2}));
3115 }
3116 else
3117 {
3118 BEAST_EXPECT(
3119 ammAlice.expectBalances(
3120 XRP(11'000'000000),
3121 MPT(ammAlice[1])(9372781066),
3122 IOUAmount{10'153'846'15384616, -2}) &&
3123 ammAlice.expectLPTokens(carol_, IOUAmount{153'846'15384616, -2}));
3124 }
3125 ammAlice.withdrawAll(carol_);
3126 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{0}));
3127 },
3128 {{XRP(10'000'000'000), gAmmmpt(10'000'000'000)}},
3129 0,
3130 std::nullopt,
3131 {all, all - fixAMMv1_3});
3132
3133 // Withdraw with EPrice limit. AssetOut is 0.
3134 testAMM(
3135 [&](AMM& ammAlice, Env& env) {
3136 ammAlice.deposit(carol_, 1'000'000'000'000);
3137 ammAlice.withdraw(carol_, MPT(ammAlice[1])(0), std::nullopt, IOUAmount{520, 0});
3138 if (!env.enabled(fixAMMv1_3))
3139 {
3140 BEAST_EXPECT(
3141 ammAlice.expectBalances(
3142 XRP(11'000'000000),
3143 MPT(ammAlice[1])(9372781065),
3144 IOUAmount{10'153'846'15384616, -2}) &&
3145 ammAlice.expectLPTokens(carol_, IOUAmount{153'846'15384616, -2}));
3146 }
3147 else
3148 {
3149 BEAST_EXPECT(
3150 ammAlice.expectBalances(
3151 XRP(11'000'000000),
3152 MPT(ammAlice[1])(9372781066),
3153 IOUAmount{10'153'846'15384616, -2}) &&
3154 ammAlice.expectLPTokens(carol_, IOUAmount{153'846'15384616, -2}));
3155 }
3156 ammAlice.withdrawAll(carol_);
3157 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{0}));
3158 },
3159 {{XRP(10'000'000'000), gAmmmpt(10'000'000'000)}},
3160 0,
3161 std::nullopt,
3162 {all, all - fixAMMv1_3});
3163
3164 // IOU/MPT combination + transfer fee
3165 {
3166 auto const test = [&](auto&& issue1, auto&& issue2) {
3167 Env env(*this);
3168 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
3169 env.close();
3170 auto const usd = issue1({
3171 .env = env,
3172 .token = "USD",
3173 .issuer = gw_,
3174 .holders = {alice_, bob_, carol_},
3175 .limit = 1'000'000,
3176 .transferFee = 25'000,
3177 });
3178 auto const btc = issue2({
3179 .env = env,
3180 .token = "BTC",
3181 .issuer = gw_,
3182 .holders = {alice_, bob_, carol_},
3183 .limit = 1'000'000,
3184 .transferFee = 25'000,
3185 });
3186 env(pay(gw_, alice_, btc(10000)));
3187 env(pay(gw_, bob_, btc(10000)));
3188 env(pay(gw_, carol_, btc(10000)));
3189 env(pay(gw_, alice_, usd(10000)));
3190 env(pay(gw_, bob_, usd(10000)));
3191 env(pay(gw_, carol_, usd(10000)));
3192 env.close();
3193 // no transfer fee on create
3194 AMM ammAlice(env, alice_, btc(2'000), usd(5));
3195 BEAST_EXPECT(ammAlice.expectBalances(btc(2'000), usd(5), IOUAmount{100, 0}));
3196 env.require(Balance(alice_, btc(8000)));
3197 env.require(Balance(alice_, usd(9995)));
3198
3199 // no transfer fee on deposit
3200 ammAlice.deposit(carol_, 100);
3201 BEAST_EXPECT(ammAlice.expectBalances(btc(4000), usd(10), IOUAmount{200, 0}));
3202 env.require(Balance(carol_, btc(8000)));
3203 env.require(Balance(carol_, usd(9995)));
3204
3205 // no transfer fee on withdraw
3206 ammAlice.withdraw(carol_, 100);
3207 BEAST_EXPECT(ammAlice.expectBalances(btc(2'000), usd(5), IOUAmount{100, 0}));
3208 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{0, 0}));
3209 env.require(Balance(carol_, btc(10000)));
3210 env.require(Balance(carol_, usd(10000)));
3211 };
3213 }
3214
3215 // Tiny withdraw
3216 testAMM(
3217 [&](AMM& ammAlice, Env&) {
3218 // By tokens
3219 ammAlice.withdraw(alice_, IOUAmount{1, -3});
3220 BEAST_EXPECT(ammAlice.expectBalances(
3221 MPT(ammAlice[0])(9'999'999'999),
3222 STAmount{USD, UINT64_C(9'999'999999), -6},
3223 IOUAmount{9'999'999'999, -3}));
3224 },
3225 {{gAmmmpt(10'000'000'000), USD(10'000)}});
3226 testAMM(
3227 [&](AMM& ammAlice, Env&) {
3228 // Single withdraw MPT from MPT/IOU
3229 ammAlice.withdraw(alice_, std::nullopt, MPT(ammAlice[0])(1));
3230 BEAST_EXPECT(ammAlice.expectBalances(
3231 MPT(ammAlice[0])(10000'000000), USD(10'000), IOUAmount{9'999'999'9995, -4}));
3232 },
3233 {{gAmmmpt(10'000'000'000), USD(10'000)}});
3234 testAMM(
3235 [&](AMM& ammAlice, Env&) {
3236 // Single withdraw IOU from MPT/IOU
3237 ammAlice.withdraw(alice_, std::nullopt, STAmount{USD, 1, -10});
3238 BEAST_EXPECT(ammAlice.expectBalances(
3239 MPT(ammAlice[0])(10'000'000'000),
3240 STAmount{USD, UINT64_C(9'999'9999999999), -10},
3241 IOUAmount{9'999'999'99999995, -8}));
3242 },
3243 {{gAmmmpt(10'000'000'000), USD(10'000)}});
3244 testAMM(
3245 [&](AMM& ammAlice, Env&) {
3246 // Single withdraw XRP from MPT/XRP
3247 ammAlice.withdraw(alice_, std::nullopt, XRPAmount(1));
3248 BEAST_EXPECT(ammAlice.expectBalances(
3249 MPT(ammAlice[1])(10'000), XRP(10'000), IOUAmount{9999999'9995, -4}));
3250 },
3251 {{XRP(10'000), gAmmmpt(10'000)}});
3252
3253 // Withdraw close to entire pool
3254 // Equal by tokens
3255 testAMM(
3256 [&](AMM& ammAlice, Env&) {
3257 ammAlice.withdraw(alice_, IOUAmount{9'999'999'999, -3});
3258 BEAST_EXPECT(ammAlice.expectBalances(
3259 MPT(ammAlice[0])(1), STAmount{USD, 1, -6}, IOUAmount{1, -3}));
3260 },
3261 {{gAmmmpt(10'000'000'000), USD(10'000)}});
3262 // USD by tokens
3263 testAMM(
3264 [&](AMM& ammAlice, Env&) {
3265 ammAlice.withdraw(alice_, IOUAmount{9'999'999}, USD(0));
3266 BEAST_EXPECT(ammAlice.expectBalances(
3267 MPT(ammAlice[0])(10'000'000'000), STAmount{USD, 1, -10}, IOUAmount{1}));
3268 },
3269 {{gAmmmpt(10'000'000'000), USD(10'000)}});
3270 // MPT by tokens
3271 testAMM(
3272 [&](AMM& ammAlice, Env&) {
3273 ammAlice.withdraw(alice_, IOUAmount{9'999'900}, MPT(ammAlice[0])(0));
3274 BEAST_EXPECT(
3275 ammAlice.expectBalances(MPT(ammAlice[0])(1), USD(10'000), IOUAmount{100}));
3276 },
3277 {{gAmmmpt(10'000'000'000), USD(10'000)}});
3278 // XRP by tokens
3279 testAMM(
3280 [&](AMM& ammAlice, Env&) {
3281 ammAlice.withdraw(alice_, IOUAmount{9'999'900}, XRP(0));
3282 BEAST_EXPECT(
3283 ammAlice.expectBalances(MPT(ammAlice[1])(10000), XRPAmount(1), IOUAmount{100}));
3284 },
3285 {{XRP(10'000), gAmmmpt(10'000)}});
3286 // USD
3287 testAMM(
3288 [&](AMM& ammAlice, Env&) {
3289 ammAlice.withdraw(alice_, STAmount{USD, UINT64_C(9'999'99999999999), -11});
3290 BEAST_EXPECT(ammAlice.expectBalances(
3291 MPT(ammAlice[0])(10'000'000'000),
3292 STAmount{USD, 1, -11},
3293 IOUAmount{316227766, -9}));
3294 },
3295 {{gAmmmpt(10'000'000'000), USD(10'000)}});
3296 // XRP
3297 testAMM(
3298 [&](AMM& ammAlice, Env&) {
3299 ammAlice.withdraw(alice_, XRPAmount{9'999'999'999});
3300 BEAST_EXPECT(
3301 ammAlice.expectBalances(MPT(ammAlice[1])(10000), XRPAmount(1), IOUAmount{100}));
3302 },
3303 {{XRP(10'000), gAmmmpt(10'000)}});
3304 // MPT
3305 testAMM(
3306 [&](AMM& ammAlice, Env&) {
3307 ammAlice.withdraw(alice_, MPT(ammAlice[0])(9'999'999'999));
3308 BEAST_EXPECT(
3309 ammAlice.expectBalances(MPT(ammAlice[0])(1), USD(10'000), IOUAmount{100}));
3310 },
3311 {{gAmmmpt(10'000'000'000), USD(10'000)}});
3312 testAMM(
3313 [&](AMM& ammAlice, Env&) {
3314 ammAlice.withdraw(alice_, MPT(ammAlice[1])(9'999));
3315 BEAST_EXPECT(
3316 ammAlice.expectBalances(MPT(ammAlice[1])(1), XRP(10'000), IOUAmount{100000}));
3317 },
3318 {{XRP(10'000), gAmmmpt(10'000)}});
3319
3320 // MPT/MPT equal withdrawal after LP deletes both zero-balance MPTokens.
3321 // AMMWithdraw must recreate both missing MPTokens; the invariant allows
3322 // up to two MPToken creations per AMMWithdraw/AMMClawback (threshold > 2).
3323 {
3324 Env env{*this};
3325 env.fund(XRP(30'000), gw_, alice_);
3326 env.close();
3327 MPTTester btc({
3328 .env = env,
3329 .issuer = gw_,
3330 .holders = {alice_},
3331 .pay = 10'000,
3332 .flags = kMptDexFlags,
3333 });
3334 MPTTester eth({
3335 .env = env,
3336 .issuer = gw_,
3337 .holders = {alice_},
3338 .pay = 10'000,
3339 .flags = kMptDexFlags,
3340 });
3341
3342 // Alice deposits everything into the MPT/MPT pool; her MPT
3343 // balances drop to zero.
3344 AMM ammAlice(env, alice_, btc(10'000), eth(10'000));
3345 BEAST_EXPECT(expectMPT(env, alice_, btc(0)));
3346 BEAST_EXPECT(expectMPT(env, alice_, eth(0)));
3347
3348 // Alice deletes both zero-balance MPTokens to reclaim reserve.
3349 btc.authorize({.account = alice_, .flags = tfMPTUnauthorize});
3350 eth.authorize({.account = alice_, .flags = tfMPTUnauthorize});
3351 BEAST_EXPECT(!env.le(keylet::mptoken(btc.issuanceID(), alice_.id())));
3352 BEAST_EXPECT(!env.le(keylet::mptoken(eth.issuanceID(), alice_.id())));
3353
3354 // Equal withdrawal succeeds: both missing MPTokens are recreated
3355 // (mptokensCreated_ == 2, which satisfies the > 2 invariant check).
3356 ammAlice.withdrawAll(alice_);
3357 BEAST_EXPECT(env.le(keylet::mptoken(btc.issuanceID(), alice_.id())));
3358 BEAST_EXPECT(env.le(keylet::mptoken(eth.issuanceID(), alice_.id())));
3359 BEAST_EXPECT(expectMPT(env, alice_, btc(10'000)));
3360 BEAST_EXPECT(expectMPT(env, alice_, eth(10'000)));
3361 BEAST_EXPECT(!ammAlice.ammExists());
3362 }
3363 }
3364
3365 void
3367 {
3368 testcase("Withdraw reserve check uses live balance");
3369
3370 using namespace jtx;
3371
3372 auto const test = [&](auto&& makeToken) {
3373 Env env(*this);
3374 env.fund(XRP(30'000), gw_, alice_, bob_);
3375 env.close();
3376
3377 auto const token = makeToken(env);
3378 AMM amm(env, gw_, XRP(100), token(100));
3379
3380 // The EUR trustline is an unrelated owner object. The XRP-only
3381 // AMM deposit adds the LP token trustline, so Alice has 2 owners.
3382 env.trust(gw_["EUR"](1), alice_);
3383 amm.deposit(DepositArg{.account = alice_, .asset1In = XRP(10)});
3384 BEAST_EXPECT(env.ownerCount(alice_) == 2);
3386
3387 // Drain Alice to one drop below the reserve for a third owner
3388 // object, accounting for the fee on the drain payment.
3389 auto const reserveForToken = reserve(env, 3);
3390 auto const targetBalance = reserveForToken - XRPAmount{1};
3391 auto const baseFee = env.current()->fees().base;
3392 auto const currentBalance = env.balance(alice_).value().xrp();
3393 auto const drainAmount = currentBalance - targetBalance - baseFee;
3394 BEAST_EXPECT(drainAmount > XRPAmount{0});
3395 env(pay(alice_, bob_, drops(drainAmount)));
3396 env.close();
3397
3398 // AMMWithdraw captures priorBalance before the fee, then the XRP
3399 // leg raises the live sandbox balance before the token leg.
3400 // XRP(2) keeps the integral MPT side positive after rounding.
3401 auto const xrpOut = XRP(2);
3402 auto const tokenOut = token(2);
3403 auto const priorBalance = env.balance(alice_).value().xrp();
3404 auto const liveBalanceAfterXrpLeg = priorBalance - baseFee + xrpOut.value().xrp();
3405 BEAST_EXPECT(priorBalance < reserveForToken);
3406 BEAST_EXPECT(liveBalanceAfterXrpLeg > priorBalance);
3407 BEAST_EXPECT(liveBalanceAfterXrpLeg >= reserveForToken);
3408
3409 // The XRP leg runs first, so the missing IOU trustline or MPToken
3410 // is reserved against the updated sandbox balance.
3411 amm.withdraw(
3412 WithdrawArg{.account = alice_, .asset1Out = xrpOut, .asset2Out = tokenOut});
3413
3414 // The withdrawal succeeds only if the missing token holding can be
3415 // reserved from the live balance after the XRP leg.
3416 BEAST_EXPECT(env.ownerCount(alice_) == 3);
3417 BEAST_EXPECT(env.balance(alice_, token).value().signum() > 0);
3418 };
3419
3420 test([&](Env&) -> PrettyAsset { return gw_["USD"]; });
3421 test([&](Env& env) -> PrettyAsset { return MPTTester({.env = env, .issuer = gw_}); });
3422 }
3423
3424 void
3426 {
3427 testcase("Invalid Fee Vote");
3428 using namespace jtx;
3429
3430 testAMM(
3431 [&](AMM& ammAlice, Env& env) {
3432 // Invalid Account
3433 Account bad("bad");
3434 env.memoize(bad);
3435 ammAlice.vote(bad, 1'000, std::nullopt, Seq(1), std::nullopt, Ter(terNO_ACCOUNT));
3436
3437 // Invalid AMM
3438 ammAlice.vote(
3439 alice_,
3440 1'000,
3441 std::nullopt,
3442 std::nullopt,
3443 {{MPT(ammAlice[1]), GBP}},
3444 Ter(terNO_AMM));
3445
3446 // Account is not LP
3447 ammAlice.vote(
3448 carol_,
3449 1'000,
3450 std::nullopt,
3451 std::nullopt,
3452 std::nullopt,
3454
3455 // Invalid asset pair
3456 ammAlice.vote(
3457 alice_,
3458 1'000,
3459 std::nullopt,
3460 std::nullopt,
3461 {{MPT(ammAlice[1]), MPT(ammAlice[1])}},
3463 },
3464 {{XRP(10'000), gAmmmpt(10'000)}});
3465
3466 // Invalid AMM
3467 testAMM(
3468 [&](AMM& ammAlice, Env& env) {
3469 ammAlice.withdrawAll(alice_);
3470 ammAlice.vote(
3471 alice_, 1'000, std::nullopt, std::nullopt, std::nullopt, Ter(terNO_AMM));
3472 },
3473 {{XRP(10'000), gAmmmpt(10'000)}});
3474
3475 // MPTokenInstance object doesn't exist
3476 {
3477 Env env(*this);
3478 env.fund(XRP(1'000), alice_);
3479 env(AMM::voteJv({.account = alice_, .tfee = 1'000, .assets = {{XRP, MPT(alice_, 0)}}}),
3480 Ter(terNO_AMM));
3481 }
3482 }
3483
3484 void
3486 {
3487 testcase("Fee Vote");
3488 using namespace jtx;
3489
3490 // One vote sets fee to 1%.
3491 testAMM(
3492 [&](AMM& ammAlice, Env& env) {
3493 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount{0}));
3494 ammAlice.vote({}, 1'000);
3495 BEAST_EXPECT(ammAlice.expectTradingFee(1'000));
3496 // Discounted fee is 1/10 of trading fee.
3497 BEAST_EXPECT(ammAlice.expectAuctionSlot(100, 0, IOUAmount{0}));
3498 },
3499 {{XRP(10'000), gAmmmpt(10'000)}});
3500
3501 auto const vote = [&](AMM& ammAlice,
3502 Env& env,
3503 int i,
3504 std::uint32_t tokens = 10'000'000,
3505 std::vector<Account>* accounts = nullptr) {
3506 Account a(std::to_string(i));
3507 ammAlice.deposit(a, tokens);
3508 ammAlice.vote(a, 50 * (i + 1));
3509 if (accounts)
3510 accounts->push_back(std::move(a));
3511 };
3512
3513 {
3514 // Eight votes fill all voting slots, set fee 0.175%.
3515 // New vote, same account, sets fee 0.225%
3516 Env env{*this};
3517 env.fund(XRP(30'000), gw_, alice_);
3518 std::vector<Account> holders = {alice_};
3519 for (int i = 0; i <= 7; ++i)
3520 {
3521 Account const a(std::to_string(i));
3522 holders.push_back(a);
3523 env.fund(XRP(30'000), a);
3524 }
3525 env.close();
3526 // create MPT and pay 30'000 to all the accounts
3527 MPTTester const btc({.env = env, .issuer = gw_, .holders = holders, .pay = 30'000});
3528 AMM ammAlice(env, alice_, XRP(10'000), btc(10'000));
3529 for (int i = 0; i < 7; ++i)
3530 vote(ammAlice, env, i);
3531 BEAST_EXPECT(ammAlice.expectTradingFee(175));
3532 Account const a("0");
3533 ammAlice.vote(a, 450);
3534 BEAST_EXPECT(ammAlice.expectTradingFee(225));
3535 }
3536
3537 {
3538 // Eight votes fill all voting slots, set fee 0.175%.
3539 // New vote, new account, higher vote weight, set higher fee 0.244%
3540 Env env{*this};
3541 env.fund(XRP(30'000), gw_, alice_);
3542 std::vector<Account> holders = {alice_};
3543 for (int i = 0; i < 8; ++i)
3544 {
3545 Account const a(std::to_string(i));
3546 holders.push_back(a);
3547 env.fund(XRP(30'000), a);
3548 }
3549 env.close();
3550 MPTTester const btc({.env = env, .issuer = gw_, .holders = holders, .pay = 30'000});
3551 AMM ammAlice(env, alice_, XRP(10'000), btc(10'000));
3552 for (int i = 0; i < 7; ++i)
3553 vote(ammAlice, env, i);
3554 BEAST_EXPECT(ammAlice.expectTradingFee(175));
3555 vote(ammAlice, env, 7, 20'000'000);
3556 BEAST_EXPECT(ammAlice.expectTradingFee(244));
3557 }
3558
3559 {
3560 // Eight votes fill all voting slots, set fee 0.219%.
3561 // New vote, new account, higher vote weight, set smaller fee 0.206%
3562 Env env{*this};
3563 env.fund(XRP(30'000), gw_, alice_);
3564 std::vector<Account> holders = {alice_};
3565 for (int i = 0; i < 8; ++i)
3566 {
3567 Account const a(std::to_string(i));
3568 holders.push_back(a);
3569 env.fund(XRP(30'000), a);
3570 }
3571 env.close();
3572 MPTTester const btc({.env = env, .issuer = gw_, .holders = holders, .pay = 30'000});
3573 AMM ammAlice(env, alice_, XRP(10'000), btc(10'000));
3574 for (int i = 7; i > 0; --i)
3575 vote(ammAlice, env, i);
3576 BEAST_EXPECT(ammAlice.expectTradingFee(219));
3577 vote(ammAlice, env, 0, 20'000'000);
3578 BEAST_EXPECT(ammAlice.expectTradingFee(206));
3579 }
3580
3581 {
3582 // Eight votes fill all voting slots. The accounts then withdraw all
3583 // tokens. An account sets a new fee and the previous slots are
3584 // deleted.
3585 Env env{*this};
3586 env.fund(XRP(30'000), gw_, alice_, carol_);
3587 std::vector<Account> holders = {alice_, carol_};
3588 for (int i = 0; i < 7; ++i)
3589 {
3590 Account const a(std::to_string(i));
3591 holders.push_back(a);
3592 env.fund(XRP(30'000), a);
3593 }
3594 env.close();
3595 MPTTester const btc({.env = env, .issuer = gw_, .holders = holders, .pay = 30'000});
3596 AMM ammAlice(env, alice_, XRP(10'000), btc(10'000));
3597 std::vector<Account> accounts;
3598 for (int i = 0; i < 7; ++i)
3599 vote(ammAlice, env, i, 10'000'000, &accounts);
3600 BEAST_EXPECT(ammAlice.expectTradingFee(175));
3601 for (int i = 0; i < 7; ++i)
3602 ammAlice.withdrawAll(accounts[i]);
3603 ammAlice.deposit(carol_, 10'000'000);
3604 ammAlice.vote(carol_, 1'000);
3605 // The initial LP set the fee to 1000. Carol gets 50% voting
3606 // power, and the new fee is 500.
3607 BEAST_EXPECT(ammAlice.expectTradingFee(500));
3608 }
3609 {
3610 // Eight votes fill all voting slots. The accounts then withdraw
3611 // some tokens. The new vote doesn't get the voting power but the
3612 // slots are refreshed and the fee is updated.
3613 Env env{*this};
3614 env.fund(XRP(30'000), gw_, alice_, carol_);
3615 std::vector<Account> holders = {alice_, carol_};
3616 for (int i = 0; i < 7; ++i)
3617 {
3618 Account const a(std::to_string(i));
3619 holders.push_back(a);
3620 env.fund(XRP(30'000), a);
3621 }
3622 env.close();
3623 MPTTester const btc({.env = env, .issuer = gw_, .holders = holders, .pay = 30'000});
3624 AMM ammAlice(env, alice_, XRP(10'000), btc(10'000));
3625 std::vector<Account> accounts;
3626 for (int i = 0; i < 7; ++i)
3627 vote(ammAlice, env, i, 10'000'000, &accounts);
3628 BEAST_EXPECT(ammAlice.expectTradingFee(175));
3629 for (int i = 0; i < 7; ++i)
3630 ammAlice.withdraw(accounts[i], 9'000'000);
3631 ammAlice.deposit(carol_, 1'000);
3632 // The vote is not added to the slots
3633 ammAlice.vote(carol_, 1'000);
3634 auto const info = ammAlice.ammRpcInfo()[jss::amm][jss::vote_slots];
3635 for (auto const& entry : info)
3636 BEAST_EXPECT(entry[jss::account] != carol_.human());
3637 // But the slots are refreshed and the fee is changed
3638 BEAST_EXPECT(ammAlice.expectTradingFee(82));
3639 }
3640 }
3641
3642 void
3644 {
3645 testcase("Invalid Bid");
3646 using namespace jtx;
3647 using namespace std::chrono;
3648
3649 // burn all the LPTokens through a AMMBid transaction
3650 {
3651 Env env(*this);
3652 env.fund(XRP(2'000), gw_, alice_);
3653 MPTTester const btc({
3654 .env = env,
3655 .issuer = gw_,
3656 .holders = {alice_},
3657 .pay = 2'000,
3658 .flags = kMptDexFlags,
3659 });
3660 AMM amm(env, gw_, XRP(1'000), btc(1'000), false, 1'000);
3661
3662 // auction slot is owned by the creator of the AMM i.e. gw
3663 BEAST_EXPECT(amm.expectAuctionSlot(100, 0, IOUAmount{0}));
3664
3665 // gw attempts to burn all her LPTokens through a bid transaction
3666 // this transaction fails because AMMBid transaction can not burn
3667 // all the outstanding LPTokens
3668 env(amm.bid({
3669 .account = gw_,
3670 .bidMin = 1'000'000,
3671 }),
3673 }
3674
3675 // burn all the LPTokens through a AMMBid transaction
3676 {
3677 Env env(*this);
3678 env.fund(XRP(2'000), gw_, alice_);
3679 MPTTester const btc({
3680 .env = env,
3681 .issuer = gw_,
3682 .holders = {alice_},
3683 .pay = 2'000,
3684 .flags = kMptDexFlags,
3685 });
3686 AMM amm(env, gw_, XRP(1'000), btc(1'000), false, 1'000);
3687
3688 // auction slot is owned by the creator of the AMM i.e. gw
3689 BEAST_EXPECT(amm.expectAuctionSlot(100, 0, IOUAmount{0}));
3690
3691 // gw burns all but one of its LPTokens through a bid transaction
3692 // this transaction succeeds because the bid price is less than
3693 // the total outstanding LPToken balance
3694 env(amm.bid({
3695 .account = gw_,
3696 .bidMin = STAmount{amm.lptIssue(), UINT64_C(999'999)},
3697 }),
3698 Ter(tesSUCCESS))
3699 .close();
3700
3701 // gw must own the auction slot
3702 BEAST_EXPECT(amm.expectAuctionSlot(100, 0, IOUAmount{999'999}));
3703
3704 // 999'999 tokens are burned, only 1 LPToken is owned by gw
3705 BEAST_EXPECT(amm.expectBalances(XRP(1'000), btc(1'000), IOUAmount{1}));
3706
3707 // gw owns only 1 LPToken in its balance
3708 BEAST_EXPECT(Number{amm.getLPTokensBalance(gw_)} == 1);
3709
3710 // gw attempts to burn the last of its LPTokens in an AMMBid
3711 // transaction. This transaction fails because it would burn all
3712 // the remaining LPTokens
3713 env(amm.bid({
3714 .account = gw_,
3715 .bidMin = 1,
3716 }),
3718 }
3719
3720 testAMM(
3721 [&](AMM& ammAlice, Env& env) {
3722 ammAlice.deposit(carol_, 1'000'000);
3723 // Invlaid Min/Max combination
3724 env(ammAlice.bid({
3725 .account = carol_,
3726 .bidMin = 200,
3727 .bidMax = 100,
3728 }),
3730
3731 // Invalid Account
3732 Account bad("bad");
3733 env.memoize(bad);
3734 env(ammAlice.bid({
3735 .account = bad,
3736 .bidMax = 100,
3737 }),
3738 Seq(1),
3740
3741 // Account is not LP
3742 Account const dan("dan");
3743 env.fund(XRP(1'000), dan);
3744 env(ammAlice.bid({
3745 .account = dan,
3746 .bidMin = 100,
3747 }),
3749 env(ammAlice.bid({
3750 .account = dan,
3751 }),
3753
3754 // Auth account is invalid.
3755 env(ammAlice.bid({
3756 .account = carol_,
3757 .bidMin = 100,
3758 .authAccounts = {bob_},
3759 }),
3760 Ter(terNO_ACCOUNT));
3761
3762 // Invalid Assets
3763 env(ammAlice.bid({
3764 .account = alice_,
3765 .bidMax = 100,
3766 .assets = {{MPT(ammAlice[1]), GBP}},
3767 }),
3768 Ter(terNO_AMM));
3769 },
3770 {{XRP(10'000), gAmmmpt(10'000)}});
3771
3772 // Invalid AMM
3773 testAMM(
3774 [&](AMM& ammAlice, Env& env) {
3775 ammAlice.withdrawAll(alice_);
3776 env(ammAlice.bid({
3777 .account = alice_,
3778 .bidMax = 100,
3779 }),
3780 Ter(terNO_AMM));
3781 },
3782 {{XRP(10'000), gAmmmpt(10'000)}});
3783
3784 // Bid price exceeds LP owned tokens
3785 {
3786 Env env(*this);
3787 fund(env, gw_, {alice_, bob_, carol_}, XRP(1'000), {USD(30'000)}, Fund::All);
3788 MPTTester const btc({
3789 .env = env,
3790 .issuer = gw_,
3791 .holders = {alice_, carol_, bob_},
3792 .pay = 30'000'000'000,
3793 .flags = kMptDexFlags,
3794 });
3795
3796 AMM ammAlice(env, alice_, btc(10'000'000'000), USD(10'000));
3797 ammAlice.deposit(carol_, 1'000'000);
3798 ammAlice.deposit(bob_, 10);
3799 env(ammAlice.bid({
3800 .account = carol_,
3801 .bidMin = 1'000'001,
3802 }),
3803 Ter(tecAMM_INVALID_TOKENS));
3804 env(ammAlice.bid({
3805 .account = carol_,
3806 .bidMax = 1'000'001,
3807 }),
3808 Ter(tecAMM_INVALID_TOKENS));
3809 env(ammAlice.bid({
3810 .account = carol_,
3811 .bidMin = 1'000,
3812 }));
3813 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount{1'000}));
3814 // Slot purchase price is more than 1000 but bob only has 10 tokens
3815 env(ammAlice.bid({
3816 .account = bob_,
3817 }),
3818 Ter(tecAMM_INVALID_TOKENS));
3819 }
3820
3821 // Bid all tokens, still own the slot
3822 {
3823 Env env(*this);
3824 env.fund(XRP(1'000), gw_, alice_, bob_);
3825 MPTTester const btc({
3826 .env = env,
3827 .issuer = gw_,
3828 .holders = {alice_, bob_},
3829 .pay = 1'000,
3830 .flags = kMptDexFlags,
3831 });
3832 AMM amm(env, gw_, XRP(10), btc(1'000));
3833 auto const lpIssue = amm.lptIssue();
3834 env.trust(STAmount{lpIssue, 100}, alice_);
3835 env.trust(STAmount{lpIssue, 50}, bob_);
3836 env(pay(gw_, alice_, STAmount{lpIssue, 100}));
3837 env(pay(gw_, bob_, STAmount{lpIssue, 50}));
3838 env(amm.bid({.account = alice_, .bidMin = 100}));
3839 // Alice doesn't have any more tokens, but
3840 // she still owns the slot.
3841 env(amm.bid({
3842 .account = bob_,
3843 .bidMax = 50,
3844 }),
3845 Ter(tecAMM_FAILED));
3846 }
3847 }
3848
3849 void
3851 {
3852 testcase("Bid");
3853 using namespace jtx;
3854 using namespace std::chrono;
3855
3856 // Auction slot initially is owned by AMM creator, who pays 0 price.
3857
3858 // Bid 110 tokens. Pay bidMin.
3859 testAMM(
3860 [&](AMM& ammAlice, Env& env) {
3861 ammAlice.deposit(carol_, 1'000'000);
3862 env(ammAlice.bid({.account = carol_, .bidMin = 110}));
3863 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount{110}));
3864 // 110 tokens are burned.
3865 BEAST_EXPECT(ammAlice.expectBalances(
3866 XRP(11'000), MPT(ammAlice[1])(11'000), IOUAmount{10'999'890, 0}));
3867 },
3868 {{XRP(10'000), gAmmmpt(10'000)}});
3869
3870 // Bid with min/max when the pay price is less than min.
3871 testAMM(
3872 [&](AMM& ammAlice, Env& env) {
3873 ammAlice.deposit(carol_, 1'000'000);
3874 // Bid exactly 110. Pay 110 because the pay price is < 110.
3875 env(ammAlice.bid({.account = carol_, .bidMin = 110, .bidMax = 110}));
3876 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount{110}));
3877 BEAST_EXPECT(ammAlice.expectBalances(
3878 XRP(11'000), MPT(ammAlice[1])(11'000), IOUAmount{10'999'890}));
3879 // Bid exactly 180-200. Pay 180 because the pay price is < 180.
3880 env(ammAlice.bid({.account = alice_, .bidMin = 180, .bidMax = 200}));
3881 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount{180}));
3882 BEAST_EXPECT(ammAlice.expectBalances(
3883 XRP(11'000), MPT(ammAlice[1])(11'000), IOUAmount{10'999'814'5, -1}));
3884 },
3885 {{XRP(10'000), gAmmmpt(10'000)}});
3886
3887 // Start bid at bidMin 110.
3888 {
3889 Env env(*this);
3890 env.fund(XRP(30'000), gw_, alice_, carol_, bob_);
3891 MPTTester const btc({
3892 .env = env,
3893 .issuer = gw_,
3894 .holders = {alice_, carol_, bob_},
3895 .pay = 30'000,
3896 .flags = kMptDexFlags,
3897 });
3898 AMM ammAlice(env, alice_, XRP(10'000), btc(10'000));
3899
3900 ammAlice.deposit(carol_, 1'000'000);
3901 // Bid, pay bidMin.
3902 env(ammAlice.bid({.account = carol_, .bidMin = 110}));
3903 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount{110}));
3904
3905 ammAlice.deposit(bob_, 1'000'000);
3906 // Bid, pay the computed price.
3907 env(ammAlice.bid({.account = bob_}));
3908 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount(1155, -1)));
3909
3910 // Bid bidMax fails because the computed price is higher.
3911 env(ammAlice.bid({
3912 .account = carol_,
3913 .bidMax = 120,
3914 }),
3916 // Bid MaxSlotPrice succeeds - pay computed price
3917 env(ammAlice.bid({.account = carol_, .bidMax = 600}));
3918 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount{121'275, -3}));
3919
3920 // Bid Min/MaxSlotPrice fails because the computed price is not
3921 // in range
3922 env(ammAlice.bid({
3923 .account = carol_,
3924 .bidMin = 10,
3925 .bidMax = 100,
3926 }),
3928 // Bid Min/MaxSlotPrice succeeds - pay computed price
3929 env(ammAlice.bid({.account = carol_, .bidMin = 100, .bidMax = 600}));
3930 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount{127'33875, -5}));
3931 }
3932
3933 // Slot states.
3934 {
3935 Env env(*this);
3936 env.fund(XRP(30'000), gw_, alice_, carol_, bob_);
3937 MPTTester const btc({
3938 .env = env,
3939 .issuer = gw_,
3940 .holders = {alice_, carol_, bob_},
3941 .pay = 30'000,
3942 .flags = kMptDexFlags,
3943 });
3944 AMM ammAlice(env, alice_, XRP(10'000), btc(10'000));
3945
3946 ammAlice.deposit(carol_, 1'000'000);
3947 ammAlice.deposit(bob_, 1'000'000);
3948 BEAST_EXPECT(ammAlice.expectBalances(
3949 XRPAmount(12'000'000001), btc(12'001), IOUAmount{12'000'000, 0}));
3950
3951 // Initial state. Pay bidMin.
3952 env(ammAlice.bid({.account = carol_, .bidMin = 110})).close();
3953 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount{110}));
3954
3955 // 1st Interval after close, price for 0th interval.
3956 env(ammAlice.bid({.account = bob_}));
3958 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 1, IOUAmount{1'155, -1}));
3959
3960 // 10th Interval after close, price for 1st interval.
3961 env(ammAlice.bid({.account = carol_}));
3963 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 10, IOUAmount{121'275, -3}));
3964
3965 // 20th Interval (expired) after close, price for 10th interval.
3966 env(ammAlice.bid({.account = bob_}));
3968 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, std::nullopt, IOUAmount{127'33875, -5}));
3969
3970 // 0 Interval.
3971 env(ammAlice.bid({.account = carol_, .bidMin = 110})).close();
3972 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, std::nullopt, IOUAmount{110}));
3973 // ~321.09 tokens burnt on bidding fees.
3974 BEAST_EXPECT(ammAlice.expectBalances(
3975 XRPAmount(12'000'000001), btc(12'001), IOUAmount{11'999'678'91000001, -8}));
3976 }
3977
3978 // Pool's fee 1%. Bid bidMin.
3979 // Auction slot owner and auth account trade at discounted fee -
3980 // 1/10 of the trading fee.
3981 // Other accounts trade at 1% fee.
3982 {
3983 Env env(*this);
3984 Account const dan("dan");
3985 Account const ed("ed");
3986 env.fund(XRP(2'000), gw_, alice_, bob_, carol_, dan, ed);
3987 MPTTester const btc({
3988 .env = env,
3989 .issuer = gw_,
3990 .holders = {alice_, bob_, carol_, dan, ed},
3991 .pay = 30'000'000'000,
3992 });
3993 fund(env, gw_, {alice_, carol_}, {USD(30'000)}, Fund::TokenOnly);
3994 fund(env, gw_, {bob_, dan, ed}, {USD(20'000)}, Fund::TokenOnly);
3995
3996 AMM ammAlice(env, alice_, btc(10'000'000'000), USD(10'000), CreateArg{.tfee = 1'000});
3997 ammAlice.deposit(bob_, 1'000'000);
3998 ammAlice.deposit(ed, 1'000'000);
3999 ammAlice.deposit(carol_, 500'000);
4000 ammAlice.deposit(dan, 500'000);
4001 auto ammTokens = ammAlice.getLPTokensBalance();
4002 env(ammAlice.bid({
4003 .account = carol_,
4004 .bidMin = 120,
4005 .authAccounts = {bob_, ed},
4006 }));
4007 auto const slotPrice = IOUAmount{5'200};
4008 ammTokens -= slotPrice;
4009 BEAST_EXPECT(ammAlice.expectAuctionSlot(100, 0, slotPrice));
4010 BEAST_EXPECT(ammAlice.expectBalances(btc(13'000'000'003), USD(13'000), ammTokens));
4011 // Discounted trade
4012 for (int i = 0; i < 10; ++i)
4013 {
4014 auto tokens = ammAlice.deposit(carol_, USD(100));
4015 ammAlice.withdraw(carol_, tokens, USD(0));
4016 tokens = ammAlice.deposit(bob_, USD(100));
4017 ammAlice.withdraw(bob_, tokens, USD(0));
4018 tokens = ammAlice.deposit(ed, USD(100));
4019 ammAlice.withdraw(ed, tokens, USD(0));
4020 }
4021 // carol, bob, and ed pay ~0.99USD in fees.
4022 BEAST_EXPECT(
4023 env.balance(carol_, USD) == STAmount(USD, UINT64_C(29'499'00572620545), -11));
4024 BEAST_EXPECT(
4025 env.balance(bob_, USD) == STAmount(USD, UINT64_C(18'999'00572616195), -11));
4026 BEAST_EXPECT(env.balance(ed, USD) == STAmount(USD, UINT64_C(18'999'00572611841), -11));
4027 // USD pool is slightly higher because of the fees.
4028 BEAST_EXPECT(ammAlice.expectBalances(
4029 btc(13'000'000'003), STAmount(USD, UINT64_C(13'002'98282151419), -11), ammTokens));
4030
4031 ammTokens = ammAlice.getLPTokensBalance();
4032 // Trade with the fee
4033 for (int i = 0; i < 10; ++i)
4034 {
4035 auto const tokens = ammAlice.deposit(dan, USD(100));
4036 ammAlice.withdraw(dan, tokens, USD(0));
4037 }
4038 // dan pays ~9.94USD, which is ~10 times more in fees than
4039 // carol, bob, ed. the discounted fee is 10 times less
4040 // than the trading fee.
4041
4042 BEAST_EXPECT(env.balance(dan, USD) == STAmount(USD, UINT64_C(19'490'05672274398), -11));
4043 // USD pool gains more in dan's fees.
4044 BEAST_EXPECT(ammAlice.expectBalances(
4045 btc(13'000'000'003), STAmount{USD, UINT64_C(13'012'92609877021), -11}, ammTokens));
4046 // Discounted fee payment
4047 ammAlice.deposit(carol_, USD(100));
4048 ammTokens = ammAlice.getLPTokensBalance();
4049 BEAST_EXPECT(ammAlice.expectBalances(
4050 MPT(ammAlice[0])(13'000'000'003),
4051 STAmount{USD, UINT64_C(13'112'92609877019), -11},
4052 ammTokens));
4053 env(pay(carol_, bob_, USD(100)), Path(~USD), Sendmax(btc(110'000'000)));
4054 env.close();
4055 // carol pays 100000 drops in fees
4056 // 99900668MPT swapped in for 100USD
4057 BEAST_EXPECT(ammAlice.expectBalances(
4058 btc(13'100'000'671), STAmount{USD, UINT64_C(13'012'92609877019), -11}, ammTokens));
4059
4060 // Payment with the trading fee
4061 env(pay(alice_, carol_, btc(100'000'000)), Path(~MPT(ammAlice[0])), Sendmax(USD(110)));
4062 env.close();
4063 // alice pays ~1.011USD in fees, which is ~10 times more
4064 // than carol's fee
4065 // 100.099431529USD swapped in for 100MPT
4066
4067 BEAST_EXPECT(ammAlice.expectBalances(
4068 btc(13'000'000'671), STAmount{USD, UINT64_C(13'114'03663044931), -11}, ammTokens));
4069
4070 // Auction slot expired, no discounted fee
4072 // clock is parent's based
4073 env.close();
4074
4075 BEAST_EXPECT(
4076 env.balance(carol_, USD) == STAmount(USD, UINT64_C(29'399'00572620547), -11));
4077 ammTokens = ammAlice.getLPTokensBalance();
4078 for (int i = 0; i < 10; ++i)
4079 {
4080 auto const tokens = ammAlice.deposit(carol_, USD(100));
4081 ammAlice.withdraw(carol_, tokens, USD(0));
4082 }
4083 // carol pays ~9.94USD in fees, which is ~10 times more in
4084 // trading fees vs discounted fee.
4085
4086 BEAST_EXPECT(
4087 env.balance(carol_, USD) == STAmount(USD, UINT64_C(29'389'06197177122), -11));
4088 BEAST_EXPECT(ammAlice.expectBalances(
4089 btc(13'000'000'671), STAmount{USD, UINT64_C(13'123'98038488356), -11}, ammTokens));
4090
4091 env(pay(carol_, bob_, USD(100)), Path(~USD), Sendmax(btc(110'000'000)));
4092 env.close();
4093 // carol pays ~1.008MPT in trading fee, which is
4094 // ~10 times more than the discounted fee.
4095 // 99.815876MPT is swapped in for 100USD
4096 BEAST_EXPECT(ammAlice.expectBalances(
4097 btc(13'100'824'793), STAmount{USD, UINT64_C(13'023'98038488356), -11}, ammTokens));
4098 }
4099
4100 // Bid tiny amount
4101 testAMM(
4102 [&](AMM& ammAlice, Env& env) {
4103 // Bid a tiny amount
4105 auto const cleanup340 = env.current()->rules().enabled(fixCleanup3_4_0);
4106 auto const minBidPrice = IOUAmount{ammAuctionMinSlotPrice(ammAlice.tokens(), 1)};
4107 auto const firstPrice = cleanup340 ? minBidPrice : IOUAmount{tiny};
4108 env(ammAlice.bid({.account = alice_, .bidMin = IOUAmount{tiny}}));
4109 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, firstPrice));
4110 BEAST_EXPECT(ammAlice.expectBalances(
4111 MPT(ammAlice[0])(10'000'000'000),
4112 USD(10'000),
4113 cleanup340 ? IOUAmount{Number{ammAlice.tokens()} - Number{minBidPrice}}
4114 : ammAlice.tokens()));
4115 // Bid the tiny amount
4116 env(ammAlice.bid({
4117 .account = alice_,
4118 .bidMin = IOUAmount{STAmount::kMinValue, STAmount::kMinOffset},
4119 }));
4120 // Pay slightly higher price
4121 BEAST_EXPECT(ammAlice.expectAuctionSlot(
4122 0, 0, IOUAmount{Number{firstPrice} * Number{105, -2}}));
4123 BEAST_EXPECT(ammAlice.expectBalances(
4124 MPT(ammAlice[0])(10'000'000'000),
4125 USD(10'000),
4126 cleanup340
4127 ? IOUAmount{Number{ammAlice.tokens()} - Number{minBidPrice} * Number{11, -1}}
4128 : ammAlice.tokens()));
4129 },
4130 {{gAmmmpt(10'000'000'000), USD(10'000)}});
4131
4132 // Reset auth account
4133 testAMM(
4134 [&](AMM& ammAlice, Env& env) {
4135 env(ammAlice.bid({
4136 .account = alice_,
4137 .bidMin = IOUAmount{100},
4138 .authAccounts = {carol_},
4139 }));
4140 BEAST_EXPECT(ammAlice.expectAuctionSlot({carol_}));
4141 env(ammAlice.bid({.account = alice_, .bidMin = IOUAmount{100}}));
4142 BEAST_EXPECT(ammAlice.expectAuctionSlot({}));
4143 Account bob("bob");
4144 Account dan("dan");
4145 fund(env, {bob, dan}, XRP(1'000));
4146 env(ammAlice.bid({
4147 .account = alice_,
4148 .bidMin = IOUAmount{100},
4149 .authAccounts = {bob, dan},
4150 }));
4151 BEAST_EXPECT(ammAlice.expectAuctionSlot({bob, dan}));
4152 },
4153 {{gAmmmpt(10'000'000'000), USD(10'000)}});
4154
4155 // Bid all tokens, still own the slot and trade at a discount
4156 {
4157 Env env(*this);
4158 env.fund(XRP(2'000), gw_, alice_, bob_);
4159 MPTTester const btc({
4160 .env = env,
4161 .issuer = gw_,
4162 .holders = {alice_, bob_},
4163 .pay = 2'000'000'000,
4164 .flags = kMptDexFlags,
4165 });
4166 fund(env, gw_, {alice_, bob_}, {USD(2'000)}, Fund::TokenOnly);
4167 AMM amm(env, gw_, btc(1'000'000'000), USD(1'010), false, 1'000);
4168 auto const lpIssue = amm.lptIssue();
4169 env.trust(STAmount{lpIssue, 500}, alice_);
4170 env.trust(STAmount{lpIssue, 50}, bob_);
4171 env(pay(gw_, alice_, STAmount{lpIssue, 500}));
4172 env(pay(gw_, bob_, STAmount{lpIssue, 50}));
4173 // Alice doesn't have anymore lp tokens
4174 env(amm.bid({.account = alice_, .bidMin = 500}));
4175 BEAST_EXPECT(amm.expectAuctionSlot(100, 0, IOUAmount{500}));
4176 BEAST_EXPECT(expectHolding(env, alice_, STAmount{lpIssue, 0}));
4177 // But trades with the discounted fee since she still owns the slot.
4178 // Alice pays ~10011 MPT in fees
4179 env(pay(alice_, bob_, USD(10)), Path(~USD), Sendmax(btc(11'000'000)));
4180 BEAST_EXPECT(amm.expectBalances(
4181 btc(1'010'010'011), USD(1'000), IOUAmount{1'004'487'562112089, -9}));
4182
4183 // Bob pays the full fee ~0.1USD
4184 env(pay(bob_, alice_, btc(10'000'000)), Path(~MPT(btc)), Sendmax(USD(11)));
4185
4186 BEAST_EXPECT(amm.expectBalances(
4187 btc(1'000'010'011),
4188 STAmount{USD, UINT64_C(1'010'10090898081), -11},
4189 IOUAmount{1'004'487'562112089, -9}));
4190 }
4191
4192 // preflight tests
4193 {
4194 Env env(*this, features);
4195 env.fund(XRP(2'000), gw_, alice_, bob_);
4196 MPTTester const btc({
4197 .env = env,
4198 .issuer = gw_,
4199 .holders = {alice_, bob_},
4200 .pay = 2'000,
4201 .flags = kMptDexFlags,
4202 });
4203 AMM amm(env, gw_, XRP(1'000), btc(1'010), false, 1'000);
4204 json::Value const tx = amm.bid({.account = alice_, .bidMin = 500});
4205
4206 {
4207 auto const jtx = env.jt(tx, Seq(1), Fee(10));
4208 env.app().config().features.erase(featureMPTokensV2);
4209 PreflightContext const ctx(
4210 env.app(), *jtx.stx, env.current()->rules(), TapNone, env.journal);
4211 auto const pf = AMMBid::checkExtraFeatures(ctx);
4212 BEAST_EXPECT(pf == false);
4213 env.app().config().features.insert(featureMPTokensV2);
4214 }
4215
4216 {
4217 auto jtx = env.jt(tx, Seq(1), Fee(10));
4218 jtx.jv["Asset2"]["currency"] = "XRP";
4219 jtx.jv["Asset2"].removeMember("mpt_issuance_id");
4220 jtx.stx = env.ust(jtx);
4221 PreflightContext const ctx(
4222 env.app(), *jtx.stx, env.current()->rules(), TapNone, env.journal);
4223 auto const pf = AMMBid::preflight(ctx);
4224 BEAST_EXPECT(pf == temBAD_AMM_TOKENS);
4225 }
4226 }
4227 }
4228
4229 void
4231 {
4232 testcase("Clawback");
4233 using namespace jtx;
4234
4235 Env env(*this);
4236 env.fund(XRP(2'000), gw_, alice_);
4237 MPT const btc = MPTTester(
4238 {.env = env, .issuer = gw_, .holders = {alice_}, .transferFee = 1'500, .pay = 40'000});
4239
4240 // alice creates AMM
4241 AMM const amm(env, alice_, XRP(1'000), btc(1'000));
4242
4243 // gw owns MPTIssuance, not allowed to set asfAllowTrustLineClawback
4244 env(fset(gw_, asfAllowTrustLineClawback), Ter(tecOWNERS));
4245 }
4246
4247 void
4249 {
4250 testcase("test clawback from AMM account");
4251 using namespace jtx;
4252
4253 Env env(*this, features);
4254 env.fund(XRP(1'000), gw_);
4255 env(fset(gw_, asfAllowTrustLineClawback));
4256 fund(env, gw_, {alice_}, XRP(1'000), {USD(1'000)}, Fund::Acct);
4257
4258 MPTTester const btc({
4259 .env = env,
4260 .issuer = gw_,
4261 .holders = {alice_},
4262 .pay = 30'000,
4263 .flags = tfMPTCanLock | kMptDexFlags,
4264 });
4265
4266 // to clawback from AMM account, must use AMMClawback instead of
4267 // Clawback
4268 auto const err = features[featureSingleAssetVault] ? tecPSEUDO_ACCOUNT : tecAMM_ACCOUNT;
4269 AMM const amm(env, gw_, XRP(100), btc(100));
4270 auto const amount = amountFromString(amm.lptIssue(), "10");
4271 env(claw(gw_, amount), Ter(err));
4272
4273 AMM const amm1(env, alice_, USD(100), btc(200));
4274 auto const amount1 = amountFromString(amm1.lptIssue(), "10");
4275 env(claw(gw_, amount1), Ter(err));
4276 }
4277
4278 void
4280 {
4281 testcase("Invalid AMM Payment");
4282 using namespace jtx;
4283 using namespace jtx::paychan;
4284 using namespace std::chrono;
4285 using namespace std::literals::chrono_literals;
4286
4287 // Can't pay into AMM account.
4288 // Can't pay out since there is no keys
4289 for (auto const& acct : {gw_, alice_})
4290 {
4291 {
4292 Env env(*this);
4293 fund(env, gw_, {alice_, carol_}, XRP(1'000));
4294 MPTTester const btc({
4295 .env = env,
4296 .issuer = gw_,
4297 .holders = {alice_, carol_},
4298 .pay = 100,
4299 .flags = kMptDexFlags,
4300 });
4301 // XRP balance is below reserve
4302 AMM const ammAlice(env, acct, XRP(10), btc(10));
4303 // Pay below reserve
4304 env(pay(carol_, ammAlice.ammAccount(), XRP(10)), Ter(tecNO_PERMISSION));
4305 // Pay above reserve
4306 env(pay(carol_, ammAlice.ammAccount(), XRP(300)), Ter(tecNO_PERMISSION));
4307 // Pay MPT
4308 env(pay(carol_, ammAlice.ammAccount(), btc(10)), Ter(tecNO_PERMISSION));
4309 }
4310
4311 {
4312 Env env(*this);
4313 fund(env, gw_, {alice_, carol_}, XRP(10'000'000));
4314 MPTTester const btc({
4315 .env = env,
4316 .issuer = gw_,
4317 .holders = {alice_, carol_},
4318 .pay = 20'000,
4319 .flags = kMptDexFlags,
4320 });
4321 // XRP balance is above reserve
4322 AMM const ammAlice(env, acct, XRP(1'000'000), btc(10'000));
4323 // Pay below reserve
4324 env(pay(carol_, ammAlice.ammAccount(), XRP(10)), Ter(tecNO_PERMISSION));
4325 // Pay above reserve
4326 env(pay(carol_, ammAlice.ammAccount(), XRP(1'000'000)), Ter(tecNO_PERMISSION));
4327 // Pay MPT
4328 env(pay(carol_, ammAlice.ammAccount(), btc(1'000)), Ter(tecNO_PERMISSION));
4329 }
4330 }
4331
4332 // Can't pay into AMM with escrow.
4333 testAMM(
4334 [&](AMM& ammAlice, Env& env) {
4335 env(escrow::create(carol_, ammAlice.ammAccount(), MPT(ammAlice[1])(1)),
4337 escrow::kFinishTime(env.now() + 1s),
4338 escrow::kCancelTime(env.now() + 2s),
4339 Fee(1'500),
4341
4342 env(escrow::create(carol_, ammAlice.ammAccount(), XRP(1)),
4344 escrow::kFinishTime(env.now() + 1s),
4345 escrow::kCancelTime(env.now() + 2s),
4346 Fee(1'500),
4348 },
4349 {{XRP(10'000), gAmmmpt(10'000)}});
4350
4351 // Can't pay into AMM with paychan.
4352 testAMM(
4353 [&](AMM& ammAlice, Env& env) {
4354 auto const pk = carol_.pk();
4355 auto const settleDelay = 10s;
4356 NetClock::time_point const cancelAfter =
4357 env.current()->header().parentCloseTime + 20s;
4358 env(create(
4359 carol_,
4360 ammAlice.ammAccount(),
4361 MPT(ammAlice[1])(1'000),
4362 settleDelay,
4363 pk,
4364 cancelAfter),
4366
4367 env(create(carol_, ammAlice.ammAccount(), XRP(1'000), settleDelay, pk, cancelAfter),
4369 },
4370 {{XRP(10'000), gAmmmpt(10'000)}});
4371
4372 // Can't pay into AMM with checks.
4373 testAMM(
4374 [&](AMM& ammAlice, Env& env) {
4375 env(check::create(env.master.id(), ammAlice.ammAccount(), XRP(100)),
4377
4378 env(check::create(env.master.id(), ammAlice.ammAccount(), MPT(ammAlice[1])(100)),
4380 },
4381 {{XRP(10'000), gAmmmpt(10'000)}});
4382
4383 // Pay amounts close to one side of the pool
4384 testAMM(
4385 [&](AMM& ammAlice, Env& env) {
4386 auto const& btc = MPT(ammAlice[1]);
4387 // Can't consume whole pool
4388 env(pay(alice_, carol_, USD(100)),
4389 Path(~USD),
4390 Sendmax(btc(1'000'000'000)),
4392 env(pay(alice_, carol_, btc(100'000'000)),
4393 Path(~btc),
4394 Sendmax(USD(1'000'000'000)),
4396 // Overflow
4397 env(pay(alice_, carol_, STAmount{USD, UINT64_C(99'999999999), -9}),
4398 Path(~USD),
4399 Sendmax(btc(1'000'000'000)),
4401 env(pay(alice_, carol_, STAmount{USD, UINT64_C(999'99999999), -8}),
4402 Path(~USD),
4403 Sendmax(btc(1'000'000'000)),
4405 env(pay(alice_, carol_, btc(99'999'999)),
4406 Path(~btc),
4407 Sendmax(USD(1'000'000'000)),
4409 // Sender doesn't have enough funds
4410 env(pay(alice_, carol_, USD(99.99)),
4411 Path(~USD),
4412 Sendmax(btc(1'000'000'000)),
4414 env(pay(alice_, carol_, btc(99'990'000)),
4415 Path(~btc),
4416 Sendmax(USD(1'000'000'000)),
4418 },
4419 {{USD(100), gAmmmpt(100'000'000)}});
4420
4421 // Globally locked MPT.
4422 {
4423 Env env(*this);
4424 env.fund(XRP(30'000), gw_, alice_, carol_);
4425 MPTTester btc({
4426 .env = env,
4427 .issuer = gw_,
4428 .holders = {alice_, carol_},
4429 .pay = 30'000,
4430 .flags = tfMPTCanLock | kMptDexFlags,
4431 });
4432
4433 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'000));
4434 btc.set({.flags = tfMPTLock});
4435
4436 env(pay(alice_, carol_, btc(1)),
4437 Path(~static_cast<MPT>(btc)),
4438 Txflags(tfPartialPayment | tfNoRippleDirect),
4439 Sendmax(XRP(10)),
4440 Ter(tecPATH_DRY));
4441 env(pay(alice_, carol_, XRP(1)),
4442 Path(~XRP),
4443 Txflags(tfPartialPayment | tfNoRippleDirect),
4444 Sendmax(btc(10)),
4445 Ter(tecPATH_DRY));
4446 }
4447
4448 // Individually locked MPT destination account.
4449 {
4450 Env env(*this);
4451 env.fund(XRP(30'000), gw_, alice_, carol_);
4452 MPTTester btc({
4453 .env = env,
4454 .issuer = gw_,
4455 .holders = {alice_, carol_},
4456 .pay = 30'000,
4457 .flags = tfMPTCanLock | kMptDexFlags,
4458 });
4459
4460 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'000));
4461 btc.set({.holder = carol_, .flags = tfMPTLock});
4462
4463 env(pay(alice_, carol_, btc(1)),
4464 Path(~static_cast<MPT>(btc)),
4465 Txflags(tfPartialPayment | tfNoRippleDirect),
4466 Sendmax(XRP(10)),
4467 Ter(tecPATH_DRY));
4468 }
4469
4470 // Individually locked MPT source account
4471 {
4472 Env env(*this);
4473 env.fund(XRP(30'000), gw_, alice_, carol_);
4474 MPTTester btc({
4475 .env = env,
4476 .issuer = gw_,
4477 .holders = {alice_, carol_},
4478 .pay = 30'000,
4479 .flags = tfMPTCanLock | kMptDexFlags,
4480 });
4481
4482 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'000));
4483 btc.set({.holder = alice_, .flags = tfMPTLock});
4484
4485 env(pay(alice_, carol_, XRP(1)),
4486 Path(~XRP),
4487 Txflags(tfPartialPayment | tfNoRippleDirect),
4488 Sendmax(btc(10)),
4489 Ter(tecPATH_DRY));
4490 }
4491
4492 // lock on both sides
4493 {
4494 Env env(*this);
4495 env.fund(XRP(30'000), gw_, alice_, carol_);
4496 MPTTester btc({
4497 .env = env,
4498 .issuer = gw_,
4499 .holders = {alice_, carol_},
4500 .pay = 30'000,
4501 .flags = tfMPTCanLock | kMptDexFlags,
4502 });
4503 MPTTester eth({
4504 .env = env,
4505 .issuer = gw_,
4506 .holders = {alice_, carol_},
4507 .pay = 30'000,
4508 .flags = tfMPTCanLock | kMptDexFlags,
4509 });
4510
4511 AMM const ammAlice(env, alice_, eth(10'000), btc(10'000));
4512 btc.set({.holder = carol_, .flags = tfMPTLock});
4513 btc.set({.holder = alice_, .flags = tfMPTLock});
4514 eth.set({.holder = carol_, .flags = tfMPTLock});
4515 eth.set({.holder = alice_, .flags = tfMPTLock});
4516
4517 env(pay(alice_, carol_, eth(1)),
4518 Path(~MPT(eth)),
4519 Txflags(tfPartialPayment | tfNoRippleDirect),
4520 Sendmax(btc(10)),
4521 Ter(tecPATH_DRY));
4522
4523 env(pay(alice_, carol_, btc(1)),
4524 Path(~MPT(btc)),
4525 Txflags(tfPartialPayment | tfNoRippleDirect),
4526 Sendmax(eth(10)),
4527 Ter(tecPATH_DRY));
4528 }
4529
4530 // Individually locked AMM MPT
4531 {
4532 Env env(*this);
4533 env.fund(XRP(30'000), gw_, alice_, carol_);
4534 MPTTester btc({
4535 .env = env,
4536 .issuer = gw_,
4537 .holders = {alice_, carol_},
4538 .pay = 30'000,
4539 .flags = tfMPTCanLock | kMptDexFlags,
4540 });
4541
4542 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'000));
4543 btc.set({.holder = ammAlice.ammAccount(), .flags = tfMPTLock});
4544
4545 env(pay(alice_, carol_, XRP(1)),
4546 Path(~XRP),
4547 Txflags(tfPartialPayment | tfNoRippleDirect),
4548 Sendmax(btc(10)),
4549 Ter(tecPATH_DRY));
4550 }
4551 }
4552
4553 void
4555 {
4556 testcase("Basic Payment");
4557 using namespace jtx;
4558
4559 // Payment 100MPT for 100XRP.
4560 // Force one path with tfNoRippleDirect.
4561 testAMM(
4562 [&](AMM& ammAlice, Env& env) {
4563 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
4564 env.fund(XRP(30'000), bob_);
4565 env.close();
4566 env(pay(bob_, carol_, MPT(ammAlice[1])(100)),
4567 Path(~MPT(ammAlice[1])),
4568 Sendmax(XRP(100)),
4569 Txflags(tfNoRippleDirect));
4570 env.close();
4571 BEAST_EXPECT(ammAlice.expectBalances(
4572 XRP(10'100), MPT(ammAlice[1])(10'000), ammAlice.tokens()));
4573 // Initial balance + 100
4574 env.require(Balance(carol_, carolMPT + MPT(ammAlice[1])(100)));
4575 // Initial balance 30,000 - 100(sendmax) - 10(tx fee)
4576 BEAST_EXPECT(
4577 expectLedgerEntryRoot(env, bob_, XRP(30'000) - XRP(100) - txFee(env, 1)));
4578 },
4579 {{XRP(10'000), gAmmmpt(10'100)}});
4580
4581 // Payment 100IOU/MPT for 100IOU/MPT. Test IOU/MPT mix.
4582 // Force one path with tfNoRippleDirect.
4583 {
4584 auto const test = [&](auto&& issue1, auto&& issue2) {
4585 Env env(*this);
4586 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
4587 env.close();
4588
4589 auto const usd = issue1({
4590 .env = env,
4591 .token = "USD",
4592 .issuer = gw_,
4593 .holders = {alice_, bob_, carol_},
4594 .limit = 1'000'000,
4595 });
4596 auto const btc = issue2({
4597 .env = env,
4598 .token = "BTC",
4599 .issuer = gw_,
4600 .holders = {alice_, bob_, carol_},
4601 .limit = 1'000'000,
4602 });
4603
4604 env(pay(gw_, alice_, btc(50000)));
4605 env(pay(gw_, bob_, btc(50000)));
4606 env(pay(gw_, carol_, btc(50000)));
4607 env(pay(gw_, alice_, usd(50000)));
4608 env(pay(gw_, bob_, usd(50000)));
4609 env(pay(gw_, carol_, usd(50000)));
4610 env.close();
4611
4612 auto const ammAlice = AMM(env, alice_, usd(10000), btc(10100));
4613 auto const carolBTC = env.balance(carol_, btc);
4614 auto const bobUSD = env.balance(bob_, usd);
4615 env(pay(bob_, carol_, btc(100)),
4616 Path(~btc),
4617 Sendmax(usd(100)),
4618 Txflags(tfNoRippleDirect | tfPartialPayment));
4619 env.close();
4620 BEAST_EXPECT(ammAlice.expectBalances(usd(10'100), btc(10'000), ammAlice.tokens()));
4621 env.require(Balance(carol_, carolBTC + btc(100)));
4622 env.require(Balance(bob_, bobUSD - usd(100)));
4623 };
4625 }
4626
4627 // Payment 100MPT for 100XRP, use default path.
4628 testAMM(
4629 [&](AMM& ammAlice, Env& env) {
4630 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
4631 env.fund(XRP(30'000), bob_);
4632 env.close();
4633 env(pay(bob_, carol_, MPT(ammAlice[1])(100)), Sendmax(XRP(100)));
4634 env.close();
4635 BEAST_EXPECT(ammAlice.expectBalances(
4636 XRP(10'100), MPT(ammAlice[1])(10'000), ammAlice.tokens()));
4637 // Initial balance + 100
4638 env.require(Balance(carol_, carolMPT + MPT(ammAlice[1])(100)));
4639 // Initial balance 30,000 - 100(sendmax) - 10(tx fee)
4640 BEAST_EXPECT(
4641 expectLedgerEntryRoot(env, bob_, XRP(30'000) - XRP(100) - txFee(env, 1)));
4642 },
4643 {{XRP(10'000), gAmmmpt(10'100)}});
4644
4645 // Payment 100IOU/MPT for 100IOU/MPT using default path.
4646 // Test IOU/MPT mix.
4647 {
4648 auto const test = [&](auto&& issue1, auto&& issue2) {
4649 Env env(*this);
4650 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
4651 env.close();
4652
4653 auto const usd = issue1({
4654 .env = env,
4655 .token = "USD",
4656 .issuer = gw_,
4657 .holders = {alice_, bob_, carol_},
4658 .limit = 1'000'000,
4659 });
4660 auto const btc = issue2({
4661 .env = env,
4662 .token = "BTC",
4663 .issuer = gw_,
4664 .holders = {alice_, bob_, carol_},
4665 .limit = 1'000'000,
4666 });
4667
4668 env(pay(gw_, alice_, btc(50000)));
4669 env(pay(gw_, bob_, btc(50000)));
4670 env(pay(gw_, carol_, btc(50000)));
4671 env(pay(gw_, alice_, usd(50000)));
4672 env(pay(gw_, bob_, usd(50000)));
4673 env(pay(gw_, carol_, usd(50000)));
4674 env.close();
4675
4676 auto const ammAlice = AMM(env, alice_, usd(10000), btc(10100));
4677 auto const carolBTC = env.balance(carol_, btc);
4678 auto const bobUSD = env.balance(bob_, usd);
4679 env(pay(bob_, carol_, btc(100)), Sendmax(usd(100)));
4680 env.close();
4681 BEAST_EXPECT(ammAlice.expectBalances(usd(10'100), btc(10'000), ammAlice.tokens()));
4682 env.require(Balance(carol_, carolBTC + btc(100)));
4683 env.require(Balance(bob_, bobUSD - usd(100)));
4684 };
4686 }
4687
4688 // This payment is identical to above. While it has
4689 // both default path and path, activeStrands has one path.
4690 testAMM(
4691 [&](AMM& ammAlice, Env& env) {
4692 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
4693 env.fund(XRP(30'000), bob_);
4694 env.close();
4695 env(pay(bob_, carol_, MPT(ammAlice[1])(100)),
4696 Path(~MPT(ammAlice[1])),
4697 Sendmax(XRP(100)));
4698 env.close();
4699 BEAST_EXPECT(ammAlice.expectBalances(
4700 XRP(10'100), MPT(ammAlice[1])(10'000), ammAlice.tokens()));
4701 // Initial balance + 100
4702 env.require(Balance(carol_, carolMPT + MPT(ammAlice[1])(100)));
4703 // Initial balance 30,000 - 100(sendmax) - 10(tx fee)
4704 BEAST_EXPECT(
4705 expectLedgerEntryRoot(env, bob_, XRP(30'000) - XRP(100) - txFee(env, 1)));
4706 },
4707 {{XRP(10'000), gAmmmpt(10'100)}});
4708
4709 // Test MPT/IOU combination for the case above.
4710 {
4711 auto const test = [&](auto&& issue1, auto&& issue2) {
4712 Env env(*this);
4713 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
4714 env.close();
4715
4716 auto const usd = issue1({
4717 .env = env,
4718 .token = "USD",
4719 .issuer = gw_,
4720 .holders = {alice_, bob_, carol_},
4721 .limit = 1'000'000,
4722 });
4723 auto const btc = issue2({
4724 .env = env,
4725 .token = "BTC",
4726 .issuer = gw_,
4727 .holders = {alice_, bob_, carol_},
4728 .limit = 1'000'000,
4729 });
4730
4731 env(pay(gw_, alice_, btc(50000)));
4732 env(pay(gw_, bob_, btc(50000)));
4733 env(pay(gw_, carol_, btc(50000)));
4734 env(pay(gw_, alice_, usd(50000)));
4735 env(pay(gw_, bob_, usd(50000)));
4736 env(pay(gw_, carol_, usd(50000)));
4737 env.close();
4738
4739 auto const ammAlice = AMM(env, alice_, usd(10000), btc(10100));
4740 auto const carolBTC = env.balance(carol_, btc);
4741 auto const bobUSD = env.balance(bob_, usd);
4742 env(pay(bob_, carol_, btc(100)), Path(~btc), Sendmax(usd(100)));
4743 env.close();
4744 BEAST_EXPECT(ammAlice.expectBalances(usd(10'100), btc(10'000), ammAlice.tokens()));
4745 env.require(Balance(carol_, carolBTC + btc(100)));
4746 env.require(Balance(bob_, bobUSD - usd(100)));
4747 };
4749 }
4750
4751 // Payment with limitQuality set.
4752 testAMM(
4753 [&](AMM& ammAlice, Env& env) {
4754 auto const carolMPT = env.balance(carol_, MPT(ammAlice[1]));
4755 env.fund(jtx::XRP(30'000), bob_);
4756 env.close();
4757 // Pays 10MPT for 10XRP. A larger payment of ~99.11MPT/100XRP
4758 // would have been sent has it not been for limitQuality.
4759 env(pay(bob_, carol_, MPT(ammAlice[1])(100)),
4760 Path(~MPT(ammAlice[1])),
4761 Sendmax(XRP(100)),
4762 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
4763 env.close();
4764 BEAST_EXPECT(ammAlice.expectBalances(
4765 XRP(10'010), MPT(ammAlice[1])(10'000), ammAlice.tokens()));
4766 // Initial balance + 10(limited by limitQuality)
4767 env.require(Balance(carol_, carolMPT + MPT(ammAlice[1])(10)));
4768 // Initial balance 30,000 - 10(limited by limitQuality) - 10(tx
4769 // fee)
4770 BEAST_EXPECT(
4771 expectLedgerEntryRoot(env, bob_, XRP(30'000) - XRP(10) - txFee(env, 1)));
4772
4773 // Fails because of limitQuality. Would have sent
4774 // ~98.91MPT/110XRP has it not been for limitQuality.
4775 env(pay(bob_, carol_, MPT(ammAlice[1])(100)),
4776 Path(~MPT(ammAlice[1])),
4777 Sendmax(XRP(100)),
4778 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality),
4779 Ter(tecPATH_DRY));
4780 env.close();
4781 },
4782 {{XRP(10'000), gAmmmpt(10'010)}});
4783
4784 // Payment with limitQuality set. MPT/IOU combination.
4785 {
4786 auto const test = [&](auto&& issue1, auto&& issue2) {
4787 Env env(*this);
4788 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
4789 env.close();
4790
4791 auto const usd = issue1({
4792 .env = env,
4793 .token = "USD",
4794 .issuer = gw_,
4795 .holders = {alice_, bob_, carol_},
4796 .limit = 1'000'000,
4797 });
4798 auto const btc = issue2({
4799 .env = env,
4800 .token = "BTC",
4801 .issuer = gw_,
4802 .holders = {alice_, bob_, carol_},
4803 .limit = 1'000'000,
4804 });
4805
4806 env(pay(gw_, alice_, btc(50000)));
4807 env(pay(gw_, bob_, btc(50000)));
4808 env(pay(gw_, carol_, btc(50000)));
4809 env(pay(gw_, alice_, usd(50000)));
4810 env(pay(gw_, bob_, usd(50000)));
4811 env(pay(gw_, carol_, usd(50000)));
4812 env.close();
4813
4814 auto const ammAlice = AMM(env, alice_, usd(10000), btc(10010));
4815 auto const carolBTC = env.balance(carol_, btc);
4816 auto const bobUSD = env.balance(bob_, usd);
4817 env(pay(bob_, carol_, btc(100)),
4818 Path(~btc),
4819 Sendmax(usd(100)),
4820 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
4821 env.close();
4822 BEAST_EXPECT(ammAlice.expectBalances(usd(10'010), btc(10'000), ammAlice.tokens()));
4823 env.require(Balance(carol_, carolBTC + btc(10)));
4824 env.require(Balance(bob_, bobUSD - usd(10)));
4825
4826 env(pay(bob_, carol_, btc(100)),
4827 Path(~btc),
4828 Sendmax(usd(100)),
4829 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality),
4830 Ter(tecPATH_DRY));
4831 env.close();
4832 };
4834 }
4835
4836 // Payment with limitQuality and transfer fee set.
4837 {
4838 Env env(*this);
4839 env.fund(XRP(30'000), gw_, alice_, bob_, carol_);
4840 env.close();
4841 MPTTester const btc({
4842 .env = env,
4843 .issuer = gw_,
4844 .holders = {alice_, carol_},
4845 .transferFee = 10'000,
4846 .pay = 30'000'000'000'000'000,
4847 .flags = kMptDexFlags,
4848 });
4849 auto const ammAlice = AMM(env, alice_, XRP(10'000), btc(10'010'000'000'000'000));
4850 env.close();
4851 auto const carolMPT = env.balance(carol_, MPT(btc));
4852 // Pays 10'000'000'000'000MPT for 10XRP. A larger payment of
4853 // ~99'110'000'000'000MPT/100XRP would have been sent has it not
4854 // been for limitQuality and the transfer fee.
4855 env(pay(bob_, carol_, btc(100'000'000'000'000)),
4856 Path(~MPT(btc)),
4857 Sendmax(XRP(110)),
4858 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
4859 env.close();
4860 BEAST_EXPECT(ammAlice.expectBalances(
4861 XRP(10'010), btc(10'000'000'000'000'000), ammAlice.tokens()));
4862 // 10'000'000'000'000MPT - 10% transfer fee
4863 env.require(Balance(carol_, carolMPT + btc(9'090'909'090'909)));
4864 BEAST_EXPECT(expectLedgerEntryRoot(env, bob_, XRP(30'000) - XRP(10) - txFee(env, 1)));
4865 }
4866
4867 // Payment with limitQuality and transfer fee set. MPT/IOU combination.
4868 {
4869 auto const test = [&](auto&& issue1, auto&& issue2) {
4870 Env env(*this);
4871 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
4872 env.close();
4873
4874 auto const usd = issue1({
4875 .env = env,
4876 .token = "USD",
4877 .issuer = gw_,
4878 .holders = {alice_, bob_, carol_},
4879 .limit = 1'000'000,
4880 //.transferFee = 10'000
4881 });
4882 auto const btc = issue2({
4883 .env = env,
4884 .token = "BTC",
4885 .issuer = gw_,
4886 .holders = {alice_, bob_, carol_},
4887 .limit = 300'000'000'000'000'000,
4888 .transferFee = 10'000,
4889 });
4890
4891 env(pay(gw_, alice_, btc(30'000'000'000'000'000)));
4892 env(pay(gw_, bob_, btc(30'000'000'000'000'000)));
4893 env(pay(gw_, carol_, btc(30'000'000'000'000'000)));
4894 env(pay(gw_, alice_, usd(50000)));
4895 env(pay(gw_, bob_, usd(50000)));
4896 env(pay(gw_, carol_, usd(50000)));
4897 env.close();
4898
4899 auto const ammAlice = AMM(env, alice_, usd(10'000), btc(10'010'000'000'000'000));
4900 auto const carolBTC = env.balance(carol_, btc);
4901 auto const bobUSD = env.balance(bob_, usd);
4902 env(pay(bob_, carol_, btc(100'000'000'000'000)),
4903 Path(~btc),
4904 Sendmax(usd(110)),
4905 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
4906 env.close();
4907 BEAST_EXPECT(ammAlice.expectBalances(
4908 usd(10'010), btc(10'000'000'000'000'000), ammAlice.tokens()));
4909 env.require(Balance(carol_, carolBTC + btc(9'090'909'090'909)));
4910 env.require(Balance(bob_, bobUSD - usd(10)));
4911 };
4913 }
4914
4915 // Fail when partial payment is not set.
4916 testAMM(
4917 [&](AMM& ammAlice, Env& env) {
4918 env.fund(jtx::XRP(30'000), bob_);
4919 env.close();
4920 env(pay(bob_, carol_, MPT(ammAlice[1])(100)),
4921 Path(~MPT(ammAlice[1])),
4922 Sendmax(XRP(100)),
4923 Txflags(tfNoRippleDirect),
4925 },
4926 {{XRP(10'000), gAmmmpt(10'000)}});
4927 // Fail when partial payment is not set. MPT/IOU combination.
4928 {
4929 auto const test = [&](auto&& issue1, auto&& issue2) {
4930 Env env(*this);
4931 env.fund(XRP(30'000), alice_, bob_, carol_, gw_);
4932 env.close();
4933
4934 auto const usd = issue1({
4935 .env = env,
4936 .token = "USD",
4937 .issuer = gw_,
4938 .holders = {alice_, bob_, carol_},
4939 .limit = 1'000'000,
4940 });
4941 auto const btc = issue2({
4942 .env = env,
4943 .token = "BTC",
4944 .issuer = gw_,
4945 .holders = {alice_, bob_, carol_},
4946 .limit = 1'000'000,
4947 });
4948
4949 env(pay(gw_, alice_, btc(50000)));
4950 env(pay(gw_, bob_, btc(50000)));
4951 env(pay(gw_, carol_, btc(50000)));
4952 env(pay(gw_, alice_, usd(50000)));
4953 env(pay(gw_, bob_, usd(50000)));
4954 env(pay(gw_, carol_, usd(50000)));
4955 env.close();
4956
4957 auto const ammAlice = AMM(env, alice_, usd(10000), btc(10000));
4958 env(pay(bob_, carol_, btc(100)),
4959 Path(~btc),
4960 Sendmax(usd(100)),
4961 Txflags(tfNoRippleDirect),
4963 };
4965 }
4966
4967 // Non-default path (with AMM) has a better quality than default path.
4968 // The max possible liquidity is taken out of non-default
4969 // path ~29.9e14XRP/29.9e14ETH, ~29.9e14ETH/~29.99e14btc. The rest
4970 // is taken from the offer.
4971 {
4972 Env env(*this);
4973 env.fund(XRP(30'000), gw_, alice_, carol_);
4974 MPTTester const btc({
4975 .env = env,
4976 .issuer = gw_,
4977 .holders = {alice_, carol_},
4978 .pay = 3'000'000'000'000'000'000,
4979 .flags = kMptDexFlags,
4980 });
4981 MPTTester const eth({
4982 .env = env,
4983 .issuer = gw_,
4984 .holders = {alice_, carol_},
4985 .pay = 3'000'000'000'000'000'000,
4986 .flags = kMptDexFlags,
4987 });
4988 env.fund(XRP(1'000), bob_);
4989 env.close();
4990 auto const ammEthXrp = AMM(env, alice_, XRP(10'000), eth(1'000'000'000'000'000'000));
4991 auto const ammBtcEth =
4992 AMM(env, alice_, eth(1'000'000'000'000'000'000), btc(1'000'000'000'000'000'000));
4993 env(offer(alice_, XRP(101), btc(10'000'000'000'000'000)), Txflags(tfPassive));
4994 env.close();
4995 env(pay(bob_, carol_, btc(10'000'000'000'000'000)),
4996 Path(~MPT(eth), ~MPT(btc)),
4997 Sendmax(XRP(102)),
4998 Txflags(tfPartialPayment));
4999 env.close();
5000 BEAST_EXPECT(ammEthXrp.expectBalances(
5001 XRPAmount(10'030'082'730), eth(9'970'00749812546872), ammEthXrp.tokens()));
5002
5003 BEAST_EXPECT(ammBtcEth.expectBalances(
5004 btc(9'970'09727766213961), eth(10'029'99250187453128), ammBtcEth.tokens()));
5005
5006 Amounts const expectedAmounts = Amounts{XRPAmount(30'201'749), btc(29'90272233786039)};
5007
5008 BEAST_EXPECT(expectOffers(env, alice_, 1, {{expectedAmounts}}));
5009
5010 // Initial (30,000 + 100)e14
5011 env.require(Balance(carol_, btc(3'010'000'000'000'000'000)));
5012 // Initial 1,000 - 30082730(AMM pool) - 70798251(offer) - 10(tx fee)
5013 BEAST_EXPECT(expectLedgerEntryRoot(
5014 env,
5015 bob_,
5016 XRP(1'000) - XRPAmount{30'082'730} - XRPAmount{70'798'251} - txFee(env, 1)));
5017 }
5018
5019 // Default path (with AMM) has a better quality than a
5020 // non-default path.
5021 // The max possible liquidity is taken out of default
5022 // path ~49XRP/49e14BTC. The rest is taken from the offer.
5023 {
5024 Env env(*this);
5025 env.fund(XRP(30'000), gw_, alice_, carol_);
5026 MPTTester const btc({
5027 .env = env,
5028 .issuer = gw_,
5029 .holders = {alice_, carol_},
5030 .pay = 3'000'000'000'000'000'000,
5031 .flags = kMptDexFlags,
5032 });
5033 MPTTester const eth({
5034 .env = env,
5035 .issuer = gw_,
5036 .holders = {alice_},
5037 .pay = 1'000'000'000'000'000'000,
5038 .flags = kMptDexFlags,
5039 });
5040 auto const ammAlice = AMM(env, alice_, XRP(10'000), btc(1'000'000'000'000'000'000));
5041 env.fund(XRP(1'000), bob_);
5042 env.close();
5043 env(offer(alice_, XRP(101), eth(10'000'000'000'000'000)), Txflags(tfPassive));
5044 env.close();
5045 env(offer(alice_, eth(10'000'000'000'000'000), btc(10'000'000'000'000'000)),
5046 Txflags(tfPassive));
5047 env.close();
5048 env(pay(bob_, carol_, btc(10'000'000'000'000'000)),
5049 Path(~MPT(eth), ~MPT(btc)),
5050 Sendmax(XRP(102)),
5051 Txflags(tfPartialPayment));
5052 env.close();
5053 BEAST_EXPECT(ammAlice.expectBalances(
5054 XRPAmount(10'050'238'637), btc(9'950'01249687578120), ammAlice.tokens()));
5055 BEAST_EXPECT(expectOffers(
5056 env,
5057 alice_,
5058 2,
5059 {{Amounts{XRPAmount(50'487'378), eth(49'98750312421880)},
5060 Amounts{eth(49'98750312421880), btc(49'98750312421880)}}}));
5061 // Initial (30,000 + 100)e14
5062 env.require(Balance(carol_, btc(30'100'00000000000000)));
5063 // Initial 1,000 - 50238637(AMM pool) - 50512622(offer) - 10(tx
5064 // fee)
5065 BEAST_EXPECT(expectLedgerEntryRoot(
5066 env,
5067 bob_,
5068 XRP(1'000) - XRPAmount{50'238'637} - XRPAmount{50'512'622} - txFee(env, 1)));
5069 }
5070
5071 // Default path with AMM and Order Book offer. AMM is consumed first,
5072 // remaining amount is consumed by the offer.
5073 {
5074 Env env(*this);
5075 fund(env, gw_, {alice_, bob_, carol_}, XRP(30'000));
5076 MPTTester const btc({
5077 .env = env,
5078 .issuer = gw_,
5079 .holders = {alice_, bob_, carol_},
5080 .pay = 30'000'000'000'000'000,
5081 .flags = kMptDexFlags,
5082 });
5083 AMM ammAlice(env, alice_, XRP(10'000), btc(10'100'000'000'000'000));
5084 env(offer(bob_, XRP(100), MPT(ammAlice[1])(100'000'000'000'000)), Txflags(tfPassive));
5085 env.close();
5086 env(pay(alice_, carol_, MPT(ammAlice[1])(200'000'000'000'000)),
5087 Sendmax(XRP(200)),
5088 Txflags(tfPartialPayment));
5089 env.close();
5090
5091 BEAST_EXPECT(ammAlice.expectBalances(
5092 XRP(10'100), MPT(ammAlice[1])(10'000'000000000001), ammAlice.tokens()));
5093 env.require(Balance(carol_, MPT(ammAlice[1])(30'199'999999999999)));
5094
5095 // Initial 30,000 - 10000(AMM pool LP) - 100(AMM offer) -
5096 // - 100(offer) - 10(tx fee) - 10(tx fee of MPTTester init as
5097 // holder) - one reserve
5098 BEAST_EXPECT(expectLedgerEntryRoot(
5099 env,
5100 alice_,
5101 XRP(30'000) - XRP(10'000) - XRP(100) - XRP(100) - ammCrtFee(env) -
5102 2 * txFee(env, 1)));
5103 BEAST_EXPECT(expectOffers(env, bob_, 0));
5104 }
5105
5106 // Default path with AMM and Order Book offer.
5107 // Order Book offer is consumed first.
5108 // Remaining amount is consumed by AMM.
5109 {
5110 Env env(*this);
5111 fund(env, gw_, {alice_, bob_, carol_}, XRP(20'000));
5112 MPTTester const btc({
5113 .env = env,
5114 .issuer = gw_,
5115 .holders = {alice_, bob_, carol_},
5116 .pay = 2'000,
5117 .flags = kMptDexFlags,
5118 });
5119 env(offer(bob_, XRP(50), btc(150)), Txflags(tfPassive));
5120 env.close();
5121 AMM const ammAlice(env, alice_, XRP(1'000), btc(1'050));
5122 env(pay(alice_, carol_, btc(200)), Sendmax(XRP(200)), Txflags(tfPartialPayment));
5123 BEAST_EXPECT(ammAlice.expectBalances(XRP(1'050), btc(1'000), ammAlice.tokens()));
5124 env.require(Balance(carol_, btc(2'200)));
5125 BEAST_EXPECT(expectOffers(env, bob_, 0));
5126 }
5127
5128 // Offer crossing XRP/MPT
5129 {
5130 Env env(*this);
5131 fund(env, gw_, {alice_, bob_, carol_}, XRP(30'000));
5132 MPTTester const btc({
5133 .env = env,
5134 .issuer = gw_,
5135 .holders = {alice_, bob_, carol_},
5136 .pay = 30'000,
5137 .flags = kMptDexFlags,
5138 });
5139 AMM ammAlice(env, alice_, XRP(10'000), btc(10'100));
5140 env(offer(bob_, MPT(ammAlice[1])(100), XRP(100)));
5141 env.close();
5142 BEAST_EXPECT(
5143 ammAlice.expectBalances(XRP(10'100), MPT(ammAlice[1])(10'000), ammAlice.tokens()));
5144 // Initial 30,000 + 100
5145 env.require(Balance(bob_, MPT(ammAlice[1])(30'100)));
5146 // Initial 30,000 - 100(offer) - 10(tx fee) - 1(tx fee for MPTTester
5147 // holder)
5148 BEAST_EXPECT(
5149 expectLedgerEntryRoot(env, bob_, XRP(30'000) - XRP(100) - 2 * txFee(env, 1)));
5150 BEAST_EXPECT(expectOffers(env, bob_, 0));
5151 }
5152
5153 // Offer crossing MPT/MPT and transfer rate
5154 // Single path AMM offer
5155 {
5156 Env env(*this);
5157 env.fund(XRP(30'000), gw_, alice_, carol_);
5158 MPTTester const btc({
5159 .env = env,
5160 .issuer = gw_,
5161 .holders = {alice_, carol_},
5162 .transferFee = 25'000,
5163 .pay = 30'000'000'000'000'000,
5164 .flags = kMptDexFlags,
5165 });
5166 MPTTester const eth({
5167 .env = env,
5168 .issuer = gw_,
5169 .holders = {alice_, carol_},
5170 .transferFee = 25'000,
5171 .pay = 30'000'000'000'000'000,
5172 .flags = kMptDexFlags,
5173 });
5174 AMM const ammAlice(env, alice_, btc(1'000'000'000'000'000), eth(1'100'000'000'000'000));
5175 // This offer crosses because the strand's out amount is small enough to match
5176 // limitQuality value and limitOut() function in StrandFlow
5177 // doesn't require an adjustment to out value.
5178 env(offer(carol_, eth(100'000'000'000'000), btc(100'000'000'000'000)));
5179 env.close();
5180 // No transfer fee
5181 BEAST_EXPECT(ammAlice.expectBalances(
5182 btc(1'100'000'000'000'000), eth(1'000'000'000'000'000), ammAlice.tokens()));
5183 // Initial 30,000'000'000'000'000 - 100'000'000'000'000(offer)-25 %
5184 // transfer fee
5185 env.require(Balance(carol_, btc(29'875'000'000'000'000)));
5186 // Initial 30,000'000'000'000'000 + 100'000'000'000'000(offer)
5187 env.require(Balance(carol_, eth(30'100'000'000'000'000)));
5188 BEAST_EXPECT(expectOffers(env, carol_, 0));
5189 }
5190
5191 // Single-path AMM offer
5192 {
5193 Env env(*this);
5194 env.fund(XRP(30'000), gw_, alice_, bob_, carol_);
5195 MPTTester const btc({
5196 .env = env,
5197 .issuer = gw_,
5198 .holders = {alice_, carol_},
5199 .transferFee = 100,
5200 .pay = 30'000'000'000'000'000,
5201 .flags = kMptDexFlags,
5202 });
5203 AMM const amm(env, alice_, XRP(1'000), btc(500'000'000'000'000));
5204 env(offer(carol_, XRP(100), btc(55'000'000'000'000)));
5205 env.close();
5206
5207 BEAST_EXPECT(
5208 amm.expectBalances(XRPAmount(909'090'910), btc(549'999999450001), amm.tokens()));
5209 // Offer ~91XRP/50e12BTC
5210 BEAST_EXPECT(expectOffers(
5211 env, carol_, 1, {{Amounts{XRPAmount{9'090'910}, btc(5'000000500000)}}}));
5212 // Carol pays 0.1% fee on 49'999999450001BTC.
5213 env.require(Balance(carol_, btc(29'949'950'000'550'548)));
5214 }
5215
5216 {
5217 Env env(*this);
5218 env.fund(XRP(30'000), gw_, alice_, carol_);
5219 MPTTester const btc({
5220 .env = env,
5221 .issuer = gw_,
5222 .holders = {alice_, carol_},
5223 .transferFee = 100,
5224 .pay = 3'000'000'000'000'000'000,
5225 .flags = kMptDexFlags,
5226 });
5227 AMM const amm(env, alice_, XRP(1'000), btc(50'000'000'000'000'000));
5228 env(offer(carol_, XRP(10), btc(5'500'000'000'000'000)));
5229 env.close();
5230
5231 BEAST_EXPECT(amm.expectBalances(XRP(990), btc(505'05050505050506), amm.tokens()));
5232 BEAST_EXPECT(expectOffers(env, carol_, 0));
5233 }
5234
5235 // Multi-path AMM offer
5236 {
5237 Env env(*this);
5238 Account const ed("ed");
5239 env.fund(XRP(30'000), gw_, alice_, bob_, carol_, ed);
5240 MPTTester const btc({
5241 .env = env,
5242 .issuer = gw_,
5243 .holders = {alice_, bob_, carol_, ed},
5244 .transferFee = 25'000,
5245 .pay = 20'000'000'000'000'000,
5246 .flags = kMptDexFlags,
5247 });
5248 MPTTester const eth({
5249 .env = env,
5250 .issuer = gw_,
5251 .holders = {alice_, bob_, carol_, ed},
5252 .transferFee = 25'000,
5253 .pay = 20'000'000'000'000'000,
5254 .flags = kMptDexFlags,
5255 });
5256 AMM const ammAlice(
5257 env, alice_, btc(10'000'000'000'000'000), eth(11'000'000'000'000'000));
5258
5259 env(offer(bob_, btc(100'000'000'000'000), XRP(10)), Txflags(tfPassive));
5260 env(offer(ed, XRP(10), eth(100'000'000'000'000)), Txflags(tfPassive));
5261 env.close();
5262 env(offer(carol_, eth(1'000'000'000'000'000), btc(1'000'000'000'000'000)));
5263 env.close();
5264
5265 BEAST_EXPECT(ammAlice.expectBalances(
5266 btc(1'060'6848287928025), eth(1'037'0658372213582), ammAlice.tokens()));
5267 // Consumed offer ~72.93e13ETH/72.93e13BTC
5268 BEAST_EXPECT(expectOffers(
5269 env, carol_, 1, {Amounts{eth(27'0658372213582), btc(27'0658372213582)}}));
5270 BEAST_EXPECT(expectOffers(env, bob_, 0));
5271 BEAST_EXPECT(expectOffers(env, ed, 0));
5272
5273 env.require(Balance(carol_, btc(19'116'439'640'089'965)));
5274 env.require(Balance(carol_, eth(20'729'341'627'786'418)));
5275 env.require(Balance(bob_, btc(20'100'000'000'000'000)));
5276 env.require(Balance(ed, eth(19'875'000'000'000'000)));
5277 }
5278
5279 // Payment and transfer fee
5280 // Scenario:
5281 // Bob sends 125BTC to pay 80EUR to Carol
5282 // Payment execution:
5283 // bob's 125BTC/1.25 = 100BTC
5284 // 100BTC/100EUR AMM offer
5285 // 100EUR/1.25 = 80EUR paid to carol
5286 {
5287 Env env(*this);
5288 env.fund(XRP(30'000), gw_, alice_, bob_, carol_);
5289 MPTTester const btc({
5290 .env = env,
5291 .issuer = gw_,
5292 .holders = {alice_, bob_, carol_},
5293 .transferFee = 25'000,
5294 .pay = 30'000,
5295 .flags = kMptDexFlags,
5296 });
5297 MPTTester const eth({
5298 .env = env,
5299 .issuer = gw_,
5300 .holders = {alice_, bob_, carol_},
5301 .transferFee = 25'000,
5302 .pay = 30'000,
5303 .flags = kMptDexFlags,
5304 });
5305 AMM const ammAlice(env, alice_, btc(1'000), eth(1'100));
5306 env(rate(gw_, 1.25));
5307 env.close();
5308 env(pay(bob_, carol_, eth(100)),
5309 Path(~MPT(eth)),
5310 Sendmax(btc(125)),
5311 Txflags(tfPartialPayment));
5312 env.close();
5313 BEAST_EXPECT(ammAlice.expectBalances(btc(1'100), eth(1'000), ammAlice.tokens()));
5314 env.require(Balance(bob_, btc(29'875)));
5315 env.require(Balance(carol_, eth(30'080)));
5316 }
5317
5318 // Payment and transfer fee, multiple steps
5319 // Scenario:
5320 // Dan's offer 200CAN/200GBP
5321 // AMM 1000GBP/10125ETH
5322 // Ed's offer 200ETH/BTC
5323 // Bob sends 195.3125CAN to pay 100BTC to Carol
5324 // Payment execution:
5325 // bob's 195.3125CAN/1.25 = 156.25CAN -> dan's offer
5326 // 156.25CAN/156.25GBP 156.25GBP/1.25 = 125GBP -> AMM's offer
5327 // 125GBP/125ETH 125ETH/1.25 = 100ETH -> ed's offer
5328 // 100ETH/100BTC 100BTC/1.25 = 80BTC paid to carol
5329 {
5330 Env env(*this);
5331 Account const dan("dan");
5332 Account const ed("ed");
5333 env.fund(XRP(30'000), gw_, alice_, bob_, carol_, dan, ed);
5334 MPTTester const btc({
5335 .env = env,
5336 .issuer = gw_,
5337 .holders = {alice_, bob_, carol_, dan, ed},
5338 .transferFee = 25'000,
5339 .pay = 30'000,
5340 .flags = kMptDexFlags,
5341 });
5342 MPTTester const eth({
5343 .env = env,
5344 .issuer = gw_,
5345 .holders = {alice_, bob_, carol_, dan, ed},
5346 .transferFee = 25'000,
5347 .pay = 30'000,
5348 .flags = kMptDexFlags,
5349 });
5350 MPTTester const can({
5351 .env = env,
5352 .issuer = gw_,
5353 .holders = {alice_, bob_, carol_, dan, ed},
5354 .transferFee = 25'000,
5355 .pay = 2'000'000,
5356 .flags = kMptDexFlags,
5357 });
5358 MPTTester const gbp({
5359 .env = env,
5360 .issuer = gw_,
5361 .holders = {alice_, bob_, carol_, dan, ed},
5362 .transferFee = 25'000,
5363 .pay = 3'000'000,
5364 .flags = kMptDexFlags,
5365 });
5366 AMM const ammAlice(env, alice_, gbp(1'000'000), eth(10'125));
5367 env(pay(gw_, bob_, can(1'953'125)));
5368 env.close();
5369 env(offer(dan, can(2'000'000), gbp(20'000)));
5370 env(offer(ed, eth(200), btc(200)));
5371 env.close();
5372 env(pay(bob_, carol_, btc(100)),
5373 Path(~MPT(gbp), ~MPT(eth), ~MPT(btc)),
5374 Sendmax(can(1'953'125)),
5375 Txflags(tfPartialPayment));
5376 env.close();
5377 env.require(Balance(bob_, can(2'000'000)));
5378 env.require(Balance(dan, can(3'562'500)));
5379 env.require(Balance(dan, gbp(2'984'375)));
5380
5381 BEAST_EXPECT(ammAlice.expectBalances(gbp(1'012'500), eth(10'000), ammAlice.tokens()));
5382 env.require(Balance(ed, eth(30'100)));
5383 env.require(Balance(ed, btc(29'900)));
5384 env.require(Balance(carol_, btc(30'080)));
5385 }
5386
5387 // Pay amounts close to one side of the pool
5388 testAMM(
5389 [&](AMM& ammAlice, Env& env) {
5390 auto const& btc = MPT(ammAlice[1]);
5391 env(pay(alice_, carol_, btc(9999)),
5392 Path(~btc),
5393 Sendmax(XRP(1)),
5394 Txflags(tfPartialPayment),
5395 Ter(tesSUCCESS));
5396 env(pay(alice_, carol_, btc(10'000)),
5397 Path(~btc),
5398 Sendmax(XRP(1)),
5399 Txflags(tfPartialPayment),
5400 Ter(tesSUCCESS));
5401 env(pay(alice_, carol_, XRP(100)),
5402 Path(~XRP),
5403 Sendmax(btc(100)),
5404 Txflags(tfPartialPayment),
5405 Ter(tesSUCCESS));
5406 env(pay(alice_, carol_, STAmount{xrpIssue(), 99'999'900}),
5407 Path(~XRP),
5408 Sendmax(btc(100)),
5409 Txflags(tfPartialPayment),
5410 Ter(tesSUCCESS));
5411 },
5412 {{XRP(100), gAmmmpt(10'000)}});
5413
5414 // Multiple paths/steps
5415 {
5416 Env env(*this);
5417 env.fund(XRP(100'000), gw_, alice_);
5418 env.fund(XRP(1'000), bob_, carol_);
5419 MPTTester const btc({
5420 .env = env,
5421 .issuer = gw_,
5422 .holders = {alice_, bob_, carol_},
5423 .pay = 500'000'000'000'000'000,
5424 .flags = kMptDexFlags,
5425 });
5426 MPTTester const eth({
5427 .env = env,
5428 .issuer = gw_,
5429 .holders = {alice_, bob_, carol_},
5430 .pay = 500'000'000'000'000'000,
5431 .flags = kMptDexFlags,
5432 });
5433 MPTTester const usd({
5434 .env = env,
5435 .issuer = gw_,
5436 .holders = {alice_, bob_, carol_},
5437 .pay = 500'000'000'000'000'000,
5438 .flags = kMptDexFlags,
5439 });
5440 MPTTester const eur({
5441 .env = env,
5442 .issuer = gw_,
5443 .holders = {alice_, bob_, carol_},
5444 .pay = 500'000'000'000'000'000,
5445 .flags = kMptDexFlags,
5446 });
5447 AMM const xrpEur(env, alice_, XRP(10'100), eur(100'000'000'000'000'000));
5448 AMM const eurBtc(
5449 env, alice_, eur(100'000'000'000'000'000), btc(102'000'000'000'000'000));
5450 AMM const btcUsd(
5451 env, alice_, btc(101'000'000'000'000'000), usd(100'000'000'000'000'000));
5452 AMM const xrpUsd(env, alice_, XRP(10'150), usd(102'000'000'000'000'000));
5453 AMM const xrpEth(env, alice_, XRP(10'000), eth(101'000'000'000'000'000));
5454 AMM const ethEur(
5455 env, alice_, eth(109'000'000'000'000'000), eur(110'000'000'000'000'000));
5456 AMM const eurUsd(
5457 env, alice_, eur(101'000'000'000'000'000), usd(100'000'000'000'000'000));
5458 env(pay(bob_, carol_, usd(1'000'000'000'000'000)),
5459 Path(~MPT(eur), ~MPT(btc), ~MPT(usd)),
5460 Path(~MPT(usd)),
5461 Path(~MPT(eth), ~MPT(eur), ~MPT(usd)),
5462 Sendmax(XRP(200)));
5463
5464 BEAST_EXPECT(xrpEth.expectBalances(
5465 XRPAmount(10'026'208'900), eth(10'073'6577924447994), xrpEth.tokens()));
5466 BEAST_EXPECT(ethEur.expectBalances(
5467 eth(10'926'3422075552006), eur(10'973'5423207873690), ethEur.tokens()));
5468 BEAST_EXPECT(eurUsd.expectBalances(
5469 eur(10'126'4576792126310), usd(9'973'9315171207179), eurUsd.tokens()));
5470 // XRP-USD path
5471 // This path provides ~73.9e12USD/74.1XRP
5472 BEAST_EXPECT(xrpUsd.expectBalances(
5473 XRPAmount(10'224'106'246), usd(10'126'0684828792821), xrpUsd.tokens()));
5474
5475 // XRP-EUR-BTC-USD
5476 // This path doesn't provide any liquidity due to how
5477 // offers are generated in multi-path.
5478 // Analytical solution shows a different distribution:
5479 // XRP-EUR-BTC-USD 11.6e12USD/11.64XRP,
5480 // XRP-USD 60.7e12USD/60.8XRP,
5481 // XRP-ETH-EUR-USD 27.6e12USD/27.6XRP
5482 BEAST_EXPECT(
5483 xrpEur.expectBalances(XRP(10'100), eur(100'000'000'000'000'000), xrpEur.tokens()));
5484 BEAST_EXPECT(eurBtc.expectBalances(
5485 eur(100'000'000'000'000'000), btc(102'000'000'000'000'000), eurBtc.tokens()));
5486 BEAST_EXPECT(btcUsd.expectBalances(
5487 btc(101'000'000'000'000'000), usd(100'000'000'000'000'000), btcUsd.tokens()));
5488 env.require(Balance(carol_, usd(501'000'000'000'000'000)));
5489 }
5490
5491 // Dependent AMM
5492 {
5493 Env env(*this);
5494 env.fund(XRP(40'000), gw_, alice_);
5495 env.fund(XRP(1'000), bob_, carol_);
5496 MPTTester const btc({
5497 .env = env,
5498 .issuer = gw_,
5499 .holders = {alice_, bob_, carol_},
5500 .pay = 50'000'000'000'000'000,
5501 .flags = kMptDexFlags,
5502 });
5503 MPTTester const eth({
5504 .env = env,
5505 .issuer = gw_,
5506 .holders = {alice_, bob_, carol_},
5507 .pay = 50'000'000'000'000'000,
5508 .flags = kMptDexFlags,
5509 });
5510 MPTTester const usd({
5511 .env = env,
5512 .issuer = gw_,
5513 .holders = {alice_, bob_, carol_},
5514 .pay = 50'000'000'000'000'000,
5515 .flags = kMptDexFlags,
5516 });
5517 MPTTester const eur({
5518 .env = env,
5519 .issuer = gw_,
5520 .holders = {alice_, bob_, carol_},
5521 .pay = 50'000'000'000'000'000,
5522 .flags = kMptDexFlags,
5523 });
5524 AMM const xrpEur(env, alice_, XRP(10'100), eur(10'000'000'000'000'000));
5525 AMM const eurBtc(env, alice_, eur(10'000'000'000'000'000), btc(10'200'000'000'000'000));
5526 AMM const btcUsd(env, alice_, btc(10'100'000'000'000'000), usd(10'000'000'000'000'000));
5527 AMM const xrpEth(env, alice_, XRP(10'000), eth(10'100'000'000'000'000));
5528 AMM const ethEur(env, alice_, eth(10'900'000'000'000'000), eur(11'000'000'000'000'000));
5529 env(pay(bob_, carol_, usd(100'000'000'000'000)),
5530 Path(~MPT(eur), ~MPT(btc), ~MPT(usd)),
5531 Path(~MPT(eth), ~MPT(eur), ~MPT(btc), ~MPT(usd)),
5532 Sendmax(XRP(200)));
5533
5534 BEAST_EXPECT(xrpEur.expectBalances(
5535 XRPAmount(10'118'738'472), eur(9'981'544436337981), xrpEur.tokens()));
5536 BEAST_EXPECT(eurBtc.expectBalances(
5537 eur(10'101'160967851758), btc(10'097'914269680647), eurBtc.tokens()));
5538 BEAST_EXPECT(btcUsd.expectBalances(
5539 btc(10'202'085730319353), usd(9'900'000'000'000'000), btcUsd.tokens()));
5540 BEAST_EXPECT(xrpEth.expectBalances(
5541 XRPAmount(10'082'446'397), eth(10'017'410727780081), xrpEth.tokens()));
5542 BEAST_EXPECT(ethEur.expectBalances(
5543 eth(10'982'589272219919), eur(10'917'294595810261), ethEur.tokens()));
5544 env.require(Balance(carol_, usd(50'100'000'000'000'000)));
5545 }
5546
5547 // AMM offers limit
5548 // Consuming 30 CLOB offers, results in hitting 30 AMM offers limit.
5549 {
5550 Env env(*this);
5551 env.fund(XRP(30'000), gw_, alice_, carol_);
5552 env.fund(XRP(1'000), bob_);
5553 MPTTester const btc({
5554 .env = env,
5555 .issuer = gw_,
5556 .holders = {alice_, bob_, carol_},
5557 .pay = 30'000'000'000'000'000,
5558 .flags = kMptDexFlags,
5559 });
5560 MPTTester const eth({
5561 .env = env,
5562 .issuer = gw_,
5563 .holders = {alice_, bob_, carol_},
5564 .pay = 400'000'000'000'000,
5565 .flags = kMptDexFlags,
5566 });
5567 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'000'000'000'000'000));
5568
5569 for (int i = 0; i < 30; ++i)
5570 env(offer(alice_, eth(1'000'000'000'000 + (10'000'000'000 * i)), XRP(1)));
5571 // This is worse quality offer than 30 offers above.
5572 // It will not be consumed because of AMM offers limit.
5573 env(offer(alice_, eth(140'000'000'000'000), XRP(100)));
5574 env(pay(bob_, carol_, btc(100'000'000'000'000)),
5575 Path(~XRP, ~MPT(btc)),
5576 Sendmax(eth(400'000'000'000'000)),
5577 Txflags(tfPartialPayment | tfNoRippleDirect));
5578
5579 BEAST_EXPECT(
5580 ammAlice.expectBalances(XRP(10'030), btc(9'970'089730807592), ammAlice.tokens()));
5581
5582 env.require(Balance(carol_, btc(30'029'910269192408)));
5583 BEAST_EXPECT(expectOffers(env, alice_, 1, {{{eth(140'000'000'000'000), XRP(100)}}}));
5584 }
5585
5586 // This payment is fulfilled
5587 {
5588 Env env(*this);
5589 env.fund(XRP(30'000), gw_, alice_, carol_);
5590 env.fund(XRP(1'000), bob_);
5591 MPTTester const btc({
5592 .env = env,
5593 .issuer = gw_,
5594 .holders = {alice_, bob_, carol_},
5595 .pay = 30'000'000'000'000'000,
5596 .flags = kMptDexFlags,
5597 });
5598 MPTTester const eth({
5599 .env = env,
5600 .issuer = gw_,
5601 .holders = {alice_, bob_, carol_},
5602 .pay = 400'000'000'000'000,
5603 .flags = kMptDexFlags,
5604 });
5605 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'000'000'000'000'000));
5606
5607 for (int i = 0; i < 29; ++i)
5608 env(offer(alice_, eth(1'000'000'000'000 + (10'000'000'000 * i)), XRP(1)));
5609 // This is worse quality offer than 30 offers above.
5610 // It will not be consumed because of AMM offers limit.
5611 env(offer(alice_, eth(140'000'000'000'000), XRP(100)));
5612 env(pay(bob_, carol_, btc(100'000'000'000'000)),
5613 Path(~XRP, ~MPT(btc)),
5614 Sendmax(eth(400'000'000'000'000)),
5615 Txflags(tfPartialPayment | tfNoRippleDirect));
5616 BEAST_EXPECT(ammAlice.expectBalances(
5617 XRPAmount{10'101'010'102}, btc(9'900'000'000'000'000), ammAlice.tokens()));
5618
5619 env.require(Balance(carol_, btc(30'100'000'000'000'000)));
5620 BEAST_EXPECT(
5621 expectOffers(env, alice_, 1, {{{eth(39'185857200000), XRPAmount{27'989'898}}}}));
5622 }
5623
5624 // Offer crossing with AMM and another offer.
5625 // AMM has a better quality and is consumed first.
5626 {
5627 Env env(*this);
5628 env.fund(XRP(30'000), gw_, alice_, carol_);
5629 env.fund(XRP(1'000), bob_);
5630 MPTTester const btc({
5631 .env = env,
5632 .issuer = gw_,
5633 .holders = {alice_, bob_, carol_},
5634 .pay = 30'000'000'000'000'000,
5635 .flags = kMptDexFlags,
5636 });
5637
5638 env(offer(bob_, XRP(100), btc(100'001'000'000'000)));
5639 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'100'000'000'000'000));
5640 env(offer(carol_, btc(100'000'000'000'000), XRP(100)));
5641
5642 BEAST_EXPECT(ammAlice.expectBalances(
5643 XRPAmount{10'049'825'372}, btc(10'049'925870493027), ammAlice.tokens()));
5644 BEAST_EXPECT(
5645 expectOffers(env, bob_, 1, {{{XRPAmount{50'074'628}, btc(50'075129506973)}}}));
5646
5647 env.require(Balance(carol_, btc(30'100'000'000'000'000)));
5648 }
5649
5650 // Individually locked MPT destination account
5651 {
5652 Env env(*this);
5653 env.fund(XRP(30'000), gw_, alice_, carol_);
5654 MPTTester btc({
5655 .env = env,
5656 .issuer = gw_,
5657 .holders = {alice_, carol_},
5658 .pay = 30'000,
5659 .flags = tfMPTCanLock | kMptDexFlags,
5660 });
5661 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'000));
5662
5663 btc.set({.holder = carol_, .flags = tfMPTLock});
5664
5665 env(pay(alice_, carol_, XRP(1)),
5666 Path(~XRP),
5667 Sendmax(btc(10)),
5668 Txflags(tfNoRippleDirect | tfPartialPayment),
5669 Ter(tesSUCCESS));
5670 }
5671
5672 // Individually locked MPT source account
5673 {
5674 Env env(*this);
5675 env.fund(XRP(30'000), gw_, alice_, carol_);
5676 MPTTester btc({
5677 .env = env,
5678 .issuer = gw_,
5679 .holders = {alice_, carol_},
5680 .pay = 30'000,
5681 .flags = tfMPTCanLock | kMptDexFlags,
5682 });
5683 AMM const ammAlice(env, alice_, XRP(10'000), btc(10'000));
5684
5685 btc.set({.holder = alice_, .flags = tfMPTLock});
5686
5687 env(pay(alice_, carol_, btc(1)),
5688 Path(~MPT(btc)),
5689 Sendmax(XRP(10)),
5690 Txflags(tfNoRippleDirect | tfPartialPayment),
5691 Ter(tesSUCCESS));
5692 }
5693 }
5694
5695 void
5697 {
5698 testcase("AMM Tokens");
5699 using namespace jtx;
5700
5701 // Offer crossing with AMM LPTokens and XRP.
5702 // AMM LPTokens come from MPT/XRP pool.
5703 testAMM(
5704 [&](AMM& ammAlice, Env& env) {
5705 auto const token1 = ammAlice.lptIssue();
5706 auto priceXRP = ammAssetOut(
5707 STAmount{XRPAmount{10'000'000'000}},
5708 STAmount{token1, 10'000'000},
5709 STAmount{token1, 5'000'000},
5710 0);
5711 // Carol places an order to buy LPTokens
5712 env(offer(carol_, STAmount{token1, 5'000'000}, priceXRP));
5713 // Alice places an order to sell LPTokens
5714 env(offer(alice_, priceXRP, STAmount{token1, 5'000'000}));
5715 // Pool's LPTokens balance doesn't change
5716 BEAST_EXPECT(ammAlice.expectBalances(
5717 XRP(10'000), MPT(ammAlice[1])(10'000), IOUAmount{10'000'000}));
5718 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{5'000'000}));
5719 BEAST_EXPECT(ammAlice.expectLPTokens(alice_, IOUAmount{5'000'000}));
5720 // Carol votes
5721 ammAlice.vote(carol_, 1'000);
5722 BEAST_EXPECT(ammAlice.expectTradingFee(500));
5723 ammAlice.vote(carol_, 0);
5724 BEAST_EXPECT(ammAlice.expectTradingFee(0));
5725 // Carol bids
5726 auto const baseFee = env.current()->fees().base;
5727 auto const carolXRP = env.balance(carol_);
5728 env(ammAlice.bid({.account = carol_, .bidMin = 100}));
5729 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{4'999'900}));
5730 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount{100}));
5731 BEAST_EXPECT(env.balance(carol_) == (carolXRP - baseFee));
5732 priceXRP = ammAssetOut(
5733 STAmount{XRPAmount{10'000'000'000}},
5734 STAmount{token1, 9'999'900},
5735 STAmount{token1, 4'999'900},
5736 0);
5737 // Carol withdraws
5738 ammAlice.withdrawAll(carol_, XRP(0));
5739 BEAST_EXPECT(env.balance(carol_) == (carolXRP - baseFee * 2 + priceXRP));
5740 BEAST_EXPECT(ammAlice.expectBalances(
5741 XRPAmount{10'000'000'000} - priceXRP,
5742 MPT(ammAlice[1])(10'000),
5743 IOUAmount{5'000'000}));
5744 BEAST_EXPECT(ammAlice.expectLPTokens(alice_, IOUAmount{5'000'000}));
5745 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{0}));
5746 },
5747 {{XRP(10000), gAmmmpt(10000)}});
5748
5749 // Offer crossing with two AMM LPTokens.
5750 // token1 comes from MPT/XRP pool.
5751 // token2 comes from XRP/IOU pool.
5752 testAMM(
5753 [&](AMM& ammAlice, Env& env) {
5754 ammAlice.deposit(carol_, 1'000'000);
5755 fund(env, gw_, {alice_, carol_}, {EUR(10'000)}, Fund::TokenOnly);
5756 AMM ammAlice1(env, alice_, XRP(10'000), EUR(10'000));
5757 ammAlice1.deposit(carol_, 1'000'000);
5758 auto const token1 = ammAlice.lptIssue();
5759 auto const token2 = ammAlice1.lptIssue();
5760 env(offer(alice_, STAmount{token1, 100}, STAmount{token2, 100}),
5761 Txflags(tfPassive));
5762 env.close();
5763 BEAST_EXPECT(expectOffers(env, alice_, 1));
5764 env(offer(carol_, STAmount{token2, 100}, STAmount{token1, 100}));
5765 env.close();
5766 BEAST_EXPECT(
5767 expectHolding(env, alice_, STAmount{token1, 10'000'100}) &&
5768 expectHolding(env, alice_, STAmount{token2, 9'999'900}));
5769 BEAST_EXPECT(
5770 expectHolding(env, carol_, STAmount{token2, 1'000'100}) &&
5771 expectHolding(env, carol_, STAmount{token1, 999'900}));
5772 BEAST_EXPECT(expectOffers(env, alice_, 0) && expectOffers(env, carol_, 0));
5773 },
5774 {{XRP(10000), gAmmmpt(10000)}});
5775
5776 // LPs pay LPTokens directly. Must trust set because the trust line
5777 // is checked for the limit, which is 0 in the AMM auto-created
5778 // trust line.
5779 testAMM(
5780 [&](AMM& ammAlice, Env& env) {
5781 auto const token1 = ammAlice.lptIssue();
5782 env.trust(STAmount{token1, 2'000'000}, carol_);
5783 env.close();
5784 ammAlice.deposit(carol_, 1'000'000);
5785 BEAST_EXPECT(
5786 ammAlice.expectLPTokens(alice_, IOUAmount{10'000'000, 0}) &&
5787 ammAlice.expectLPTokens(carol_, IOUAmount{1'000'000, 0}));
5788 // Pool balance doesn't change, only tokens moved from
5789 // one line to another.
5790 env(pay(alice_, carol_, STAmount{token1, 100}));
5791 env.close();
5792 BEAST_EXPECT(
5793 // Alice initial token1 10,000,000 - 100
5794 ammAlice.expectLPTokens(alice_, IOUAmount{9'999'900, 0}) &&
5795 // Carol initial token1 1,000,000 + 100
5796 ammAlice.expectLPTokens(carol_, IOUAmount{1'000'100, 0}));
5797
5798 env.trust(STAmount{token1, 20'000'000}, alice_);
5799 env.close();
5800 env(pay(carol_, alice_, STAmount{token1, 100}));
5801 env.close();
5802 // Back to the original balance
5803 BEAST_EXPECT(
5804 ammAlice.expectLPTokens(alice_, IOUAmount{10'000'000, 0}) &&
5805 ammAlice.expectLPTokens(carol_, IOUAmount{1'000'000, 0}));
5806 },
5807 {{XRP(10000), gAmmmpt(10000)}});
5808 }
5809
5810 void
5812 {
5813 testcase("Amendment");
5814 using namespace jtx;
5815 FeatureBitset const feature{testableAmendments() - featureMPTokensV2};
5816 Env env{*this, feature};
5817
5818 env.fund(XRP(30'000), gw_, alice_);
5819 env.close();
5820 MPT const btc = MPTTester({
5821 .env = env,
5822 .issuer = gw_,
5823 .holders = {alice_},
5824 .pay = 10'000,
5825 .flags = tfMPTCanClawback | tfMPTCanTransfer,
5826 });
5827
5828 AMM amm(env, alice_, XRP(1'000), btc(1'000), Ter(temDISABLED));
5829
5830 env(amm.bid({.bidMax = 1000}), Ter(temMALFORMED));
5831 env(amm.bid({}), Ter(temDISABLED));
5832 amm.vote(VoteArg{.tfee = 100, .err = Ter(temDISABLED)});
5833 amm.withdraw(WithdrawArg{.tokens = 100, .err = Ter(temMALFORMED)});
5834 amm.withdraw(WithdrawArg{.err = Ter(temDISABLED)});
5835 amm.deposit(DepositArg{.asset1In = USD(100), .err = Ter(temDISABLED)});
5836 amm.ammDelete(alice_, Ter(temDISABLED));
5837 }
5838
5839 void
5841 {
5842 testcase("AMMAndCLOB, offer quality change");
5843 using namespace jtx;
5844 auto const gw = Account("gw");
5845 auto const lP1 = Account("LP1");
5846 auto const lP2 = Account("LP2");
5847
5848 auto const prep = [&](auto const& offerCb, auto const& expectCb) {
5849 Env env(*this, features);
5850 env.fund(XRP(30'000'000'000), gw);
5851 env.fund(XRP(10'000), lP1);
5852 env.fund(XRP(10'000), lP2);
5853 MPTTester const tst(
5854 {.env = env, .issuer = gw, .holders = {lP1, lP2}, .flags = kMptDexFlags});
5855
5856 env(offer(gw, XRP(11'500'000'000), tst(1'000'000'000'000'000)));
5857
5858 env(offer(lP1, tst(25'000'000), XRPAmount(287'500'000)));
5859
5860 // Either AMM or CLOB offer
5861 offerCb(env, tst);
5862
5863 env(offer(lP2, tst(25'000'000), XRPAmount(287'500'000)));
5864
5865 expectCb(env, tst);
5866 };
5867
5868 // If we replace AMM with an equivalent CLOB offer, which AMM generates
5869 // when it is consumed, then the result must be equivalent, too.
5870 STAmount lp2TSTBalance;
5871 std::string lp2TakerGets;
5872 std::string lp2TakerPays;
5873 // Execute with AMM first
5874 prep(
5875 [&](Env& env, MPTTester tst) { AMM const amm(env, lP1, tst(25'000'000), XRP(250)); },
5876 [&](Env& env, MPTTester tst) {
5877 lp2TSTBalance = env.balance(lP2, MPT(tst));
5878 auto const offer = getAccountOffers(env, lP2)["offers"][0u];
5879 lp2TakerGets = offer["taker_gets"].asString();
5880 lp2TakerPays = offer["taker_pays"]["value"].asString();
5881 });
5882 // Execute with CLOB offer
5883 prep(
5884 [&](Env& env, MPTTester tst) {
5885 env(offer(lP1, XRPAmount{18'095'131}, tst(1'687'379)), Txflags(tfPassive));
5886 },
5887 [&](Env& env, MPTTester tst) {
5888 BEAST_EXPECT(lp2TSTBalance == env.balance(lP2, MPT(tst)));
5889 auto const offer = getAccountOffers(env, lP2)["offers"][0u];
5890 BEAST_EXPECT(lp2TakerGets == offer["taker_gets"].asString());
5891 BEAST_EXPECT(lp2TakerPays == offer["taker_pays"]["value"].asString());
5892 });
5893 }
5894
5895 void
5897 {
5898 testcase("AMM payment offer generation picks economically coarser integral side");
5899
5900 using namespace jtx;
5901
5902 enum class GeneratedFirst { TakerPays, TakerGets };
5903
5904 auto const check = [&](std::uint64_t mptUnitsPerXRP, GeneratedFirst generatedFirst) {
5906 XRPAmount{1'000'000}, MPTAmount{1'000'000'125}};
5907 TAmounts<XRPAmount, MPTAmount> const clobOffer{
5908 kDropsPerXrp, MPTAmount{static_cast<std::int64_t>(mptUnitsPerXRP)}};
5909 Quality const clobQuality{clobOffer};
5910
5911 auto const expectedAmounts = generatedFirst == GeneratedFirst::TakerGets
5912 ? getAMMOfferStartWithTakerGets(pool, clobQuality, 0)
5913 : getAMMOfferStartWithTakerPays(pool, clobQuality, 0);
5914 auto const otherAmounts = generatedFirst == GeneratedFirst::TakerGets
5915 ? getAMMOfferStartWithTakerPays(pool, clobQuality, 0)
5916 : getAMMOfferStartWithTakerGets(pool, clobQuality, 0);
5917 BEAST_EXPECT(expectedAmounts);
5918 BEAST_EXPECT(otherAmounts);
5919 if (!expectedAmounts || !otherAmounts)
5920 return;
5921
5922 // Make the tested branch observable: these cases are chosen so the
5923 // payment consumes different AMM amounts depending on which side
5924 // is generated first.
5925 BEAST_EXPECT(*expectedAmounts != *otherAmounts);
5926
5927 Env env(*this, features);
5928 auto const gw = Account("gw");
5929 auto const lp = Account("lp");
5930 auto const maker = Account("maker");
5931 auto const taker = Account("taker");
5932 auto const dst = Account("dst");
5933
5934 env.fund(XRP(10'000), gw, lp, maker, taker, dst);
5935 env.close();
5936
5937 MPTTester const token(
5938 {.env = env, .issuer = gw, .holders = {lp, maker, dst}, .flags = kMptDexFlags});
5939 env(pay(gw, lp, token(pool.out.value())));
5940 env(pay(gw, maker, token(10'000'000)));
5941 env.close();
5942
5943 AMM const amm(env, lp, drops(pool.in), token(pool.out.value()));
5944 auto const makerOfferSeq = env.seq(maker);
5945 env(offer(maker, XRP(1), token(mptUnitsPerXRP)), Txflags(tfPassive));
5946 env.close();
5947
5948 env(pay(taker, dst, token(expectedAmounts->out.value())),
5949 Sendmax(drops(expectedAmounts->in)));
5950 env.close();
5951
5952 BEAST_EXPECT(amm.expectBalances(
5953 drops(pool.in + expectedAmounts->in),
5954 token((pool.out - expectedAmounts->out).value()),
5955 amm.tokens()));
5956 env.require(Balance(dst, token(expectedAmounts->out.value())));
5957 BEAST_EXPECT(env.le(keylet::offer(maker.id(), SeqProxy::rawSequence(makerOfferSeq))));
5958 };
5959
5960 // CLOB price: 10'000'000 MPT per 1 XRP, so one raw MPT unit is worth
5961 // 0.1 drops. One drop is the economically coarser unit and the AMM
5962 // offer is generated from takerPays.
5963 check(10 * kDropsPerXrp.drops(), GeneratedFirst::TakerPays);
5964
5965 // CLOB price: 1'000'000 MPT per 1 XRP, so one raw MPT unit is worth
5966 // one drop. Ties use takerGets to preserve the historical XRP-output
5967 // behavior.
5968 check(kDropsPerXrp.drops(), GeneratedFirst::TakerGets);
5969
5970 // CLOB price: 100'000 MPT per 1 XRP, so one raw MPT unit is worth
5971 // 10 drops. MPT is the economically coarser unit and the AMM offer is
5972 // generated from takerGets.
5973 check(kDropsPerXrp.drops() / 10, GeneratedFirst::TakerGets);
5974 }
5975
5976 void
5978 {
5979 testcase("Trading Fee");
5980 using namespace jtx;
5981
5982 // Single Deposit, 1% fee
5983 testAMM(
5984 [&](AMM& ammAlice, Env& env) {
5985 // No fee
5986 ammAlice.deposit(carol_, MPT(ammAlice[1])(3'000));
5987 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{1'000}));
5988 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(3'000));
5989 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{0}));
5990 env.require(Balance(carol_, MPT(ammAlice[1])(30'000)));
5991 // Set fee to 1%
5992 ammAlice.vote(alice_, 1'000);
5993 BEAST_EXPECT(ammAlice.expectTradingFee(1'000));
5994
5995 // Carol gets fewer LPToken ~994, because of the single deposit
5996 // fee
5997 ammAlice.deposit(carol_, MPT(ammAlice[1])(3'000));
5998 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{994'981155689671, -12}));
5999 env.require(Balance(carol_, MPT(ammAlice[1])(27'000)));
6000
6001 // Set fee to 0
6002 ammAlice.vote(alice_, 0);
6003 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(0));
6004
6005 // Carol gets back less than the original deposit
6006 if (!features[fixAMMv1_3])
6007 {
6008 env.require(Balance(carol_, MPT(ammAlice[1])(29'995)));
6009 }
6010 else
6011 {
6012 env.require(Balance(carol_, MPT(ammAlice[1])(29'994)));
6013 }
6014 },
6015 {{USD(1000), gAmmmpt(1000)}},
6016 0,
6017 std::nullopt,
6018 {features});
6019
6020 // Single deposit with EP not exceeding specified:
6021 // 100MPT with EP not to exceed 0.1 (AssetIn/TokensOut). 1% fee.
6022 testAMM(
6023 [&](AMM& ammAlice, Env& env) {
6024 BEAST_EXPECT(ammAlice.expectTradingFee(1'000));
6025 auto const balance = env.balance(carol_, MPT(ammAlice[1]));
6026 auto const tokensFee = ammAlice.deposit(
6027 carol_,
6028 MPT(ammAlice[1])(1000),
6029 std::nullopt,
6030 STAmount{ammAlice.lptIssue(), 1, -1});
6031 auto const deposit = balance - env.balance(carol_, MPT(ammAlice[1]));
6032 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(0));
6033 ammAlice.vote(alice_, 0);
6034 BEAST_EXPECT(ammAlice.expectTradingFee(0));
6035 auto const tokensNoFee = ammAlice.deposit(carol_, deposit);
6036 BEAST_EXPECT(tokensFee == IOUAmount(485636'0611129, -7));
6037 if (!features[fixAMMv1_3])
6038 {
6039 BEAST_EXPECT(tokensNoFee == IOUAmount(487659'8005807, -7));
6040 }
6041 else
6042 {
6043 BEAST_EXPECT(tokensNoFee == IOUAmount(487612'21584827, -8));
6044 }
6045 },
6046 {{XRP(10'000), gAmmmpt(10'000)}},
6047 1'000,
6048 std::nullopt,
6049 {features});
6050
6051 // Single deposit with EP not exceeding specified:
6052 // 200MPT with EP not to exceed 0.002020 (AssetIn/TokensOut). 1% fee
6053 testAMM(
6054 [&](AMM& ammAlice, Env& env) {
6055 BEAST_EXPECT(ammAlice.expectTradingFee(1'000));
6056 auto const balance = env.balance(carol_, MPT(ammAlice[1]));
6057 auto const tokensFee = ammAlice.deposit(
6058 carol_,
6059 MPT(ammAlice[1])(200),
6060 std::nullopt,
6061 STAmount{ammAlice.lptIssue(), 2020, -6});
6062 auto const deposit = balance - env.balance(carol_, MPT(ammAlice[1]));
6063 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(0));
6064 ammAlice.vote(alice_, 0);
6065 BEAST_EXPECT(ammAlice.expectTradingFee(0));
6066 auto const tokensNoFee = ammAlice.deposit(carol_, deposit);
6067 if (!features[fixAMMv1_3])
6068 {
6069 BEAST_EXPECT(tokensFee == IOUAmount(98'019'80198019, -8));
6070 BEAST_EXPECT(tokensNoFee == IOUAmount(98'495'13933557, -8));
6071 }
6072 else
6073 {
6074 BEAST_EXPECT(tokensFee == IOUAmount(97527'05893345, -8));
6075 BEAST_EXPECT(tokensNoFee == IOUAmount(98000'10293049, -8));
6076 }
6077 },
6078 {{XRP(10'000), gAmmmpt(10'000)}},
6079 1'000,
6080 std::nullopt,
6081 {features});
6082
6083 // Single Withdrawal, 1% fee
6084 testAMM(
6085 [&](AMM& ammAlice, Env& env) {
6086 // No fee
6087 ammAlice.deposit(carol_, MPT(ammAlice[1])(3'000));
6088 BEAST_EXPECT(ammAlice.expectLPTokens(carol_, IOUAmount{1'000}));
6089 env.require(Balance(carol_, MPT(ammAlice[1])(27'000)));
6090 // Set fee to 1%
6091 ammAlice.vote(alice_, 1'000);
6092 BEAST_EXPECT(ammAlice.expectTradingFee(1'000));
6093 // Single withdrawal. Carol gets ~5USD less than deposited.
6094 ammAlice.withdrawAll(carol_, MPT(ammAlice[1])(0));
6095 if (!features[fixAMMv1_3])
6096 {
6097 env.require(Balance(carol_, MPT(ammAlice[1])(29'995)));
6098 }
6099 else
6100 {
6101 env.require(Balance(carol_, MPT(ammAlice[1])(29'994)));
6102 }
6103 },
6104 {{USD(1000), gAmmmpt(1000)}},
6105 0,
6106 std::nullopt,
6107 {features});
6108
6109 // Withdraw with EPrice limit, 1% fee.
6110 testAMM(
6111 [&](AMM& ammAlice, Env& env) {
6112 ammAlice.deposit(carol_, 1'000'000);
6113 auto const tokensFee = ammAlice.withdraw(
6114 carol_, MPT(ammAlice[1])(100), std::nullopt, IOUAmount{520, 0});
6115 env.require(Balance(carol_, MPT(ammAlice[1])(30443)));
6116
6117 // Set to original pool size
6118 auto const deposit =
6119 env.balance(carol_, MPT(ammAlice[1])) - MPT(ammAlice[1])(29'000);
6120 ammAlice.deposit(carol_, deposit);
6121 // fee 0%
6122 ammAlice.vote(alice_, 0);
6123 BEAST_EXPECT(ammAlice.expectTradingFee(0));
6124 auto const tokensNoFee = ammAlice.withdraw(carol_, deposit);
6125 if (!features[fixAMMv1_3])
6126 {
6127 env.require(Balance(carol_, MPT(ammAlice[1])(30443)));
6128 }
6129 else
6130 {
6131 env.require(Balance(carol_, MPT(ammAlice[1])(30442)));
6132 }
6133 BEAST_EXPECT(tokensNoFee == IOUAmount(746'327'46496649, -8));
6134 BEAST_EXPECT(tokensFee == IOUAmount(750'588'23529411, -8));
6135 },
6136 {{XRP(10'000), gAmmmpt(10'000)}},
6137 1'000,
6138 std::nullopt,
6139 {features});
6140
6141 // Payment, 1% fee
6142 {
6143 Env env{*this, features};
6144 env.fund(XRP(30'000), gw_, alice_, bob_, carol_);
6145 env.close();
6146 MPT const btc = MPTTester({
6147 .env = env,
6148 .issuer = gw_,
6149 .holders = {alice_, bob_, carol_},
6150 .pay = 30'000,
6151 .flags = kMptDexFlags,
6152 });
6153
6154 auto const usd = gw_["USD"];
6155 env.trust(usd(30'000), alice_);
6156 env(pay(gw_, alice_, usd(30'000)));
6157 env.trust(usd(30'000), bob_);
6158 env(pay(gw_, bob_, usd(1'000)));
6159 env.trust(usd(30'000), carol_);
6160 env(pay(gw_, carol_, usd(30'000)));
6161 env.close();
6162
6163 AMM amm(env, alice_, btc(1000), usd(1010));
6164
6165 env.require(Balance(alice_, btc(29'000)));
6166 env.require(Balance(alice_, usd(28'990)));
6167 env.require(Balance(carol_, btc(30'000)));
6168
6169 // Carol pays to Alice with no fee
6170 env(pay(carol_, alice_, usd(10)),
6171 Path(~usd),
6172 Sendmax(btc(10)),
6173 Txflags(tfNoRippleDirect));
6174 env.close();
6175
6176 // Alice has 10USD more and Carol has 10BTC less
6177 env.require(Balance(alice_, btc(29'000)));
6178 env.require(Balance(alice_, usd(29'000)));
6179 env.require(Balance(carol_, btc(29'990)));
6180
6181 // Set fee to 1%
6182 amm.vote(alice_, 1'000);
6183 BEAST_EXPECT(amm.expectTradingFee(1'000));
6184 // Bob pays to Carol with 1% fee
6185 env(pay(bob_, carol_, btc(10)),
6186 Path(~btc),
6187 Sendmax(usd(15)),
6188 Txflags(tfNoRippleDirect));
6189 env.close();
6190 // Bob sends 10.1~USD to pay 10BTC
6191 env.require(Balance(bob_, STAmount{usd, UINT64_C(989'8989898989899), -13}));
6192 // Carol got 10BTC
6193 env.require(Balance(carol_, btc(30'000)));
6194 BEAST_EXPECT(amm.expectBalances(
6195 btc(1'000), STAmount{usd, UINT64_C(1'010'10101010101), -11}, amm.tokens()));
6196 }
6197
6198 // Offer crossing, 0.05% fee MPT/XRP
6199 testAMM(
6200 [&](AMM& ammAlice, Env& env) {
6201 auto const btc = MPT(ammAlice[1]);
6202 auto const carolXRP = env.balance(carol_);
6203 auto const baseFee = env.current()->fees().base;
6204 env(offer(carol_, btc(10), XRP(10)));
6205 env.close();
6206 env.require(Balance(carol_, btc(30'010)));
6207 env.require(Balance(carol_, carolXRP - baseFee - XRP(10)));
6208
6209 // Change pool composition back
6210 env(offer(carol_, XRP(10), btc(10)));
6211 env.close();
6212 env.require(Balance(carol_, btc(30'000)));
6213 env.require(Balance(carol_, carolXRP - baseFee * 2));
6214
6215 // set fee to 0.05%
6216 ammAlice.vote(alice_, 50);
6217 BEAST_EXPECT(ammAlice.expectTradingFee(50));
6218 env(offer(carol_, btc(10), XRP(10)));
6219 env.close();
6220 env.require(Balance(carol_, btc(30'009)));
6221 env.require(Balance(carol_, carolXRP - baseFee * 3 - XRPAmount(8'995'507)));
6222 BEAST_EXPECT(expectOffers(env, carol_, 1, {{Amounts{btc(1), XRP(1)}}}));
6223 },
6224 {{XRP(1000), gAmmmpt(1010)}},
6225 0,
6226 std::nullopt,
6227 {features});
6228
6229 // Offer crossing, 0.5% fee MPT/IOU
6230 testAMM(
6231 [&](AMM& ammAlice, Env& env) {
6232 auto const btc = MPT(ammAlice[1]);
6233 env(offer(carol_, btc(10'000'000), USD(10)));
6234 env.close();
6235 env.require(Balance(carol_, btc(1'020'001'000)));
6236 env.require(Balance(carol_, USD(29'990)));
6237
6238 // Change pool composition back
6239 env(offer(carol_, USD(10), btc(10'000'000)));
6240 env.close();
6241 env.require(Balance(carol_, btc(1'010'001'000)));
6242 env.require(Balance(carol_, USD(30'000)));
6243
6244 // set fee to 0.5%
6245 ammAlice.vote(alice_, 500);
6246 BEAST_EXPECT(ammAlice.expectTradingFee(500));
6247 env(offer(carol_, btc(10'000'000), USD(10)));
6248 env.close();
6249 env.require(Balance(carol_, btc(1'014'975'874)));
6250 env.require(Balance(carol_, STAmount{USD, UINT64_C(29'995'02512600184), -11}));
6251 BEAST_EXPECT(expectOffers(
6252 env,
6253 carol_,
6254 1,
6255 {{Amounts{btc(5'025126), STAmount{USD, UINT64_C(5'025126), -6}}}}));
6256 },
6257 {{USD(1000), gAmmmpt(1010000000)}},
6258 0,
6259 std::nullopt,
6260 {features});
6261
6262 // Payment with AMM and CLOB offer, 0 fee
6263 // AMM liquidity is consumed first up to CLOB offer quality
6264 // CLOB offer is fully consumed next
6265 // Remaining amount is consumed via AMM liquidity
6266 {
6267 Env env{*this, features};
6268 Account const ed("ed");
6269 fund(env, gw_, {alice_, bob_, carol_, ed}, XRP(30'000), {USD(2'000)});
6270 MPT const btc = MPTTester({
6271 .env = env,
6272 .issuer = gw_,
6273 .holders = {alice_, bob_, carol_, ed},
6274 .pay = 30'000'000000,
6275 .flags = kMptDexFlags,
6276 });
6277
6278 env(offer(carol_, btc(5'000000), USD(5)));
6279 AMM const ammAlice(env, alice_, USD(1'005), btc(1'000'000000));
6280 env(pay(bob_, ed, USD(10)),
6281 Path(~USD),
6282 Sendmax(btc(15'000000)),
6283 Txflags(tfNoRippleDirect));
6284
6285 env.require(Balance(ed, USD(2'010)));
6286 env.require(Balance(bob_, btc(29'989'999999)));
6287 BEAST_EXPECT(ammAlice.expectBalances(
6288 btc(1'005'000001),
6289 STAmount{USD, UINT64_C(999'9999999999999), -13},
6290 ammAlice.tokens()));
6291 BEAST_EXPECT(expectOffers(env, carol_, 0));
6292 }
6293
6294 // Payment with AMM and CLOB offer. Same as above but with 0.25%
6295 // fee.
6296 {
6297 Env env{*this, features};
6298 Account const ed("ed");
6299 fund(env, gw_, {alice_, bob_, carol_, ed}, XRP(30'000), {USD(2'000)});
6300 MPT const btc = MPTTester({
6301 .env = env,
6302 .issuer = gw_,
6303 .holders = {alice_, bob_, carol_, ed},
6304 .pay = 30'000'000000,
6305 .flags = kMptDexFlags,
6306 });
6307
6308 env(offer(carol_, btc(5'000000), USD(5)));
6309 // Set 0.25% fee
6310 AMM const ammAlice(env, alice_, USD(1'005), btc(1'000'000000), false, 250);
6311 env(pay(bob_, ed, USD(10)),
6312 Path(~USD),
6313 Sendmax(btc(15'000000)),
6314 Txflags(tfNoRippleDirect));
6315 env.require(Balance(ed, USD(2'010)));
6316 env.require(Balance(bob_, btc(29'989'987453)));
6317 BEAST_EXPECT(ammAlice.expectBalances(btc(1'005'012547), USD(1'000), ammAlice.tokens()));
6318 BEAST_EXPECT(expectOffers(env, carol_, 0));
6319 }
6320
6321 // Payment with AMM and CLOB offer. AMM has a better
6322 // spot price quality, but 1% fee offsets that. As the result
6323 // the entire trade is executed via LOB.
6324 {
6325 Env env{*this, features};
6326 Account const ed("ed");
6327 fund(env, gw_, {alice_, bob_, carol_, ed}, XRP(30'000), {USD(2'000)});
6328 MPT const btc = MPTTester({
6329 .env = env,
6330 .issuer = gw_,
6331 .holders = {alice_, bob_, carol_, ed},
6332 .pay = 30'000'000000,
6333 .flags = kMptDexFlags,
6334 });
6335
6336 env(offer(carol_, btc(10'000000), USD(10)));
6337 // Set 1% fee
6338 AMM const ammAlice(env, alice_, USD(1'005), btc(1'000'000000), false, 1'000);
6339 env(pay(bob_, ed, USD(10)),
6340 Path(~USD),
6341 Sendmax(btc(15'000000)),
6342 Txflags(tfNoRippleDirect));
6343 env.require(Balance(ed, USD(2'010)));
6344 env.require(Balance(bob_, btc(29'990'000000)));
6345 BEAST_EXPECT(ammAlice.expectBalances(btc(1'000'000000), USD(1'005), ammAlice.tokens()));
6346 BEAST_EXPECT(expectOffers(env, carol_, 0));
6347 }
6348
6349 // Payment with AMM and CLOB offer. AMM has a better
6350 // spot price quality, but 1% fee offsets that.
6351 // The CLOB offer is consumed first and the remaining
6352 // amount is consumed via AMM liquidity.
6353 {
6354 Env env{*this, features};
6355 Account const ed("ed");
6356 fund(env, gw_, {alice_, bob_, carol_, ed}, XRP(30'000), {USD(2'000)});
6357 MPT const btc = MPTTester({
6358 .env = env,
6359 .issuer = gw_,
6360 .holders = {alice_, bob_, carol_, ed},
6361 .pay = 30'000'000000,
6362 .flags = kMptDexFlags,
6363 });
6364
6365 env(offer(carol_, btc(9'000000), USD(9)));
6366 // Set 1% fee
6367 AMM const ammAlice(env, alice_, USD(1'005), btc(1'000'000000), false, 1'000);
6368 env(pay(bob_, ed, USD(10)),
6369 Path(~USD),
6370 Sendmax(btc(15'000000)),
6371 Txflags(tfNoRippleDirect));
6372 env.require(Balance(ed, USD(2'010)));
6373 env.require(Balance(bob_, btc(29'989'993923)));
6374 BEAST_EXPECT(ammAlice.expectBalances(btc(1'001'006077), USD(1'004), ammAlice.tokens()));
6375 BEAST_EXPECT(expectOffers(env, carol_, 0));
6376 }
6377 }
6378
6379 void
6381 {
6382 testcase("Adjusted Deposit/Withdraw Tokens");
6383 using namespace jtx;
6384
6385 // Deposit/Withdraw USD from USD/MPT pool
6386 {
6387 Env env(*this);
6388 Account const gw{"gateway"};
6389 Account const alice{"alice"};
6390 Account const bob("bob");
6391 Account const carol("carol");
6392 Account const ed("ed");
6393 Account const paul("paul");
6394 Account const dan("dan");
6395 Account const chris("chris");
6396 Account const simon("simon");
6397 Account const ben("ben");
6398 Account const natalie("natalie");
6399 std::vector<Account> const holders{
6400 alice,
6401 bob,
6402 carol,
6403 ed,
6404 paul,
6405 dan,
6406 chris,
6407 simon,
6408 ben,
6409 natalie,
6410 };
6411 env.fund(XRP(100000), gw, alice, bob, carol, ed, paul, dan, chris, simon, ben, natalie);
6412 env.close();
6413
6414 MPT const btc = MPTTester({
6415 .env = env,
6416 .issuer = gw,
6417 .holders = holders,
6418 .pay = 40'000'000000,
6419 .flags = kMptDexFlags,
6420 });
6421
6422 auto const usd = gw["USD"];
6423 for (auto const& holder : holders)
6424 {
6425 env.trust(usd(1'500'000), holder);
6426 env(pay(gw, holder, usd(1'500'000)));
6427 }
6428 env.close();
6429
6430 auto const aliceUSD = env.balance(alice, usd);
6431 auto const bobUSD = env.balance(bob, usd);
6432 auto const carolUSD = env.balance(carol, usd);
6433 auto const edUSD = env.balance(ed, usd);
6434 auto const paulUSD = env.balance(paul, usd);
6435 auto const danUSD = env.balance(dan, usd);
6436 auto const chrisUSD = env.balance(chris, usd);
6437 auto const simonUSD = env.balance(simon, usd);
6438 auto const benUSD = env.balance(ben, usd);
6439 auto const natalieUSD = env.balance(natalie, usd);
6440
6441 AMM ammAlice(env, alice, btc(10'000'000000), usd(10000));
6442 BEAST_EXPECT(
6443 ammAlice.expectBalances(btc(10'000'000000), usd(10'000), IOUAmount{10'000'000}));
6444
6445 for (int i = 0; i < 10; ++i)
6446 {
6447 ammAlice.deposit(ben, STAmount{usd, 1, -10});
6448 ammAlice.withdrawAll(ben, usd(0));
6449 ammAlice.deposit(simon, usd(0.1));
6450 ammAlice.withdrawAll(simon, usd(0));
6451 ammAlice.deposit(chris, usd(1));
6452 ammAlice.withdrawAll(chris, usd(0));
6453 ammAlice.deposit(dan, usd(10));
6454 ammAlice.withdrawAll(dan, usd(0));
6455 ammAlice.deposit(bob, usd(100));
6456 ammAlice.withdrawAll(bob, usd(0));
6457 ammAlice.deposit(carol, usd(1'000));
6458 ammAlice.withdrawAll(carol, usd(0));
6459 ammAlice.deposit(ed, usd(10'000));
6460 ammAlice.withdrawAll(ed, usd(0));
6461 ammAlice.deposit(paul, usd(100'000));
6462 ammAlice.withdrawAll(paul, usd(0));
6463 ammAlice.deposit(natalie, usd(1'000'000));
6464 ammAlice.withdrawAll(natalie, usd(0));
6465 }
6466
6467 BEAST_EXPECT(ammAlice.expectBalances(
6468 btc(10'000'000000),
6469 STAmount{usd, UINT64_C(10000'0000000001), -10},
6470 IOUAmount{10'000'000}));
6471
6472 env.require(Balance(bob, bobUSD));
6473 env.require(Balance(carol, carolUSD));
6474 env.require(Balance(ed, edUSD));
6475 env.require(Balance(paul, paulUSD));
6476 env.require(Balance(dan, danUSD));
6477 env.require(Balance(chris, chrisUSD));
6478 env.require(Balance(simon, simonUSD));
6479 env.require(Balance(ben, benUSD));
6480 env.require(Balance(natalie, natalieUSD));
6481
6482 ammAlice.withdrawAll(alice);
6483 BEAST_EXPECT(!ammAlice.ammExists());
6484 env.require(Balance(alice, aliceUSD));
6485 }
6486
6487 // Same as above but deposit/withdraw MPT from USD/MPT pool
6488 {
6489 Env env(*this);
6490 Account const gw{"gateway"};
6491 Account const alice{"alice"};
6492 Account const bob("bob");
6493 Account const carol("carol");
6494 Account const ed("ed");
6495 Account const paul("paul");
6496 Account const dan("dan");
6497 Account const chris("chris");
6498 Account const simon("simon");
6499 Account const ben("ben");
6500 Account const natalie("natalie");
6501 std::vector<Account> const holders{
6502 alice,
6503 bob,
6504 carol,
6505 ed,
6506 paul,
6507 dan,
6508 chris,
6509 simon,
6510 ben,
6511 natalie,
6512 };
6513 env.fund(XRP(100000), gw, alice, bob, carol, ed, paul, dan, chris, simon, ben, natalie);
6514 env.close();
6515
6516 MPT const btc = MPTTester({
6517 .env = env,
6518 .issuer = gw,
6519 .holders = holders,
6520 .pay = 40'000'000000,
6521 .flags = kMptDexFlags,
6522 });
6523
6524 auto const usd = gw["USD"];
6525 for (auto const& holder : holders)
6526 {
6527 env.trust(usd(1'500'000), holder);
6528 env(pay(gw, holder, usd(1'500'000)));
6529 }
6530 env.close();
6531
6532 auto const aliceBTC = env.balance(alice, btc);
6533 auto const bobBTC = env.balance(bob, btc);
6534 auto const carolBTC = env.balance(carol, btc);
6535 auto const edBTC = env.balance(ed, btc);
6536 auto const paulBTC = env.balance(paul, btc);
6537 auto const danBTC = env.balance(dan, btc);
6538 auto const chrisBTC = env.balance(chris, btc);
6539 auto const simonBTC = env.balance(simon, btc);
6540 auto const benBTC = env.balance(ben, btc);
6541 auto const natalieBTC = env.balance(natalie, btc);
6542
6543 AMM ammAlice(env, alice, btc(10'000'000000), usd(10000));
6544 BEAST_EXPECT(
6545 ammAlice.expectBalances(btc(10'000'000000), usd(10'000), IOUAmount{10'000'000}));
6546
6547 for (int i = 0; i < 10; ++i)
6548 {
6549 ammAlice.deposit(ben, btc(1));
6550 ammAlice.withdrawAll(ben, btc(0));
6551 ammAlice.deposit(simon, btc(1'000));
6552 ammAlice.withdrawAll(simon, btc(0));
6553 ammAlice.deposit(chris, btc(1));
6554 ammAlice.withdrawAll(chris, btc(0));
6555 ammAlice.deposit(dan, btc(10));
6556 ammAlice.withdrawAll(dan, btc(0));
6557 ammAlice.deposit(bob, btc(100));
6558 ammAlice.withdrawAll(bob, btc(0));
6559 ammAlice.deposit(carol, btc(1'000));
6560 ammAlice.withdrawAll(carol, btc(0));
6561 ammAlice.deposit(ed, btc(10'000));
6562 ammAlice.withdrawAll(ed, btc(0));
6563 ammAlice.deposit(paul, btc(100'000));
6564 ammAlice.withdrawAll(paul, btc(0));
6565 ammAlice.deposit(natalie, btc(1'000'000));
6566 ammAlice.withdrawAll(natalie, btc(0));
6567 }
6568
6569 BEAST_EXPECT(
6570 ammAlice.expectBalances(btc(10'000'000090), usd(10'000), IOUAmount{10'000'000}));
6571
6572 env.require(Balance(bob, bobBTC - btc(10)));
6573 env.require(Balance(carol, carolBTC - btc(10)));
6574 env.require(Balance(ed, edBTC - btc(10)));
6575 env.require(Balance(paul, paulBTC - btc(10)));
6576 env.require(Balance(dan, danBTC - btc(10)));
6577 env.require(Balance(chris, chrisBTC - btc(10)));
6578 env.require(Balance(simon, simonBTC - btc(10)));
6579 env.require(Balance(ben, benBTC - btc(10)));
6580 env.require(Balance(natalie, natalieBTC - btc(10)));
6581
6582 ammAlice.withdrawAll(alice);
6583 BEAST_EXPECT(!ammAlice.ammExists());
6584 env.require(Balance(alice, aliceBTC + btc(90)));
6585 }
6586 }
6587
6588 void
6590 {
6591 testcase("AMMID");
6592 using namespace jtx;
6593
6594 // MPT/XRP
6595 testAMM(
6596 [&](AMM& amm, Env& env) {
6597 amm.setClose(false);
6598 auto const info = env.rpc(
6599 "json",
6600 "account_info",
6601 std::string(R"({"account": ")" + to_string(amm.ammAccount()) + "\"}"));
6602 try
6603 {
6604 BEAST_EXPECT(
6605 info[jss::result][jss::account_data][jss::AMMID].asString() ==
6606 to_string(amm.ammID()));
6607 }
6608 catch (...)
6609 {
6610 fail();
6611 }
6612
6613 amm.deposit(carol_, 1'000);
6614
6615 auto const affected =
6616 env.meta()->getJson(JsonOptions::Values::None)[sfAffectedNodes.fieldName];
6617 try
6618 {
6619 bool found = false;
6620 for (auto const& node : affected)
6621 {
6622 if (node.isMember(sfModifiedNode.fieldName) &&
6623 node[sfModifiedNode.fieldName][sfLedgerEntryType.fieldName]
6624 .asString() == "AccountRoot" &&
6625 node[sfModifiedNode.fieldName][sfFinalFields.fieldName][jss::Account]
6626 .asString() == to_string(amm.ammAccount()))
6627 {
6628 found =
6629 node[sfModifiedNode.fieldName][sfFinalFields.fieldName][jss::AMMID]
6630 .asString() == to_string(amm.ammID());
6631 break;
6632 }
6633 }
6634 BEAST_EXPECT(found);
6635 }
6636 catch (...)
6637 {
6638 fail();
6639 }
6640 },
6641 {{XRP(1000), gAmmmpt(1000'000)}});
6642 }
6643
6644 void
6646 {
6647 testcase("Offer/Strand Selection");
6648 using namespace jtx;
6649 Account const ed("ed");
6650 Account const gw1("gw1");
6651
6652 // These tests are expected to fail if the OwnerPaysFee feature
6653 // is ever supported. Updates will need to be made to AMM handling
6654 // in the payment engine, and these tests will need to be updated.
6655
6656 struct MPTList
6657 {
6658 MPTTester const usd;
6659 MPTTester const eth;
6660 MPTTester const can;
6661 };
6662
6663 auto const prep =
6664 [&](Env& env, uint16_t gwTransferFee, uint16_t gw1TransferFee) -> MPTList {
6665 env.fund(XRP(2'000), gw_, gw1, alice_, bob_, carol_, ed);
6666 MPTTester usd({
6667 .env = env,
6668 .issuer = gw_,
6669 .holders = {alice_, bob_, carol_, ed},
6670 .transferFee = gwTransferFee,
6671 .pay = 2'000'000'000,
6672 .flags = kMptDexFlags,
6673 });
6674 MPTTester eth({
6675 .env = env,
6676 .issuer = gw1,
6677 .holders = {alice_, bob_, carol_, ed},
6678 .transferFee = gw1TransferFee,
6679 .pay = 2'000'000'000,
6680 .flags = kMptDexFlags,
6681 });
6682 MPTTester can({
6683 .env = env,
6684 .issuer = gw1,
6685 .holders = {alice_, bob_, carol_, ed},
6686 .transferFee = gw1TransferFee,
6687 .pay = 2'000'000'000,
6688 .flags = kMptDexFlags,
6689 });
6690 env.close();
6691
6692 return MPTList{
6693 .usd = std::move(usd),
6694 .eth = std::move(eth),
6695 .can = std::move(can),
6696 };
6697 };
6698
6699 static constexpr std::uint32_t kLowRate = 10'000;
6700 static constexpr std::uint32_t kHighRate = 50'000;
6701 for (auto const& rates :
6702 {std::make_pair(kLowRate, kHighRate), std::make_pair(kHighRate, kLowRate)})
6703 {
6704 // Offer Selection
6705
6706 // Cross-currency payment: AMM has the same spot price quality
6707 // as CLOB's offer and can't generate a better quality offer.
6708 // The transfer fee in this case doesn't change the CLOB quality
6709 // because trIn is ignored on adjustment and trOut on payment is
6710 // also ignored because ownerPaysTransferFee is false in this
6711 // case. Run test for 0) offer, 1) AMM, 2) offer and AMM to
6712 // verify that the quality is better in the first case, and CLOB
6713 // is selected in the second case.
6714 {
6716 for (auto i = 0; i < 3; ++i)
6717 {
6718 Env env(*this, features);
6719 auto const mpts = prep(env, rates.first, rates.second);
6720 auto const usd = mpts.usd;
6721 auto const eth = mpts.eth;
6722 auto const can = mpts.can;
6724
6725 if (i == 0 || i == 2)
6726 {
6727 env(offer(ed, eth(400'000'000), usd(400'000'000)), Txflags(tfPassive));
6728 env.close();
6729 }
6730 if (i > 0)
6731 amm.emplace(env, ed, usd(1'000'000'000), eth(1'000'000'000));
6732 env(pay(carol_, bob_, usd(100'000'000)),
6733 Path(~MPT(usd)),
6734 Sendmax(eth(500'000'000)));
6735 env.close();
6736 // CLOB and AMM, AMM is not selected
6737 if (i == 2)
6738 {
6739 // NOLINTBEGIN(bugprone-unchecked-optional-access) i==2 implies amm is
6740 // emplaced (i>0)
6741 BEAST_EXPECT(amm->expectBalances(
6742 usd(1'000'000'000), eth(1'000'000'000), amm->tokens()));
6743 // NOLINTEND(bugprone-unchecked-optional-access)
6744 }
6745 env.require(Balance(bob_, usd(2'100'000'000)));
6746 q[i] = Quality(
6747 Amounts{
6748 eth(2'000'000'000) - env.balance(carol_, MPT(eth)),
6749 env.balance(bob_, MPT(usd)) - usd(2'000'000'000)});
6750 }
6751 // CLOB is better quality than AMM
6752 BEAST_EXPECT(q[0] > q[1]);
6753 // AMM is not selected with CLOB
6754 BEAST_EXPECT(q[0] == q[2]);
6755 }
6756 // Offer crossing: AMM has the same spot price quality
6757 // as CLOB's offer and can't generate a better quality offer.
6758 // The transfer fee in this case doesn't change the CLOB quality
6759 // because the quality adjustment is ignored for the offer
6760 // crossing.
6761 for (auto i = 0; i < 3; ++i)
6762 {
6763 Env env(*this, features);
6764 auto const mpts = prep(env, rates.first, rates.second);
6765 auto const usd = mpts.usd;
6766 auto const eth = mpts.eth;
6767 auto const can = mpts.can;
6769 if (i == 0 || i == 2)
6770 {
6771 env(offer(ed, eth(400'000'000), usd(400'000'000)), Txflags(tfPassive));
6772 env.close();
6773 }
6774 if (i > 0)
6775 amm.emplace(env, ed, usd(1'000'000'000), eth(1'000'000'000));
6776 env(offer(alice_, usd(400'000'000), eth(400'000'000)));
6777 env.close();
6778 // AMM is not selected
6779 if (i > 0)
6780 {
6781 // NOLINTBEGIN(bugprone-unchecked-optional-access) emplaced when i > 0
6782 BEAST_EXPECT(
6783 amm->expectBalances(usd(1'000'000'000), eth(1'000'000'000), amm->tokens()));
6784 // NOLINTEND(bugprone-unchecked-optional-access)
6785 }
6786 if (i == 0 || i == 2)
6787 {
6788 // Fully crosses
6789 BEAST_EXPECT(expectOffers(env, alice_, 0));
6790 }
6791 // Fails to cross because AMM is not selected
6792 else
6793 {
6794 BEAST_EXPECT(expectOffers(
6795 env, alice_, 1, {Amounts{usd(400'000'000), eth(400'000'000)}}));
6796 }
6797 BEAST_EXPECT(expectOffers(env, ed, 0));
6798 }
6799
6800 // Show that the CLOB quality reduction
6801 // results in AMM offer selection.
6802
6803 // Same as the payment but reduced offer quality
6804 {
6806 for (auto i = 0; i < 3; ++i)
6807 {
6808 Env env(*this, features);
6809 auto const mpts = prep(env, rates.first, rates.second);
6810 auto const usd = mpts.usd;
6811 auto const eth = mpts.eth;
6812 auto const can = mpts.can;
6814 if (i == 0 || i == 2)
6815 {
6816 env(offer(ed, eth(400'000'000), usd(330'000'000)), Txflags(tfPassive));
6817 env.close();
6818 }
6819 if (i > 0)
6820 amm.emplace(env, ed, usd(1'000'000'000), eth(1'000'000'000));
6821 env(pay(carol_, bob_, usd(100'000'000)),
6822 Path(~MPT(usd)),
6823 Sendmax(eth(500'000'000)));
6824 env.close();
6825 // AMM and CLOB are selected
6826 if (i > 0)
6827 {
6828 // NOLINTBEGIN(bugprone-unchecked-optional-access) emplaced when i > 0
6829 BEAST_EXPECT(!amm->expectBalances(
6830 usd(1'000'000'000), eth(1'000'000'000), amm->tokens()));
6831 // NOLINTEND(bugprone-unchecked-optional-access)
6832 }
6833
6834 if (i == 2)
6835 {
6836 if (rates.first == kLowRate)
6837 {
6838 BEAST_EXPECT(expectOffers(
6839 env,
6840 ed,
6841 1,
6842 {{Amounts{
6843 eth(377'824'111),
6844 usd(311'704'892),
6845 }}}));
6846 }
6847 else
6848 {
6849 BEAST_EXPECT(expectOffers(
6850 env,
6851 ed,
6852 1,
6853 {{Amounts{
6854 eth(329'339'263),
6855 usd(271'704'892),
6856 }}}));
6857 }
6858 }
6859 env.require(Balance(bob_, usd(2'100'000'000)));
6860 q[i] = Quality(
6861 Amounts{
6862 eth(2'000'000'000) - env.balance(carol_, MPT(eth)),
6863 env.balance(bob_, MPT(usd)) - usd(2'000'000'000)});
6864 }
6865 // AMM is better quality
6866 BEAST_EXPECT(q[1] > q[0]);
6867 // AMM and CLOB produce better quality
6868 BEAST_EXPECT(q[2] > q[1]);
6869 }
6870
6871 // Same as the offer-crossing but reduced offer quality
6872 for (auto i = 0; i < 3; ++i)
6873 {
6874 Env env(*this, features);
6875 auto const mpts = prep(env, rates.first, rates.second);
6876 auto const usd = mpts.usd;
6877 auto const eth = mpts.eth;
6878 auto const can = mpts.can;
6880 if (i == 0 || i == 2)
6881 {
6882 env(offer(ed, eth(400'000'000), usd(325'000'002)), Txflags(tfPassive));
6883 env.close();
6884 }
6885 if (i > 0)
6886 amm.emplace(env, ed, usd(1'000'000'000), eth(1'000'000'000));
6887 env(offer(alice_, usd(325'000'000), eth(400'000'000)));
6888 env.close();
6889 // AMM is selected in both cases
6890 if (i > 0)
6891 {
6892 // NOLINTBEGIN(bugprone-unchecked-optional-access) emplaced when i > 0
6893 BEAST_EXPECT(!amm->expectBalances(
6894 usd(1'000'000'000), eth(1'000'000'000), amm->tokens()));
6895 // NOLINTEND(bugprone-unchecked-optional-access)
6896 }
6897 // Partially crosses, AMM is selected, CLOB fails
6898 // limitQuality
6899 if (i == 2)
6900 {
6901 if (rates.first == kLowRate)
6902 {
6903 // Ed offer is partially crossed.
6904 // Rounding in favor of the AMM makes limitQuality work
6905 BEAST_EXPECT(expectOffers(
6906 env,
6907 ed,
6908 1,
6909 {{Amounts{
6910 eth(121'368'836),
6911 usd(98'612'180),
6912 }}}));
6913 BEAST_EXPECT(expectOffers(env, alice_, 0));
6914 }
6915 else
6916 {
6917 // Ed offer is partially crossed.
6918 BEAST_EXPECT(expectOffers(
6919 env,
6920 ed,
6921 1,
6922 {{Amounts{
6923 eth(121'368'836),
6924 usd(98'612'180),
6925 }}}));
6926 BEAST_EXPECT(expectOffers(env, alice_, 0));
6927 }
6928 }
6929 }
6930
6931 // Strand selection
6932
6933 // Two book steps strand quality is 1.
6934 // AMM strand's best quality is equal to AMM's spot price
6935 // quality, which is 1. Both strands (steps) are adjusted
6936 // for the transfer fee in qualityUpperBound. In case
6937 // of two strands, AMM offers have better quality and are
6938 // consumed first, remaining liquidity is generated by CLOB
6939 // offers. Liquidity from two strands is better in this case
6940 // than in case of one strand with two book steps. Liquidity
6941 // from one strand with AMM has better quality than either one
6942 // strand with two book steps or two strands. It may appear
6943 // unintuitive, but one strand with AMM is optimized and
6944 // generates one AMM offer, while in case of two strands,
6945 // multiple AMM offers are generated, which results in slightly
6946 // worse overall quality.
6947 {
6949 for (auto i = 0; i < 3; ++i)
6950 {
6951 Env env(*this, features);
6952 auto const mpts = prep(env, rates.first, rates.second);
6953 auto const usd = mpts.usd;
6954 auto const eth = mpts.eth;
6955 auto const can = mpts.can;
6957
6958 if (i == 0 || i == 2)
6959 {
6960 env(offer(ed, eth(400'000'000), can(375'000'000)), Txflags(tfPassive));
6961 env(offer(ed, can(375'000'000), usd(338'000'000))), Txflags(tfPassive);
6962 }
6963
6964 if (i > 0)
6965 amm.emplace(env, ed, eth(1'000'000'000), usd(1'000'000'000));
6966
6967 env(pay(carol_, bob_, usd(100'000'000)),
6968 Path(~MPT(usd)),
6969 Path(~MPT(can), ~MPT(usd)),
6970 Sendmax(eth(600'000'000)));
6971 env.close();
6972
6973 env.require(Balance(bob_, usd(2'100'000'000)));
6974
6975 if (i == 2)
6976 {
6977 // NOLINTBEGIN(bugprone-unchecked-optional-access) i==2 implies amm is
6978 // emplaced (i>0)
6979 if (rates.first == kLowRate)
6980 {
6981 // Liquidity is consumed from AMM strand only
6982 BEAST_EXPECT(amm->expectBalances(
6983 eth(1'124'584'936), usd(889'999'993), amm->tokens()));
6984 }
6985 else
6986 {
6987 BEAST_EXPECT(amm->expectBalances(
6988 eth(1'103'723'909), usd(906'023'688), amm->tokens()));
6989 BEAST_EXPECT(expectOffers(
6990 env,
6991 ed,
6992 2,
6993 {{Amounts{
6994 eth(327'069'745),
6995 can(306'627'886),
6996 },
6997 Amounts{
6998 can(312'843'533),
6999 usd(281'976'305),
7000 }}}));
7001 }
7002 // NOLINTEND(bugprone-unchecked-optional-access)
7003 }
7004 q[i] = Quality(
7005 Amounts{
7006 eth(2'000'000'000) - env.balance(carol_, MPT(eth)),
7007 env.balance(bob_, MPT(usd)) - usd(2'000'000'000)});
7008 }
7009 BEAST_EXPECT(q[1] > q[0]);
7010 BEAST_EXPECT(q[2] > q[0] && q[2] < q[1]);
7011 }
7012 }
7013 }
7014
7015 void
7017 {
7018 testcase("Malformed");
7019 using namespace jtx;
7020
7021 testAMM(
7022 [&](AMM& ammAlice, Env& env) {
7023 WithdrawArg const args{
7024 .flags = tfSingleAsset,
7025 .err = Ter(temMALFORMED),
7026 };
7027 ammAlice.withdraw(args);
7028 },
7029 {{XRP(10'000), gAmmmpt(10'000)}});
7030
7031 testAMM(
7032 [&](AMM& ammAlice, Env& env) {
7033 WithdrawArg const args{
7034 .flags = tfOneAssetLPToken,
7035 .err = Ter(temMALFORMED),
7036 };
7037 ammAlice.withdraw(args);
7038 },
7039 {{XRP(10'000), gAmmmpt(10'000)}});
7040
7041 testAMM(
7042 [&](AMM& ammAlice, Env& env) {
7043 WithdrawArg const args{
7044 .flags = tfLimitLPToken,
7045 .err = Ter(temMALFORMED),
7046 };
7047 ammAlice.withdraw(args);
7048 },
7049 {{XRP(10'000), gAmmmpt(10'000)}});
7050
7051 testAMM(
7052 [&](AMM& ammAlice, Env& env) {
7053 WithdrawArg const args{
7054 .asset1Out = MPT(ammAlice[1])(100),
7055 .asset2Out = MPT(ammAlice[1])(100),
7056 .err = Ter(temBAD_AMM_TOKENS),
7057 };
7058 ammAlice.withdraw(args);
7059 },
7060 {{XRP(10'000), gAmmmpt(10'000)}});
7061
7062 testAMM(
7063 [&](AMM& ammAlice, Env& env) {
7064 MPTTester const btc({
7065 .env = env,
7066 .issuer = gw_,
7067 .holders = {alice_, carol_},
7068 .pay = 2'000,
7069 .flags = tfMPTCanLock | kMptDexFlags,
7070 });
7071 WithdrawArg const args{
7072 .asset1Out = XRP(100),
7073 .asset2Out = btc(100),
7074 .err = Ter(temBAD_AMM_TOKENS),
7075 };
7076 ammAlice.withdraw(args);
7077 },
7078 {{XRP(10'000), gAmmmpt(10'000)}});
7079
7080 testAMM(
7081 [&](AMM& ammAlice, Env& env) {
7082 json::Value jv;
7083 jv[jss::TransactionType] = jss::AMMWithdraw;
7084 jv[jss::Flags] = tfLimitLPToken;
7085 jv[jss::Account] = alice_.human();
7086 ammAlice.setTokens(jv);
7087 XRP(100).value().setJson(jv[jss::Amount]);
7088 MPTTester const btc({
7089 .env = env,
7090 .issuer = gw_,
7091 .holders = {alice_, carol_},
7092 .pay = 2'000,
7093 .flags = tfMPTCanLock | kMptDexFlags,
7094 });
7095 btc(100).value().setJson(jv[jss::EPrice]);
7096 env(jv, Ter(telENV_RPC_FAILED));
7097 },
7098 {{XRP(10'000), gAmmmpt(10'000)}});
7099 }
7100
7101 void
7103 {
7104 testcase("AMM Offer Blocked By LOB");
7105 using namespace jtx;
7106
7107 // Low quality LOB offer blocks AMM liquidity
7108
7109 // USD/MPT crosses AMM despite of low quality LOB
7110 {
7111 Env env(*this, features);
7112
7113 fund(env, gw_, {alice_, carol_}, XRP(1'000'000), {USD(1'000'000)});
7114 MPT const btc = MPTTester({
7115 .env = env,
7116 .issuer = gw_,
7117 .holders = {alice_, carol_},
7118 .pay = 40'000'000000,
7119 .flags = kMptDexFlags,
7120 });
7121
7122 env(offer(alice_, btc(1), USD(0.01)));
7123 env.close();
7124 AMM const amm(env, gw_, btc(200'000), USD(100'000));
7125 env(offer(carol_, USD(0.49), btc(1)));
7126 env.close();
7127
7128 BEAST_EXPECT(amm.expectBalances(btc(200'001), USD(99'999.51), amm.tokens()));
7129 BEAST_EXPECT(expectOffers(env, alice_, 1, {{Amounts{btc(1), USD(0.01)}}}));
7130 // Carol's offer crosses AMM
7131 BEAST_EXPECT(expectOffers(env, carol_, 0));
7132 }
7133
7134 // XRP/MPT crosses AMM despite of low quality LOB
7135 {
7136 Env env(*this, features);
7137
7138 fund(env, gw_, {alice_, carol_}, XRP(1'000'000), {USD(1'000'000)});
7139 MPT const btc = MPTTester({
7140 .env = env,
7141 .issuer = gw_,
7142 .holders = {alice_, carol_},
7143 .pay = 40'000'000000,
7144 .flags = kMptDexFlags,
7145 });
7146
7147 env(offer(alice_, btc(1), XRP(0.01)));
7148 env.close();
7149 AMM const amm(env, gw_, btc(200'000), XRP(100'000));
7150 env(offer(carol_, XRP(0.49), btc(1)));
7151 env.close();
7152
7153 BEAST_EXPECT(amm.expectBalances(btc(200'001), XRP(99'999.51), amm.tokens()));
7154 BEAST_EXPECT(expectOffers(env, alice_, 1, {{Amounts{btc(1), XRP(0.01)}}}));
7155 // Carol's offer crosses AMM
7156 BEAST_EXPECT(expectOffers(env, carol_, 0));
7157 }
7158 }
7159
7160 void
7162 {
7163 testcase("LPToken Balance");
7164 using namespace jtx;
7165
7166 Env env(*this, features);
7167 Account const gw{"gateway"}, alice{"alice"}, bob{"bob"};
7168 env.fund(XRP(100000), gw, alice, bob);
7169 env.close();
7170 env(fset(gw, asfAllowTrustLineClawback));
7171 env.close();
7172
7173 auto const usd = gw["USD"];
7174 env.trust(usd(100000), alice);
7175 env(pay(gw, alice, usd(50000)));
7176 env.trust(usd(100000), bob);
7177 env(pay(gw, bob, usd(40000)));
7178 env.close();
7179
7180 MPT const btc = MPTTester({
7181 .env = env,
7182 .issuer = gw,
7183 .holders = {alice, bob},
7184 .pay = 40'000'000000,
7185 .flags = tfMPTCanClawback | tfMPTCanLock | kMptDexFlags,
7186 });
7187
7188 AMM amm(env, alice, btc(2), usd(1));
7189 amm.deposit(alice, IOUAmount{1'876123487565916, -15});
7190 amm.deposit(bob, IOUAmount{1'000});
7191 amm.withdraw(alice, IOUAmount{1'876123487565916, -15});
7192 amm.withdrawAll(bob);
7193
7194 auto const lpToken =
7195 getAccountLines(env, alice, amm.lptIssue())[jss::lines][0u][jss::balance].asString();
7196 auto const lpTokenBalance =
7197 amm.ammRpcInfo()[jss::amm][jss::lp_token][jss::value].asString();
7198
7199 BEAST_EXPECT(lpToken == "1.414213562374011" && lpTokenBalance == "1.4142135623741");
7200
7201 auto res = isOnlyLiquidityProvider(*env.current(), amm.lptIssue(), alice);
7202 BEAST_EXPECT(res && res.value());
7203
7204 amm.withdrawAll(alice);
7205 BEAST_EXPECT(!amm.ammExists());
7206 }
7207
7208 void
7210 {
7211 testcase("AMM with vault shares not allowed");
7212 using namespace jtx;
7213
7214 // AMMTestBase::testableAmendments() strips featureSingleAssetVault,
7215 // but vault shares require it. Use the global jtx set directly.
7217
7218 env.fund(XRP(100'000), gw_, alice_);
7219 env(fset(gw_, asfDefaultRipple));
7220 env.close();
7221
7222 PrettyAsset const iou = gw_["IOU"];
7223 env.trust(iou(1'000'000), alice_);
7224 env(pay(gw_, alice_, iou(10'000)));
7225 env.close();
7226
7227 Vault const vault{env};
7228 auto [createTx, vaultKeylet] = vault.create({.owner = alice_, .asset = iou});
7229 env(createTx);
7230 env.close();
7231
7232 env(vault.deposit({.depositor = alice_, .id = vaultKeylet.key, .amount = iou(200)}));
7233 env.close();
7234
7235 auto const vaultSle = env.le(vaultKeylet);
7236 if (!BEAST_EXPECT(vaultSle))
7237 return;
7238
7239 auto const shareMPTID = vaultSle->at(sfShareMPTID);
7240 STAmount const shareAmt{MPTIssue{shareMPTID}, 100'000'000};
7241 AMM const amm{env, alice_, XRP(100), shareAmt, Ter(tecWRONG_ASSET)};
7242 env.close();
7243 }
7244
7245 void
7247 {
7248 testcase("test AMMDeposit with frozen assets");
7249 using namespace jtx;
7250
7251 // This lambda function is used to create trustline, MPT.
7252 // and create an AMM account.
7253 // And also test the callback function.
7254 auto const testAMMDeposit = [&](Env& env,
7255 std::function<void(AMM & amm, MPTTester & btc)> cb) {
7256 env.fund(XRP(1'000), gw_, alice_);
7257 env.close();
7258 MPTTester btc({
7259 .env = env,
7260 .issuer = gw_,
7261 .holders = {alice_},
7262 .pay = 30'000,
7263 .flags = tfMPTCanLock | kMptDexFlags,
7264 });
7265
7266 AMM amm(env, alice_, btc(100), XRP(100));
7267 env.close();
7268 btc.set({.holder = alice_, .flags = tfMPTLock});
7269 cb(amm, btc);
7270 };
7271
7272 // Deposit two assets, one of which is locked,
7273 // then we should get tecLOCKED error.
7274 {
7275 Env env(*this);
7276 testAMMDeposit(env, [&](AMM& amm, MPTTester& btc) {
7277 amm.deposit(alice_, btc(100), XRP(100), std::nullopt, tfTwoAsset, Ter(tecLOCKED));
7278 });
7279 }
7280
7281 // Deposit one asset, which is the locked token,
7282 // then we should get tecLOCKED error.
7283 {
7284 Env env(*this);
7285 testAMMDeposit(env, [&](AMM& amm, MPTTester& btc) {
7286 amm.deposit(
7287 alice_, btc(100), std::nullopt, std::nullopt, tfSingleAsset, Ter(tecLOCKED));
7288 });
7289 }
7290
7291 // Deposit one asset which is not the frozen token,
7292 // but the other asset is frozen. We should get tecLOCKED error
7293 // when feature AMMClawback is enabled.
7294 {
7295 Env env(*this);
7296 testAMMDeposit(env, [&](AMM& amm, MPTTester& btc) {
7297 amm.deposit(
7298 alice_, XRP(100), std::nullopt, std::nullopt, tfSingleAsset, Ter(tecLOCKED));
7299 });
7300 }
7301 }
7302
7303 void
7305 {
7306 testcase("Auto Delete");
7307
7308 using namespace jtx;
7309 FeatureBitset const all{testableAmendments()};
7310
7311 {
7312 Env env(
7313 *this,
7315 cfg->fees.referenceFee = XRPAmount(1);
7316 return cfg;
7317 }),
7318 all);
7319 env.fund(XRP(1'000), gw_, alice_);
7320 MPTTester const usd({.env = env, .issuer = gw_, .holders = {alice_}, .pay = 20'000});
7321 MPTTester const btc({.env = env, .issuer = gw_, .holders = {alice_}, .pay = 20'000});
7322 AMM amm(env, gw_, usd(10'000), btc(10'000));
7323 for (auto i = 0; i < kMaxDeletableAmmTrustLines + 10; ++i)
7324 {
7325 Account const a{std::to_string(i)};
7326 env.fund(XRP(1'000), a);
7327 env(trust(a, STAmount{amm.lptIssue(), 10'000}));
7328 env.close();
7329 }
7330 // The trustlines are partially deleted,
7331 // AMM is set to an empty state.
7332 amm.withdrawAll(gw_);
7333 BEAST_EXPECT(amm.ammExists());
7334
7335 // Bid,Vote,Deposit,Withdraw,SetTrust failing with
7336 // tecAMM_EMPTY. Deposit succeeds with tfTwoAssetIfEmpty option.
7337 env(amm.bid({
7338 .account = alice_,
7339 .bidMin = 1000,
7340 }),
7341 Ter(tecAMM_EMPTY));
7342 amm.vote({.account = alice_, .tfee = 100, .err = Ter(tecAMM_EMPTY)});
7343 amm.withdraw({.account = alice_, .tokens = 100, .err = Ter(tecAMM_EMPTY)});
7344 amm.deposit({.account = alice_, .asset1In = usd(100), .err = Ter(tecAMM_EMPTY)});
7345 env(trust(alice_, STAmount{amm.lptIssue(), 10'000}), Ter(tecAMM_EMPTY));
7346
7347 // Can deposit with tfTwoAssetIfEmpty option
7348 amm.deposit({
7349 .account = alice_,
7350 .asset1In = usd(1'000),
7351 .asset2In = btc(1'000),
7352 .flags = tfTwoAssetIfEmpty,
7353 .tfee = 1'000,
7354 });
7355 BEAST_EXPECT(amm.expectBalances(usd(1'000), btc(1'000), IOUAmount{1'000}));
7356 BEAST_EXPECT(amm.expectTradingFee(1'000));
7357 BEAST_EXPECT(amm.expectAuctionSlot(100, 0, IOUAmount{0}));
7358
7359 // Withdrawing all tokens deletes AMM since the number
7360 // of remaining trustlines is less than max
7361 amm.withdrawAll(alice_);
7362 BEAST_EXPECT(!amm.ammExists());
7363 BEAST_EXPECT(!env.le(keylet::ownerDir(amm.ammAccount())));
7364 }
7365
7366 {
7367 Env env(
7368 *this,
7370 cfg->fees.referenceFee = XRPAmount(1);
7371 return cfg;
7372 }),
7373 all);
7374 env.fund(XRP(1'000), gw_, alice_);
7375 MPTTester const usd({.env = env, .issuer = gw_, .holders = {alice_}, .pay = 20'000});
7376 MPTTester const btc({.env = env, .issuer = gw_, .holders = {alice_}, .pay = 20'000});
7377 AMM amm(env, gw_, usd(10'000), btc(10'000));
7378 for (auto i = 0; i < (kMaxDeletableAmmTrustLines * 2) + 10; ++i)
7379 {
7380 Account const a{std::to_string(i)};
7381 env.fund(XRP(1'000), a);
7382 env(trust(a, STAmount{amm.lptIssue(), 10'000}));
7383 env.close();
7384 }
7385 // The trustlines are partially deleted.
7386 amm.withdrawAll(gw_);
7387 BEAST_EXPECT(amm.ammExists());
7388
7389 // AMMDelete has to be called twice to delete AMM.
7390 amm.ammDelete(alice_, Ter(tecINCOMPLETE));
7391 BEAST_EXPECT(amm.ammExists());
7392 // Deletes remaining trustlines and deletes AMM.
7393 amm.ammDelete(alice_);
7394 BEAST_EXPECT(!amm.ammExists());
7395 BEAST_EXPECT(!env.le(keylet::ownerDir(amm.ammAccount())));
7396
7397 // Try redundant delete
7398 amm.ammDelete(alice_, Ter(terNO_AMM));
7399 }
7400
7401 {
7402 Env env(
7403 *this,
7405 cfg->fees.referenceFee = XRPAmount(1);
7406 return cfg;
7407 }),
7408 all);
7409 env.fund(XRP(1'000), gw_, alice_, carol_);
7410 MPTTester const usd(
7411 {.env = env, .issuer = gw_, .holders = {alice_, carol_}, .pay = 20'000});
7412 MPTTester const btc(
7413 {.env = env, .issuer = gw_, .holders = {alice_, carol_}, .pay = 20'000});
7414 AMM amm(env, gw_, usd(10'000), btc(10'000));
7415 amm.deposit({.account = alice_, .tokens = 1'000});
7416 amm.deposit({.account = carol_, .tokens = 1'000});
7417 amm.withdrawAll(alice_);
7418 amm.withdrawAll(carol_);
7419 amm.withdrawAll(gw_);
7420 BEAST_EXPECT(!amm.ammExists());
7421 }
7422
7423 // This test validates both invariant changes work together for
7424 // the specific case of MPT/MPT pools with > kMaxDeletableAmmTrustLines.
7425 {
7426 Env env(
7427 *this,
7429 cfg->fees.referenceFee = XRPAmount(1);
7430 return cfg;
7431 }),
7432 all);
7433
7434 env.fund(XRP(1'000), gw_, alice_);
7435 MPT const usd =
7436 MPTTester({.env = env, .issuer = gw_, .holders = {alice_}, .pay = 20'000});
7437 MPT const btc =
7438 MPTTester({.env = env, .issuer = gw_, .holders = {alice_}, .pay = 20'000});
7439
7440 // MPT/MPT pool with MANY trustlines
7441 AMM amm(env, gw_, usd(10'000), btc(10'000));
7442 for (auto i = 0; i < (kMaxDeletableAmmTrustLines * 2) + 10; ++i)
7443 {
7444 Account const a{std::to_string(i)};
7445 env.fund(XRP(1'000), a);
7446 env(trust(a, STAmount{amm.lptIssue(), 10'000}));
7447 env.close();
7448 }
7449
7450 amm.withdrawAll(gw_);
7451 // AMM is in empty state, but can't be auto-deleted because of the LPTokens trustlines.
7452 BEAST_EXPECT(amm.expectBalances(usd(0), btc(0), IOUAmount(0)));
7453 BEAST_EXPECT(amm.ammExists());
7454
7455 // Critical: MPT/MPT pool + tecINCOMPLETE
7456 amm.ammDelete(alice_, Ter(tecINCOMPLETE));
7457 BEAST_EXPECT(amm.ammExists());
7458
7459 amm.ammDelete(alice_);
7460 BEAST_EXPECT(!amm.ammExists());
7461 }
7462 }
7463
7464 void
7466 {
7467 testcase("Deposit integral overflow (MPT)");
7468
7469 using namespace jtx;
7470
7471 // Without fixCleanup3_4_0 the exception escapes and is converted to
7472 // tefEXCEPTION by applySteps. With the amendment, applyGuts guards it
7473 // and fails cleanly with tecAMM_FAILED.
7474 auto const err = features[fixCleanup3_4_0] ? Ter(tecAMM_FAILED) : Ter(tefEXCEPTION);
7475
7476 // MPT counterpart of AMM_test::testDepositIntegralOverflow. A two-asset
7477 // deposit with a huge Amount against a tiny pool leg makes
7478 // frac = Amount / balance enormous, so the computed deposit for the
7479 // other (integral) leg exceeds Number's int64 range (kMaxRep ~=
7480 // 9.22e18) and the conversion to an integral STAmount throws out of
7481 // doApply - which applySteps would surface as tefEXCEPTION.
7482 //
7483 // The default amendments include fixCleanup3_4_0, under which applyGuts
7484 // guards the overflow and fails cleanly with tecAMM_FAILED. This
7485 // verifies the guarded path: no overflow escapes.
7486
7487 // XRP/MPT - the exact pool the report (Antithesis) calls out. A tiny
7488 // mpt(1) balance and a huge MPT Amount drive frac; the XRP leg is what
7489 // overflows: XRP(10) is 1e7 drops, so getRoundedAsset(XRP, frac) is
7490 // 1e7 * 1e13 = 1e20 drops, well past kMaxRep.
7491 {
7492 // The deposit intentionally overflows, which logs at error.
7493 // Disable the log threshold to keep the test output clean.
7494 Env env(*this, envconfig(), features, nullptr, beast::Severity::Disabled);
7495 if (!features[fixCleanup3_4_0])
7496 env.disableFeature(fixCleanup3_4_0);
7497 env.fund(XRP(30'000), gw_, alice_);
7498 env.close();
7499
7500 // kMptDexFlags (CanTrade | CanTransfer), which AMMs require, is
7501 // the default. alice must hold enough MPT to fund the pool and the
7502 // oversized deposit.
7503 MPT const mpt = MPTTester({
7504 .env = env,
7505 .issuer = gw_,
7506 .holders = {alice_},
7507 .pay = 100'000'000'000'000, // 1e14
7508 .maxAmt = 1'000'000'000'000'000, // 1e15
7509 });
7510 env.close();
7511
7512 AMM amm(env, alice_, XRP(10), mpt(1));
7513 amm.deposit(
7514 DepositArg{
7515 .account = alice_,
7516 .asset1In = mpt(10'000'000'000'000), // 1e13
7517 .asset2In = XRP(1),
7518 .err = err,
7519 });
7520 }
7521
7522 // IOU/MPT - the MPT leg is the one that overflows. A classic IOU
7523 // trustline drives frac (huge USD Amount vs USD(1) balance); the
7524 // MPT-side deposit is then mptBalance * frac = 10'000 * 1e16 = 1e20.
7525 {
7526 // The deposit intentionally overflows, which logs at error.
7527 // Disable the log threshold to keep the test output clean.
7528 Env env(*this, envconfig(), features, nullptr, beast::Severity::Disabled);
7529 env.fund(XRP(30'000), gw_, alice_);
7530 env(trust(alice_, STAmount{USD, 1, 20}));
7531 env(pay(gw_, alice_, STAmount{USD, 1, 18}));
7532 env.close();
7533
7534 MPT const mpt =
7535 MPTTester({.env = env, .issuer = gw_, .holders = {alice_}, .pay = 1'000'000});
7536 env.close();
7537
7538 AMM amm(env, alice_, mpt(10'000), USD(1));
7539 amm.deposit(
7540 DepositArg{
7541 .account = alice_,
7542 .asset1In = STAmount{USD, 1, 16},
7543 .asset2In = mpt(1),
7544 .err = err,
7545 });
7546 }
7547 }
7548
7549 void
7551 {
7552 testcase("Withdraw integral no overflow (MPT)");
7553
7554 using namespace jtx;
7555
7556 // MPT counterpart of AMM_test::testWithdrawIntegralNoOverflow and the
7557 // sibling of testDepositIntegralOverflowMPT. AMMWithdraw::
7558 // equalWithdrawLimit has the same getRoundedAsset(integralBalance,
7559 // frac) structure as the deposit path and is likewise not wrapped in a
7560 // try/catch. It is safe only because withdraw preclaim (checkAmount)
7561 // rejects a requested Amount greater than the pool balance with
7562 // tecAMM_BALANCE *before* the math runs, so frac = Amount / balance
7563 // stays <= 1 and the Number -> integral STAmount conversion cannot
7564 // overflow. Deposit has no such bound, which is why only the deposit
7565 // path was exposed.
7566 //
7567 // These mirror the deposit tests: the same oversized two-asset
7568 // request is rejected cleanly. If the preclaim bound is ever weakened,
7569 // equalWithdrawLimit would be reached with a huge frac and
7570 // Number::operator rep() would escape as tefEXCEPTION, failing this.
7571
7572 // XRP/MPT - the pool the report calls out. Requesting far more of the
7573 // tiny MPT leg than the pool holds is rejected before the math.
7574 {
7575 Env env(*this);
7576 env.fund(XRP(30'000), gw_, alice_);
7577 env.close();
7578
7579 MPT const mpt = MPTTester({
7580 .env = env,
7581 .issuer = gw_,
7582 .holders = {alice_},
7583 .pay = 100'000'000'000'000, // 1e14
7584 .maxAmt = 1'000'000'000'000'000, // 1e15
7585 });
7586 env.close();
7587
7588 // alice holds all LPTokens of a tiny XRP/MPT pool.
7589 AMM amm(env, alice_, XRP(10), mpt(1));
7590 amm.withdraw(
7592 .account = alice_,
7593 .asset1Out = mpt(10'000'000'000'000), // 1e13 > mpt(1)
7594 .asset2Out = XRP(1),
7595 .err = Ter(tecAMM_BALANCE),
7596 });
7597 }
7598
7599 // IOU/MPT - requesting far more of the tiny IOU leg than the pool
7600 // holds is likewise rejected.
7601 {
7602 Env env(*this);
7603 env.fund(XRP(30'000), gw_, alice_);
7604 env(trust(alice_, STAmount{USD, 1, 20}));
7605 env(pay(gw_, alice_, STAmount{USD, 1, 18}));
7606 env.close();
7607
7608 MPT const mpt =
7609 MPTTester({.env = env, .issuer = gw_, .holders = {alice_}, .pay = 1'000'000});
7610 env.close();
7611
7612 AMM amm(env, alice_, mpt(10'000), USD(1));
7613 amm.withdraw(
7615 .account = alice_,
7616 .asset1Out = STAmount{USD, 1, 16}, // > USD(1)
7617 .asset2Out = mpt(1),
7618 .err = Ter(tecAMM_BALANCE),
7619 });
7620 }
7621 }
7622
7623 void
7625 {
7626 testcase("Dangling AMM MPToken freeze check");
7627
7628 using namespace jtx;
7629 FeatureBitset const all{testableAmendments()};
7630
7631 Env env(*this, all);
7632
7633 env.fund(XRP(1'000), gw_, alice_);
7634 MPTTester usd({.env = env, .issuer = gw_});
7635 MPTTester const btc({.env = env, .issuer = gw_});
7636
7637 AMM amm(env, gw_, usd(10'000), btc(10'000));
7638 for (auto i = 0; i < kMaxDeletableAmmTrustLines + 10; ++i)
7639 {
7640 Account const a{std::to_string(i)};
7641 env.fund(XRP(1'000), a);
7642 env(trust(a, STAmount{amm.lptIssue(), 10'000}));
7643 env.close();
7644 }
7645
7646 // With too many LP-token trust lines to delete in one pass, the AMM
7647 // remains in an empty state with zero-balance MPToken objects.
7648 amm.withdrawAll(gw_);
7649 BEAST_EXPECT(amm.ammExists());
7650 BEAST_EXPECT(amm.expectBalances(usd(0), btc(0), IOUAmount{0}));
7651
7652 auto const ammToken = env.le(keylet::mptoken(usd.issuanceID(), amm.ammAccount()));
7653 if (!BEAST_EXPECT(ammToken))
7654 return;
7655 BEAST_EXPECT((*ammToken)[sfMPTAmount] == 0);
7656
7657 usd.destroy();
7658 BEAST_EXPECT(env.le(keylet::mptokenIssuance(usd.issuanceID())) == nullptr);
7659 BEAST_EXPECT(!isFrozen(*env.current(), amm.ammAccount(), *ammToken));
7660 // A Payment cannot cross this empty AMM because BookStep skips AMMs
7661 // with zero LPTokenBalance. Probe the same ZeroIfFrozen balance read
7662 // used by AMM accounting.
7663 auto const balance = accountHolds(
7664 *env.current(),
7665 amm.ammAccount(),
7666 MPTIssue{usd.issuanceID()},
7669 env.journal);
7670
7671 BEAST_EXPECT(balance == usd(0));
7672 }
7673
7674 void
7675 run() override
7676 {
7680 for (auto const& f : jtx::amendmentCombinations({fixCleanup3_3_0, featureAMMClawback}, all))
7682 testDeposit();
7684 testWithdraw();
7687 testFeeVote();
7689 testBid(all);
7690 testBid(all - fixCleanup3_4_0);
7691 testClawback();
7693 testClawbackFromAMMAccount(all - featureSingleAssetVault);
7696 testAMMTokens();
7697 testAmendment();
7698 testAMMAndCLOB(all);
7700 testTradingFee(all);
7701 testTradingFee(all - fixAMMv1_3);
7702 testAdjustedTokens(all);
7703 testAMMID();
7704 testSelection(all);
7705 testMalformed();
7706 testFixAMMOfferBlockedByLOB(all - fixAMMv1_3);
7708 testLPTokenBalance(all);
7709 testLPTokenBalance(all - fixAMMv1_3);
7714 testDepositIntegralOverflowMPT(all - fixCleanup3_4_0);
7717 }
7718};
7719
7721
7722} // namespace xrpl::test
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
Value removeMember(char const *key)
Remove and return the named member.
virtual Config & config()=0
std::unordered_set< UInt256, beast::Uhash<> > features
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:26
A currency issued by an account.
Definition Issue.h:18
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
Represents the logical ratio of output currency to input currency.
Definition Quality.h:90
int signum() const noexcept
Definition STAmount.h:522
static constexpr int kMinOffset
Definition STAmount.h:61
static constexpr std::uint64_t kMinValue
Definition STAmount.h:65
json::Value getJson(JsonOptions) const override
Definition STIssue.cpp:88
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
constexpr value_type value() const
Returns the underlying value.
Definition XRPAmount.h:225
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 getLPTokensBalance(std::optional< AccountID > const &account=std::nullopt) const
Definition AMM.cpp:282
bool ammExists() const
Definition AMM.cpp:344
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
AccountID const & ammAccount() const
void setTokens(json::Value &jv, std::optional< std::pair< Asset, Asset > > const &assets=std::nullopt)
Definition AMM.cpp:392
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
static json::Value voteJv(VoteArg const &arg)
Definition AMM.cpp:691
bool expectLPTokens(AccountID const &account, IOUAmount const &tokens) const
Definition AMM.cpp:296
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
AccountID id() const
Returns the Account ID.
A balance matches.
Definition balance.h:24
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:311
std::uint32_t ownerCount(Account const &account) const
Return the number of objects owned by an account.
Definition Env.cpp:266
bool enabled(UInt256 feature) const
Definition Env.h:886
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition Env.cpp:302
Account const & master
Definition Env.h:165
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:1058
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:723
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
Definition Env.cpp:201
void disableFeature(UInt256 const feature)
Definition Env.cpp:717
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:354
std::shared_ptr< STTx const > ust(JTx const &jt)
Create a STTx from a JTx without sanitizing Use to inject bogus values into test transactions by firs...
Definition Env.cpp:664
std::shared_ptr< STObject const > meta()
Return metadata for the last JTx.
Definition Env.cpp:538
void memoize(Account const &account)
Associate AccountID with account.
Definition Env.cpp:174
beast::Journal const journal
Definition Env.h:204
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
NetClock::time_point now()
Returns the current network time.
Definition Env.h:326
Set the fee on a JTx.
Definition fee.h:20
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Definition mpt.h:512
void create(MPTCreate const &arg=MPTCreate{}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:346
void set(MPTSet const &set={}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:581
Converts to MPT Issue or STAmount.
Add a path.
Definition paths.h:47
Sets the SendMax on a JTx.
Definition sendmax.h:16
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
Set the flags on a JTx.
Definition txflags.h:14
T is_same_v
T make_pair(T... args)
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
Keylet offer(AccountID const &id, SeqProxy const &seq) noexcept
An offer from an account.
Definition Indexes.cpp:310
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:415
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Definition Indexes.cpp:483
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:547
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:541
Check operations.
Definition check.h:18
json::Value create(A const &account, A const &dest, STAmount const &sendMax)
Create a check.
Deposit preauthorize operations.
Definition deposit.h:16
json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount)
Definition escrow.cpp:24
auto const kCondition
Definition escrow.h:100
auto const kCancelTime
Set the "CancelAfter" time tag on a JTx.
Definition escrow.h:98
auto const kFinishTime
Set the "FinishAfter" time tag on a JTx.
Definition escrow.h:93
std::array< std::uint8_t, 39 > const kCb1
Definition escrow.h:60
Paychan operations.
Definition paychan.cpp:28
json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount, NetClock::duration const &settleDelay, PublicKey const &pk, std::optional< NetClock::time_point > const &cancelAfter, std::optional< std::uint32_t > const &dstTag)
Definition paychan.cpp:31
static NoneT const kNone
Definition tags.h:9
auto const kMptDexFlags
Definition mpt.h:47
json::Value claw(Account const &account, STAmount const &amount, std::optional< Account > const &mptHolder)
Definition trust.cpp:51
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)
bool expectHolding(Env const &env, AccountID const &account, STAmount const &value, bool defaultLimits)
void testHelper2TokensMix(TTester &&tester)
std::vector< FeatureBitset > amendmentCombinations(std::initializer_list< UInt256 > features, FeatureBitset seed)
Returns all 2^N permutations of a seed FeatureBitset with each subset of the given features excluded.
Definition Env.h:123
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
bool expectOffers(Env const &env, AccountID const &account, std::uint16_t size, std::vector< Amounts > const &toMatch)
XRPAmount txFee(Env const &env, std::uint16_t n)
FeatureBitset testableAmendments()
Definition Env.h:92
bool expectMPT(Env const &env, AccountID const &account, STAmount const &value)
static auto gAmmmpt
Definition AMMTest.h:46
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Definition trust.cpp:18
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
Definition rate.cpp:15
json::Value getAccountLines(Env &env, AccountID const &acctId)
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
STTx createTx(bool disabling, LedgerIndex seq, PublicKey const &txKey)
Create ttUNL_MODIFY Tx.
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
@ telENV_RPC_FAILED
Definition TER.h:54
@ terNO_AMM
Definition TER.h:229
@ terNO_ACCOUNT
Definition TER.h:219
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
constexpr std::uint32_t kTotalTimeSlotSecs
Definition AMMCore.h:19
Number ammAuctionMinSlotPrice(Number const &lptAMMBalance, std::uint16_t tradingFee)
Minimum auction slot price: LPTokens * TradingFee / kAuctionSlotMinFeeFraction.
Definition AMMCore.h:94
std::expected< bool, TER > isOnlyLiquidityProvider(ReadView const &view, Issue const &ammIssue, AccountID const &lpAccount)
Return true if the Liquidity Provider is the only AMM provider, false otherwise.
TAmounts< STAmount, STAmount > Amounts
Definition Quality.h:69
constexpr XRPAmount kDropsPerXrp
Number of drops per 1 XRP.
Definition XRPAmount.h:247
@ tefEXCEPTION
Definition TER.h:166
STAmount amountFromString(Asset const &asset, std::string const &amount)
STAmount ammAssetOut(STAmount const &assetBalance, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee)
Calculate asset withdrawal by tokens.
constexpr std::uint16_t kMaxDeletableAmmTrustLines
The maximum number of trustlines to delete as part of AMM account deletion cleanup.
Definition Protocol.h:436
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
std::optional< TAmounts< TIn, TOut > > getAMMOfferStartWithTakerGets(TAmounts< TIn, TOut > const &pool, Quality const &targetQuality, std::uint16_t const &tfee)
Generate AMM offer starting with takerGets when AMM pool from the payment perspective is IOU(in)/XRP(...
Definition AMMHelpers.h:199
MPTID noMPT()
Definition MPTIssue.h:114
std::optional< TAmounts< TIn, TOut > > getAMMOfferStartWithTakerPays(TAmounts< TIn, TOut > const &pool, Quality const &targetQuality, std::uint16_t tfee)
Generate AMM offer starting with takerPays when AMM pool from the payment perspective is XRP(in)/IOU(...
Definition AMMHelpers.h:267
BaseUInt< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
Definition UintTypes.h:54
bool isFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptIssue, std::uint8_t depth=0)
Returns true if account cannot send or receive tokens of mptIssue because a freeze applies.
constexpr std::uint16_t kAuctionSlotTimeIntervals
Definition AMMCore.h:20
@ temBAD_FEE
Definition TER.h:80
@ temBAD_MPT
Definition TER.h:133
@ temBAD_AMM_TOKENS
Definition TER.h:117
@ temMALFORMED
Definition TER.h:75
@ temDISABLED
Definition TER.h:102
@ temBAD_AMOUNT
Definition TER.h:77
MPTID badMPT()
Definition MPTIssue.h:121
@ tecWRONG_ASSET
Definition TER.h:371
@ tecPSEUDO_ACCOUNT
Definition TER.h:373
@ tecAMM_EMPTY
Definition TER.h:343
@ tecLOCKED
Definition TER.h:369
@ tecPATH_PARTIAL
Definition TER.h:293
@ tecAMM_INVALID_TOKENS
Definition TER.h:342
@ tecINSUF_RESERVE_LINE
Definition TER.h:299
@ tecAMM_FAILED
Definition TER.h:341
@ tecAMM_NOT_EMPTY
Definition TER.h:344
@ tecPATH_DRY
Definition TER.h:305
@ tecINCOMPLETE
Definition TER.h:346
@ tecUNFUNDED_AMM
Definition TER.h:339
@ tecOBJECT_NOT_FOUND
Definition TER.h:337
@ tecNO_AUTH
Definition TER.h:311
@ tecAMM_BALANCE
Definition TER.h:340
@ tecFROZEN
Definition TER.h:314
@ tecAMM_ACCOUNT
Definition TER.h:345
@ tecOWNERS
Definition TER.h:309
@ tecNO_PERMISSION
Definition TER.h:316
@ tecDUPLICATE
Definition TER.h:326
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
@ tesSUCCESS
Definition TER.h:253
constexpr std::uint32_t kAuctionSlotIntervalDuration
Definition AMMCore.h:25
T push_back(T... args)
Zero allows classes to offer efficient comparisons to zero.
Definition Zero.h:26
Represents a pair of input and output currencies.
Definition Quality.h:29
Basic tests of AMM functionality involving MPT assets, excluding those that use offers.
void testAMMOfferGenerationPolicy(FeatureBitset features)
void testTradingFee(FeatureBitset features)
void testClawbackFromAMMAccount(FeatureBitset features)
void testFixAMMOfferBlockedByLOB(FeatureBitset features)
void testLPTokenBalance(FeatureBitset features)
void testAdjustedTokens(FeatureBitset features)
void testSelection(FeatureBitset features)
void testInvalidDeposit(FeatureBitset features)
void testDepositIntegralOverflowMPT(FeatureBitset features)
void run() override
Runs the suite.
void testBid(FeatureBitset features)
void testAMMAndCLOB(FeatureBitset features)
json::Value jv
Definition JTx.h:28
STAmount const & value() const
Set the sequence number on a JTx.
Definition seq.h:16
T to_string(T... args)