rippled
Loading...
Searching...
No Matches
amount.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/amount.h>
3
4#include <xrpl/basics/safe_cast.h>
5
6#include <iomanip>
7
8namespace ripple {
9namespace test {
10namespace jtx {
11
12#if 0
15 AnyAmount const& amount)
16{
17 if (amount.is_any)
18 {
19 os << amount.value.getText() << "/" <<
20 to_string(amount.value.issue().currency) <<
21 "*";
22 return os;
23 }
24 os << amount.value.getText() << "/" <<
25 to_string(amount.value.issue().currency) <<
26 "(" << amount.name() << ")";
27 return os;
28}
29#endif
30
31PrettyAmount::operator AnyAmount() const
32{
33 return {amount_};
34}
35
36template <typename T>
37static std::string
38to_places(T const d, std::uint8_t places)
39{
40 assert(places <= std::numeric_limits<T>::digits10);
41
43 oss << std::setprecision(places) << std::fixed << d;
44
45 std::string out = oss.str();
46 out.erase(out.find_last_not_of('0') + 1, std::string::npos);
47 if (out.back() == '.')
48 out.pop_back();
49
50 return out;
51}
52
54operator<<(std::ostream& os, PrettyAmount const& amount)
55{
56 if (amount.value().native())
57 {
58 // measure in hundredths
59 auto const c = dropsPerXRP.drops() / 100;
60 auto const n = amount.value().mantissa();
61 if (n < c)
62 {
63 if (amount.value().negative())
64 os << "-" << n << " drops";
65 else
66 os << n << " drops";
67 return os;
68 }
69 auto const d = double(n) / dropsPerXRP.drops();
70 if (amount.value().negative())
71 os << "-";
72
73 os << to_places(d, 6) << " XRP";
74 }
75 else if (amount.value().holds<Issue>())
76 {
77 os << amount.value().getText() << "/"
78 << to_string(amount.value().issue().currency) << "(" << amount.name()
79 << ")";
80 }
81 else
82 {
83 auto const& mptIssue = amount.value().asset().get<MPTIssue>();
84 os << amount.value().getText() << "/" << to_string(mptIssue) << "("
85 << amount.name() << ")";
86 }
87 return os;
88}
89
90//------------------------------------------------------------------------------
91
92XRP_t const XRP{};
93
96{
97 return {STAmount(issue(), 1, -81), account.name()};
98}
99
102{
103 return {
104 STAmount(issue(), safe_cast<std::uint64_t>(m.n), -81), account.name()};
105}
106
108operator<<(std::ostream& os, IOU const& iou)
109{
110 os << to_string(iou.issue().currency) << "(" << iou.account.name() << ")";
111 return os;
112}
113
114any_t const any{};
115
116} // namespace jtx
117} // namespace test
118} // namespace ripple
constexpr TIss const & get() const
A currency issued by an account.
Definition Issue.h:14
Currency currency
Definition Issue.h:16
constexpr bool holds() const noexcept
Definition STAmount.h:446
Asset const & asset() const
Definition STAmount.h:464
std::string getText() const override
Definition STAmount.cpp:664
bool negative() const noexcept
Definition STAmount.h:452
Issue const & issue() const
Definition STAmount.h:477
std::uint64_t mantissa() const noexcept
Definition STAmount.h:458
bool native() const noexcept
Definition STAmount.h:439
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:158
std::string const & name() const
Return the name.
Definition Account.h:68
Converts to IOU Issue or STAmount.
PrettyAmount operator()(T v) const
T fixed(T... args)
std::ostream & operator<<(std::ostream &os, PrettyAmount const &amount)
Definition amount.cpp:54
constexpr XRPAmount dropsPerXRP
any_t const any
Returns an amount representing "any issuer".
Definition amount.cpp:114
static std::string to_places(T const d, std::uint8_t places)
Definition amount.cpp:38
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
T setprecision(T... args)
T str(T... args)
Amount specifier with an option for any issuer.
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...
std::string const & name() const