1#include <test/jtx/AMM.h>
2#include <test/jtx/AMMTest.h>
3#include <test/jtx/CaptureLogs.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/TestHelpers.h>
6#include <test/jtx/amount.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/offer.h>
12#include <test/jtx/paths.h>
13#include <test/jtx/pay.h>
14#include <test/jtx/paychan.h>
15#include <test/jtx/rate.h>
16#include <test/jtx/sendmax.h>
17#include <test/jtx/seq.h>
18#include <test/jtx/sig.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>
24#include <xrpld/core/Config.h>
26#include <xrpl/basics/Number.h>
27#include <xrpl/basics/base_uint.h>
28#include <xrpl/basics/chrono.h>
29#include <xrpl/basics/safe_cast.h>
30#include <xrpl/beast/unit_test/suite.h>
31#include <xrpl/beast/utility/Journal.h>
32#include <xrpl/json/json_value.h>
33#include <xrpl/ledger/ApplyView.h>
34#include <xrpl/ledger/helpers/AMMHelpers.h>
35#include <xrpl/ledger/helpers/AccountRootHelpers.h>
36#include <xrpl/protocol/AMMCore.h>
37#include <xrpl/protocol/AccountID.h>
38#include <xrpl/protocol/AmountConversions.h>
39#include <xrpl/protocol/Feature.h>
40#include <xrpl/protocol/Indexes.h>
41#include <xrpl/protocol/Issue.h>
42#include <xrpl/protocol/LedgerFormats.h>
43#include <xrpl/protocol/Protocol.h>
44#include <xrpl/protocol/Quality.h>
45#include <xrpl/protocol/Rules.h>
46#include <xrpl/protocol/SField.h>
47#include <xrpl/protocol/STAmount.h>
48#include <xrpl/protocol/TER.h>
49#include <xrpl/protocol/TxFlags.h>
50#include <xrpl/protocol/UintTypes.h>
51#include <xrpl/protocol/jss.h>
52#include <xrpl/tx/Transactor.h>
53#include <xrpl/tx/transactors/dex/AMMBid.h>
55#include <boost/regex/v5/regex.hpp>
56#include <boost/regex/v5/regex_search.hpp>
109 [&](
AMM& ammAlice,
Env&) {
121 [&](
AMM& ammAlice,
Env&) {
132 [&](
AMM& ammAlice,
Env&) {
135 {{
USD(20'000),
BTC(0.5)}});
155 env(
fset(
gw_, asfRequireAuth));
173 env(
fset(
gw_, asfGlobalFreeze));
184 BEAST_EXPECT(
amm.expectTradingFee(1'000));
185 BEAST_EXPECT(
amm.expectAuctionSlot(100, 0,
IOUAmount{0}));
211 BEAST_EXPECT(!ammAlice.ammExists());
219 BEAST_EXPECT(!ammAlice.ammExists());
227 BEAST_EXPECT(!ammAlice.ammExists());
241 BEAST_EXPECT(!ammAlice.ammExists());
249 BEAST_EXPECT(!ammAlice.ammExists());
257 BEAST_EXPECT(!ammAlice.ammExists());
276 BEAST_EXPECT(!ammAlice.ammExists());
300 BEAST_EXPECT(!ammAlice.ammExists());
328 env(
fset(
gw_, asfRequireAuth));
341 env(
fset(
gw_, asfGlobalFreeze));
345 for (
auto const& account : {
alice_,
gw_})
348 BEAST_EXPECT(!
amm.ammExists());
370 auto const startingXrp =
XRP(1'000) +
reserve(env, 3) + env.
current()->fees().base * 4;
385 auto const startingXrp =
reserve(env, 4) + env.
current()->fees().base * 5;
422 AMM const ammAMMToken(
428 AMM const ammAMMToken1(
440 auto const token1 = ammAlice.
lptIssue();
441 auto const token2 = ammAlice1.lptIssue();
442 AMM const ammAMMTokens(
462 env(
fclear(gw1, asfDefaultRipple));
465 auto const usD1 = gw1[
"USD"];
493 {tfLPToken, 1'000, std::nullopt,
USD(100), std::nullopt, std::nullopt},
494 {tfLPToken, 1'000,
XRP(100), std::nullopt, std::nullopt, std::nullopt},
495 {tfLPToken, 1'000, std::nullopt, std::nullopt,
STAmount{
USD, 1, -1}, std::nullopt},
504 {tfLPToken, 1'000,
XRP(100), std::nullopt,
STAmount{
USD, 1, -1}, std::nullopt},
505 {tfLPToken, 1'000, std::nullopt, std::nullopt, std::nullopt, 1'000},
506 {tfSingleAsset, 1'000, std::nullopt, std::nullopt, std::nullopt, std::nullopt},
507 {tfSingleAsset, std::nullopt, std::nullopt,
USD(100), std::nullopt, std::nullopt},
516 {tfSingleAsset, std::nullopt,
USD(100), std::nullopt, std::nullopt, 1'000},
517 {tfTwoAsset, 1'000, std::nullopt, std::nullopt, std::nullopt, std::nullopt},
518 {tfTwoAsset, std::nullopt,
XRP(100),
USD(100),
STAmount{
USD, 1, -1}, std::nullopt},
519 {tfTwoAsset, std::nullopt,
XRP(100), std::nullopt, std::nullopt, std::nullopt},
520 {tfTwoAsset, std::nullopt,
XRP(100),
USD(100), std::nullopt, 1'000},
529 {tfOneAssetLPToken, 1'000, std::nullopt, std::nullopt, std::nullopt, std::nullopt},
530 {tfOneAssetLPToken, std::nullopt,
XRP(100),
USD(100), std::nullopt, std::nullopt},
539 {tfOneAssetLPToken, 1'000,
XRP(100), std::nullopt, std::nullopt, 1'000},
540 {tfLimitLPToken, 1'000, std::nullopt, std::nullopt, std::nullopt, std::nullopt},
541 {tfLimitLPToken, 1'000,
USD(100), std::nullopt, std::nullopt, std::nullopt},
542 {tfLimitLPToken, std::nullopt,
USD(100),
XRP(100), std::nullopt, std::nullopt},
543 {tfLimitLPToken, std::nullopt,
XRP(100), std::nullopt,
STAmount{
USD, 1, -1}, 1'000},
544 {tfTwoAssetIfEmpty, std::nullopt, std::nullopt, std::nullopt, std::nullopt, 1'000},
545 {tfTwoAssetIfEmpty, 1'000, std::nullopt, std::nullopt, std::nullopt, std::nullopt},
555 tfTwoAssetIfEmpty | tfLPToken,
563 for (
auto const& it : invalidOptions)
581 jv[jss::Account] =
alice_.human();
582 jv[jss::TransactionType] = jss::AMMDeposit;
596 jv[jss::Account] =
alice_.human();
597 jv[jss::TransactionType] = jss::AMMDeposit;
601 jv[jss::Flags] = tfLPToken;
740 [&](
AMM& ammAlice,
Env& env) {
741 auto const enabledV13 = env.
current()->rules().enabled(fixAMMv1_3);
761 {features, features - fixAMMv1_3});
771 [&](
AMM& ammAlice,
Env& env) {
772 env(
fset(
gw_, asfGlobalFreeze));
773 auto const freezeBlocksAll =
774 features[featureAMMClawback] || features[fixCleanup3_3_0];
775 if (!freezeBlocksAll)
789 for (
auto const& account : {
carol_,
gw_})
799 account, 1'000'000, std::nullopt, std::nullopt,
Ter(
tecFROZEN));
811 [&](
AMM& ammAlice,
Env& env) {
814 auto const freezeBlocksAll =
815 features[featureAMMClawback] || features[fixCleanup3_3_0];
816 if (!freezeBlocksAll)
843 if (!features[fixCleanup3_3_0])
863 [&](
AMM& ammAlice,
Env& env) {
882 {{
USD(20'000),
BTC(0.5)}});
886 Env env(*
this, features);
888 env(
fset(
gw_, asfRequireAuth));
901 if (features[featureAMMClawback] || features[fixCleanup3_3_0])
973 auto const startingXrp =
reserve(env, 4) + env.
current()->fees().base * 4;
1007 auto const startingXrp =
reserve(env, 4) + env.
current()->fees().base * 4;
1175 {.pool = {{
USD(1'000'000),
XRP(1'000'000)}}, .features = {features - fixAMMv1_3}});
1218 using namespace jtx;
1222 auto const baseFee = env.
current()->fees().base;
1235 for (
Number const& deltaLPTokens :
1236 {
Number{UINT64_C(100000'0000000009), -10},
Number{UINT64_C(100000'0000000001), -10}})
1241 IOUAmount const newLPTokens{deltaLPTokens};
1253 BEAST_EXPECT((finalLPToken - initLPToken ==
IOUAmount{1, 5}));
1254 BEAST_EXPECT(finalLPToken - initLPToken < deltaLPTokens);
1258 Number const fr = deltaLPTokens / 1e7;
1262 Number const deltaXRP = fr * 1e10;
1263 Number const deltaUSD = fr * 1e4;
1271 XRP(10'000) + depositXRP,
1272 USD(10'000) + depositUSD,
1387 STAmount{USD, UINT64_C(10'000'000001), -6},
1407 for (
auto const& feat : {all, all - fixAMMv1_3})
1409 Env env(*
this, feat);
1500 using namespace jtx;
1503 [&](
AMM& ammAlice,
Env& env) {
1505 .asset1Out =
XRP(100),
1513 [&](
AMM& ammAlice,
Env& env) {
1515 .asset1Out =
USD(100),
1526 env(
fset(
gw_, asfRequireAuth));
1539 .asset1Out =
USD(100),
1575 NotTEC>>
const invalidOptions = {
1590 tfSingleAsset | tfTwoAsset,
1593 {1'000, std::nullopt, std::nullopt, std::nullopt, tfWithdrawAll,
temMALFORMED},
1599 tfWithdrawAll | tfLPToken,
1602 {std::nullopt, std::nullopt,
USD(100), std::nullopt, tfWithdrawAll,
temMALFORMED},
1608 tfWithdrawAll | tfOneAssetWithdrawAll,
1611 {std::nullopt,
USD(100), std::nullopt, std::nullopt, tfWithdrawAll,
temMALFORMED},
1617 tfOneAssetWithdrawAll,
1620 {1'000, std::nullopt,
USD(100), std::nullopt, std::nullopt,
temMALFORMED},
1642 for (
auto const& it : invalidOptions)
1653 Ter(std::get<5>(it)));
1685 tfOneAssetWithdrawAll,
1734 STAmount{
USD, UINT64_C(9'999'9999999999999), -13},
1741 [&](
AMM& ammAlice,
Env& env) {
1751 alice_,
IOUAmount{9'999'999'9999, -4}, std::nullopt, std::nullopt, err);
1761 {all, all - fixAMMv1_3});
1764 [&](
AMM& ammAlice,
Env& env) {
1773 alice_,
IOUAmount{9'999'999'999999999, -9}, std::nullopt, std::nullopt, err);
1783 {all, all - fixAMMv1_3});
1793 [&](
AMM& ammAlice,
Env& env) {
1794 auto const fix330 = env.
current()->rules().enabled(fixCleanup3_3_0);
1795 ammAlice.
deposit({.account =
gw_, .asset1In =
USD(1'000), .asset2In =
XRP(1'000)});
1796 env(
fset(
gw_, asfGlobalFreeze));
1799 for (
auto const& account : {
alice_,
gw_})
1804 auto const frozenErr =
1806 ammAlice.
withdraw(account,
USD(100), std::nullopt, std::nullopt, frozenErr);
1807 ammAlice.
withdraw(account, 1'000, std::nullopt, std::nullopt, frozenErr);
1817 [&](
AMM& ammAlice,
Env& env) {
1818 auto const fix330 = env.
current()->rules().enabled(fixCleanup3_3_0);
1826 ammAlice.
withdraw(
alice_, 1'000, std::nullopt, std::nullopt, indivFreezeErr);
1827 ammAlice.
withdraw(
alice_,
USD(100), std::nullopt, std::nullopt, indivFreezeErr);
1861 [&](
AMM& ammAlice,
Env& env) {
1870 {all, all - fixAMMv1_3});
1890 [&](
AMM& ammAlice,
Env&) {
1898 [&](
AMM& ammAlice,
Env&) {
1920 [&](
AMM& ammAlice,
Env& env) {
1926 auto const err = [&] {
1929 if (env.
enabled(fixCleanup3_3_0))
1940 {.features = {all, all - fixAMMv1_3, all - fixCleanup3_3_0}, .noLog =
true});
1986 using namespace jtx;
1991 auto const baseFee = env.
current()->fees().base.drops();
2036 [&](
AMM& ammAlice,
Env& env) {
2052 {all, all - fixAMMv1_3});
2100 [&](
AMM& ammAlice,
Env& env) {
2125 {all, all - fixAMMv1_3});
2167 [&](
AMM& ammAlice,
Env& env) {
2188 {.features = {all, all - fixAMMv1_3}, .noLog =
true});
2192 [&](
AMM& ammAlice,
Env& env) {
2214 {all, all - fixAMMv1_3});
2247 STAmount{USD, UINT64_C(9'999'999999), -6},
2251 [&](
AMM& ammAlice,
Env& env) {
2268 {all, all - fixAMMv1_3});
2317 [&](
AMM& ammAlice,
Env& env) {
2318 auto const err = env.
enabled(fixCleanup3_3_0) || env.
enabled(fixCleanup3_4_0)
2324 .asset1Out =
USD(0),
2332 {all - fixCleanup3_3_0 - fixCleanup3_4_0, all - fixCleanup3_4_0, all});
2339 using namespace jtx;
2353 std::nullopt, 1'001, std::nullopt, std::nullopt, std::nullopt,
Ter(
temBAD_FEE));
2386 using namespace jtx;
2391 ammAlice.
vote({}, 1'000);
2397 auto vote = [&](
AMM& ammAlice,
2400 int fundUSD = 100'000,
2410 fund(env,
gw_, {a}, {
USD(fundUSD)}, Fund::Acct);
2412 ammAlice.
vote(a, 50 * (i + 1));
2419 [&](
AMM& ammAlice,
Env& env) {
2420 for (
int i = 0; i < 7; ++i)
2421 vote(ammAlice, env, i, 10'000);
2432 for (
int i = 0; i < 7; ++i)
2433 vote(ammAlice, env, i);
2436 ammAlice.
vote(a, 450);
2443 for (
int i = 0; i < 7; ++i)
2444 vote(ammAlice, env, i);
2446 vote(ammAlice, env, 7, 100'000, 20'000'000);
2453 for (
int i = 7; i > 0; --i)
2454 vote(ammAlice, env, i);
2456 vote(ammAlice, env, 0, 100'000, 20'000'000);
2465 for (
int i = 0; i < 7; ++i)
2466 vote(ammAlice, env, i, 100'000, 10'000'000, &accounts);
2468 for (
int i = 0; i < 7; ++i)
2482 for (
int i = 0; i < 7; ++i)
2483 vote(ammAlice, env, i, 100'000, 10'000'000, &accounts);
2485 for (
int i = 0; i < 7; ++i)
2486 ammAlice.
withdraw(accounts[i], 9'000'000);
2490 auto const info = ammAlice.
ammRpcInfo()[jss::amm][jss::vote_slots];
2491 for (
auto const& entry : info)
2492 BEAST_EXPECT(entry[jss::account] !=
carol_.human());
2502 using namespace jtx;
2512 BEAST_EXPECT(
amm.expectAuctionSlot(100, 0,
IOUAmount{0}));
2519 .bidMin = 1'000'000,
2531 BEAST_EXPECT(
amm.expectAuctionSlot(100, 0,
IOUAmount{0}));
2538 .bidMin = STAmount{amm.lptIssue(), UINT64_C(999'999)},
2544 BEAST_EXPECT(
amm.expectAuctionSlot(100, 0,
IOUAmount{999'999}));
2550 BEAST_EXPECT(
Number{
amm.getLPTokensBalance(
gw_)} == 1);
2567 .flags = tfWithdrawAll,
2573 for (
auto bid : {0, -100})
2622 .authAccounts = {bob_},
2630 .assets = {{USD, GBP}},
2637 .bidMax = STAmount{USD, 100},
2639 Ter(temBAD_AMM_TOKENS));
2642 .bidMin = STAmount{USD, 100},
2644 Ter(temBAD_AMM_TOKENS));
2648 testAMM([&](AMM& ammAlice, Env& env) {
2649 ammAlice.withdrawAll(alice_);
2658 testAMM([&](AMM& ammAlice, Env& env) {
2663 env.fund(
XRP(1'000), bob_, ed, bill, scott, james);
2665 ammAlice.deposit(carol_, 1'000'000);
2669 .authAccounts = {bob_, ed, bill, scott, james},
2675 testAMM([&](AMM& ammAlice, Env& env) {
2676 fund(env, gw_, {bob_},
XRP(1'000), {USD(100)}, Fund::Acct);
2677 ammAlice.deposit(carol_, 1'000'000);
2678 ammAlice.deposit(bob_, 10);
2681 .bidMin = 1'000'001,
2683 Ter(tecAMM_INVALID_TOKENS));
2686 .bidMax = 1'000'001,
2688 Ter(tecAMM_INVALID_TOKENS));
2693 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, IOUAmount{1'000}));
2698 Ter(tecAMM_INVALID_TOKENS));
2704 fund(env, gw_, {alice_, bob_},
XRP(1'000), {USD(1'000)});
2705 AMM
amm(env, gw_,
XRP(10), USD(1'000));
2706 auto const lpIssue =
amm.lptIssue();
2707 env.trust(STAmount{lpIssue, 100}, alice_);
2708 env.trust(STAmount{lpIssue, 50}, bob_);
2709 env(
pay(gw_, alice_, STAmount{lpIssue, 100}));
2710 env(
pay(gw_, bob_, STAmount{lpIssue, 50}));
2711 env(
amm.bid({.account = alice_, .bidMin = 100}));
2718 Ter(tecAMM_FAILED));
2726 using namespace jtx;
2733 [&](
AMM& ammAlice,
Env& env) {
2735 env(ammAlice.
bid({.account = carol_, .bidMin = 110}));
2748 [&](
AMM& ammAlice,
Env& env) {
2751 env(ammAlice.
bid({.account = carol_, .bidMin = 110, .bidMax = 110}));
2756 env(ammAlice.
bid({.account = alice_, .bidMin = 180, .bidMax = 200}));
2768 [&](
AMM& ammAlice,
Env& env) {
2771 env(ammAlice.
bid({.account = carol_, .bidMin = 110}));
2777 env(ammAlice.
bid({.account = bob_}));
2787 env(ammAlice.
bid({.account = carol_, .bidMax = 600}));
2799 env(ammAlice.
bid({.account = carol_, .bidMin = 100, .bidMax = 600}));
2809 [&](
AMM& ammAlice,
Env& env) {
2814 if (!features[fixAMMv1_3])
2826 env(ammAlice.
bid({.account = carol_, .bidMin = 110})).
close();
2830 env(ammAlice.
bid({.account = bob_}));
2835 env(ammAlice.
bid({.account = carol_}));
2840 env(ammAlice.
bid({.account = bob_}));
2845 env(ammAlice.
bid({.account = carol_, .bidMin = 110})).
close();
2848 if (!features[fixAMMv1_3])
2869 [&](
AMM& ammAlice,
Env& env) {
2874 ammAlice.
deposit(ed, 1'000'000);
2876 ammAlice.
deposit(dan, 500'000);
2881 .authAccounts = {bob_, ed},
2883 auto const slotPrice =
IOUAmount{5'200};
2884 ammTokens -= slotPrice;
2886 if (!features[fixAMMv1_3])
2896 for (
int i = 0; i < 10; ++i)
2913 if (!features[fixAMMv1_3])
2916 XRP(13'000),
STAmount(
USD, UINT64_C(13'002'98282151422), -11), ammTokens));
2927 for (
int i = 0; i < 10; ++i)
2929 auto const tokens = ammAlice.
deposit(dan,
USD(100));
2935 if (!features[fixAMMv1_3])
2946 if (!features[fixAMMv1_3])
2949 XRP(13'000),
STAmount{
USD, UINT64_C(13'012'92609877023), -11}, ammTokens));
2955 STAmount{USD, UINT64_C(13'012'92609877024), -11},
2961 if (!features[fixAMMv1_3])
2964 XRP(13'000),
STAmount{
USD, UINT64_C(13'112'92609877023), -11}, ammTokens));
2970 STAmount{USD, UINT64_C(13'112'92609877024), -11},
2977 if (!features[fixAMMv1_3])
2981 STAmount{USD, UINT64_C(13'012'92609877023), -11},
2988 STAmount{USD, UINT64_C(13'012'92609877024), -11},
2997 if (!features[fixAMMv1_3])
3001 STAmount{USD, UINT64_C(13'114'03663047269), -11},
3008 STAmount{USD, UINT64_C(13'114'03663044937), -11},
3015 if (!features[fixAMMv1_3])
3019 STAmount(
USD, UINT64_C(29'399'00572620544), -11));
3022 for (
int i = 0; i < 10; ++i)
3029 if (!features[fixAMMv1_3])
3033 STAmount(
USD, UINT64_C(29'389'06197177124), -11));
3036 STAmount{USD, UINT64_C(13'123'98038490689), -11},
3043 STAmount(
USD, UINT64_C(29'389'06197177129), -11));
3046 STAmount{USD, UINT64_C(13'123'98038488352), -11},
3054 if (!features[fixAMMv1_3])
3076 [&](AMM& ammAlice, Env& env) {
3078 auto const cleanup340 = features[fixCleanup3_4_0];
3079 auto const expectedPrice = cleanup340 ? minBidPrice : IOUAmount{0};
3080 auto const expectedTokens = cleanup340
3081 ? IOUAmount{Number{ammAlice.tokens()} - Number{minBidPrice}}
3082 : ammAlice.tokens();
3084 env.close(
seconds(kTotalTimeSlotSecs + 1));
3086 env(ammAlice.bid({.account = alice_}));
3087 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, expectedPrice));
3088 BEAST_EXPECT(ammAlice.expectBalances(
XRP(10'000), USD(10'000), expectedTokens));
3090 ammAlice.vote(alice_, 1'000);
3091 BEAST_EXPECT(ammAlice.expectAuctionSlot(100, 0, expectedPrice));
3100 [&](AMM& ammAlice, Env& env) {
3102 auto const tiny = Number{STAmount::kMinValue, STAmount::kMinOffset};
3103 auto const cleanup340 = features[fixCleanup3_4_0];
3105 auto const firstPrice = cleanup340 ? minBidPrice : IOUAmount{tiny};
3106 env(ammAlice.bid({.account = alice_, .bidMin = IOUAmount{tiny}}));
3107 BEAST_EXPECT(ammAlice.expectAuctionSlot(0, 0, firstPrice));
3108 BEAST_EXPECT(ammAlice.expectBalances(
3111 cleanup340 ? IOUAmount{Number{ammAlice.tokens()} - Number{minBidPrice}}
3112 : ammAlice.tokens()));
3116 .bidMin = IOUAmount{STAmount::kMinValue, STAmount::kMinOffset},
3119 BEAST_EXPECT(ammAlice.expectAuctionSlot(
3120 0, 0, IOUAmount{Number{firstPrice} * Number{105, -2}}));
3121 BEAST_EXPECT(ammAlice.expectBalances(
3125 ? IOUAmount{Number{ammAlice.tokens()} - Number{minBidPrice} * Number{11, -1}}
3126 : ammAlice.tokens()));
3135 [&](AMM& ammAlice, Env& env) {
3138 .bidMin = IOUAmount{100},
3139 .authAccounts = {carol_},
3141 BEAST_EXPECT(ammAlice.expectAuctionSlot({carol_}));
3142 env(ammAlice.bid({.account = alice_, .bidMin = IOUAmount{100}}));
3143 BEAST_EXPECT(ammAlice.expectAuctionSlot({}));
3146 fund(env, {bob, dan},
XRP(1'000));
3149 .bidMin = IOUAmount{100},
3150 .authAccounts = {bob, dan},
3152 BEAST_EXPECT(ammAlice.expectAuctionSlot({bob, dan}));
3161 Env env(*
this, features);
3162 fund(env, gw_, {alice_, bob_},
XRP(2'000), {USD(2'000)});
3163 AMM
amm(env, gw_,
XRP(1'000), USD(1'010),
false, 1'000);
3164 auto const lpIssue =
amm.lptIssue();
3165 env.trust(STAmount{lpIssue, 500}, alice_);
3166 env.trust(STAmount{lpIssue, 50}, bob_);
3167 env(
pay(gw_, alice_, STAmount{lpIssue, 500}));
3168 env(
pay(gw_, bob_, STAmount{lpIssue, 50}));
3170 env(
amm.bid({.account = alice_, .bidMin = 500}));
3171 BEAST_EXPECT(
amm.expectAuctionSlot(100, 0, IOUAmount{500}));
3172 BEAST_EXPECT(
expectHolding(env, alice_, STAmount{lpIssue, 0}));
3175 env(
pay(alice_, bob_, USD(10)), Path(~USD), Sendmax(
XRP(11)));
3176 BEAST_EXPECT(
amm.expectBalances(
3177 XRPAmount{1'010'010'011}, USD(1'000), IOUAmount{1'004'487'562112089, -9}));
3179 env(
pay(bob_, alice_,
XRP(10)), Path(~XRP), Sendmax(USD(11)));
3180 BEAST_EXPECT(
amm.expectBalances(
3181 XRPAmount{1'000'010'011},
3182 STAmount{USD, UINT64_C(1'010'100908980811), -12},
3183 IOUAmount{1'004'487'562112089, -9}));
3188 Env env(*
this, features);
3189 auto const baseFee = env.current()->fees().base;
3191 fund(env, gw_, {alice_, bob_},
XRP(2'000), {USD(2'000)});
3192 AMM
amm(env, gw_,
XRP(1'000), USD(1'010),
false, 1'000);
3193 json::Value const tx =
amm.bid({.account = alice_, .bidMin = 500});
3196 auto jtx = env.jt(tx, Seq(1), Fee(baseFee));
3197 jtx.jv[
"TxnSignature"] =
"deadbeef";
3198 jtx.stx = env.ust(jtx);
3199 PreflightContext
const pfCtx(
3200 env.app(), *jtx.stx, env.current()->rules(), TapNone, env.journal);
3201 auto const pf = Transactor::invokePreflight<AMMBid>(pfCtx);
3206 auto jtx = env.jt(tx, Seq(1), Fee(baseFee));
3207 jtx.jv[
"Asset2"][
"currency"] =
"XRP";
3208 jtx.jv[
"Asset2"].removeMember(
"issuer");
3209 jtx.stx = env.ust(jtx);
3210 PreflightContext
const pfCtx(
3211 env.app(), *jtx.stx, env.current()->rules(), TapNone, env.journal);
3212 auto const pf = Transactor::invokePreflight<AMMBid>(pfCtx);
3213 BEAST_EXPECT(pf == temBAD_AMM_TOKENS);
3222 using namespace jtx;
3234 AMM const ammAlice(env, acct,
XRP(10),
USD(10));
3246 AMM const ammAlice(env, acct,
XRP(1'000'000),
USD(100));
3256 auto const baseFee = env.
current()->fees().base;
3267 auto const pk =
carol_.pk();
3268 auto const settleDelay = 100s;
3283 [&](
AMM& ammAlice,
Env& env) {
3320 env(
fset(
gw_, asfGlobalFreeze));
3324 Txflags(tfPartialPayment | tfNoRippleDirect),
3329 Txflags(tfPartialPayment | tfNoRippleDirect),
3341 Txflags(tfPartialPayment | tfNoRippleDirect),
3346 Txflags(tfPartialPayment | tfNoRippleDirect),
3359 Txflags(tfNoRippleDirect | tfPartialPayment),
3368 using namespace jtx;
3373 [&](
AMM& ammAlice,
Env& env) {
3388 {{
XRP(10'000),
USD(10'100)}},
3395 [&](
AMM& ammAlice,
Env& env) {
3407 {{
XRP(10'000),
USD(10'100)}},
3415 [&](
AMM& ammAlice,
Env& env) {
3427 {{
XRP(10'000),
USD(10'100)}},
3434 [&](
AMM& ammAlice,
Env& env) {
3442 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
3457 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality),
3461 {{
XRP(10'000),
USD(10'010)}},
3468 [&](
AMM& ammAlice,
Env& env) {
3479 Txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
3488 {{
XRP(10'000),
USD(10'010)}},
3495 [&](
AMM& ammAlice,
Env& env) {
3504 {{
XRP(10'000),
USD(10'000)}},
3514 Env env(*
this, features);
3528 BEAST_EXPECT(ammEurXrp.expectBalances(
3531 ammEurXrp.tokens()));
3532 BEAST_EXPECT(ammUsdEur.expectBalances(
3535 ammUsdEur.tokens()));
3538 Amounts const expectedAmounts = env.
closed()->rules().enabled(fixReducedOffersV2)
3556 [&](
AMM& ammAlice,
Env& env) {
3582 STAmount(
USD, UINT64_C(49'98750312422), -11)}}}));
3601 [&](
AMM& ammAlice,
Env& env) {
3623 {{
XRP(10'000),
USD(10'100)}},
3632 Env env(*
this, features);
3647 [&](
AMM& ammAlice,
Env& env) {
3660 {{
XRP(10'000),
USD(10'100)}},
3668 [&](
AMM& ammAlice,
Env& env) {
3684 {{
GBP(1'000),
EUR(1'100)}},
3695 BEAST_EXPECT(
amm.expectBalances(
3707 {{
XRP(1'000),
USD(500)}},
3717 BEAST_EXPECT(
amm.expectBalances(
3721 {{
XRP(1'000),
USD(500)}},
3727 [&](
AMM& ammAlice,
Env& env) {
3749 STAmount{GBP, UINT64_C(1'060'684828792832), -12},
3750 STAmount{EUR, UINT64_C(1'037'06583722134), -11},
3759 STAmount{
GBP, UINT64_C(27'06583722134028), -14}}}));
3778 {{
GBP(1'000),
EUR(1'100)}},
3791 [&](
AMM& ammAlice,
Env& env) {
3804 {{
GBP(1'000),
EUR(1'100)}},
3821 [&](
AMM& ammAlice,
Env& env) {
3824 auto const can =
gw_[
"CAN"];
3825 fund(env,
gw_, {dan}, {can(200),
GBP(200)}, Fund::Acct);
3827 fund(env,
gw_, {
bob_}, {can(195.3125)}, Fund::Acct);
3831 env(
offer(dan, can(200),
GBP(200)));
3845 {{
GBP(10'000),
EUR(10'125)}},
3852 [&](
AMM& ammAlice,
Env& env) {
3881 Env env(*
this, features);
3882 auto const eth =
gw_[
"ETH"];
3888 {
EUR(50'000),
BTC(50'000), eth(50'000),
USD(50'000)});
3894 AMM const xrpEth(env,
alice_,
XRP(10'000), eth(10'100));
3895 AMM const ethEur(env,
alice_, eth(10'900),
EUR(11'000));
3904 STAmount{eth, UINT64_C(10'073'65779244461), -11},
3907 STAmount{eth, UINT64_C(10'926'34220755539), -11},
3908 STAmount{EUR, UINT64_C(10'973'5423207872), -10},
3911 STAmount{EUR, UINT64_C(10'126'4576792128), -10},
3912 STAmount{USD, UINT64_C(9'973'93151712057), -11},
3927 BEAST_EXPECT(xrpEur.expectBalances(
XRP(10'100),
EUR(10'000), xrpEur.tokens()));
3936 Env env(*
this, features);
3937 auto const eth =
gw_[
"ETH"];
3943 {
EUR(50'000),
BTC(50'000), eth(50'000),
USD(50'000)});
3948 AMM const xrpEth(env,
alice_,
XRP(10'000), eth(10'100));
3949 AMM const ethEur(env,
alice_, eth(10'900),
EUR(11'000));
3954 BEAST_EXPECT(xrpEur.expectBalances(
3959 STAmount{EUR, UINT64_C(10'101'16096785188), -11},
3960 STAmount{BTC, UINT64_C(10'097'91426968059), -11},
3963 STAmount{BTC, UINT64_C(10'202'08573031941), -11},
USD(9'900), btcUsd.
tokens()));
3966 STAmount{eth, UINT64_C(10'017'41072777996), -11},
3969 STAmount{eth, UINT64_C(10'982'58927222004), -11},
3970 STAmount{EUR, UINT64_C(10'917'2945958102), -10},
3978 [&](
AMM& ammAlice,
Env& env) {
3982 for (
int i = 0; i < 30; ++i)
3990 Txflags(tfPartialPayment | tfNoRippleDirect));
4005 [&](
AMM& ammAlice,
Env& env) {
4009 for (
int i = 0; i < 29; ++i)
4017 Txflags(tfPartialPayment | tfNoRippleDirect));
4035 Env env(*
this, features);
4042 STAmount{USD, UINT64_C(10'049'92587049303), -11},
4054 [&](
AMM& ammAlice,
Env& env) {
4061 Txflags(tfNoRippleDirect | tfPartialPayment),
4074 using namespace jtx;
4078 auto const baseFee = env.
current()->fees().base.drops();
4079 auto const token1 = ammAlice.
lptIssue();
4100 env(ammAlice.
bid({.account = carol_, .bidMin = 100}));
4125 ammAlice1.deposit(
carol_, 1'000'000);
4126 auto const token1 = ammAlice.
lptIssue();
4127 auto const token2 = ammAlice1.lptIssue();
4146 auto const token1 = ammAlice.
lptIssue();
4178 using namespace jtx;
4181 auto const info = env.
rpc(
4185 auto const flags = info[jss::result][jss::account_data][jss::Flags].
asUInt();
4186 BEAST_EXPECT(flags == (lsfDisableMaster | lsfDefaultRipple | lsfDepositAuth));
4194 using namespace jtx;
4210 auto const tsta = a[
"TST"];
4211 auto const tstb = b[
"TST"];
4220 env.
trust(tsta(10'000), c);
4221 env.
trust(tstb(10'000), c);
4222 env(
pay(a, c, tsta(10'000)));
4223 env(
pay(b, c, tstb(10'000)));
4224 AMM const amm(env, c, tsta(5'000), tstb(5'000));
4225 auto const ammIss =
Issue(tsta.currency,
amm.ammAccount());
4235 Txflags(tfPartialPayment | tfNoRippleDirect),
4243 testcase(
"AMMAndCLOB, offer quality change");
4244 using namespace jtx;
4245 auto const gw =
Account(
"gw");
4246 auto const tst = gw[
"TST"];
4247 auto const lP1 =
Account(
"LP1");
4248 auto const lP2 =
Account(
"LP2");
4250 auto const prep = [&](
auto const& offerCb,
auto const& expectCb) {
4251 Env env(*
this, features);
4252 env.
fund(
XRP(30'000'000'000), gw);
4253 env(
offer(gw,
XRP(11'500'000'000), tst(1'000'000'000)));
4274 [&](
Env& env) {
AMM const amm(env, lP1, tst(25),
XRP(250)); },
4276 lp2TSTBalance =
getAccountLines(env, lP2, tst)[
"lines"][0u][
"balance"].asString();
4278 lp2TakerGets =
offer[
"taker_gets"].asString();
4279 lp2TakerPays =
offer[
"taker_pays"][
"value"].asString();
4293 BEAST_EXPECT(lp2TakerGets ==
offer[
"taker_gets"].asString());
4294 BEAST_EXPECT(lp2TakerPays ==
offer[
"taker_pays"][
"value"].asString());
4302 using namespace jtx;
4306 [&](
AMM& ammAlice,
Env& env) {
4328 {{
USD(1'000),
EUR(1'000)}},
4336 [&](
AMM& ammAlice,
Env& env) {
4338 auto const tokensFee =
4347 BEAST_EXPECT(tokensFee ==
IOUAmount(485'636'0611129, -7));
4348 BEAST_EXPECT(tokensNoFee ==
IOUAmount(487'644'85901109, -8));
4358 [&](
AMM& ammAlice,
Env& env) {
4360 auto const tokensFee =
4369 BEAST_EXPECT(tokensFee ==
IOUAmount(98'000'00000002, -8));
4370 BEAST_EXPECT(tokensNoFee ==
IOUAmount(98'475'81871545, -8));
4379 [&](
AMM& ammAlice,
Env& env) {
4393 {{
USD(1'000),
EUR(1'000)}},
4400 [&](
AMM& ammAlice,
Env& env) {
4402 auto const tokensFee =
4405 auto const balanceAfterWithdraw = [&] {
4406 if (!features[fixAMMv1_3])
4408 return STAmount(
USD, UINT64_C(30'443'43891402714), -11);
4411 return STAmount(
USD, UINT64_C(30'443'43891402713), -11);
4415 auto const deposit = balanceAfterWithdraw -
USD(29'000);
4421 if (!features[fixAMMv1_3])
4425 STAmount(
USD, UINT64_C(30'443'43891402716), -11));
4431 STAmount(
USD, UINT64_C(30'443'43891402713), -11));
4435 if (!features[fixAMMv1_3])
4437 BEAST_EXPECT(tokensNoFee ==
IOUAmount(746'579'80779912, -8));
4441 BEAST_EXPECT(tokensNoFee ==
IOUAmount(746'579'80779911, -8));
4443 BEAST_EXPECT(tokensFee ==
IOUAmount(750'588'23529411, -8));
4452 [&](
AMM& ammAlice,
Env& env) {
4488 {{
USD(1'000),
EUR(1'010)}},
4495 [&](
AMM& ammAlice,
Env& env) {
4521 STAmount{
USD, UINT64_C(5'025125628140703), -15}}}}));
4523 STAmount{USD, UINT64_C(1'004'97487437186), -11},
4524 STAmount{EUR, UINT64_C(1'005'025125628141), -12},
4527 {{
USD(1'000),
EUR(1'010)}},
4537 Env env(*
this, features);
4553 Env env(*
this, features);
4572 Env env(*
this, features);
4577 AMM const ammAlice(env,
alice_,
USD(1'005),
EUR(1'000),
false, 1'000);
4590 Env env(*
this, features);
4595 AMM const ammAlice(env,
alice_,
USD(1'005),
EUR(1'000),
false, 1'000);
4609 testcase(
"Adjusted Deposit/Withdraw Tokens");
4611 using namespace jtx;
4615 [&](
AMM& ammAlice,
Env& env) {
4623 Account const natalie(
"natalie");
4627 {bob, ed, paul, dan, chris, simon, ben, natalie},
4630 for (
int i = 0; i < 10; ++i)
4654 if (features[fixAMMv1_3])
4673 if (!features[fixAMMv1_3])
4676 env, natalie,
STAmount{
USD, UINT64_C(1'500'000'000000005), -9}));
4684 if (features[fixAMMv1_3])
4706 [&](
AMM& ammAlice,
Env& env) {
4714 Account const natalie(
"natalie");
4718 {bob, ed, paul, dan, chris, simon, ben, natalie},
4722 for (
int i = 0; i < 10; ++i)
4743 auto const baseFee = env.
current()->fees().base.drops();
4744 if (!features[fixAMMv1_3])
4752 auto const xrpBalance = (
XRP(2'000'000) -
txFee(env, 20)).getText();
4777 auto const xrpBalance =
XRP(2'000'000) -
txFee(env, 20) -
drops(10);
4778 auto const xrpBalanceText = xrpBalance.getText();
4805 using namespace jtx;
4828 BEAST_EXPECT(
amm.ammExists());
4838 std::nullopt, 100, std::nullopt, std::nullopt, std::nullopt,
Ter(
tecAMM_EMPTY));
4855 BEAST_EXPECT(
amm.expectBalances(
XRP(10'000),
USD(10'000),
amm.tokens()));
4856 BEAST_EXPECT(
amm.expectTradingFee(1'000));
4857 BEAST_EXPECT(
amm.expectAuctionSlot(100, 0,
IOUAmount{0}));
4862 BEAST_EXPECT(!
amm.ammExists());
4885 BEAST_EXPECT(
amm.ammExists());
4889 BEAST_EXPECT(
amm.ammExists());
4892 BEAST_EXPECT(!
amm.ammExists());
4904 using namespace jtx;
4916 using namespace jtx;
4918 amm.setClose(
false);
4919 auto const info = env.
rpc(
4926 info[jss::result][jss::account_data][jss::AMMID].asString() ==
4934 auto const affected =
4939 for (
auto const& node : affected)
4941 if (node.isMember(sfModifiedNode.fieldName) &&
4942 node[sfModifiedNode.fieldName][sfLedgerEntryType.fieldName].asString() ==
4944 node[sfModifiedNode.fieldName][sfFinalFields.fieldName][jss::Account]
4947 found = node[sfModifiedNode.fieldName][sfFinalFields.fieldName][jss::AMMID]
4952 BEAST_EXPECT(found);
4964 testcase(
"Offer/Strand Selection");
4965 using namespace jtx;
4968 auto const eth = gw1[
"ETH"];
4969 auto const can = gw1[
"CAN"];
4975 auto const prep = [&](
Env& env,
auto gwRate,
auto gw1Rate) {
4980 env(
rate(gw1, gw1Rate));
4998 for (
auto i = 0; i < 3; ++i)
5000 Env env(*
this, features);
5001 prep(env, rates.first, rates.second);
5003 if (i == 0 || i == 2)
5009 amm.emplace(env, ed,
USD(1'000), eth(1'000));
5017 amm->expectBalances(
USD(1'000), eth(1'000),
amm->tokens()));
5026 BEAST_EXPECT(q[0] > q[1]);
5028 BEAST_EXPECT(q[0] == q[2]);
5035 for (
auto i = 0; i < 3; ++i)
5037 Env env(*
this, features);
5038 prep(env, rates.first, rates.second);
5040 if (i == 0 || i == 2)
5046 amm.emplace(env, ed,
USD(1'000), eth(1'000));
5054 amm->expectBalances(
USD(1'000), eth(1'000),
amm->tokens()));
5056 if (i == 0 || i == 2)
5075 for (
auto i = 0; i < 3; ++i)
5077 Env env(*
this, features);
5078 prep(env, rates.first, rates.second);
5080 if (i == 0 || i == 2)
5086 amm.emplace(env, ed,
USD(1'000), eth(1'000));
5094 !
amm->expectBalances(
USD(1'000), eth(1'000),
amm->tokens()));
5098 if (rates.first == 1.5)
5105 STAmount{eth, UINT64_C(378'6327949540812), -13},
5106 STAmount{
USD, UINT64_C(283'9745962155609), -13}}}}));
5115 STAmount{eth, UINT64_C(325'2994616207479), -13},
5116 STAmount{
USD, UINT64_C(243'9745962155609), -13}}}}));
5126 BEAST_EXPECT(q[1] > q[0]);
5128 BEAST_EXPECT(q[2] > q[1]);
5132 for (
auto i = 0; i < 3; ++i)
5134 Env env(*
this, features);
5135 prep(env, rates.first, rates.second);
5137 if (i == 0 || i == 2)
5143 amm.emplace(env, ed,
USD(1'000), eth(1'000));
5151 !
amm->expectBalances(
USD(1'000), eth(1'000),
amm->tokens()));
5157 if (rates.first == 1.5)
5166 STAmount{eth, UINT64_C(335'0889359326475), -13},
5179 STAmount{eth, UINT64_C(335'0889359326475), -13},
5205 for (
auto i = 0; i < 3; ++i)
5207 Env env(*
this, features);
5208 prep(env, rates.first, rates.second);
5211 if (i == 0 || i == 2)
5213 env(
offer(ed, eth(400), can(400)),
Txflags(tfPassive));
5219 amm.emplace(env, ed, eth(1'000),
USD(1'000));
5231 if (rates.first == 1.5)
5236 amm->expectBalances(
5237 STAmount{eth, UINT64_C(1'176'660389557593), -12},
5245 amm->expectBalances(
5246 STAmount{eth, UINT64_C(1'179'54009433964), -11},
5247 STAmount{USD, UINT64_C(847'7880529867501), -13},
5254 STAmount{eth, UINT64_C(343'3179205198749), -13},
5255 STAmount{can, UINT64_C(343'3179205198749), -13},
5258 STAmount{can, UINT64_C(362'2119470132499), -13},
5268 BEAST_EXPECT(q[1] > q[0]);
5269 BEAST_EXPECT(q[2] > q[0] && q[2] < q[1]);
5277 testcase(
"Fix Default Inner Object");
5278 using namespace jtx;
5289 Env env(*
this, features);
5291 AMM amm(env,
gw_,
XRP(10),
USD(10), {.tfee = tfee, .close = closeLedger});
5320 testcase(
"Fix changeSpotPriceQuality");
5321 using namespace jtx;
5326 SucceedShouldSucceedResize,
5339 {
"0.001519763260828713",
"1558701",
Quality{5414253689393440221}, 1000, FailShouldSucceed},
5340 {
"0.01099814367603737",
"1892611",
Quality{5482264816516900274}, 1000, FailShouldSucceed},
5341 {
"0.78",
"796599",
Quality{5630392334958379008}, 1000, FailShouldSucceed},
5342 {
"105439.2955578965",
"49398693",
Quality{5910869983721805038}, 400, FailShouldSucceed},
5343 {
"12408293.23445213",
"4340810521",
Quality{5911611095910090752}, 997, FailShouldSucceed},
5344 {
"1892611",
"0.01099814367603737",
Quality{6703103457950430139}, 1000, FailShouldSucceed},
5345 {
"423028.8508101858",
"3392804520",
Quality{5837920340654162816}, 600, FailShouldSucceed},
5346 {
"44565388.41001027",
"73890647",
Quality{6058976634606450001}, 1000, FailShouldSucceed},
5347 {
"66831.68494832662",
"16",
Quality{6346111134641742975}, 0, FailShouldSucceed},
5348 {
"675.9287302203422",
"1242632304",
Quality{5625960929244093294}, 300, FailShouldSucceed},
5349 {
"7047.112186735699",
"1649845866",
Quality{5696855348026306945}, 504, FailShouldSucceed},
5350 {
"840236.4402981238",
"47419053",
Quality{5982561601648018688}, 499, FailShouldSucceed},
5351 {
"992715.618909774",
"189445631733",
Quality{5697835648288106944}, 815, SucceedShouldSucceedResize},
5352 {
"504636667521",
"185545883.9506651",
Quality{6343802275337659280}, 503, SucceedShouldSucceedResize},
5353 {
"992706.7218636649",
"189447316000",
Quality{5697835648288106944}, 797, SucceedShouldSucceedResize},
5354 {
"1.068737911388205",
"127860278877",
Quality{5268604356368739396}, 293, SucceedShouldSucceedResize},
5355 {
"17932506.56880419",
"189308.6043676173",
Quality{6206460598195440068}, 311, SucceedShouldSucceedResize},
5356 {
"1.066379294658174",
"128042251493",
Quality{5268559341368739328}, 270, SucceedShouldSucceedResize},
5357 {
"350131413924",
"1576879.110907892",
Quality{6487411636539049449}, 650,
Fail},
5358 {
"422093460",
"2.731797662057464",
Quality{6702911108534394924}, 1000,
Fail},
5359 {
"76128132223",
"367172.7148422662",
Quality{6487263463413514240}, 548,
Fail},
5360 {
"132701839250",
"280703770.7695443",
Quality{6273750681188885075}, 562,
Fail},
5361 {
"994165.7604612011",
"189551302411",
Quality{5697835592690668727}, 815,
Fail},
5362 {
"45053.33303227917",
"86612695359",
Quality{5625695218943638190}, 500,
Fail},
5363 {
"199649.077043865",
"14017933007",
Quality{5766034667318524880}, 324,
Fail},
5364 {
"27751824831.70903",
"78896950",
Quality{6272538159621630432}, 500,
Fail},
5365 {
"225.3731275781907",
"156431793648",
Quality{5477818047604078924}, 989,
Fail},
5366 {
"199649.077043865",
"14017933007",
Quality{5766036094462806309}, 324,
Fail},
5367 {
"3.590272027140361",
"20677643641",
Quality{5406056147042156356}, 808,
Fail},
5368 {
"1.070884664490231",
"127604712776",
Quality{5268620608623825741}, 293,
Fail},
5369 {
"3272.448829820197",
"6275124076",
Quality{5625710328924117902}, 81,
Fail},
5370 {
"0.009059512633902926",
"7994028",
Quality{5477511954775533172}, 1000,
Fail},
5373 {
"10",
"10.0",
Quality{xrpIouAmounts10100}, 100,
Fail},
5374 {
"10.0",
"10",
Quality{iouXrpAmounts10100}, 100,
Fail},
5375 {
"69864389131",
"287631.4543025075",
Quality{6487623473313516078}, 451, Succeed},
5376 {
"4328342973",
"12453825.99247381",
Quality{6272522264364865181}, 997, Succeed},
5377 {
"32347017",
"7003.93031579449",
Quality{6347261126087916670}, 1000, Succeed},
5378 {
"61697206161",
"36631.4583206413",
Quality{6558965195382476659}, 500, Succeed},
5379 {
"1654524979",
"7028.659825511603",
Quality{6487551345110052981}, 504, Succeed},
5380 {
"88621.22277293179",
"5128418948",
Quality{5766347291552869205}, 380, Succeed},
5381 {
"1892611",
"0.01099814367603737",
Quality{6703102780512015436}, 1000, Succeed},
5382 {
"4542.639373338766",
"24554809",
Quality{5838994982188783710}, 0, Succeed},
5383 {
"5132932546",
"88542.99750172683",
Quality{6419203342950054537}, 380, Succeed},
5384 {
"78929964.1549083",
"1506494795",
Quality{5986890029845558688}, 589, Succeed},
5385 {
"10096561906",
"44727.72453735605",
Quality{6487455290284644551}, 250, Succeed},
5386 {
"5092.219565514988",
"8768257694",
Quality{5626349534958379008}, 503, Succeed},
5387 {
"1819778294",
"8305.084302902864",
Quality{6487429398998540860}, 415, Succeed},
5388 {
"6970462.633911943",
"57359281",
Quality{6054087899185946624}, 850, Succeed},
5389 {
"3983448845",
"2347.543644281467",
Quality{6558965195382476659}, 856, Succeed},
5392 {
"771493171",
"1.243473020567508",
Quality{6707566798038544272}, 100, SucceedShouldFail},
5396 boost::regex
const rx(
"^\\d+$");
5397 boost::smatch match;
5399 auto const rules = env.
current()->rules();
5403 for (
auto const& t :
tests)
5410 auto const& quality = std::get<Quality>(t);
5411 auto const tfee = std::get<std::uint16_t>(t);
5412 auto const status = std::get<Status>(t);
5413 auto const poolInIsXRP = boost::regex_search(std::get<0>(t), match, rx);
5414 auto const poolOutIsXRP = boost::regex_search(std::get<1>(t), match, rx);
5415 assert(!(poolInIsXRP && poolOutIsXRP));
5416 auto const poolIn = getPool(std::get<0>(t), poolInIsXRP);
5417 auto const poolOut = getPool(std::get<1>(t), poolOutIsXRP);
5418 auto const amounts =
5422 if (status == SucceedShouldSucceedResize)
5424 BEAST_EXPECT(
Quality{*amounts} >= quality);
5426 else if (status == Succeed)
5428 BEAST_EXPECT(
Quality{*amounts} >= quality);
5430 else if (status == FailShouldSucceed)
5432 BEAST_EXPECT(
Quality{*amounts} >= quality);
5434 else if (status == SucceedShouldFail)
5438 BEAST_EXPECT(
false);
5449 auto const tinyOffer = [&] {
5462 auto const takerPays =
5467 BEAST_EXPECT(
Quality(tinyOffer) < quality);
5469 else if (status == FailShouldSucceed)
5472 BEAST_EXPECT(
false);
5474 else if (status == SucceedShouldFail)
5486 BEAST_EXPECT(!res.has_value());
5493 using namespace jtx;
5497 .flags = tfSingleAsset,
5505 .flags = tfOneAssetLPToken,
5513 .flags = tfLimitLPToken,
5521 .asset1Out =
XRP(100),
5522 .asset2Out =
XRP(100),
5530 .asset1Out =
XRP(100),
5531 .asset2Out =
BAD(100),
5539 jv[jss::TransactionType] = jss::AMMWithdraw;
5540 jv[jss::Flags] = tfLimitLPToken;
5541 jv[jss::Account] =
alice_.human();
5543 XRP(100).value().setJson(jv[jss::Amount]);
5544 USD(100).value().setJson(jv[jss::EPrice]);
5553 using namespace jtx;
5559 Account const gatehub{
"gatehub"};
5560 Account const bitstamp{
"bitstamp"};
5561 Account const trader{
"trader"};
5562 auto const usdGH = gatehub[
"USD"];
5563 auto const btcGH = gatehub[
"BTC"];
5564 auto const usdBIT = bitstamp[
"USD"];
5568 char const* testCase;
5569 double const poolUsdBIT;
5570 double const poolUsdGH;
5577 double const offer1BtcGH = 0.1;
5578 double const offer2BtcGH = 0.1;
5579 double const offer2UsdGH = 1;
5580 double const rateBIT = 0.0;
5581 double const rateGH = 0.0;
5586 for (
auto const& input : {
5588 .testCase =
"Test Fix Overflow Offer",
5591 .sendMaxUsdBIT{usdBIT(50)},
5592 .sendUsdGH{usdGH, Uint64T(272'455089820359), -12},
5593 .goodUsdGH{usdGH, Uint64T(96'7543114222965), -13},
5594 .goodUsdBIT{usdBIT, Uint64T(8'464739069098152), -15},
5595 .lpTokenBalance = {28'61817604250837, -14},
5596 .lpTokenBalanceAlt =
IOUAmount{28'61817604250836, -14},
5604 .testCase =
"Overflow test {1, 100, 0.111}",
5607 .sendMaxUsdBIT{usdBIT(0.111)},
5608 .sendUsdGH{usdGH, 100},
5609 .goodUsdGH{usdGH, Uint64T(90'04347888284201), -14},
5610 .goodUsdBIT{usdBIT, Uint64T(1'111), -3},
5611 .lpTokenBalance{10, 0},
5612 .offer1BtcGH = 1e-5,
5614 .offer2UsdGH = 1e-5,
5619 .testCase =
"Overflow test {1, 100, 1.00}",
5622 .sendMaxUsdBIT{usdBIT(1.00)},
5623 .sendUsdGH{usdGH, 100},
5624 .goodUsdGH{usdGH, Uint64T(52'94379354424135), -14},
5625 .goodUsdBIT{usdBIT, Uint64T(2), 0},
5626 .lpTokenBalance{10, 0},
5627 .offer1BtcGH = 1e-5,
5629 .offer2UsdGH = 1e-5,
5634 .testCase =
"Overflow test {1, 100, 4.6432}",
5637 .sendMaxUsdBIT{usdBIT(4.6432)},
5638 .sendUsdGH{usdGH, 100},
5639 .goodUsdGH{usdGH, Uint64T(35'44113971506987), -14},
5640 .goodUsdBIT{usdBIT, Uint64T(2'821579689703954), -15},
5641 .lpTokenBalance{10, 0},
5642 .offer1BtcGH = 1e-5,
5644 .offer2UsdGH = 1e-5,
5649 .testCase =
"Overflow test {1, 100, 10}",
5652 .sendMaxUsdBIT{usdBIT(10)},
5653 .sendUsdGH{usdGH, 100},
5654 .goodUsdGH{usdGH, Uint64T(35'44113971506987), -14},
5655 .goodUsdBIT{usdBIT, Uint64T(2'821579689703954), -15},
5656 .lpTokenBalance{10, 0},
5657 .offer1BtcGH = 1e-5,
5659 .offer2UsdGH = 1e-5,
5664 .testCase =
"Overflow test {50, 100, 5.55}",
5667 .sendMaxUsdBIT{usdBIT(5.55)},
5668 .sendUsdGH{usdGH, 100},
5669 .goodUsdGH{usdGH, Uint64T(90'0434788828413), -13},
5670 .goodUsdBIT{usdBIT, Uint64T(55'55), -2},
5671 .lpTokenBalance{Uint64T(70'71067811865475), -14},
5672 .offer1BtcGH = 1e-5,
5674 .offer2UsdGH = 1e-5,
5679 .testCase =
"Overflow test {50, 100, 50.00}",
5682 .sendMaxUsdBIT{usdBIT(50.00)},
5683 .sendUsdGH{usdGH, 100},
5684 .goodUsdGH{usdGH, Uint64T(52'94379354424092), -14},
5685 .goodUsdBIT{usdBIT, Uint64T(100), 0},
5686 .lpTokenBalance{Uint64T(70'71067811865475), -14},
5687 .offer1BtcGH = 1e-5,
5689 .offer2UsdGH = 1e-5,
5694 .testCase =
"Overflow test {50, 100, 232.16}",
5697 .sendMaxUsdBIT{usdBIT(232.16)},
5698 .sendUsdGH{usdGH, 100},
5699 .goodUsdGH{usdGH, Uint64T(35'44113971506987), -14},
5700 .goodUsdBIT{usdBIT, Uint64T(141'0789844851962), -13},
5701 .lpTokenBalance{70'71067811865475, -14},
5702 .offer1BtcGH = 1e-5,
5704 .offer2UsdGH = 1e-5,
5709 .testCase =
"Overflow test {50, 100, 500}",
5712 .sendMaxUsdBIT{usdBIT(500)},
5713 .sendUsdGH{usdGH, 100},
5714 .goodUsdGH{usdGH, Uint64T(35'44113971506987), -14},
5715 .goodUsdBIT{usdBIT, Uint64T(141'0789844851962), -13},
5716 .lpTokenBalance{70'71067811865475, -14},
5717 .offer1BtcGH = 1e-5,
5719 .offer2UsdGH = 1e-5,
5726 for (
auto const& features : {all - fixAMMv1_3, all})
5730 env.
fund(
XRP(5'000), gatehub, bitstamp, trader);
5733 if (input.rateGH != 0.0)
5734 env(
rate(gatehub, input.rateGH));
5735 if (input.rateBIT != 0.0)
5736 env(
rate(bitstamp, input.rateBIT));
5738 env(
trust(trader, usdGH(10'000'000)));
5739 env(
trust(trader, usdBIT(10'000'000)));
5740 env(
trust(trader, btcGH(10'000'000)));
5743 env(
pay(gatehub, trader, usdGH(100'000)));
5744 env(
pay(gatehub, trader, btcGH(100'000)));
5745 env(
pay(bitstamp, trader, usdBIT(100'000)));
5748 AMM const amm{env, trader, usdGH(input.poolUsdGH), usdBIT(input.poolUsdBIT)};
5751 IOUAmount const preSwapLPTokenBalance =
amm.getLPTokensBalance();
5753 env(
offer(trader, usdBIT(1), btcGH(input.offer1BtcGH)));
5754 env(
offer(trader, btcGH(input.offer2BtcGH), usdGH(input.offer2UsdGH)));
5757 env(
pay(trader, trader, input.sendUsdGH),
5759 Path(~btcGH, ~usdGH),
5764 auto const& goodUsdGH = input.goodUsdGH;
5765 auto const& goodUsdBIT = input.goodUsdBIT;
5766 auto const lpTokenBalance = [&] {
5767 if (not env.
enabled(fixAMMv1_3))
5768 return input.lpTokenBalance;
5770 return input.lpTokenBalanceAlt.value_or(input.lpTokenBalance);
5773 BEAST_EXPECT(
amm.expectBalances(goodUsdGH, goodUsdBIT, lpTokenBalance));
5777 BEAST_EXPECT(
amm.getLPTokensBalance() == preSwapLPTokenBalance);
5781 Number const sqrtPoolProduct =
root2(goodUsdGH * goodUsdBIT);
5791 BEAST_EXPECT((sqrtPoolProduct +
Number{1, -14} >= lpTokenBalance));
5800 using namespace jtx;
5802 STAmount const xrpPool{
XRP, UINT64_C(51600'000981)};
5803 STAmount const iouPool{
USD, UINT64_C(803040'9987141784), -10};
5805 STAmount const xrpBob{
XRP, UINT64_C(1092'878933)};
5806 STAmount const iouBob{
USD, UINT64_C(3'988035892323031), -28};
5811 auto [xrpBegin, iouBegin, lptBegin] =
amm.balances(
XRP,
USD);
5823 BEAST_EXPECT(
amm.expectBalances(xrpBegin, iouBegin,
amm.tokens()));
5825 {{xrpPool, iouPool}},
5834 testcase(
"AMM Offer Blocked By LOB");
5835 using namespace jtx;
5841 Env env(*
this, features);
5863 Env env(*
this, features);
5884 Env env(*
this, features);
5893 BEAST_EXPECT(
amm.expectBalances(
5902 Env env(*
this, features);
5908 BEAST_EXPECT(
amm.expectBalances(
5919 using namespace jtx;
5933 auto const lpToken =
5935 auto const lpTokenBalance =
amm.ammRpcInfo()[jss::amm][jss::lp_token][jss::value];
5936 BEAST_EXPECT(lpToken ==
"1414.213562373095" && lpTokenBalance ==
"1414.213562373");
5938 BEAST_EXPECT(!
amm.ammExists());
5943 for (
auto const& lp : {
gw_,
bob_})
5945 Env env(*
this, features);
5946 auto const abc =
gw_[
"ABC"];
5952 {
USD(1'000'000'000), abc(1'000'000'000'000)});
5953 AMM amm(env, lp, abc(2'000'000),
USD(1));
5958 auto const lpToken =
5960 auto const lpTokenBalance =
amm.ammRpcInfo()[jss::amm][jss::lp_token][jss::value];
5961 BEAST_EXPECT(lpToken ==
"1414.213562373095" && lpTokenBalance ==
"1414.213562373");
5962 amm.withdrawAll(lp);
5963 BEAST_EXPECT(!
amm.ammExists());
5969 Env env(*
this, features);
5974 BEAST_EXPECT(res && !res.value());
5976 BEAST_EXPECT(res && !res.value());
5980 Env env(*
this, features);
5985 BEAST_EXPECT(res && !res.value());
5987 BEAST_EXPECT(res && !res.value());
5991 Env env(*
this, features);
5993 auto const yan = gw1[
"YAN"];
5995 fund(env, gw1, {
gw_},
XRP(1'000), {yan(1'000)}, Fund::TokenOnly);
5999 BEAST_EXPECT(res && !res.value());
6001 BEAST_EXPECT(res && !res.value());
6008 testcase(
"test clawback from AMM account");
6009 using namespace jtx;
6012 Env env(*
this, features);
6014 env(
fset(
gw_, asfAllowTrustLineClawback));
6020 if (!features[featureAMMClawback])
6024 env(
fclear(
gw_, asfAllowTrustLineClawback));
6049 Issue const usd(
USD.issue().currency,
amm.ammAccount());
6051 env(
claw(
gw_, amount), error);
6058 testcase(
"test AMMDeposit with frozen assets");
6059 using namespace jtx;
6076 Env env(*
this, features);
6077 testAMMDeposit(env, [&](
AMM&
amm) {
6085 Env env(*
this, features);
6086 testAMMDeposit(env, [&](
AMM&
amm) {
6092 if (features[featureAMMClawback] || features[fixCleanup3_3_0])
6097 Env env(*
this, features);
6098 testAMMDeposit(env, [&](
AMM&
amm) {
6108 Env env(*
this, features);
6109 testAMMDeposit(env, [&](
AMM&
amm) {
6119 testcase(
"Fix Reserve Check On Withdrawal");
6120 using namespace jtx;
6124 auto const test = [&](
auto&& cb) {
6125 Env env(*
this, features);
6126 auto const startingXrp =
reserve(env, 2) + env.
current()->fees().base * 5;
6146 .asset1Out =
EUR(0.1),
6147 .asset2Out =
USD(0.1),
6153 .asset1Out =
USD(0.1),
6154 .asset2Out =
EUR(0.1),
6172 testcase(
"Fail pseudo-account allocation " + suffix);
6184 for (
int i = 0; i < 256; ++i)
6209 testcase(
"Deposit and Withdraw Rounding V2");
6210 using namespace jtx;
6212 auto const xpm =
gw_[
"XPM"];
6214 STAmount xpmBalance{xpm, UINT64_C(18'610'359'80246901), -8};
6215 STAmount amount{xpm, UINT64_C(6'566'496939465400), -12};
6219 Env env(*
this, features);
6227 STAmount const lptAMMBalance{
amm.lptIssue(), UINT64_C(3'234'987'266'485968), -6};
6228 auto const burn =
IOUAmount{
amm.getLPTokensBalance() - lptAMMBalance};
6230 env(
amm.bid(
BidArg{.account = gw_, .bidMin = burn, .bidMax = burn}));
6241 auto const [amount, amount2, lptAMM] =
amm.balances(
XRP, xpm);
6242 auto const withdraw =
STAmount{xpm, 1, -5};
6244 auto const [amountAfter, amount2After, lptAMMAfter] =
amm.balances(
XRP, xpm);
6247 BEAST_EXPECT((amount2 - amount2After) > withdraw);
6251 BEAST_EXPECT((amount2 - amount2After) <= withdraw);
6260 auto const [amount, amount2, lptBalance] =
amm.balances(
GBP,
EUR);
6265 auto const res =
root2(amount * amount2);
6269 BEAST_EXPECT(res < lptBalance);
6273 BEAST_EXPECT(res >= lptBalance);
6281 using namespace jtx;
6294 [&](
AMM& ammAlice,
Env& env) {
6300 {
GBP(100'000),
EUR(100'000)},
6311 {{
GBP(30'000),
EUR(30'000)}},
6319 [&](
AMM& ammAlice,
Env& env) {
6323 STAmount const depositEuro{
EUR, UINT64_C(10'1234567890123456), -16};
6324 STAmount const depositGBP{
GBP, UINT64_C(10'1234567890123456), -16};
6327 DepositArg{.account =
bob_, .asset1In = depositEuro, .asset2In = depositGBP});
6328 invariant(ammAlice, env,
"dep2",
false);
6330 {{
GBP(30'000),
EUR(30'000)}},
6336 for (
auto const& exponent : {1, 2, 3, 4, -3 , -6, -9})
6339 [&](
AMM& ammAlice,
Env& env) {
6345 {
GBP(100'000),
EUR(100'000)},
6355 .asset1In = depositEuro,
6356 .asset2In = depositGBP,
6358 invariant(ammAlice, env,
"dep3", exponent != -3 && !env.
enabled(fixAMMv1_3));
6360 {{
GBP(10'000),
EUR(30'000)}},
6368 [&](
AMM& ammAlice,
Env& env) {
6374 invariant(ammAlice, env,
"dep4",
false);
6376 {{
GBP(7'000),
EUR(30'000)}},
6382 for (
auto const& tokens : {
6394 [&](
AMM& ammAlice,
Env& env) {
6400 {
GBP(100'000),
EUR(1'000'000)},
6410 invariant(ammAlice, env,
"dep5",
false);
6412 {{
GBP(7'000),
EUR(30'000)}},
6421 [&](
AMM& ammAlice,
Env& env) {
6426 invariant(ammAlice, env,
"dep6",
false);
6428 {{
GBP(30'000),
EUR(30'000)}},
6439 using namespace jtx;
6443 [&](
AMM& ammAlice,
Env& env) {
6445 invariant(ammAlice, env,
"with1",
false);
6447 {{
GBP(7'000),
EUR(30'000)}},
6454 [&](
AMM& ammAlice,
Env& env) {
6456 invariant(ammAlice, env,
"with2",
false);
6458 {{
GBP(7'000),
EUR(30'000)}},
6465 [&](
AMM& ammAlice,
Env& env) {
6471 .flags = tfTwoAsset,
6473 invariant(ammAlice, env,
"with3",
false);
6475 {{
GBP(7'000),
EUR(30'000)}},
6486 [&](
AMM& ammAlice,
Env& env) {
6491 .flags = tfSingleAsset,
6493 invariant(ammAlice, env,
"with4",
false);
6495 {{
GBP(7'000),
EUR(30'000)}},
6502 [&](
AMM& ammAlice,
Env& env) {
6512 .flags = tfOneAssetWithdrawAll,
6514 invariant(ammAlice, env,
"with5",
false);
6516 {{
GBP(7'000),
EUR(30'000)}},
6523 [&](
AMM& ammAlice,
Env& env) {
6529 .flags = tfOneAssetLPToken,
6531 invariant(ammAlice, env,
"with6",
false);
6533 {{
GBP(7'000),
EUR(30'000)}},
6540 [&](
AMM& ammAlice,
Env& env) {
6546 .flags = tfLimitLPToken,
6548 invariant(ammAlice, env,
"with7",
true);
6550 {{
GBP(7'000),
EUR(30'000)}},
6559 testcase(
"Test AuthAccounts reset after empty pool reinitialization");
6560 using namespace jtx;
6587 .authAccounts = {bob_, dan},
6590 BEAST_EXPECT(
amm.expectAuctionSlot({bob_.id(), dan.id()}));
6593 amm.withdrawAll(alice_);
6594 amm.withdrawAll(carol_);
6595 BEAST_EXPECT(
amm.ammExists());
6598 BEAST_EXPECT(
amm.getLPTokensBalance() ==
IOUAmount{0});
6599 BEAST_EXPECT(
amm.expectAuctionSlot({bob_.id(), dan.id()}));
6613 BEAST_EXPECT(ammSle && ammSle->isFieldPresent(sfAuctionSlot));
6618 BEAST_EXPECT(
amm.expectAuctionSlot(50, 0,
IOUAmount{0}));
6621 BEAST_EXPECT(slot[sfAccount] == ed.id());
6623 BEAST_EXPECT(ammSle->getFieldU16(sfTradingFee) == 500);
6625 auto const& votes = ammSle->getFieldArray(sfVoteSlots);
6626 BEAST_EXPECT(votes.size() == 1);
6629 BEAST_EXPECT(votes[0].getAccountID(sfAccount) == ed.id());
6630 BEAST_EXPECT(votes[0].getFieldU16(sfTradingFee) == 500);
6634 if (features[fixCleanup3_2_0])
6636 BEAST_EXPECT(!slot.isFieldPresent(sfAuthAccounts));
6641 slot.isFieldPresent(sfAuthAccounts) && !slot.getFieldArray(sfAuthAccounts).empty());
6648 testcase(
"Deposit integral overflow");
6650 using namespace jtx;
6686 .asset1In = asset1In,
6688 .err =
Ter(expected),
6705 testcase(
"Deposit EPrice integral overflow");
6707 using namespace jtx;
6744 .err =
Ter(expected),
6770 testcase(
"Withdraw integral no overflow");
6772 using namespace jtx;
6791 Env env(*
this, features);
6807 .asset2Out =
XRP(1),
6830 testBid(all - fixCleanup3_4_0);
void pass()
Record a successful test condition.
void fail(String const &reason, char const *file, int line)
Record a failure.
TestcaseT testcase
Memberspace for declaring test cases.
RAII class to set and restore the current transaction rules.
Floating point representation of amounts with high dynamic range.
A currency issued by an account.
std::chrono::time_point< NetClock > time_point
Sets the new scale and restores the old scale when it leaves scope.
Number is a floating point type that can represent a wide range of values.
static RoundingMode getround()
Represents the logical ratio of output currency to input currency.
Asset const & asset() const
json::Value getJson(JsonOptions) const override
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
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 getLPTokensBalance(std::optional< AccountID > const &account=std::nullopt) const
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 setTokens(json::Value &jv, std::optional< std::pair< Asset, Asset > > const &assets=std::nullopt)
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 expectLPTokens(AccountID const &account, IOUAmount const &tokens) const
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.
AccountID id() const
Returns the Account ID.
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.
bool enabled(UInt256 feature) const
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &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 memoize(Account const &account)
Associate AccountID with account.
beast::Journal const journal
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
NetClock::time_point now()
Returns the current network time.
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.
@ Object
object value (collection of name/value pairs).
Keylet computation functions.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
json::Value create(A const &account, A const &dest, STAmount const &sendMax)
Create a check.
Deposit preauthorize operations.
json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount)
auto const kCancelTime
Set the "CancelAfter" time tag on a JTx.
auto const kFinishTime
Set the "FinishAfter" time tag on a JTx.
std::array< std::uint8_t, 39 > const kCb1
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)
json::Value claw(Account const &account, STAmount const &amount, std::optional< Account > const &mptHolder)
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)
bool expectHolding(Env const &env, AccountID const &account, STAmount const &value, bool defaultLimits)
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.
XrpT const XRP
Converts to XRP Issue or STAmount.
bool expectOffers(Env const &env, AccountID const &account, std::uint16_t size, std::vector< Amounts > const &toMatch)
XRPAmount txFee(Env const &env, std::uint16_t n)
json::Value fclear(Account const &account, std::uint32_t off)
Remove account flag.
FeatureBitset testableAmendments()
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
static AutofillT const kAutofill
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
json::Value accountBalance(Env &env, Account const &acct)
json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
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.
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.
constexpr std::uint32_t kTotalTimeSlotSecs
bool isXRP(AccountID const &c)
Number ammAuctionMinSlotPrice(Number const &lptAMMBalance, std::uint16_t tradingFee)
Minimum auction slot price: LPTokens * TradingFee / kAuctionSlotMinFeeFraction.
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
@ Fail
Should not be retried in this ledger.
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
STAmount amountFromString(Asset const &asset, std::string const &amount)
TOut swapAssetIn(TAmounts< TIn, TOut > const &pool, TIn const &assetIn, std::uint16_t tfee)
AMM pool invariant - the product (A * B) after swap in/out has to remain at least the same: (A + in) ...
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.
std::string to_string(BaseUInt< Bits, Tag > const &a)
T toAmount(STAmount const &amt)=delete
Asset getAsset(T const &amt)
TERSubset< CanCvtToNotTEC > NotTEC
std::optional< TAmounts< TIn, TOut > > changeSpotPriceQuality(TAmounts< TIn, TOut > const &pool, Quality const &quality, std::uint16_t tfee, beast::Journal j)
Generate AMM offer so that either updated Spot Price Quality (SPQ) is equal to LOB quality (in this c...
std::optional< Number > solveQuadraticEqSmallest(Number const &a, Number const &b, Number const &c)
Solve quadratic equation to find takerGets or takerPays.
AccountID pseudoAccountAddress(ReadView const &view, UInt256 const &pseudoOwnerKey)
Generate a pseudo-account address from a pseudo owner key.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
constexpr std::uint16_t kAuctionSlotTimeIntervals
Dest safeDowncast(Src *s) noexcept
bool isTesSuccess(TER x) noexcept
TIn swapAssetOut(TAmounts< TIn, TOut > const &pool, TOut const &assetOut, std::uint16_t tfee)
Swap assetOut out of the pool and swap in a proportional amount of the other asset.
TERSubset< CanCvtToTER > TER
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
@ tecINSUFFICIENT_RESERVE
constexpr std::uint32_t kVoteWeightScaleFactor
constexpr std::uint32_t kAuctionSlotIntervalDuration
Zero allows classes to offer efficient comparisons to zero.
Represents a pair of input and output currencies.
Basic tests of AMM that do not use offers.
void testAMMAndCLOB(FeatureBitset features)
static FeatureBitset testableAmendments()
static std::vector< FeatureBitset > amendmentCombinations(std::initializer_list< UInt256 > features)
void run() override
Runs the suite.
void testTradingFee(FeatureBitset features)
void testWithdrawRounding(FeatureBitset all)
void testWithdrawIntegralNoOverflow()
void testOverflowOffer(FeatureBitset featuresInitial)
void testFixAMMOfferBlockedByLOB(FeatureBitset features)
void testSelection(FeatureBitset features)
void testInvalidDeposit(FeatureBitset features)
void invariant(jtx::AMM &amm, jtx::Env &env, std::string const &msg, bool shouldFail)
void testBid(FeatureBitset features)
void testInstanceCreate()
void testDepositEPriceIntegralOverflow()
NumberMantissaScaleGuard const sg
void testFixChangeSpotPriceQuality(FeatureBitset features)
void testDepositRounding(FeatureBitset all)
void testInvalidFeeVote()
void testInvalidInstance()
void testInvalidWithdraw()
void testStaleAuthAccountsAfterReinit(FeatureBitset features)
void testAMMClawback(FeatureBitset features)
void testDepositIntegralOverflow()
void testLPTokenBalance(FeatureBitset features)
void testAMMDepositWithFrozenAssets(FeatureBitset features)
void testAdjustedTokens(FeatureBitset features)
void testFailedPseudoAccount()
void testBasicPaymentEngine(FeatureBitset features)
void testFixReserveCheckOnWithdrawal(FeatureBitset features)
void testDepositAndWithdrawRounding(FeatureBitset features)
void testFixDefaultInnerObj()
void testInvalidAMMPayment()
Set the sequence number on a JTx.