xrpld
Loading...
Searching...
No Matches
AmountConversions.h
1#pragma once
2
3#include <xrpl/protocol/IOUAmount.h>
4#include <xrpl/protocol/Protocol.h>
5#include <xrpl/protocol/STAmount.h>
6#include <xrpl/protocol/XRPAmount.h>
7
8#include <type_traits>
9
10namespace xrpl {
11
12inline STAmount
13toSTAmount(IOUAmount const& iou, Asset const& asset)
14{
15 XRPL_ASSERT(asset.holds<Issue>(), "xrpl::toSTAmount : is Issue");
16 bool const isNeg = iou.signum() < 0;
17 std::uint64_t const umant = isNeg ? -iou.mantissa() : iou.mantissa();
18 return STAmount(asset, umant, iou.exponent(), isNeg, STAmount::Unchecked());
19}
20
21inline STAmount
23{
24 return toSTAmount(iou, noIssue());
25}
26
27inline STAmount
29{
30 bool const isNeg = xrp.signum() < 0;
31 std::uint64_t const umant = isNeg ? -xrp.drops() : xrp.drops();
32 return STAmount(umant, isNeg);
33}
34
35inline STAmount
36toSTAmount(XRPAmount const& xrp, Asset const& asset)
37{
38 XRPL_ASSERT(isXRP(asset), "xrpl::toSTAmount : is XRP");
39 return toSTAmount(xrp);
40}
41
42inline STAmount
44{
45 return STAmount(mpt, noMPT());
46}
47
48inline STAmount
49toSTAmount(MPTAmount const& mpt, Asset const& asset)
50{
51 XRPL_ASSERT(asset.holds<MPTIssue>(), "xrpl::toSTAmount : is MPT");
52 return STAmount(mpt, asset.get<MPTIssue>());
53}
54
55template <class T>
56T
57toAmount(STAmount const& amt) = delete;
58
59template <>
62{
63 return amt;
64}
65
66template <>
69{
70 XRPL_ASSERT(
72 "xrpl::toAmount<IOUAmount> : maximum mantissa");
73 bool const isNeg = amt.negative();
74 std::int64_t const sMant = isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
75
76 XRPL_ASSERT(!isXRP(amt), "xrpl::toAmount<IOUAmount> : is not XRP");
77 return IOUAmount(sMant, amt.exponent());
78}
79
80template <>
83{
84 XRPL_ASSERT(
86 "xrpl::toAmount<XRPAmount> : maximum mantissa");
87 bool const isNeg = amt.negative();
88 std::int64_t const sMant = isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
89
90 XRPL_ASSERT(isXRP(amt), "xrpl::toAmount<XRPAmount> : is XRP");
91 return XRPAmount(sMant);
92}
93
94template <>
97{
98 XRPL_ASSERT(
99 amt.holds<MPTIssue>() && amt.mantissa() <= kMaxMpTokenAmount && amt.exponent() == 0,
100 "xrpl::toAmount<MPTAmount> : maximum mantissa");
101 if (amt.mantissa() > kMaxMpTokenAmount || amt.exponent() != 0)
102 Throw<std::runtime_error>("toAmount<MPTAmount>: invalid mantissa or exponent");
103 bool const isNeg = amt.negative();
104 std::int64_t const sMant = isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
105
106 return MPTAmount(sMant);
107}
108
109template <class T>
110T
111toAmount(IOUAmount const& amt) = delete;
112
113template <>
116{
117 return amt;
118}
119
120template <class T>
121T
122toAmount(XRPAmount const& amt) = delete;
123
124template <>
127{
128 return amt;
129}
130
131template <class T>
132T
133toAmount(MPTAmount const& amt) = delete;
134
135template <>
138{
139 return amt;
140}
141
142template <typename T>
143T
145{
147 if (isXRP(asset))
148 Number::setround(mode);
149
150 if constexpr (std::is_same_v<IOUAmount, T>)
151 {
152 return IOUAmount(n);
153 }
154 else if constexpr (std::is_same_v<XRPAmount, T>)
155 {
156 return XRPAmount(static_cast<std::int64_t>(n));
157 }
158 else if constexpr (std::is_same_v<MPTAmount, T>)
159 {
160 return MPTAmount(static_cast<std::int64_t>(n));
161 }
162 else if constexpr (std::is_same_v<STAmount, T>)
163 {
164 if (isXRP(asset))
165 return STAmount(asset, static_cast<std::int64_t>(n));
166 return STAmount(asset, n);
167 }
168 else
169 {
170 static constexpr bool kAlwaysFalse = !std::is_same_v<T, T>;
171 static_assert(kAlwaysFalse, "Unsupported type for toAmount");
172 }
173}
174
175template <typename T>
176T
177toMaxAmount(Asset const& asset)
178{
179 if constexpr (std::is_same_v<IOUAmount, T>)
180 {
182 }
183 else if constexpr (std::is_same_v<XRPAmount, T>)
184 {
185 return XRPAmount(static_cast<std::int64_t>(STAmount::kMaxNativeN));
186 }
187 else if constexpr (std::is_same_v<MPTAmount, T>)
188 {
190 }
191 else if constexpr (std::is_same_v<STAmount, T>)
192 {
193 return asset.visit(
194 [](Issue const& issue) {
195 if (isXRP(issue))
196 return STAmount(issue, static_cast<std::int64_t>(STAmount::kMaxNativeN));
198 },
199 [](MPTIssue const& issue) { return STAmount(issue, kMaxMpTokenAmount); });
200 }
201 else
202 {
203 static constexpr bool kAlwaysFalse = !std::is_same_v<T, T>;
204 static_assert(kAlwaysFalse, "Unsupported type for toMaxAmount");
205 }
206}
207
208inline STAmount
210{
211 return toAmount<STAmount>(asset, n, mode);
212}
213
214template <typename T>
215Asset
216getAsset(T const& amt)
217{
218 if constexpr (std::is_same_v<IOUAmount, T>)
219 {
220 return noIssue();
221 }
222 else if constexpr (std::is_same_v<XRPAmount, T>)
223 {
224 return xrpIssue();
225 }
226 else if constexpr (std::is_same_v<MPTAmount, T>)
227 {
228 return noMPT();
229 }
230 else if constexpr (std::is_same_v<STAmount, T>)
231 {
232 return amt.asset();
233 }
234 else
235 {
236 static constexpr bool kAlwaysFalse = !std::is_same_v<T, T>;
237 static_assert(kAlwaysFalse, "Unsupported type for getIssue");
238 }
239}
240
241template <typename T>
242constexpr T
243get(STAmount const& a)
244{
245 if constexpr (std::is_same_v<IOUAmount, T>)
246 {
247 return a.iou();
248 }
249 else if constexpr (std::is_same_v<XRPAmount, T>)
250 {
251 return a.xrp();
252 }
253 else if constexpr (std::is_same_v<MPTAmount, T>)
254 {
255 return a.mpt();
256 }
257 else if constexpr (std::is_same_v<STAmount, T>)
258 {
259 return a;
260 }
261 else
262 {
263 constexpr bool kAlwaysFalse = !std::is_same_v<T, T>;
264 static_assert(kAlwaysFalse, "Unsupported type for get");
265 }
266}
267
268} // namespace xrpl
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition Asset.h:107
constexpr TIss const & get() const
constexpr bool holds() const
Definition Asset.h:166
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:24
mantissa_type mantissa() const noexcept
Definition IOUAmount.h:165
exponent_type exponent() const noexcept
Definition IOUAmount.h:159
int signum() const noexcept
Return the sign of the amount.
Definition IOUAmount.h:151
A currency issued by an account.
Definition Issue.h:13
Number is a floating point type that can represent a wide range of values.
Definition Number.h:306
static RoundingMode setround(RoundingMode inMode)
Definition Number.cpp:111
static RoundingMode getround()
Definition Number.cpp:105
constexpr bool holds() const noexcept
Definition STAmount.h:460
std::uint64_t mantissa() const noexcept
Definition STAmount.h:472
IOUAmount iou() const
Definition STAmount.cpp:286
bool negative() const noexcept
Definition STAmount.h:466
static constexpr std::uint64_t kMaxNativeN
Definition STAmount.h:58
MPTAmount mpt() const
Definition STAmount.cpp:301
int exponent() const noexcept
Definition STAmount.h:441
static constexpr std::uint64_t kMaxValue
Definition STAmount.h:53
XRPAmount xrp() const
Definition STAmount.cpp:271
static constexpr int kMaxOffset
Definition STAmount.h:48
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:159
constexpr int signum() const noexcept
Return the sign of the amount.
Definition XRPAmount.h:150
T is_same_v
T max(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:97
bool isXRP(AccountID const &c)
Definition AccountID.h:70
T toMaxAmount(Asset const &asset)
MPTAmount toAmount< MPTAmount >(STAmount const &amt)
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
T toAmount(STAmount const &amt)=delete
XRPAmount toAmount< XRPAmount >(STAmount const &amt)
Asset getAsset(T const &amt)
MPTID noMPT()
Definition MPTIssue.h:103
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition Issue.h:105
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
Definition Protocol.h:238
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
STAmount toSTAmount(IOUAmount const &iou, Asset const &asset)
STAmount toAmount< STAmount >(STAmount const &amt)