xrpld
Loading...
Searching...
No Matches
XRPAmount.h
1#pragma once
2
3#include <xrpl/basics/MathUtilities.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/json/json_forwards.h>
8#include <xrpl/json/json_value.h>
9#include <xrpl/protocol/Units.h>
10
11#include <boost/multiprecision/cpp_int.hpp>
12#include <boost/operators.hpp>
13
14#include <cstdint>
15#include <istream>
16#include <limits>
17#include <optional>
18#include <ostream>
19#include <stdexcept>
20#include <string>
21#include <type_traits>
22
23namespace xrpl {
24
25class XRPAmount : private boost::totally_ordered<XRPAmount>,
26 private boost::additive<XRPAmount>,
27 private boost::equality_comparable<XRPAmount, std::int64_t>,
28 private boost::additive<XRPAmount, std::int64_t>
29{
30public:
31 using UnitType = unit::dropTag;
33
34private:
36
37public:
38 XRPAmount() = default;
39 constexpr XRPAmount(XRPAmount const& other) = default;
40 constexpr XRPAmount&
41 operator=(XRPAmount const& other) = default;
42
43 // Round to nearest, even on tie.
44 explicit XRPAmount(Number const& x) : XRPAmount(static_cast<value_type>(x))
45 {
46 }
47
49 {
50 }
51
52 constexpr XRPAmount&
54 {
55 drops_ = 0;
56 return *this;
57 }
58
59 constexpr explicit XRPAmount(value_type drops) : drops_(drops)
60 {
61 }
62
65 {
66 drops_ = drops;
67 return *this;
68 }
69
70 constexpr XRPAmount
71 operator*(value_type const& rhs) const
72 {
73 if (auto const result = checkedMul(drops_, rhs))
74 return XRPAmount{*result};
75 // Not reached in constant evaluation: an overflowing constant
76 // expression fails to compile instead.
77 XRPAmount product{*this};
78 product *= rhs;
79 return product;
80 }
81
82 friend constexpr XRPAmount
84 {
85 // multiplication is commutative
86 return rhs * lhs;
87 }
88
90 operator+=(XRPAmount const& other)
91 {
92 return *this += other.drops();
93 }
94
96 operator-=(XRPAmount const& other)
97 {
98 return *this -= other.drops();
99 }
100
101 XRPAmount&
102 operator+=(value_type const& rhs);
103
104 XRPAmount&
105 operator-=(value_type const& rhs);
106
107 XRPAmount&
108 operator*=(value_type const& rhs);
109
111 operator-() const
112 {
113 return XRPAmount{-drops_};
114 }
115
116 bool
117 operator==(XRPAmount const& other) const
118 {
119 return drops_ == other.drops_;
120 }
121
122 bool
124 {
125 return drops_ == other;
126 }
127
128 bool
129 operator<(XRPAmount const& other) const
130 {
131 return drops_ < other.drops_;
132 }
133
137 explicit constexpr
138 operator bool() const noexcept
139 {
140 return drops_ != 0;
141 }
142
143 operator Number() const noexcept
144 {
145 return drops();
146 }
147
151 [[nodiscard]] constexpr int
152 signum() const noexcept
153 {
154 if (drops_ < 0)
155 return -1;
156 return (drops_ != 0) ? 1 : 0;
157 }
158
162 [[nodiscard]] constexpr value_type
163 drops() const
164 {
165 return drops_;
166 }
167
168 [[nodiscard]] constexpr double
169 decimalXRP() const;
170
171 template <class Dest>
172 [[nodiscard]] std::optional<Dest>
173 dropsAs() const
174 {
178 {
179 return std::nullopt;
180 }
181 return static_cast<Dest>(drops_);
182 }
183
184 template <class Dest>
185 Dest
186 dropsAs(Dest defaultValue) const
187 {
188 return dropsAs<Dest>().value_or(defaultValue);
189 }
190
191 template <class Dest>
192 [[nodiscard]] Dest
193 dropsAs(XRPAmount defaultValue) const
194 {
195 return dropsAs<Dest>().value_or(defaultValue.drops());
196 }
197
198 /* Clips a 64-bit value to a 32-bit JSON number. It is only used
199 * in contexts that don't expect the value to ever approach
200 * the 32-bit limits (i.e. fees and reserves).
201 */
202 [[nodiscard]] json::Value
204 {
205 static_assert(
207 "Expected XRPAmount to be a signed integral type");
208
209 constexpr auto kMin = std::numeric_limits<json::Int>::min();
210 constexpr auto kMax = std::numeric_limits<json::Int>::max();
211
212 if (drops_ < kMin)
213 return kMin;
214 if (drops_ > kMax)
215 return kMax;
216 return static_cast<json::Int>(drops_);
217 }
218
224 [[nodiscard]] constexpr value_type
225 value() const
226 {
227 return drops_;
228 }
229
230 friend std::istream&
232 {
233 s >> val.drops_;
234 return s;
235 }
236
237 static XRPAmount
239 {
240 return XRPAmount{1};
241 }
242};
243
247constexpr XRPAmount kDropsPerXrp{1'000'000};
248
249constexpr double
251{
252 return static_cast<double>(drops_) / kDropsPerXrp.drops();
253}
254
255// Output XRPAmount as just the drops value.
256template <class Char, class Traits>
259{
260 return os << q.drops();
261}
262
263inline std::string
264to_string(XRPAmount const& amount)
265{
266 return std::to_string(amount.drops());
267}
268
269inline XRPAmount
270mulRatio(XRPAmount const& amt, std::uint32_t num, std::uint32_t den, bool roundUp)
271{
272 using namespace boost::multiprecision;
273
274 if (den == 0u)
275 Throw<std::runtime_error>("division by zero");
276
277 int128_t const amt128(amt.drops());
278 auto const neg = amt.drops() < 0;
279 auto const m = amt128 * num;
280 auto r = m / den;
281 if (m % den)
282 {
283 if (!neg && roundUp)
284 r += 1;
285 if (neg && !roundUp)
286 r -= 1;
287 }
289 Throw<std::overflow_error>("XRP mulRatio overflow");
290 return XRPAmount(r.convert_to<XRPAmount::value_type>());
291}
292
293} // namespace xrpl
Represents a JSON value.
Definition json_value.h:121
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
json::Value jsonClipped() const
Definition XRPAmount.h:203
static XRPAmount minPositiveAmount()
Definition XRPAmount.h:238
XRPAmount & operator-=(XRPAmount const &other)
Definition XRPAmount.h:96
constexpr XRPAmount(beast::Zero)
Definition XRPAmount.h:48
bool operator<(XRPAmount const &other) const
Definition XRPAmount.h:129
XRPAmount()=default
constexpr XRPAmount(value_type drops)
Definition XRPAmount.h:59
constexpr value_type value() const
Returns the underlying value.
Definition XRPAmount.h:225
friend std::istream & operator>>(std::istream &s, XRPAmount &val)
Definition XRPAmount.h:231
Dest dropsAs(XRPAmount defaultValue) const
Definition XRPAmount.h:193
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:163
std::int64_t value_type
Definition XRPAmount.h:32
value_type drops_
Definition XRPAmount.h:35
constexpr XRPAmount(XRPAmount const &other)=default
XRPAmount(Number const &x)
Definition XRPAmount.h:44
constexpr XRPAmount operator*(value_type const &rhs) const
Definition XRPAmount.h:71
unit::dropTag UnitType
Definition XRPAmount.h:31
bool operator==(XRPAmount const &other) const
Definition XRPAmount.h:117
bool operator==(value_type other) const
Definition XRPAmount.h:123
XRPAmount & operator+=(XRPAmount const &other)
Definition XRPAmount.h:90
constexpr XRPAmount & operator=(XRPAmount const &other)=default
Dest dropsAs(Dest defaultValue) const
Definition XRPAmount.h:186
XRPAmount & operator*=(value_type const &rhs)
constexpr XRPAmount & operator=(beast::Zero)
Definition XRPAmount.h:53
XRPAmount operator-() const
Definition XRPAmount.h:111
std::optional< Dest > dropsAs() const
Definition XRPAmount.h:173
friend constexpr XRPAmount operator*(value_type lhs, XRPAmount const &rhs)
Definition XRPAmount.h:83
constexpr double decimalXRP() const
Definition XRPAmount.h:250
XRPAmount & operator=(value_type drops)
Definition XRPAmount.h:64
constexpr int signum() const noexcept
Return the sign of the amount.
Definition XRPAmount.h:152
T is_integral_v
T is_signed_v
T lowest(T... args)
T max(T... args)
T min(T... args)
int Int
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr XRPAmount kDropsPerXrp
Number of drops per 1 XRP.
Definition XRPAmount.h:247
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition base_uint.h:673
constexpr std::optional< std::int64_t > checkedMul(std::int64_t a, std::int64_t b) noexcept
Multiply two signed 64-bit integers, returning std::nullopt when the exact mathematical product is no...
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
Zero allows classes to offer efficient comparisons to zero.
Definition Zero.h:26
T to_string(T... args)