1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/PathSet.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/balance.h>
7#include <test/jtx/flags.h>
8#include <test/jtx/jtx_json.h>
9#include <test/jtx/offer.h>
10#include <test/jtx/owners.h>
11#include <test/jtx/paths.h>
12#include <test/jtx/pay.h>
13#include <test/jtx/sendmax.h>
14#include <test/jtx/ter.h>
15#include <test/jtx/trust.h>
16#include <test/jtx/txflags.h>
18#include <xrpl/basics/contract.h>
19#include <xrpl/basics/safe_cast.h>
20#include <xrpl/beast/unit_test/suite.h>
21#include <xrpl/ledger/ApplyView.h>
22#include <xrpl/ledger/PaymentSandbox.h>
23#include <xrpl/protocol/AccountID.h>
24#include <xrpl/protocol/Book.h>
25#include <xrpl/protocol/Feature.h>
26#include <xrpl/protocol/Indexes.h>
27#include <xrpl/protocol/Issue.h>
28#include <xrpl/protocol/Keylet.h>
29#include <xrpl/protocol/LedgerFormats.h>
30#include <xrpl/protocol/SField.h>
31#include <xrpl/protocol/STAmount.h>
32#include <xrpl/protocol/STPathSet.h>
33#include <xrpl/protocol/TER.h>
34#include <xrpl/protocol/TxFlags.h>
35#include <xrpl/protocol/UintTypes.h>
36#include <xrpl/tx/paths/RippleCalc.h>
37#include <xrpl/tx/paths/detail/Steps.h>
38#include <xrpl/tx/transactors/dex/AMMContext.h>
70 return lsfHighNoRipple;
71 return lsfLowNoRipple;
86 auto const useHigh = src.
id() > dst.
id();
87 return sle->isFlag(
trustFlag(flag, useHigh));
138 for (
auto const s : sb)
179 template <
class Col,
class AccFactory,
class IssFactory,
class CurrencyFactory>
185 CurrencyFactory&& currencyF,
223 col.emplace_back(acc, cur, iss);
298 assert(!offererIndex || offererIndex < numAct);
307 for (
size_t id = 0;
id < numAct; ++id)
310 for (
size_t id = 0;
id < numCur; ++id)
335 for (
auto ai2 =
accounts.begin(); ai2 != aie; ++ai2)
341 env.
trust((*ai1)[cn](1'000'000), *ai2);
347 auto const& src = *ai1;
348 auto const& dst = *ai2;
349 env(
pay(src, dst, src[cn](500000)));
365 for (
auto takerPays = ious.
begin(), ie = ious.
end(); takerPays != ie; ++takerPays)
367 for (
auto takerGets = ious.
begin(); takerGets != ie; ++takerGets)
369 if (takerPays == takerGets)
371 auto const owner = offererIndex ?
accounts[*offererIndex] : takerGets->account;
372 if (owner.id() != takerGets->account.id())
373 env(
pay(takerGets->account, owner, (*takerGets)(1000)));
375 env(
offer(owner, (*takerPays)(1000), (*takerGets)(1000)),
Txflags(tfPassive));
381 for (
auto const& iou : ious)
383 auto const owner = offererIndex ?
accounts[*offererIndex] : iou.account;
394 auto add = [&](
auto const& a) {
399 auto const b = (*sle)[sfBalance];
417 auto const sle = v.
read(k);
420 return (*sle)[sfBalance];
423 auto const sle = v.
read(k);
426 return (*sle)[sfBalance];
434 auto const b1 = xrpBalance(v1, ak);
435 auto const b2 = xrpBalance(v2, ak);
441 for (
auto ai2 =
accounts.begin(); ai2 != aie; ++ai2)
449 auto const b1 = lineBalance(v1, lk);
450 auto const b2 = lineBalance(v2, lk);
456 return diffs.
empty();
492 auto const resultSize = prefix.
size() + suffix.
size() + 2;
493 outerResult.
reserve(resultSize);
498 outerResult = prefix;
500 outerResult, accF, issF, currencyF, existingAcc, existingCur, existingIss);
506 result = outerResult;
508 result, accF, issF, currencyF, existingAcc, existingCur, existingIss);
510 f(sendMax, deliver, result);
525 auto const alice =
Account(
"alice");
526 auto const bob =
Account(
"bob");
527 auto const carol =
Account(
"carol");
530 auto const usd = gw[
"USD"];
531 auto const eur = gw[
"EUR"];
533 auto const eurC = eur.currency;
534 auto const usdC = usd.currency;
542 auto test = [&,
this](
544 Issue const& deliver,
548 auto&&... expSteps) {
562 BEAST_EXPECT(ter == expTer);
563 if (
sizeof...(expSteps) != 0)
568 Env env(*
this, features);
569 env.
fund(
XRP(10000), alice, bob, gw);
570 env.
trust(usd(1000), alice, bob);
571 env.
trust(eur(1000), alice, bob);
572 env(
pay(gw, alice, eur(100)));
613 Env env(*
this, features);
614 env.
fund(
XRP(10000), alice, bob, carol, gw);
618 env.
trust(usd(1000), alice, bob, carol);
621 env(
pay(gw, alice, usd(100)));
622 env(
pay(gw, carol, usd(100)));
631 D{.src = alice, .dst = gw, .currency = usdC},
632 D{.src = gw, .dst = bob, .currency = usdC});
633 env.
trust(eur(1000), alice, bob);
642 D{.src = alice, .dst = gw, .currency = usdC},
643 B{usd, eur, std::nullopt},
644 D{.src = gw, .dst = bob, .currency = eurC});
653 D{.src = alice, .dst = gw, .currency = usdC},
654 B{usd, eur, std::nullopt},
655 D{.src = gw, .dst = bob, .currency = eurC});
658 env.
trust(carol[
"USD"](1000), bob);
665 D{.src = alice, .dst = gw, .currency = usdC},
666 B{usd, carol[
"USD"], std::nullopt},
667 D{.src = carol, .dst = bob, .currency = usdC});
677 B{
XRP, usd, std::nullopt},
678 D{.src = gw, .dst = bob, .currency = usdC});
688 D{.src = alice, .dst = gw, .currency = usdC},
689 B{usd,
XRP, std::nullopt},
699 D{.src = alice, .dst = gw, .currency = usdC},
700 B{usd,
XRP, std::nullopt},
701 B{
XRP, eur, std::nullopt},
702 D{.src = gw, .dst = bob, .currency = eurC});
787 Env env(*
this, features);
789 env.
fund(
XRP(10000), alice, bob, carol, gw);
790 env.
trust(usd(10000), alice, bob, carol);
791 env.
trust(eur(10000), alice, bob, carol);
793 env(
pay(gw, bob, usd(100)));
794 env(
pay(gw, bob, eur(100)));
796 env(
offer(bob,
XRP(100), usd(100)));
797 env(
offer(bob, usd(100), eur(100)),
Txflags(tfPassive));
798 env(
offer(bob, eur(100), usd(100)),
Txflags(tfPassive));
801 env(
pay(alice, carol, usd(100)),
802 Path(~usd, ~eur, ~usd),
809 Env env(*
this, features);
811 env.
trust(usd(1000), alice, bob);
812 env(
pay(gw, alice, usd(100)));
818 Env env(*
this, features);
819 env.
fund(
XRP(10000), alice, bob, gw);
820 env.
trust(usd(1000), alice, bob);
821 env(
pay(gw, alice, usd(100)));
824 env(
fset(alice, asfGlobalFreeze));
826 env(
fclear(alice, asfGlobalFreeze));
830 env(
fset(gw, asfGlobalFreeze));
832 env(
fclear(gw, asfGlobalFreeze));
836 env(
fset(bob, asfGlobalFreeze));
838 env(
fclear(bob, asfGlobalFreeze));
843 Env env(*
this, features);
844 env.
fund(
XRP(10000), alice, bob, gw);
845 env.
trust(usd(1000), alice, bob);
846 env(
pay(gw, alice, usd(100)));
848 env(
trust(gw, alice[
"USD"](0), tfSetFreeze));
856 Env env(*
this, features);
857 env.
fund(
XRP(10000), alice, bob, gw);
858 env(
fset(gw, asfRequireAuth));
859 env.
trust(usd(1000), alice, bob);
861 env(
trust(gw, alice[
"USD"](1000), tfSetfAuth));
863 env(
pay(gw, alice, usd(100)));
882 BEAST_EXPECT(
equal(strand, D{alice, gw, usdC}));
887 Env env(*
this, features);
888 env.
fund(
XRP(10000), alice, bob, gw);
889 env.
trust(usd(1000), alice, bob);
890 env(
pay(gw, alice, usd(100)));
911 equal(strand, D{alice, gw, usdC}, B{usd,
xrpIssue(), std::nullopt}, XRPS{bob}));
921 auto const alice =
Account(
"alice");
922 auto const bob =
Account(
"bob");
923 auto const carol =
Account(
"carol");
925 auto const usd = gw[
"USD"];
926 auto const eur = gw[
"EUR"];
929 Env env(*
this, features);
930 env.
fund(
XRP(10000), alice, bob, gw);
932 env.
trust(usd(1000), alice, bob);
933 env.
trust(eur(1000), alice, bob);
934 env.
trust(bob[
"USD"](1000), alice, gw);
935 env.
trust(bob[
"EUR"](1000), alice, gw);
940 env(
offer(bob, bob[
"USD"](100), bob[
"EUR"](100)),
Txflags(tfPassive));
952 env(
pay(alice, alice, eur(1)),
955 Txflags(tfNoRippleDirect | tfPartialPayment),
960 Env env(*
this, features);
962 env.
fund(
XRP(10000), alice, bob, carol, gw);
963 env.
trust(usd(10000), alice, bob, carol);
965 env(
pay(gw, bob, usd(100)));
971 env(
pay(alice, carol,
XRP(100)),
978 Env env(*
this, features);
980 env.
fund(
XRP(10000), alice, bob, carol, gw);
981 env.
trust(usd(10000), alice, bob, carol);
983 env(
pay(gw, bob, usd(100)));
989 env(
pay(alice, carol,
XRP(100)),
1001 using namespace jtx;
1003 auto const alice =
Account(
"alice");
1004 auto const bob =
Account(
"bob");
1005 auto const carol =
Account(
"carol");
1006 auto const gw =
Account(
"gw");
1007 auto const usd = gw[
"USD"];
1008 auto const eur = gw[
"EUR"];
1009 auto const cny = gw[
"CNY"];
1012 Env env(*
this, features);
1014 env.
fund(
XRP(10000), alice, bob, carol, gw);
1015 env.
trust(usd(10000), alice, bob, carol);
1017 env(
pay(gw, bob, usd(100)));
1018 env(
pay(gw, alice, usd(100)));
1024 env(
pay(alice, carol, usd(100)),
1031 Env env(*
this, features);
1033 env.
fund(
XRP(10000), alice, bob, carol, gw);
1034 env.
trust(usd(10000), alice, bob, carol);
1035 env.
trust(eur(10000), alice, bob, carol);
1036 env.
trust(cny(10000), alice, bob, carol);
1038 env(
pay(gw, bob, usd(100)));
1039 env(
pay(gw, bob, eur(100)));
1040 env(
pay(gw, bob, cny(100)));
1043 env(
offer(bob, usd(100), eur(100)),
Txflags(tfPassive));
1044 env(
offer(bob, eur(100), cny(100)),
Txflags(tfPassive));
1047 env(
pay(alice, carol, cny(100)),
1049 Path(~usd, ~eur, ~usd, ~cny),
1059 using namespace jtx;
1061 auto const alice =
Account(
"alice");
1062 auto const bob =
Account(
"bob");
1063 auto const gw =
Account(
"gw");
1064 auto const usd = gw[
"USD"];
1066 Env env(*
this, features);
1067 env.
fund(
XRP(10000), alice, bob, gw);
1069 STAmount const sendMax{usd, 100, 1};
1142 using namespace jtx;
1150 testLoop(sa - featurePermissionedDEX);
void fail(String const &reason, char const *file, int line)
Record a failure.
TestcaseT testcase
Memberspace for declaring test cases.
Maintains AMM info per overall payment engine execution and individual iteration.
A currency issued by an account.
A wrapper which makes credits unavailable to balances.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
virtual beast::Journal getJournal(std::string const &name)=0
static Output rippleCalculate(PaymentSandbox &view, STAmount const &saMaxAmountReq, STAmount const &saDstAmountReq, AccountID const &uDstAccountID, AccountID const &uSrcAccountID, STPathSet const &spsPaths, std::optional< uint256 > const &domainID, ServiceRegistry ®istry, Input const *const pInputs=nullptr)
ElementComboIter(STPathElement const *prev=nullptr)
bool const allowCompound_
void emplaceInto(Col &col, AccFactory &&accF, IssFactory &&issF, CurrencyFactory &¤cyF, std::optional< AccountID > const &existingAcc, std::optional< Currency > const &existingCur, std::optional< AccountID > const &existingIss)
bool hasAny(std::initializer_list< SB > sb) const
STPathElement const * prev_
size_t count(std::initializer_list< SB > sb) const
TestPath & pushBack(Issue const &iss)
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.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
void require(Args const &... args)
Check a set of requirements.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Sets the SendMax on a JTx.
Set the expected result code for a JTx The test will fail if the code doesn't match.
T emplace_back(T... args)
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.
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
STPathElement allPathElements(AccountID const &a, Asset const &asset)
XrpT const XRP
Converts to XRP Issue or STAmount.
json::Value fclear(Account const &account, std::uint32_t off)
Remove account flag.
FeatureBitset testableAmendments()
STPathElement cpe(PathAsset const &pa)
STPathElement ipe(Asset const &asset)
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)
STPathElement iape(AccountID const &account)
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
STPathElement ape(AccountID const &a)
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
bool getTrustFlag(jtx::Env const &env, jtx::Account const &src, jtx::Account const &dst, Currency const &cur, TrustFlag flag)
std::uint32_t trustFlag(TrustFlag f, bool useHigh)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
std::pair< TER, Strand > toStrand(ReadView const &sb, AccountID const &src, AccountID const &dst, Asset const &deliver, std::optional< Quality > const &limitQuality, std::optional< Asset > const &sendMaxAsset, STPath const &path, bool ownerPaysTransferFee, OfferCrossing offerCrossing, AMMContext &ammContext, std::optional< uint256 > const &domainID, beast::Journal j)
Create a Strand for the specified path.
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
constexpr Dest safeCast(Src s) noexcept
Currency const & xrpCurrency()
XRP currency.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
AccountID const & noAccount()
A placeholder for empty accounts.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
AccountID const & xrpAccount()
Compute AccountID from public key.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
A pair of SHAMap key and LedgerEntryType.
StateGuard(ExistingElementPool &p)
std::vector< jtx::Account > accounts
std::int64_t totalXRP(ReadView const &v, bool incRoot)
bool checkBalances(ReadView const &v1, ReadView const &v2)
ResetState getResetState() const
std::vector< xrpl::Currency > currencies
void resetTo(ResetState const &s)
jtx::Account getAccount(size_t id)
jtx::Account getAvailAccount()
void setupEnv(jtx::Env &env, size_t numAct, size_t numCur, std::optional< size_t > const &offererIndex)
void forEachElementPair(STAmount const &sendMax, STAmount const &deliver, std::vector< STPathElement > const &prefix, std::vector< STPathElement > const &suffix, std::optional< AccountID > const &existingAcc, std::optional< Currency > const &existingCur, std::optional< AccountID > const &existingIss, F &&f)
std::vector< std::string > currencyNames
xrpl::Currency getCurrency(size_t id)
xrpl::Currency getAvailCurrency()
std::tuple< size_t, size_t > ResetState
void testRIPD1373(FeatureBitset features)
void testLoop(FeatureBitset features)
void run() override
Runs the suite.
void testToStrand(FeatureBitset features)
void testNoAccount(FeatureBitset features)