1#include <test/jtx/AMM.h>
2#include <test/jtx/AMMTest.h>
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/PathSet.h>
6#include <test/jtx/TestHelpers.h>
7#include <test/jtx/amount.h>
8#include <test/jtx/balance.h>
9#include <test/jtx/delivermin.h>
10#include <test/jtx/fee.h>
11#include <test/jtx/flags.h>
12#include <test/jtx/jtx_json.h>
13#include <test/jtx/multisign.h>
14#include <test/jtx/noop.h>
15#include <test/jtx/offer.h>
16#include <test/jtx/owners.h>
17#include <test/jtx/paths.h>
18#include <test/jtx/pay.h>
19#include <test/jtx/quality.h>
20#include <test/jtx/rate.h>
21#include <test/jtx/regkey.h>
22#include <test/jtx/require.h>
23#include <test/jtx/sendmax.h>
24#include <test/jtx/sig.h>
25#include <test/jtx/tags.h>
26#include <test/jtx/ter.h>
27#include <test/jtx/trust.h>
28#include <test/jtx/txflags.h>
30#include <xrpl/basics/Number.h>
31#include <xrpl/beast/unit_test/suite.h>
32#include <xrpl/json/json_value.h>
33#include <xrpl/json/to_string.h>
34#include <xrpl/ledger/ApplyView.h>
35#include <xrpl/ledger/OpenView.h>
36#include <xrpl/ledger/PaymentSandbox.h>
37#include <xrpl/ledger/Sandbox.h>
38#include <xrpl/ledger/helpers/OfferHelpers.h>
39#include <xrpl/protocol/AccountID.h>
40#include <xrpl/protocol/Feature.h>
41#include <xrpl/protocol/Indexes.h>
42#include <xrpl/protocol/Issue.h>
43#include <xrpl/protocol/KeyType.h>
44#include <xrpl/protocol/LedgerFormats.h>
45#include <xrpl/protocol/SField.h>
46#include <xrpl/protocol/STPathSet.h>
47#include <xrpl/protocol/Seed.h>
48#include <xrpl/protocol/TER.h>
49#include <xrpl/protocol/TxFlags.h>
50#include <xrpl/protocol/UintTypes.h>
51#include <xrpl/protocol/XRPAmount.h>
52#include <xrpl/protocol/jss.h>
53#include <xrpl/tx/paths/Flow.h>
54#include <xrpl/tx/paths/detail/Steps.h>
76 testableAmendments() - featureSingleAssetVault - featureLendingProtocol - featureMPTokensV2,
83 testcase(
"Incorrect Removal of Funded Offers");
96 Env env{*
this, features};
122 STAmount{BTC, UINT64_C(1'001'000000374815), -12},
USD(100'000), ammCarol.
tokens()));
136 Env env{*
this, features};
141 auto const usD1 = gw1[
"USD"];
142 auto const usD2 = gw2[
"USD"];
147 env(
trust(
bob_, usD1(1'000), tfSetNoRipple));
149 env(
trust(
bob_, usD2(1'000), tfSetNoRipple));
152 env(
pay(gw1, dan, usD1(10'000)));
157 AMM const ammDan(env, dan,
XRP(10'000), usD1(10'000));
168 Env env{*
this, features};
173 auto const usD1 = gw1[
"USD"];
174 auto const usD2 = gw2[
"USD"];
183 env(
pay(gw1, dan, usD1(10'050)));
188 AMM const ammDan(env, dan,
XRP(10'000), usD1(10'050));
209 auto const startBalance =
XRP(1'000'000);
215 [&](
AMM& ammAlice,
Env& env) {
234 {{
XRP(10'100),
USD(10'000)}},
242 [&](
AMM& ammAlice,
Env& env) {
257 {{
XRP(10'100),
USD(10'000)}},
264 [&](
AMM& ammAlice,
Env& env) {
273 {{
XRP(10'100),
USD(10'000)}},
280 [&](
AMM& ammAlice,
Env& env) {
295 {{
XRP(11'000),
USD(9'000)}},
304 testcase(
"Offer Crossing with XRP, Normal order");
308 Env env{*
this, features};
317 auto const xrpTransferred =
XRPAmount{3'061'224'490};
320 BEAST_EXPECT(ammAlice.expectBalances(
321 XRP(150'000) + xrpTransferred,
USD(49),
IOUAmount{273'861'278752583, -8}));
332 testcase(
"Offer Crossing with Limit Override");
336 Env env{*
this, features};
351 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] ==
"-1");
354 jrr[jss::node][sfBalance.fieldName] ==
361 testcase(
"Currency Conversion: Entire Offer");
365 Env env{*
this, features};
386 jrr[jss::node][sfBalance.fieldName] ==
393 testcase(
"Currency Conversion: In Parts");
398 [&](
AMM& ammAlice,
Env& env) {
418 {{
XRP(10'000),
USD(10'000)}},
427 testcase(
"Cross Currency Payment: Start with XRP");
432 [&](
AMM& ammAlice,
Env& env) {
441 {{
XRP(10'000),
USD(10'100)}},
450 testcase(
"Cross Currency Payment: End with XRP");
455 [&](
AMM& ammAlice,
Env& env) {
465 {{
XRP(10'100),
USD(10'000)}},
474 testcase(
"Cross Currency Payment: Bridged");
478 Env env{*
this, features};
480 auto const gw1 =
Account{
"gateway_1"};
481 auto const gw2 =
Account{
"gateway_2"};
482 auto const dan =
Account{
"dan"};
483 auto const usD1 = gw1[
"USD"];
484 auto const euR1 = gw2[
"EUR"];
493 env(
trust(dan, euR1(1'000)));
499 env(
pay(gw2, dan, dan[
"EUR"](400)));
502 AMM const ammCarol(env,
carol_, usD1(5'000),
XRP(50'000));
504 env(
offer(dan,
XRP(500), euR1(50)));
508 jtp[0u][0u][jss::currency] =
"XRP";
512 XRP(49'700),
STAmount{usD1, UINT64_C(5'030'181086519115), -12}, ammCarol.
tokens()));
520 testcase(
"Offer Fees Consume Funds");
524 Env env{*
this, features};
526 auto const gw1 =
Account{
"gateway_1"};
527 auto const gw2 =
Account{
"gateway_2"};
528 auto const gw3 =
Account{
"gateway_3"};
529 auto const localAlice =
Account{
"alice"};
530 auto const localBob =
Account{
"bob"};
531 auto const usD1 = gw1[
"USD"];
532 auto const usD2 = gw2[
"USD"];
533 auto const usD3 = gw3[
"USD"];
541 auto const startingXrp =
542 XRP(100) + env.
current()->fees().accountReserve(3, 1) + env.
current()->fees().base * 4;
544 env.
fund(startingXrp, gw1, gw2, gw3, localAlice);
545 env.
fund(
XRP(2'000), localBob);
548 env(
trust(localAlice, usD1(1'000)));
549 env(
trust(localAlice, usD2(1'000)));
550 env(
trust(localAlice, usD3(1'000)));
551 env(
trust(localBob, usD1(1'200)));
552 env(
trust(localBob, usD2(1'100)));
554 env(
pay(gw1, localBob, localBob[
"USD"](1'200)));
556 AMM const ammBob(env, localBob,
XRP(1'000), usD1(1'200));
559 env(
offer(localAlice, usD1(200),
XRP(200)));
564 XRP(1'100),
STAmount{usD1, UINT64_C(1'090'909090909091), -12}, ammBob.
tokens()));
567 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] ==
"109.090909090909");
569 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName] ==
XRP(350).value().getText());
575 testcase(
"Offer Create, then Cross");
579 Env env{*
this, features};
598 BEAST_EXPECT(jrr[jss::node][sfBalance.fieldName][jss::value] ==
"-0.8995000001");
604 testcase(
"Offer tfSell: Basic Sell");
609 [&](
AMM& ammAlice,
Env& env) {
618 {{
XRP(9'900),
USD(10'100)}},
627 testcase(
"Offer tfSell: 2x Sell Exceed Limit");
631 Env env{*
this, features};
633 auto const startingXrp =
XRP(100) +
reserve(env, 1) + env.
current()->fees().base * 2;
660 testcase(
"Client Issue: Gateway Cross Currency");
664 Env env{*
this, features};
666 auto const xts =
gw_[
"XTS"];
667 auto const xxx =
gw_[
"XXX"];
669 auto const startingXrp =
XRP(100.1) +
reserve(env, 1) + env.
current()->fees().base * 2;
672 AMM const ammAlice(env,
alice_, xts(100), xxx(100));
676 payment[jss::id] = env.
seq(
bob_);
677 payment[jss::build_path] =
true;
679 payment[jss::tx_json][jss::Sequence] =
682 payment[jss::tx_json][jss::SendMax] =
684 payment[jss::tx_json][jss::Flags] = tfPartialPayment;
685 auto const jrr = env.
rpc(
"json",
"submit",
to_string(payment));
686 BEAST_EXPECT(jrr[jss::result][jss::status] ==
"success");
687 BEAST_EXPECT(jrr[jss::result][jss::engine_result] ==
"tesSUCCESS");
688 BEAST_EXPECT(ammAlice.expectBalances(
689 STAmount(xts, UINT64_C(101'0101010101011), -13), xxx(99), ammAlice.tokens()));
702 Env env{*
this, features};
720 BEAST_EXPECT(ammAlice.expectBalances(
XRP(10'100),
USD(10'000), ammAlice.tokens()));
728 Env env{*
this, features};
748 BEAST_EXPECT(ammAlice.expectBalances(
XRP(10'100),
USD(10'000), ammAlice.tokens()));
756 Env env{*
this, features};
789 testcase(
"Combine tfSell with tfFillOrKill");
797 Env env{*
this, features};
803 BEAST_EXPECT(ammBob.expectBalances(
XRP(20'000),
USD(200), ammBob.tokens()));
807 Env env{*
this, features};
814 BEAST_EXPECT(ammBob.expectBalances(
815 XRP(20'220),
STAmount{
USD, UINT64_C(197'8239366963403), -13}, ammBob.tokens()));
823 Env env{*
this, features};
830 BEAST_EXPECT(ammBob.expectBalances(
831 XRP(21'500),
STAmount{
USD, UINT64_C(186'046511627907), -12}, ammBob.tokens()));
842 Env env{*
this, features};
862 [&](
AMM& ammAlice,
Env& env) {
874 {{
XRP(10'000),
USD(10'100)}},
882 [&](
AMM& ammAlice,
Env& env) {
894 {{
XRP(10'100),
USD(10'000)}},
901 Env env{*
this, features};
932 Env env{*
this, features};
967 Env env{*
this, features};
1010 Env env{*
this, features};
1055 using namespace jtx;
1057 Env env{*
this, features};
1059 auto const usdBob =
bob_[
"USD"];
1060 auto const f = env.
current()->fees().base;
1064 AMM const ammBob(env,
bob_,
XRP(10'000), usdBob(10'100));
1082 using namespace jtx;
1084 Env env{*
this, features};
1087 auto const fee = env.
current()->fees().base;
1090 auto const ann =
Account(
"ann");
1091 auto const aBux = ann[
"BUX"];
1092 auto const localBob =
Account(
"bob");
1093 auto const cam =
Account(
"cam");
1094 auto const dan =
Account(
"dan");
1095 auto const dBux = dan[
"BUX"];
1098 env.
fund(
reserve(env, 4) + (fee * 4), ann, localBob, cam, dan);
1101 env(
trust(localBob, aBux(400)));
1103 env(
trust(cam, dBux(100)));
1105 env(
pay(dan, localBob, dBux(100)));
1109 env(
pay(ann, cam, dBux(60)),
Path(localBob, dan),
Sendmax(aBux(200)));
1121 AMM const ammBob(env, localBob, aBux(30), dBux(30));
1123 env(
trust(ann, dBux(100)));
1149 using namespace jtx;
1151 Env env{*
this, features};
1153 auto const ann =
Account(
"ann");
1154 auto const localBob =
Account(
"bob");
1155 auto const cam =
Account(
"cam");
1156 auto const carol =
Account(
"carol");
1157 auto const aBux = ann[
"BUX"];
1158 auto const bBux = localBob[
"BUX"];
1160 auto const fee = env.
current()->fees().base;
1162 env.
fund(
reserve(env, 4) + (fee * 5), ann, localBob, cam);
1165 env(
trust(ann, bBux(40)));
1166 env(
trust(cam, aBux(40)));
1167 env(
trust(localBob, aBux(30)));
1168 env(
trust(cam, bBux(40)));
1169 env(
trust(carol, bBux(400)));
1170 env(
trust(carol, aBux(400)));
1173 env(
pay(ann, cam, aBux(35)));
1174 env(
pay(localBob, cam, bBux(35)));
1175 env(
pay(localBob, carol, bBux(400)));
1176 env(
pay(ann, carol, aBux(400)));
1178 AMM const ammCarol(env, carol, aBux(300), bBux(330));
1183 env(
offer(cam, aBux(29), bBux(30), tfPassive));
1190 env(
offer(cam, bBux(30), aBux(30)));
1194 STAmount{aBux, UINT64_C(309'3541659651604), -13},
1195 STAmount{bBux, UINT64_C(320'0215509984419), -13},
1202 STAmount{bBux, UINT64_C(20'0215509984419), -13},
1203 STAmount{aBux, UINT64_C(20'0215509984419), -13}}}}));
1211 using namespace jtx;
1213 Env env{*
this, features};
1215 auto const aliceUSD =
alice_[
"USD"];
1216 auto const bobUSD =
bob_[
"USD"];
1222 env(
fset(
gw_, asfRequireAuth));
1261 testcase(
"lsfRequireAuth, unauthorized AMM pseudo-account");
1263 using namespace jtx;
1265 bool const pseudoExempt = features[fixCleanup3_4_0];
1267 Env env{*
this, features};
1269 auto const aliceUSD =
alice_[
"USD"];
1270 auto const bobUSD =
bob_[
"USD"];
1275 env(
fset(
gw_, asfRequireAuth));
1289 auto const ammLineAuthorized = [&] ->
bool {
1292 if (!BEAST_EXPECT(line))
1294 return line->isFlag(
1295 ammAlice.
ammAccount() >
USD.issue().account ? lsfLowAuth : lsfHighAuth);
1297 BEAST_EXPECT(!ammLineAuthorized());
1323 BEAST_EXPECT(!ammLineAuthorized());
1331 using namespace jtx;
1333 Env env{*
this, features};
1343 env(
fset(
gw_, asfRequireAuth));
1395 using namespace jtx;
1431 using namespace jtx;
1444 BEAST_EXPECT(st.
empty());
1448 BEAST_EXPECT(sa ==
XRP(100'000'000));
1461 using namespace jtx;
1464 auto const aud =
gw_[
"AUD"];
1479 BEAST_EXPECT(std::get<0>(result).empty());
1486 using namespace jtx;
1487 auto const charlie =
Account(
"charlie");
1492 AMM const ammCharlie(env, charlie,
XRP(10),
USD(11));
1494 BEAST_EXPECT(sa ==
XRP(1));
1496 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
1498 auto const& pathElem = st[0][0];
1500 pathElem.isOffer() && pathElem.getIssuerID() ==
gw_.id() &&
1501 pathElem.getCurrency() ==
USD.currency);
1508 AMM const ammCharlie(env, charlie,
XRP(11),
USD(10));
1511 BEAST_EXPECT(sa ==
USD(1));
1513 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
1515 auto const& pathElem = st[0][0];
1517 pathElem.isOffer() && pathElem.getIssuerID() ==
xrpAccount() &&
1526 testcase(
"Path Find: XRP -> XRP and XRP -> IOU");
1527 using namespace jtx;
1539 env.
fund(
XRP(1'000), a3, g1, g2, g3);
1543 env.
trust(g1[
"XYZ"](5'000), a1);
1544 env.
trust(g3[
"ABC"](5'000), a1);
1545 env.
trust(g2[
"XYZ"](5'000), a2);
1546 env.
trust(g3[
"ABC"](5'000), a2);
1547 env.
trust(a2[
"ABC"](1'000), a3);
1548 env.
trust(g1[
"XYZ"](100'000), m1);
1549 env.
trust(g2[
"XYZ"](100'000), m1);
1550 env.
trust(g3[
"ABC"](100'000), m1);
1553 env(
pay(g1, a1, g1[
"XYZ"](3'500)));
1554 env(
pay(g3, a1, g3[
"ABC"](1'200)));
1555 env(
pay(g1, m1, g1[
"XYZ"](25'000)));
1556 env(
pay(g2, m1, g2[
"XYZ"](25'000)));
1557 env(
pay(g3, m1, g3[
"ABC"](25'000)));
1560 AMM const ammM1G1G2(env, m1, g1[
"XYZ"](1'000), g2[
"XYZ"](1'000));
1561 AMM const ammM1XrpG3(env, m1,
XRP(10'000), g3[
"ABC"](1'000));
1567 auto const& sendAmt =
XRP(10);
1569 BEAST_EXPECT(
equal(da, sendAmt));
1570 BEAST_EXPECT(st.
empty());
1576 auto const& sendAmt =
XRP(200);
1579 BEAST_EXPECT(
equal(da, sendAmt));
1580 BEAST_EXPECT(st.
empty());
1584 auto const& sendAmt = g3[
"ABC"](10);
1586 BEAST_EXPECT(
equal(da, sendAmt));
1592 auto const& sendAmt = a2[
"ABC"](1);
1594 BEAST_EXPECT(
equal(da, sendAmt));
1600 auto const& sendAmt = a3[
"ABC"](1);
1602 BEAST_EXPECT(
equal(da, sendAmt));
1611 testcase(
"Path Find: non-XRP -> XRP");
1612 using namespace jtx;
1619 env.
fund(
XRP(1'000), a1, a2, g3);
1623 env.
trust(g3[
"ABC"](1'000), a1, a2);
1624 env.
trust(g3[
"ABC"](100'000), m1);
1627 env(
pay(g3, a1, g3[
"ABC"](1'000)));
1628 env(
pay(g3, a2, g3[
"ABC"](1'000)));
1629 env(
pay(g3, m1, g3[
"ABC"](1'200)));
1632 AMM const ammM1(env, m1, g3[
"ABC"](1'000),
XRP(10'010));
1637 auto const& sendAmt =
XRP(10);
1638 std::tie(st, sa, da) =
findPaths(env, a1, a2, sendAmt, std::nullopt, a2[
"ABC"].currency);
1639 BEAST_EXPECT(
equal(da, sendAmt));
1640 BEAST_EXPECT(
equal(sa, a1[
"ABC"](1)));
1647 testcase(
"Path Find: non-XRP -> non-XRP, same currency");
1648 using namespace jtx;
1661 env.
fund(
XRP(1'000), a1, a2, a3, g1, g2, g3, g4);
1663 env.
fund(
XRP(21'000), m1, m2);
1666 env.
trust(g1[
"HKD"](2'000), a1);
1667 env.
trust(g2[
"HKD"](2'000), a2);
1668 env.
trust(g1[
"HKD"](2'000), a3);
1669 env.
trust(g1[
"HKD"](100'000), m1);
1670 env.
trust(g2[
"HKD"](100'000), m1);
1671 env.
trust(g1[
"HKD"](100'000), m2);
1672 env.
trust(g2[
"HKD"](100'000), m2);
1675 env(
pay(g1, a1, g1[
"HKD"](1'000)));
1676 env(
pay(g2, a2, g2[
"HKD"](1'000)));
1677 env(
pay(g1, a3, g1[
"HKD"](1'000)));
1678 env(
pay(g1, m1, g1[
"HKD"](1'200)));
1679 env(
pay(g2, m1, g2[
"HKD"](5'000)));
1680 env(
pay(g1, m2, g1[
"HKD"](1'200)));
1681 env(
pay(g2, m2, g2[
"HKD"](5'000)));
1684 AMM const ammM1(env, m1, g1[
"HKD"](1'010), g2[
"HKD"](1'000));
1685 AMM const ammM2XrpG2(env, m2,
XRP(10'000), g2[
"HKD"](1'010));
1686 AMM const ammM2G1Xrp(env, m2, g1[
"HKD"](1'010),
XRP(10'000));
1694 auto const& sendAmt = g1[
"HKD"](10);
1696 findPaths(env, a1, g1, sendAmt, std::nullopt, g1[
"HKD"].currency);
1697 BEAST_EXPECT(st.
empty());
1698 BEAST_EXPECT(
equal(da, sendAmt));
1699 BEAST_EXPECT(
equal(sa, a1[
"HKD"](10)));
1705 auto const& sendAmt = a1[
"HKD"](10);
1707 findPaths(env, a1, g1, sendAmt, std::nullopt, g1[
"HKD"].currency);
1708 BEAST_EXPECT(st.
empty());
1709 BEAST_EXPECT(
equal(da, sendAmt));
1710 BEAST_EXPECT(
equal(sa, a1[
"HKD"](10)));
1716 auto const& sendAmt = a3[
"HKD"](10);
1718 findPaths(env, a1, a3, sendAmt, std::nullopt, g1[
"HKD"].currency);
1719 BEAST_EXPECT(
equal(da, sendAmt));
1720 BEAST_EXPECT(
equal(sa, a1[
"HKD"](10)));
1727 auto const& sendAmt = g2[
"HKD"](10);
1729 findPaths(env, g1, g2, sendAmt, std::nullopt, g1[
"HKD"].currency);
1730 BEAST_EXPECT(
equal(da, sendAmt));
1731 BEAST_EXPECT(
equal(sa, g1[
"HKD"](10)));
1743 auto const& sendAmt = g2[
"HKD"](10);
1745 findPaths(env, a1, g2, sendAmt, std::nullopt, g1[
"HKD"].currency);
1746 BEAST_EXPECT(
equal(da, sendAmt));
1747 BEAST_EXPECT(
equal(sa, a1[
"HKD"](10)));
1760 auto const& sendAmt = a2[
"HKD"](10);
1762 findPaths(env, a1, a2, sendAmt, std::nullopt, g1[
"HKD"].currency);
1763 BEAST_EXPECT(
equal(da, sendAmt));
1764 BEAST_EXPECT(
equal(sa, a1[
"HKD"](10)));
1777 testcase(
"Path Find: non-XRP -> non-XRP, same currency");
1778 using namespace jtx;
1788 env.
fund(
XRP(1'000), a1, a2, a3, g1, g2);
1791 env.
trust(g1[
"HKD"](2'000), a1);
1792 env.
trust(g2[
"HKD"](2'000), a2);
1793 env.
trust(a2[
"HKD"](2'000), a3);
1794 env.
trust(g1[
"HKD"](100'000), m1);
1795 env.
trust(g2[
"HKD"](100'000), m1);
1798 env(
pay(g1, a1, g1[
"HKD"](1'000)));
1799 env(
pay(g2, a2, g2[
"HKD"](1'000)));
1800 env(
pay(g1, m1, g1[
"HKD"](5'000)));
1801 env(
pay(g2, m1, g2[
"HKD"](5'000)));
1804 AMM const ammM1(env, m1, g1[
"HKD"](1'010), g2[
"HKD"](1'000));
1808 auto const& sendAmt = a2[
"HKD"](10);
1811 std::tie(st, sa, da) =
findPaths(env, g1, a2, sendAmt, std::nullopt, g1[
"HKD"].currency);
1812 BEAST_EXPECT(
equal(da, sendAmt));
1813 BEAST_EXPECT(
equal(sa, g1[
"HKD"](10)));
1822 using namespace jtx;
1824 Env env(*
this, features);
1830 auto const ammXrpPool = env.
current()->fees().increment * 2;
1847 AMM const ammBob(env,
bob_, ammXrpPool,
USD(150));
1852 Txflags(tfNoRippleDirect | tfPartialPayment));
1855 BEAST_EXPECT(carolUSD >
USD(0) && carolUSD <
USD(50));
1863 using namespace jtx;
1867 Env env(*
this, features);
1879 BEAST_EXPECT(ammBob.expectBalances(
BTC(150),
USD(100), ammBob.tokens()));
1883 Env env(*
this, features);
1896 BEAST_EXPECT(ammBobBtcXrp.expectBalances(
BTC(150),
XRP(100), ammBobBtcXrp.tokens()));
1901 Env env(*
this, features);
1913 BEAST_EXPECT(ammBob.expectBalances(
XRP(150),
USD(100), ammBob.tokens()));
1917 Env env(*
this, features);
1929 BEAST_EXPECT(ammBob.expectBalances(
USD(150),
XRP(100), ammBob.tokens()));
1933 Env env(*
this, features);
1982 Env env(*
this, features);
2015 auto const flowResult = [&] {
2020 auto const ipe = [](
Issue const& iss) {
2052 BEAST_EXPECT(flowResult.removableOffers.size() == 1);
2054 if (flowResult.removableOffers.empty())
2057 for (
auto const& o : flowResult.removableOffers)
2081 Env env(*
this, features);
2101 Txflags(tfNoRippleDirect | tfPartialPayment));
2104 BEAST_EXPECT(ammBob.expectBalances(
USD(8.4),
XRPAmount{20}, ammBob.tokens()));
2112 using namespace jtx;
2116 Env env(*
this, features);
2127 Txflags(tfNoRippleDirect | tfPartialPayment));
2133 BEAST_EXPECT(
amm.expectBalances(
2143 Env env(*
this, features);
2163 Txflags(tfNoRippleDirect | tfPartialPayment));
2176 BEAST_EXPECT(
amm.expectBalances(
2184 Env env(*
this, features);
2197 AMM const amm2(env, ed,
EUR(1'000),
USD(1'000));
2202 Txflags(tfNoRippleDirect | tfPartialPayment));
2223 Env env(*
this, features);
2234 BEAST_EXPECT(
amm.expectBalances(
USD(1'100),
EUR(1'000),
amm.tokens()));
2242 Env env(*
this, features);
2255 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2264 BEAST_EXPECT(
amm.expectBalances(
2265 STAmount{GBP, UINT64_C(1'142'857142857143), -12},
USD(875),
amm.tokens()));
2273 Env env(*
this, features);
2286 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2295 BEAST_EXPECT(
amm.expectBalances(
2296 STAmount{GBP, UINT64_C(1'024'000000000001), -12},
USD(1'171.875),
amm.tokens()));
2304 Env env(*
this, features);
2326 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2340 STAmount{
GBP, UINT64_C(1'470'42105263158), -11}));
2347 STAmount{
EUR, UINT64_C(929'57894736842), -11}}}));
2350 BEAST_EXPECT(
amm.expectBalances(
2351 STAmount{EUR, UINT64_C(1'056'336842105264), -12},
2352 STAmount{USD, UINT64_C(1'325'334821428571), -12},
2361 Env env(*
this, features);
2383 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2396 BEAST_EXPECT(
amm.expectBalances(
2397 STAmount{GBP, UINT64_C(1'056'336842105264), -12},
2398 STAmount{EUR, UINT64_C(946'6677295918366), -13},
2406 STAmount{
EUR, UINT64_C(1'442'665816326531), -12}));
2413 STAmount{
USD, UINT64_C(1'340'267857142857), -12}}}));
2421 Env env(*
this, features);
2434 AMM const amm2(env, ed,
EUR(1'000),
USD(1'400));
2441 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2451 STAmount{GBP, UINT64_C(1'086'024691358027), -12},
2452 STAmount{EUR, UINT64_C(920'7893779556188), -13},
2457 STAmount{EUR, UINT64_C(1'063'368497635505), -12},
2458 STAmount{USD, UINT64_C(1'316'570881226053), -12},
2467 Env env(*
this, features);
2482 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2487 STAmount{GBP, UINT64_C(1'108'148148148151), -12},
2488 STAmount{EUR, UINT64_C(902'4064171122975), -13},
2493 STAmount{EUR, UINT64_C(1'078'074866310162), -12},
2494 STAmount{USD, UINT64_C(1'298'611111111111), -12},
2510 using namespace jtx;
2523 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
2525 BEAST_EXPECT(ammBob.expectBalances(
XRP(1'050),
USD(1'000), ammBob.tokens()));
2536 using namespace jtx;
2574 auto const jpy =
gw_[
"JPY"];
2582 AMM const ammAliceXrpJpy(env,
alice_,
XRP(100), jpy(100));
2597 using namespace jtx;
2598 Env env(*
this, features);
2599 auto const dan =
Account(
"dan");
2600 auto const ed =
Account(
"ed");
2611 AMM const ammEd(env, ed,
XRP(9),
USD(11));
2637 testcase(
"Convert all of an asset using DeliverMin");
2639 using namespace jtx;
2642 Env env(*
this, features);
2676 Env env(*
this, features);
2690 Env env(*
this, features);
2711 auto const dan =
Account(
"dan");
2712 Env env(*
this, features);
2720 AMM const ammDan(env, dan,
XRP(1'000),
USD(1'100));
2730 STAmount{USD, UINT64_C(999'99999909091), -11},
2740 using namespace jtx;
2746 Env env(*
this, features);
2766 env(
fset(becky, asfDepositAuth));
2782 using namespace jtx;
2803 auto const failedIouPayments = [
this, &env] {
2825 failedIouPayments();
2842 failedIouPayments();
2849 failedIouPayments();
2872 Env env(*
this, features);
2878 env.
fund(
XRP(1'000), g1, alice, bob);
2881 env.
trust(g1[
"USD"](100), bob);
2882 env.
trust(g1[
"USD"](205), alice);
2885 env(
pay(g1, bob, g1[
"USD"](10)));
2886 env(
pay(g1, alice, g1[
"USD"](205)));
2889 AMM const ammAlice(env, alice,
XRP(500), g1[
"USD"](105));
2895 BEAST_EXPECT(lines[jss::lines][0u][jss::account] == g1.
human());
2896 BEAST_EXPECT(lines[jss::lines][0u][jss::limit] ==
"100");
2897 BEAST_EXPECT(lines[jss::lines][0u][jss::balance] ==
"10");
2904 BEAST_EXPECT(lines[jss::lines][0u][jss::account] == g1.
human());
2905 BEAST_EXPECT(lines[jss::lines][0u][jss::limit] ==
"205");
2907 BEAST_EXPECT(lines[jss::lines][0u][jss::balance] ==
"100");
2912 env(
pay(alice, bob, g1[
"USD"](1)));
2915 env(
pay(bob, alice, g1[
"USD"](1)));
2920 env(
trust(g1, bob[
"USD"](0), tfSetFreeze));
2926 env(
offer(bob, g1[
"USD"](5),
XRP(25)));
2936 env(
pay(alice, bob, g1[
"USD"](1)));
2947 for (
auto const& it : lines[jss::lines])
2949 if (it[jss::account] == bob.
human())
2955 if (!BEAST_EXPECT(bobLine))
2957 BEAST_EXPECT(bobLine[jss::freeze] ==
true);
2958 BEAST_EXPECT(bobLine[jss::balance] ==
"-16");
2965 for (
auto const& it : lines[jss::lines])
2967 if (it[jss::account] == g1.
human())
2973 if (!BEAST_EXPECT(g1Line))
2975 BEAST_EXPECT(g1Line[jss::freeze_peer] ==
true);
2976 BEAST_EXPECT(g1Line[jss::balance] ==
"16");
2982 env(
trust(g1, bob[
"USD"](0), tfClearFreeze));
2987 auto ff = affected[1u][sfModifiedNode.fieldName][sfFinalFields.fieldName];
2989 ff[sfLowLimit.fieldName] ==
2991 BEAST_EXPECT(!(ff[jss::Flags].asUInt() & lsfLowFreeze));
2992 BEAST_EXPECT(!(ff[jss::Flags].asUInt() & lsfHighFreeze));
3003 Env env(*
this, features);
3013 env.
fund(
XRP(20'000), a2, a3, a4);
3016 env.
trust(g1[
"USD"](1'200), a1);
3017 env.
trust(g1[
"USD"](200), a2);
3018 env.
trust(g1[
"BTC"](100), a3);
3019 env.
trust(g1[
"BTC"](100), a4);
3022 env(
pay(g1, a1, g1[
"USD"](1'000)));
3023 env(
pay(g1, a2, g1[
"USD"](100)));
3024 env(
pay(g1, a3, g1[
"BTC"](100)));
3025 env(
pay(g1, a4, g1[
"BTC"](100)));
3028 AMM const ammG1(env, g1,
XRP(10'000), g1[
"USD"](100));
3037 auto offers = env.
rpc(
3038 "book_offers",
std::string(
"USD/") + g1.
human(),
"XRP")[jss::result][jss::offers];
3042 for (
auto const&
offer : offers)
3050 "book_offers",
"XRP",
std::string(
"USD/") + g1.
human())[jss::result][jss::offers];
3054 for (
auto const&
offer : offers)
3065 AMM ammA3(env, a3, g1[
"BTC"](1),
XRP(1));
3071 env(
pay(g1, a2, g1[
"USD"](1)));
3074 env(
pay(a2, g1, g1[
"USD"](1)));
3077 env(
pay(a2, a1, g1[
"USD"](1)));
3080 env(
pay(a1, a2, g1[
"USD"](1)));
3089 env(
fset(g1, asfGlobalFreeze));
3103 auto offers = env.
rpc(
3104 "book_offers",
"XRP",
std::string(
"USD/") + g1.
human())[jss::result][jss::offers];
3109 "book_offers",
std::string(
"USD/") + g1.
human(),
"XRP")[jss::result][jss::offers];
3117 env(
pay(g1, a2, g1[
"USD"](1)));
3120 env(
pay(a2, g1, g1[
"USD"](1)));
3130 testcase(
"Offers for Frozen Trust Lines");
3133 Env env(*
this, features);
3140 env.
fund(
XRP(2'000), g1, a3, a4);
3144 env.
trust(g1[
"USD"](1'000), a2);
3145 env.
trust(g1[
"USD"](2'000), a3);
3146 env.
trust(g1[
"USD"](2'001), a4);
3149 env(
pay(g1, a3, g1[
"USD"](2'000)));
3150 env(
pay(g1, a4, g1[
"USD"](2'001)));
3153 AMM const ammA3(env, a3,
XRP(1'000), g1[
"USD"](1'001));
3163 env(
offer(a4,
XRP(999), g1[
"USD"](999)));
3170 env(
trust(g1, a3am, tfSetFreeze));
3172 BEAST_EXPECT(info[jss::amm][jss::asset2_frozen].asBool());
3183 env(
trust(g1, a4[
"USD"](0), tfSetFreeze));
3187 env(
offer(a2, g1[
"USD"](999),
XRP(999)));
3199 testcase(
"Multisign AMM Transactions");
3201 using namespace jtx;
3202 Env env{*
this, features};
3207 fund(env,
gw_, {alice, becky, zelda},
XRP(20'000), {
USD(20'000)});
3211 env(
regkey(alice, alie));
3212 env(
fset(alice, asfDisableMaster),
Sig(alice));
3215 env(
signers(alice, 2, {{becky, 1}, {bogie, 1}}),
Sig(alie));
3219 Msig const ms{becky, bogie};
3236 ammAlice.
deposit(alice, 1'000'000);
3239 ammAlice.
withdraw(alice, 1'000'000);
3242 ammAlice.
vote({}, 1'000);
3245 env(ammAlice.
bid({.account = alice, .bidMin = 100}), ms).
close();
3256 using namespace jtx;
3260 Env env(*
this, features);
3278 using namespace jtx;
3282 Env env(*
this, features);
3283 auto const bobUsd =
bob_[
"USD"];
3284 auto const bobEur =
bob_[
"EUR"];
3291 AMM const ammBobXrpUsd(env,
bob_,
XRP(100), bobUsd(100));
3294 AMM const ammBobUsdEur(env,
bob_, bobUsd(100), bobEur(100));
3309 Txflags(tfNoRippleDirect | tfPartialPayment),
3314 Env env(*
this, features);
3328 Env env(*
this, features);
3347 using namespace jtx;
3349 auto const cny =
gw_[
"CNY"];
3352 Env env(*
this, features);
3373 Env env(*
this, features);
3387 AMM const ammBobEurCny(env,
bob_,
EUR(100), cny(100));
3413 using namespace jtx;
3426 using namespace jtx;
3434 using namespace jtx;
A generic endpoint for log messages.
TestcaseT testcase
Memberspace for declaring test cases.
Floating point representation of amounts with high dynamic range.
A currency issued by an account.
Sets the new scale and restores the old scale when it leaves scope.
bool modify(ModifyType const &f)
Modify the open ledger.
Writable ledger view that accumulates state and tx changes.
A wrapper which makes credits unavailable to balances.
bool pushBack(STPath const &e)
pushBack adds a path to the set.
Discardable, editable view to a ledger.
virtual beast::Journal getJournal(std::string const &name)=0
virtual OpenLedger & getOpenLedger()=0
SLE::pointer peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
Tests of AMM that use offers too.
void testRippleState(FeatureBitset features)
void testOfferFeesConsumeFunds(FeatureBitset features)
void testTransferRateOffer(FeatureBitset features)
void testCrossCurrencyBridged(FeatureBitset features)
void testOffersWhenFrozen(FeatureBitset features)
void testDirectToDirectPath(FeatureBitset features)
void testTransferRateNoOwnerFee(FeatureBitset features)
void testFillModes(FeatureBitset features)
void pathFindConsumeAll()
void testSellFlagExceedLimit(FeatureBitset features)
void testRmFundedOffer(FeatureBitset features)
void viaOffersViaGateway()
void testSelfIssueOffer(FeatureBitset features)
void testStepLimit(FeatureBitset features)
void testSellFlagBasic(FeatureBitset features)
void testTxMultisign(FeatureBitset features)
void testEnforceNoRipple(FeatureBitset features)
void testGlobalFreeze(FeatureBitset features)
void testLoop(FeatureBitset features)
void testBadPathAssert(FeatureBitset features)
void run() override
Runs the suite.
void testBridgedCross(FeatureBitset features)
void testCrossingLimits()
void testMissingAuth(FeatureBitset features)
void testCurrencyConversionEntire(FeatureBitset features)
void testCurrencyConversionInParts(FeatureBitset features)
void testToStrand(FeatureBitset features)
NumberMantissaScaleGuard const sg_
void testCrossCurrencyEndXRP(FeatureBitset features)
void testFalseDry(FeatureBitset features)
void testOfferCreateThenCross(FeatureBitset features)
void testOfferCrossWithXRP(FeatureBitset features)
void testBookStep(FeatureBitset features)
void testConvertAllOfAnAsset(FeatureBitset features)
void testPseudoAccountRequireAuth(FeatureBitset features)
void testGatewayCrossCurrency(FeatureBitset features)
void testSellWithFillOrKill(FeatureBitset features)
void testRequireAuth(FeatureBitset features)
void testPayment(FeatureBitset features)
void testRIPD1373(FeatureBitset features)
void testOfferCrossWithLimitOverride(FeatureBitset features)
void testCrossCurrencyStartXRP(FeatureBitset features)
TestPath & pushBack(Issue const &iss)
jtx::Account const alice_
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
static FeatureBitset testableAmendments()
jtx::Account const carol_
static XRPAmount reserve(jtx::Env &env, std::uint32_t count)
static XRPAmount ammCrtFee(jtx::Env &env)
Convenience class to test AMM functionality.
bool expectTradingFee(std::uint16_t fee) const
json::Value bid(BidArg const &arg)
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
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)
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)
json::Value ammRpcInfo(std::optional< AccountID > const &account=std::nullopt, std::optional< std::string > const &ledgerIndex=std::nullopt, std::optional< Asset > const &asset1=std::nullopt, std::optional< Asset > const &asset2=std::nullopt, std::optional< AccountID > const &ammAccount=std::nullopt, bool ignoreParams=false, unsigned apiVersion=rpc::kApiInvalidVersion) const
Send amm_info RPC command.
AccountID const & ammAccount() const
void vote(std::optional< Account > const &account, std::uint32_t feeVal, std::optional< std::uint32_t > const &flags=std::nullopt, std::optional< jtx::Seq > const &seq=std::nullopt, std::optional< std::pair< Asset, Asset > > const &assets=std::nullopt, std::optional< Ter > const &ter=std::nullopt)
bool expectBalances(STAmount const &asset1, STAmount const &asset2, IOUAmount const &lpt, std::optional< AccountID > const &account=std::nullopt) const
Verify the AMM balances.
Immutable cryptographic account descriptor.
std::string const & human() const
Returns the human readable public key.
Sets the DeliverMin on a JTx.
A transaction testing environment.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
SLE::const_pointer le(Account const &account) const
Return an account root.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
std::shared_ptr< STObject const > meta()
Return metadata for the last JTx.
void require(Args const &... args)
Check a set of requirements.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Set a multisignature on a JTx.
Match clear account flags.
Match the number of items in the account's owner directory.
Set Paths, SendMax on a JTx.
Sets the QualityIn on a trust JTx.
Sets the QualityOut on a trust JTx as a percentage.
Check a set of conditions.
Sets the SendMax on a JTx.
Set the regular signature on a JTx.
Set the expected result code for a JTx The test will fail if the code doesn't match.
@ Array
array value (ordered list)
Keylet offer(AccountID const &id, SeqProxy const &seq) noexcept
An offer from an account.
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
void nOffers(Env &env, std::size_t n, Account const &account, STAmount const &in, STAmount const &out)
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
std::vector< STAmount > fund(jtx::Env &env, jtx::Account const &gw, std::vector< jtx::Account > const &accounts, std::vector< STAmount > const &amts, Fund how)
bool expectLedgerEntryRoot(Env &env, Account const &acct, STAmount const &expectedValue)
json::Value regkey(Account const &account, DisabledT)
Disable the regular key.
PrettyAmount xrpMinusFee(Env const &env, std::int64_t xrpAmount)
STPathElement allPathElements(AccountID const &a, Asset const &asset)
bool expectHolding(Env const &env, AccountID const &account, STAmount const &value, bool defaultLimits)
XrpT const XRP
Converts to XRP Issue or STAmount.
std::tuple< STPathSet, STAmount, STAmount > findPaths(jtx::Env &env, jtx::Account const &src, jtx::Account const &dst, STAmount const &saDstAmount, std::optional< STAmount > const &saSendMax, std::optional< PathAsset > const &srcAsset, std::optional< AccountID > const &srcIssuer, std::optional< UInt256 > const &domain)
json::Value noop(Account const &account)
The null transaction.
bool expectOffers(Env const &env, AccountID const &account, std::uint16_t size, std::vector< Amounts > const &toMatch)
bool same(STPathSet const &st1, Args const &... args)
XRPAmount txFee(Env const &env, std::uint16_t n)
json::Value fclear(Account const &account, std::uint32_t off)
Remove account flag.
FeatureBitset testableAmendments()
OwnerCount< ltOFFER > Offers
Match the number of offers in the account's owner directory.
STPathElement cpe(PathAsset const &pa)
STPathElement ipe(Asset const &asset)
json::Value ledgerEntryState(Env &env, Account const &acctA, Account const &acctB, std::string const ¤cy)
std::array< Account, 1+sizeof...(Args)> noripple(Account const &account, Args const &... args)
Designate accounts as no-ripple in Env::fund.
bool equal(STAmount const &sa1, STAmount const &sa2)
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
json::Value ledgerEntryRoot(Env &env, Account const &acct)
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
STPath stpath(Args const &... args)
json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
json::Value getAccountLines(Env &env, AccountID const &acctId)
json::Value signers(Account const &account, std::uint32_t quorum, std::vector< Signer > const &v)
bool checkArraySize(json::Value const &val, unsigned int size)
json::Value getAccountOffers(Env &env, AccountID const &acct, bool current)
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
bool isOffer(jtx::Env const &env, jtx::Account const &account, STAmount const &takerPays, STAmount const &takerGets)
An offer exists.
constexpr XRPAmount
Convert XRP to drops (integral types).
BEAST_DEFINE_TESTSUITE_PRIO(AccountDelete, app, xrpl, 2)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
TER offerDelete(ApplyView &view, SLE::Ref sle, beast::Journal j)
Delete an offer.
TAmounts< STAmount, STAmount > Amounts
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Currency const & xrpCurrency()
XRP currency.
std::string to_string(BaseUInt< Bits, Tag > const &a)
StrandResult< TInAmt, TOutAmt > flow(PaymentSandbox const &baseView, Strand const &strand, std::optional< TInAmt > const &maxIn, TOutAmt const &out, beast::Journal j)
Request out amount from a strand.
TERSubset< CanCvtToTER > TER
AccountID const & xrpAccount()
Compute AccountID from public key.
Represents an XRP, IOU, or MPT quantity This customizes the string conversion and supports XRP conver...
STAmount const & value() const