rippled
Loading...
Searching...
No Matches
AMMTest.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_TEST_JTX_AMMTEST_H_INCLUDED
21#define RIPPLE_TEST_JTX_AMMTEST_H_INCLUDED
22
23#include <test/jtx/Account.h>
24#include <test/jtx/amount.h>
25#include <test/jtx/ter.h>
26
27#include <xrpl/beast/unit_test/suite.h>
28#include <xrpl/protocol/Feature.h>
29
30namespace ripple {
31namespace test {
32namespace jtx {
33
34class AMM;
35
36enum class Fund { All, Acct, Gw, IOUOnly };
37
38void
39fund(
40 jtx::Env& env,
41 jtx::Account const& gw,
42 std::vector<jtx::Account> const& accounts,
43 std::vector<STAmount> const& amts,
44 Fund how);
45
46void
47fund(
48 jtx::Env& env,
49 jtx::Account const& gw,
50 std::vector<jtx::Account> const& accounts,
51 STAmount const& xrp,
52 std::vector<STAmount> const& amts = {},
53 Fund how = Fund::All);
54
55void
56fund(
57 jtx::Env& env,
58 std::vector<jtx::Account> const& accounts,
59 STAmount const& xrp,
60 std::vector<STAmount> const& amts = {},
61 Fund how = Fund::All);
62
64{
65protected:
75
76public:
78
79protected:
83 void
84 testAMM(
85 std::function<void(jtx::AMM&, jtx::Env&)>&& cb,
86 std::optional<std::pair<STAmount, STAmount>> const& pool = std::nullopt,
87 std::uint16_t tfee = 0,
88 std::optional<jtx::ter> const& ter = std::nullopt,
90};
91
93{
94protected:
96 reserve(jtx::Env& env, std::uint32_t count) const;
97
99 ammCrtFee(jtx::Env& env) const;
100
101 /* Path_test */
102 /************************************************/
103 class gate
104 {
105 private:
108 bool signaled_ = false;
109
110 public:
111 // Thread safe, blocks until signaled or period expires.
112 // Returns `true` if signaled.
113 template <class Rep, class Period>
114 bool
116 {
118 auto b = cv_.wait_for(lk, rel_time, [this] { return signaled_; });
119 signaled_ = false;
120 return b;
121 }
122
123 void
125 {
127 signaled_ = true;
128 cv_.notify_all();
129 }
130 };
131
133 pathTestEnv();
134
137 jtx::Env& env,
138 jtx::Account const& src,
139 jtx::Account const& dst,
140 STAmount const& saDstAmount,
141 std::optional<STAmount> const& saSendMax = std::nullopt,
142 std::optional<Currency> const& saSrcCurrency = std::nullopt);
143
146 jtx::Env& env,
147 jtx::Account const& src,
148 jtx::Account const& dst,
149 STAmount const& saDstAmount,
150 std::optional<STAmount> const& saSendMax = std::nullopt,
151 std::optional<Currency> const& saSrcCurrency = std::nullopt);
152};
153
154} // namespace jtx
155} // namespace test
156} // namespace ripple
157
158#endif // RIPPLE_TEST_JTX_AMMTEST_H_INCLUDED
Represents a JSON value.
Definition: json_value.h:148
A testsuite class.
Definition: suite.h:55
jtx::Account const alice
Definition: AMMTest.h:68
jtx::Account const gw
Definition: AMMTest.h:66
jtx::Account const bob
Definition: AMMTest.h:69
jtx::Account const carol
Definition: AMMTest.h:67
void testAMM(std::function< void(jtx::AMM &, jtx::Env &)> &&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={supported_amendments()})
testAMM() funds 30,000XRP and 30,000IOU for each non-XRP asset to Alice and Carol
Definition: AMMTest.cpp:102
bool wait_for(std::chrono::duration< Rep, Period > const &rel_time)
Definition: AMMTest.h:115
std::condition_variable cv_
Definition: AMMTest.h:106
std::tuple< STPathSet, STAmount, STAmount > find_paths(jtx::Env &env, jtx::Account const &src, jtx::Account const &dst, STAmount const &saDstAmount, std::optional< STAmount > const &saSendMax=std::nullopt, std::optional< Currency > const &saSrcCurrency=std::nullopt)
Definition: AMMTest.cpp:239
XRPAmount ammCrtFee(jtx::Env &env) const
Definition: AMMTest.cpp:160
XRPAmount reserve(jtx::Env &env, std::uint32_t count) const
Definition: AMMTest.cpp:154
Json::Value find_paths_request(jtx::Env &env, jtx::Account const &src, jtx::Account const &dst, STAmount const &saDstAmount, std::optional< STAmount > const &saSendMax=std::nullopt, std::optional< Currency > const &saSrcCurrency=std::nullopt)
Definition: AMMTest.cpp:180
Convenience class to test AMM functionality.
Definition: AMM.h:124
Immutable cryptographic account descriptor.
Definition: Account.h:39
A transaction testing environment.
Definition: Env.h:120
Converts to IOU Issue or STAmount.
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition: ter.h:35
void fund(jtx::Env &env, jtx::Account const &gw, std::vector< jtx::Account > const &accounts, std::vector< STAmount > const &amts, Fund how)
Definition: AMMTest.cpp:36
FeatureBitset supported_amendments()
Definition: Env.h:73
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26