rippled
Loading...
Searching...
No Matches
balance.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 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#include <test/jtx/balance.h>
21
22namespace ripple {
23namespace test {
24namespace jtx {
25
26void
28 Env& env,
29 AccountID const& account,
30 bool none,
31 STAmount const& value,
32 Issue const& issue)
33{
34 if (isXRP(issue))
35 {
36 auto const sle = env.le(keylet::account(account));
37 if (none)
38 {
39 env.test.expect(!sle);
40 }
41 else if (env.test.expect(sle))
42 {
43 env.test.expect(sle->getFieldAmount(sfBalance) == value);
44 }
45 }
46 else
47 {
48 auto const sle = env.le(keylet::line(account, issue));
49 if (none)
50 {
51 env.test.expect(!sle);
52 }
53 else if (env.test.expect(sle))
54 {
55 auto amount = sle->getFieldAmount(sfBalance);
56 amount.setIssuer(issue.account);
57 if (account > issue.account)
58 amount.negate();
59 env.test.expect(amount == value);
60 }
61 }
62}
63
64void
66 Env& env,
67 AccountID const& account,
68 bool none,
69 STAmount const& value,
70 MPTIssue const& mptIssue)
71{
72 auto const sle = env.le(keylet::mptoken(mptIssue.getMptID(), account));
73 if (none)
74 {
75 env.test.expect(!sle);
76 }
77 else if (env.test.expect(sle))
78 {
79 STAmount const amount{mptIssue, sle->getFieldU64(sfMPTAmount)};
80 env.test.expect(amount == value);
81 }
82}
83
84void
86{
87 return std::visit(
88 [&](auto const& issue) {
89 doBalance(env, account_.id(), none_, value_, issue);
90 },
91 value_.asset().value());
92}
93
94} // namespace jtx
95} // namespace test
96} // namespace ripple
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition suite.h:229
constexpr value_type const & value() const
Definition Asset.h:156
A currency issued by an account.
Definition Issue.h:33
AccountID account
Definition Issue.h:36
constexpr MPTID const & getMptID() const
Definition MPTIssue.h:46
Asset const & asset() const
Definition STAmount.h:483
AccountID id() const
Returns the Account ID.
Definition Account.h:111
A transaction testing environment.
Definition Env.h:121
beast::unit_test::suite & test
Definition Env.h:123
std::shared_ptr< SLE const > le(Account const &account) const
Return an account root.
Definition Env.cpp:277
void operator()(Env &) const
Definition balance.cpp:85
Account const account_
Definition balance.h:42
STAmount const value_
Definition balance.h:43
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:540
Keylet line(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:244
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:184
static none_t const none
Definition tags.h:34
void doBalance(Env &env, AccountID const &account, bool none, STAmount const &value, Issue const &issue)
Definition balance.cpp:27
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
bool isXRP(AccountID const &c)
Definition AccountID.h:90
T visit(T... args)