xrpld
Loading...
Searching...
No Matches
balance.cpp
1#include <test/jtx/balance.h>
2
3#include <test/jtx/Env.h>
4
5#include <xrpl/protocol/AccountID.h>
6#include <xrpl/protocol/Indexes.h>
7#include <xrpl/protocol/Issue.h>
8#include <xrpl/protocol/MPTIssue.h>
9#include <xrpl/protocol/SField.h>
10#include <xrpl/protocol/STAmount.h>
11
12#include <variant>
13
14namespace xrpl::test::jtx {
15
16#define TEST_EXPECT(cond) env.test.expect(cond, __FILE__, __LINE__)
17#define TEST_EXPECTS(cond, reason) \
18 ((cond) ? (env.test.pass(), true) : (env.test.fail((reason), __FILE__, __LINE__), false))
19
20void
21doBalance(Env& env, AccountID const& account, bool kNone, STAmount const& value, Issue const& issue)
22{
23 if (isXRP(issue))
24 {
25 auto const sle = env.le(keylet::account(account));
26 if (kNone)
27 {
28 TEST_EXPECT(!sle);
29 }
30 else if (TEST_EXPECT(sle))
31 {
32 TEST_EXPECTS(
33 sle->getFieldAmount(sfBalance) == value,
34 sle->getFieldAmount(sfBalance).getText() + " / " + value.getText());
35 }
36 }
37 else
38 {
39 auto const sle = env.le(keylet::trustLine(account, issue));
40 if (kNone)
41 {
42 TEST_EXPECT(!sle);
43 }
44 else if (TEST_EXPECT(sle))
45 {
46 auto amount = sle->getFieldAmount(sfBalance);
47 amount.get<Issue>().account = value.getIssuer();
48 if (account > value.getIssuer())
49 amount.negate();
50 TEST_EXPECTS(amount == value, amount.getText());
51 }
52 }
53}
54
55void
57 Env& env,
58 AccountID const& account,
59 bool kNone,
60 STAmount const& value,
61 MPTIssue const& mptIssue)
62{
63 auto const sle = env.le(keylet::mptoken(mptIssue.getMptID(), account));
64 if (kNone)
65 {
66 TEST_EXPECT(!sle);
67 }
68 else if (TEST_EXPECT(sle))
69 {
70 STAmount const amount{mptIssue, sle->getFieldU64(sfMPTAmount)};
71 TEST_EXPECT(amount == value);
72 }
73}
74
75void
77{
79 [&](auto const& issue) { doBalance(env, account_.id(), none_, value_, issue); },
80 value_.asset().value());
81}
82
83} // namespace xrpl::test::jtx
A currency issued by an account.
Definition Issue.h:13
constexpr MPTID const & getMptID() const
Definition MPTIssue.h:33
void operator()(Env &) const
Definition balance.cpp:76
STAmount const value_
Definition balance.h:23
Account const account_
Definition balance.h:22
A transaction testing environment.
Definition Env.h:143
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:284
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:533
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:241
static NoneT const kNone
Definition tags.h:9
void doBalance(Env &env, AccountID const &account, bool kNone, STAmount const &value, Issue const &issue)
Definition balance.cpp:21
bool isXRP(AccountID const &c)
Definition AccountID.h:70
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
T visit(T... args)