20 #include <ripple/basics/Log.h>
21 #include <ripple/basics/contract.h>
22 #include <ripple/basics/safe_cast.h>
23 #include <ripple/beast/core/LexicalCast.h>
24 #include <ripple/protocol/STAmount.h>
25 #include <ripple/protocol/SystemParameters.h>
26 #include <ripple/protocol/UintTypes.h>
27 #include <ripple/protocol/jss.h>
28 #include <boost/algorithm/string.hpp>
29 #include <boost/multiprecision/cpp_int.hpp>
30 #include <boost/regex.hpp>
48 Throw<std::runtime_error>(
"amount is not native!");
86 Throw<std::runtime_error>(
"negative zero is not canonical");
99 Throw<std::runtime_error>(
"invalid native currency");
104 Throw<std::runtime_error>(
"invalid native account");
107 int offset =
static_cast<int>(
value >> (64 - 10));
109 value &= ~(1023ull << (64 - 10));
113 bool isNegative = (offset & 256) == 0;
114 offset = (offset & 255) - 97;
119 Throw<std::runtime_error>(
"invalid currency value");
131 Throw<std::runtime_error>(
"invalid currency value");
153 , mIsNegative(negative)
168 , mIsNegative(negative)
184 , mIsNegative(negative)
190 :
STBase(name), mOffset(0), mIsNative(true)
200 , mIsNegative(negative)
215 , mIsNegative(negative)
227 , mIsNegative(mantissa != 0 && negative)
237 : mIssue(issue), mValue(mantissa), mOffset(exponent), mIsNegative(negative)
243 : mIssue(issue), mOffset(exponent)
266 , mOffset(amount.exponent())
268 , mIsNegative(amount <
beast::zero)
279 : mOffset(0), mIsNative(true), mIsNegative(amount <
beast::zero)
282 mValue = unsafe_cast<std::uint64_t>(-amount.
drops());
284 mValue = unsafe_cast<std::uint64_t>(amount.
drops());
292 return std::make_unique<STAmount>(sit, name);
304 return emplace(n, buf, std::move(*
this));
316 Throw<std::logic_error>(
317 "Cannot return non-native STAmount as XRPAmount");
331 Throw<std::logic_error>(
"Cannot return native STAmount as IOUAmount");
366 Throw<std::runtime_error>(
"Can't add amounts that are't comparable!");
368 if (v2 == beast::zero)
371 if (v1 == beast::zero)
412 if ((fv >= -10) && (fv <= 10))
456 if (offerOut == beast::zero)
461 if (r == beast::zero)
465 return (ret << (64 - 8)) | r.
mantissa();
533 if (*
this == beast::zero)
542 bool const scientific(
560 size_t const pad_prefix = 27;
561 size_t const pad_suffix = 23;
565 val.
append(pad_prefix,
'0');
567 val.
append(pad_suffix,
'0');
569 size_t const offset(
mOffset + 43);
571 auto pre_from(val.
begin());
572 auto const pre_to(val.
begin() + offset);
574 auto const post_from(val.
begin() + offset);
575 auto post_to(val.
end());
580 pre_from += pad_prefix;
582 assert(post_to >= post_from);
584 pre_from =
std::find_if(pre_from, pre_to, [](
char c) {
return c !=
'0'; });
589 post_to -= pad_suffix;
591 assert(post_to >= post_from);
596 [](
char c) {
return c !=
'0'; })
600 if (pre_from == pre_to)
603 ret.
append(pre_from, pre_to);
605 if (post_to != post_from)
608 ret.
append(post_from, post_to);
635 if (*
this == beast::zero)
656 return v && (*v == *
this);
704 Throw<std::runtime_error>(
705 "Native currency amount out of range");
721 Throw<std::runtime_error>(
722 "Native currency amount out of range");
729 Throw<std::runtime_error>(
"Native currency amount out of range");
752 Throw<std::runtime_error>(
"value overflow");
767 Throw<std::runtime_error>(
"value overflow");
799 int exponent =
static_cast<int>(rate >> (64 - 8)) - 100;
807 static boost::regex
const reNumber(
812 "([eE]([+-]?)([0-9]+))?"
814 boost::regex_constants::optimize);
818 if (!boost::regex_match(amount, match, reNumber))
819 Throw<std::runtime_error>(
"Number '" + amount +
"' is not valid");
832 if ((match[2].length() + match[4].length()) > 32)
833 Throw<std::runtime_error>(
"Number '" + amount +
"' is overlong");
835 bool negative = (match[1].matched && (match[1] ==
"-"));
838 if (
isXRP(issue) && match[3].matched)
839 Throw<std::runtime_error>(
"XRP must be specified in integral drops.");
844 if (!match[4].matched)
847 beast::lexicalCastThrow<std::uint64_t>(
std::string(match[2]));
853 mantissa = beast::lexicalCastThrow<std::uint64_t>(match[2] + match[4]);
854 exponent = -(match[4].length());
857 if (match[5].matched)
860 if (match[6].matched && (match[6] ==
"-"))
861 exponent -= beast::lexicalCastThrow<int>(
std::string(match[7]));
863 exponent += beast::lexicalCastThrow<int>(
std::string(match[7]));
866 return {issue, mantissa, exponent, negative};
874 bool negative =
false;
883 Throw<std::runtime_error>(
884 "XRP may not be specified with a null Json value");
888 value = v[jss::value];
889 currency = v[jss::currency];
890 issuer = v[jss::issuer];
902 boost::split(elements, val, boost::is_any_of(
"\t\n\r ,/"));
904 if (elements.
size() > 3)
905 Throw<std::runtime_error>(
"invalid amount string");
909 if (elements.
size() > 1)
910 currency = elements[1];
912 if (elements.
size() > 2)
913 issuer = elements[2];
926 Throw<std::runtime_error>(
"XRP may not be specified as an object");
933 Throw<std::runtime_error>(
"invalid currency");
936 Throw<std::runtime_error>(
"invalid issuer");
939 Throw<std::runtime_error>(
"invalid issuer");
944 if (value.
asInt() >= 0)
946 mantissa = value.
asInt();
950 mantissa = -value.
asInt();
962 mantissa = ret.mantissa();
963 exponent = ret.exponent();
964 negative = ret.negative();
968 Throw<std::runtime_error>(
"invalid amount type");
971 return {name, issue, mantissa, exponent, native, negative};
985 <<
"amountFromJsonNoThrow: caught: " << e.
what();
1007 Throw<std::runtime_error>(
1008 "Can't compare amounts that are't comparable!");
1067 boost::multiprecision::uint128_t ret;
1069 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1074 Throw<std::overflow_error>(
1079 return static_cast<uint64_t
>(ret);
1089 boost::multiprecision::uint128_t ret;
1091 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1097 Throw<std::overflow_error>(
1103 return static_cast<uint64_t
>(ret);
1109 if (den == beast::zero)
1110 Throw<std::runtime_error>(
"division by zero");
1112 if (num == beast::zero)
1147 numOffset - denOffset - 17,
1154 if (v1 == beast::zero || v2 == beast::zero)
1164 if (minV > 3000000000ull)
1165 Throw<std::runtime_error>(
"Native value overflow");
1167 if (((maxV >> 32) * minV) > 2095475792ull)
1168 Throw<std::runtime_error>(
"Native value overflow");
1203 offset1 + offset2 + 14,
1223 value += (loops >= 2) ? 9 : 10;
1249 if (v1 == beast::zero || v2 == beast::zero)
1252 bool const xrp =
isXRP(issue);
1261 if (minV > 3000000000ull)
1262 Throw<std::runtime_error>(
"Native value overflow");
1264 if (((maxV >> 32) * minV) > 2095475792ull)
1265 Throw<std::runtime_error>(
"Native value overflow");
1304 int offset = offset1 + offset2 + 14;
1305 if (resultNegative != roundUp)
1307 STAmount result(issue, amount, offset, resultNegative);
1309 if (roundUp && !resultNegative && !result)
1323 return STAmount(issue, amount, offset, resultNegative);
1335 if (den == beast::zero)
1336 Throw<std::runtime_error>(
"division by zero");
1338 if (num == beast::zero)
1373 numVal,
tenTo17, denVal, (resultNegative != roundUp) ? denVal - 1 : 0);
1375 int offset = numOffset - denOffset - 17;
1377 if (resultNegative != roundUp)
1380 STAmount result(issue, amount, offset, resultNegative);
1381 if (roundUp && !resultNegative && !result)
1395 return STAmount(issue, amount, offset, resultNegative);