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#ifndef RIPPLE_TEST_JTX_AMMTEST_H_INCLUDED
20#define RIPPLE_TEST_JTX_AMMTEST_H_INCLUDED
21
22#include <test/jtx/Account.h>
23#include <test/jtx/amount.h>
24#include <test/jtx/ter.h>
25#include <xrpl/beast/unit_test/suite.h>
26#include <xrpl/protocol/Feature.h>
27
28namespace ripple {
29namespace test {
30namespace jtx {
31
32class AMM;
33
34enum class Fund { All, Acct, Gw, IOUOnly };
35
36void
37fund(
38 jtx::Env& env,
39 jtx::Account const& gw,
40 std::vector<jtx::Account> const& accounts,
41 std::vector<STAmount> const& amts,
42 Fund how);
43
44void
45fund(
46 jtx::Env& env,
47 jtx::Account const& gw,
48 std::vector<jtx::Account> const& accounts,
49 STAmount const& xrp,
50 std::vector<STAmount> const& amts = {},
51 Fund how = Fund::All);
52
53void
54fund(
55 jtx::Env& env,
56 std::vector<jtx::Account> const& accounts,
57 STAmount const& xrp,
58 std::vector<STAmount> const& amts = {},
59 Fund how = Fund::All);
60
62{
63protected:
73
74public:
76
77protected:
81 void
82 testAMM(
83 std::function<void(jtx::AMM&, jtx::Env&)>&& cb,
84 std::optional<std::pair<STAmount, STAmount>> const& pool = std::nullopt,
85 std::uint16_t tfee = 0,
86 std::optional<jtx::ter> const& ter = std::nullopt,
88};
89
91{
92protected:
94 reserve(jtx::Env& env, std::uint32_t count) const;
95
97 ammCrtFee(jtx::Env& env) const;
98
99 /* Path_test */
100 /************************************************/
101 class gate
102 {
103 private:
106 bool signaled_ = false;
107
108 public:
109 // Thread safe, blocks until signaled or period expires.
110 // Returns `true` if signaled.
111 template <class Rep, class Period>
112 bool
114 {
116 auto b = cv_.wait_for(lk, rel_time, [this] { return signaled_; });
117 signaled_ = false;
118 return b;
119 }
120
121 void
123 {
125 signaled_ = true;
126 cv_.notify_all();
127 }
128 };
129
131 pathTestEnv();
132
135 jtx::Env& env,
136 jtx::Account const& src,
137 jtx::Account const& dst,
138 STAmount const& saDstAmount,
139 std::optional<STAmount> const& saSendMax = std::nullopt,
140 std::optional<Currency> const& saSrcCurrency = std::nullopt);
141
144 jtx::Env& env,
145 jtx::Account const& src,
146 jtx::Account const& dst,
147 STAmount const& saDstAmount,
148 std::optional<STAmount> const& saSendMax = std::nullopt,
149 std::optional<Currency> const& saSrcCurrency = std::nullopt);
150};
151
152} // namespace jtx
153} // namespace test
154} // namespace ripple
155
156#endif // RIPPLE_TEST_JTX_AMMTEST_H_INCLUDED
Represents a JSON value.
Definition: json_value.h:147
A testsuite class.
Definition: suite.h:53
jtx::Account const alice
Definition: AMMTest.h:66
jtx::Account const gw
Definition: AMMTest.h:64
jtx::Account const bob
Definition: AMMTest.h:67
jtx::Account const carol
Definition: AMMTest.h:65
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:101
bool wait_for(std::chrono::duration< Rep, Period > const &rel_time)
Definition: AMMTest.h:113
std::condition_variable cv_
Definition: AMMTest.h:104
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:238
XRPAmount ammCrtFee(jtx::Env &env) const
Definition: AMMTest.cpp:159
XRPAmount reserve(jtx::Env &env, std::uint32_t count) const
Definition: AMMTest.cpp:153
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:179
Convenience class to test AMM functionality.
Definition: AMM.h:122
Immutable cryptographic account descriptor.
Definition: Account.h:38
A transaction testing environment.
Definition: Env.h:117
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:34
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:35
FeatureBitset supported_amendments()
Definition: Env.h:70
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26