xrpld
Loading...
Searching...
No Matches
BookDirs_test.cpp
1
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/offer.h>
6#include <test/jtx/pay.h>
7
8#include <xrpl/beast/unit_test/suite.h>
9#include <xrpl/ledger/BookDirs.h>
10#include <xrpl/protocol/Book.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Issue.h>
13#include <xrpl/protocol/SField.h>
14
15#include <iterator>
16#include <optional>
17
18namespace xrpl::test {
19
21{
22 void
24 {
25 using namespace jtx;
26 Env env(*this, features);
27 auto gw = Account("gw");
28 auto usd = gw["USD"];
29 env.fund(XRP(1000000), "alice", "bob", "gw");
30 env.close();
31
32 {
33 Book const book(xrpIssue(), usd, std::nullopt);
34 {
35 auto d = BookDirs(*env.current(), book);
36 BEAST_EXPECT(std::begin(d) == std::end(d));
37 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
38 }
39 {
40 auto d = BookDirs(*env.current(), reversed(book));
41 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 0);
42 }
43 }
44
45 {
46 env(offer("alice", Account("alice")["USD"](50), XRP(10)));
47 auto d =
48 BookDirs(*env.current(), Book(Account("alice")["USD"], xrpIssue(), std::nullopt));
49 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
50 }
51
52 {
53 env(offer("alice", gw["CNY"](50), XRP(10)));
54 auto d = BookDirs(*env.current(), Book(gw["CNY"], xrpIssue(), std::nullopt));
55 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
56 }
57
58 {
59 env.trust(Account("bob")["CNY"](10), "alice");
60 env(pay("bob", "alice", Account("bob")["CNY"](10)));
61 env(offer("alice", usd(50), Account("bob")["CNY"](10)));
62 auto d = BookDirs(*env.current(), Book(usd, Account("bob")["CNY"], std::nullopt));
63 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 1);
64 }
65
66 {
67 auto aud = gw["AUD"];
68 for (auto i = 1, j = 3; i <= 3; ++i, --j)
69 {
70 for (auto k = 0; k < 80; ++k)
71 env(offer("alice", aud(i), XRP(j)));
72 }
73
74 auto d = BookDirs(*env.current(), Book(aud, xrpIssue(), std::nullopt));
75 BEAST_EXPECT(std::distance(d.begin(), d.end()) == 240);
76 auto i = 1, j = 3, k = 0;
77 for (auto const& e : d)
78 {
79 BEAST_EXPECT(e->getFieldAmount(sfTakerPays) == aud(i));
80 BEAST_EXPECT(e->getFieldAmount(sfTakerGets) == XRP(j));
81 if (++k % 80 == 0)
82 {
83 ++i;
84 --j;
85 }
86 }
87 }
88 }
89
90 void
91 run() override
92 {
93 using namespace jtx;
94 auto const sa = testableAmendments();
95 testBookdir(sa - featurePermissionedDEX);
96 testBookdir(sa);
97 }
98};
99
101
102} // namespace xrpl::test
T begin(T... args)
A testsuite class.
Definition suite.h:50
Specifies an order book.
Definition Book.h:16
A transaction testing environment.
Definition Env.h:143
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:296
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:327
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:353
T distance(T... args)
T end(T... args)
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
FeatureBitset testableAmendments()
Definition Env.h:76
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:97
Book reversed(Book const &book)
Definition Book.cpp:30
void testBookdir(FeatureBitset features)
void run() override
Runs the suite.