xrpld
Loading...
Searching...
No Matches
amount.cpp
1#include <test/jtx/amount.h>
2
3#include <test/jtx/Account.h>
4
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/safe_cast.h>
7#include <xrpl/protocol/Issue.h>
8#include <xrpl/protocol/MPTIssue.h>
9#include <xrpl/protocol/UintTypes.h>
10
11#include <cassert>
12#include <cstdint>
13#include <iomanip>
14#include <ios>
15#include <limits>
16#include <ostream>
17#include <sstream>
18#include <string>
19
20namespace xrpl::test::jtx {
21
22PrettyAmount::
23operator AnyAmount() const
24{
25 return {amount_};
26}
27
28template <typename T>
29static std::string
30toPlaces(T const d, std::uint8_t places)
31{
32 assert(places <= std::numeric_limits<T>::digits10);
33
35 oss << std::setprecision(places) << std::fixed << d;
36
37 std::string out = oss.str();
38 out.erase(out.find_last_not_of('0') + 1, std::string::npos);
39 if (out.back() == '.')
40 out.pop_back();
41
42 return out;
43}
44
46operator<<(std::ostream& os, PrettyAmount const& amount)
47{
48 amount.value().asset().visit(
49 [&](Issue const& issue) {
50 if (issue.native())
51 {
52 // measure in hundredths
53 auto const c = kJtxDropsPerXrp.drops() / 100;
54 auto const n = amount.value().mantissa();
55 if (n < c)
56 {
57 if (amount.value().negative())
58 {
59 os << "-" << n << " drops";
60 }
61 else
62 {
63 os << n << " drops";
64 }
65 }
66 else
67 {
68 auto const d = double(n) / kJtxDropsPerXrp.drops();
69 if (amount.value().negative())
70 {
71 os << "-";
72 }
73
74 os << toPlaces(d, 6) << " XRP";
75 }
76 }
77 else
78 {
79 os << amount.value().getText() << "/" << to_string(issue.currency) << "("
80 << amount.name() << ")";
81 }
82 },
83 [&](MPTIssue const& issue) {
84 os << amount.value().getText() << "/" << to_string(issue) << "(" << amount.name()
85 << ")";
86 });
87 return os;
88}
89
90//------------------------------------------------------------------------------
91
92XrpT const XRP{};
93
96{
97 return {STAmount(issue(), 1, -81), account.name()};
98}
99
105
107operator<<(std::ostream& os, IOU const& iou)
108{
109 os << to_string(iou.currency) << "(" << iou.account.name() << ")";
110 return os;
111}
112
114operator<<(std::ostream& os, MPT const& mpt)
115{
116 os << to_string(mpt.issuanceID);
117 return os;
118}
119
120AnyT const kAny{};
121
122} // namespace xrpl::test::jtx
T back(T... args)
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition Asset.h:107
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
bool native() const
Definition Issue.cpp:54
std::uint64_t mantissa() const noexcept
Definition STAmount.h:472
std::string getText() const override
Definition STAmount.cpp:646
bool negative() const noexcept
Definition STAmount.h:466
Asset const & asset() const
Definition STAmount.h:478
std::string const & name() const
Return the name.
Definition jtx/Account.h:61
Converts to IOU Issue or STAmount.
PrettyAmount operator()(T v) const
Converts to MPT Issue or STAmount.
T erase(T... args)
T find_last_not_of(T... args)
T fixed(T... args)
constexpr XRPAmount kJtxDropsPerXrp
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
std::ostream & operator<<(std::ostream &os, PrettyAmount const &amount)
Definition amount.cpp:46
AnyT const kAny
Returns an amount representing "any issuer".
Definition amount.cpp:120
static std::string toPlaces(T const d, std::uint8_t places)
Definition amount.cpp:30
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safeCast(Src s) noexcept
Definition safe_cast.h:21
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
T pop_back(T... args)
T setprecision(T... args)
T str(T... args)
Amount specifier with an option for any issuer.
Represents an XRP, IOU, or MPT quantity This customizes the string conversion and supports XRP conver...
std::string const & name() const
STAmount const & value() const