xrpld
Loading...
Searching...
No Matches
AMMTest.h
1#pragma once
2
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/ter.h>
7
8#include <xrpl/beast/unit_test/suite.h>
9#include <xrpl/protocol/Feature.h>
10#include <xrpl/protocol/STAmount.h>
11#include <xrpl/protocol/XRPAmount.h>
12
13#include <chrono>
14#include <condition_variable>
15#include <cstdint>
16#include <functional>
17#include <mutex>
18#include <optional>
19#include <utility>
20#include <vector>
21
22namespace xrpl::test::jtx {
23
24class AMM;
25
26enum class Fund { All, Acct, Gw, TokenOnly };
27
29{
34 // For now, just disable SAV entirely, which locks in the small Number
35 // mantissas
36 jtx::testableAmendments() - featureSingleAssetVault - featureLendingProtocol};
37
38 bool noLog = false;
39};
40
41// A hint to testAMM() or fund() to create/fund MPT.
42// A distinct MPT is created if both AMM assets
43// are MPT. The actual MPT asset can be accessed
44// via AMM::operator[](0|1).
45inline static auto gAmmmpt = MPT("AMM");
46
47[[maybe_unused]] std::vector<STAmount>
48fund(
49 jtx::Env& env,
50 jtx::Account const& gw,
51 std::vector<jtx::Account> const& accounts,
52 std::vector<STAmount> const& amts,
53 Fund how);
54
55[[maybe_unused]] std::vector<STAmount>
56fund(
57 jtx::Env& env,
58 jtx::Account const& gw,
59 std::vector<jtx::Account> const& accounts,
60 STAmount const& xrp,
61 std::vector<STAmount> const& amts = {},
62 Fund how = Fund::All);
63
64[[maybe_unused]] std::vector<STAmount>
65fund(
66 jtx::Env& env,
67 std::vector<jtx::Account> const& accounts,
68 STAmount const& xrp,
69 std::vector<STAmount> const& amts = {},
70 Fund how = Fund::All,
71 std::optional<Account> const& mptIssuer = std::nullopt);
72
80
82{
83protected:
88 jtx::IOU const USD; // NOLINT(readability-identifier-naming)
89 jtx::IOU const EUR; // NOLINT(readability-identifier-naming)
90 jtx::IOU const GBP; // NOLINT(readability-identifier-naming)
91 jtx::IOU const BTC; // NOLINT(readability-identifier-naming)
92 jtx::IOU const BAD; // NOLINT(readability-identifier-naming)
93
94public:
96
97 static FeatureBitset
99 {
100 // For now, just disable SAV entirely, which locks in the small Number
101 // mantissas
102 return jtx::testableAmendments() - featureSingleAssetVault - featureLendingProtocol;
103 }
104
105protected:
110 void
111 testAMM(
112 std::function<void(jtx::AMM&, jtx::Env&)> const& cb,
113 std::optional<std::pair<STAmount, STAmount>> const& pool = std::nullopt,
114 std::uint16_t tfee = 0,
115 std::optional<jtx::Ter> const& ter = std::nullopt,
116 std::vector<FeatureBitset> const& features = {testableAmendments()});
117
118 void
119 testAMM(std::function<void(jtx::AMM&, jtx::Env&)> const& cb, TestAMMArg const& arg);
120};
121
123{
124protected:
125 static XRPAmount
126 reserve(jtx::Env& env, std::uint32_t count);
127
128 static XRPAmount
129 ammCrtFee(jtx::Env& env);
130
131 /* Path_test */
132 /************************************************/
133 class Gate
134 {
135 private:
138 bool signaled_ = false;
139
140 public:
141 // Thread safe, blocks until signaled or period expires.
142 // Returns `true` if signaled.
143 template <class Rep, class Period>
144 bool
146 {
148 auto b = cv_.wait_for(lk, relTime, [this] { return signaled_; });
149 signaled_ = false;
150 return b;
151 }
152
153 void
155 {
156 std::scoped_lock const lk(mutex_);
157 signaled_ = true;
158 cv_.notify_all();
159 }
160 };
161
163 pathTestEnv();
164};
165
166} // namespace xrpl::test::jtx
A testsuite class.
Definition suite.h:52
std::string const & arg() const
Return the argument associated with the runner.
Definition suite.h:292
jtx::Account const alice_
Definition AMMTest.h:86
void testAMM(std::function< void(jtx::AMM &, jtx::Env &)> const &cb, std::optional< std::pair< STAmount, STAmount > > const &pool=std::nullopt, std::uint16_t tfee=0, std::optional< jtx::Ter > const &ter=std::nullopt, std::vector< FeatureBitset > const &features={testableAmendments()})
testAMM() funds 30,000XRP and 30,000IOU for each non-XRP asset to Alice and Carol
Definition AMMTest.cpp:120
jtx::Account const gw_
Definition AMMTest.h:84
static FeatureBitset testableAmendments()
Definition AMMTest.h:98
jtx::Account const carol_
Definition AMMTest.h:85
jtx::Account const bob_
Definition AMMTest.h:87
bool waitFor(std::chrono::duration< Rep, Period > const &relTime)
Definition AMMTest.h:145
std::condition_variable cv_
Definition AMMTest.h:136
static XRPAmount reserve(jtx::Env &env, std::uint32_t count)
Definition AMMTest.cpp:198
static XRPAmount ammCrtFee(jtx::Env &env)
Definition AMMTest.cpp:204
Convenience class to test AMM functionality.
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
A transaction testing environment.
Definition Env.h:161
Converts to IOU Issue or STAmount.
Converts to MPT Issue or STAmount.
std::vector< STAmount > fund(jtx::Env &env, jtx::Account const &gw, std::vector< jtx::Account > const &accounts, std::vector< STAmount > const &amts, Fund how)
Definition AMMTest.cpp:34
FeatureBitset testableAmendments()
Definition Env.h:92
static auto gAmmmpt
Definition AMMTest.h:45
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:54
std::optional< jtx::Ter > ter
Definition AMMTest.h:32
std::optional< std::pair< STAmount, STAmount > > pool
Definition AMMTest.h:30
std::vector< FeatureBitset > features
Definition AMMTest.h:33
std::optional< std::pair< STAmount, STAmount > > const & pool
Definition AMMTest.h:75
std::optional< jtx::Ter > const & ter
Definition AMMTest.h:77
std::vector< FeatureBitset > const & features
Definition AMMTest.h:78