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>
41 getStaticSTAmountCanonicalizeSwitchover()
43 static LocalValue<bool> r{
true};
51 return *getStaticSTAmountCanonicalizeSwitchover();
57 *getStaticSTAmountCanonicalizeSwitchover() = v;
69 Throw<std::runtime_error>(
"amount is not native!");
107 Throw<std::runtime_error>(
"negative zero is not canonical");
120 Throw<std::runtime_error>(
"invalid native currency");
125 Throw<std::runtime_error>(
"invalid native account");
128 int offset =
static_cast<int>(
value >> (64 - 10));
130 value &= ~(1023ull << (64 - 10));
134 bool isNegative = (offset & 256) == 0;
135 offset = (offset & 255) - 97;
140 Throw<std::runtime_error>(
"invalid currency value");
152 Throw<std::runtime_error>(
"invalid currency value");
174 , mIsNegative(negative)
189 , mIsNegative(negative)
205 , mIsNegative(negative)
211 :
STBase(name), mOffset(0), mIsNative(true)
221 , mIsNegative(negative)
236 , mIsNegative(negative)
248 , mIsNegative(mantissa != 0 && negative)
258 : mIssue(issue), mValue(mantissa), mOffset(exponent), mIsNegative(negative)
264 : mIssue(issue), mOffset(exponent)
287 , mOffset(amount.exponent())
289 , mIsNegative(amount <
beast::zero)
300 : mOffset(0), mIsNative(true), mIsNegative(amount <
beast::zero)
303 mValue = unsafe_cast<std::uint64_t>(-amount.
drops());
305 mValue = unsafe_cast<std::uint64_t>(amount.
drops());
313 return std::make_unique<STAmount>(sit, name);
325 return emplace(n, buf, std::move(*
this));
337 Throw<std::logic_error>(
338 "Cannot return non-native STAmount as XRPAmount");
352 Throw<std::logic_error>(
"Cannot return native STAmount as IOUAmount");
400 Throw<std::runtime_error>(
"Can't add amounts that are't comparable!");
402 if (v2 == beast::zero)
405 if (v1 == beast::zero)
453 if ((fv >= -10) && (fv <= 10))
497 if (offerOut == beast::zero)
502 if (r == beast::zero)
506 return (ret << (64 - 8)) | r.
mantissa();
574 if (*
this == beast::zero)
583 bool const scientific(
601 size_t const pad_prefix = 27;
602 size_t const pad_suffix = 23;
606 val.
append(pad_prefix,
'0');
608 val.
append(pad_suffix,
'0');
610 size_t const offset(
mOffset + 43);
612 auto pre_from(val.
begin());
613 auto const pre_to(val.
begin() + offset);
615 auto const post_from(val.
begin() + offset);
616 auto post_to(val.
end());
621 pre_from += pad_prefix;
623 assert(post_to >= post_from);
625 pre_from =
std::find_if(pre_from, pre_to, [](
char c) {
return c !=
'0'; });
630 post_to -= pad_suffix;
632 assert(post_to >= post_from);
637 [](
char c) {
return c !=
'0'; })
641 if (pre_from == pre_to)
644 ret.
append(pre_from, pre_to);
646 if (post_to != post_from)
649 ret.
append(post_from, post_to);
676 if (*
this == beast::zero)
697 return v && (*v == *
this);
745 Throw<std::runtime_error>(
746 "Native currency amount out of range");
773 Throw<std::runtime_error>(
774 "Native currency amount out of range");
782 Throw<std::runtime_error>(
"Native currency amount out of range");
811 Throw<std::runtime_error>(
"value overflow");
826 Throw<std::runtime_error>(
"value overflow");
858 int exponent =
static_cast<int>(rate >> (64 - 8)) - 100;
866 static boost::regex
const reNumber(
871 "([eE]([+-]?)([0-9]+))?"
873 boost::regex_constants::optimize);
877 if (!boost::regex_match(amount, match, reNumber))
878 Throw<std::runtime_error>(
"Number '" + amount +
"' is not valid");
891 if ((match[2].length() + match[4].length()) > 32)
892 Throw<std::runtime_error>(
"Number '" + amount +
"' is overlong");
894 bool negative = (match[1].matched && (match[1] ==
"-"));
897 if (
isXRP(issue) && match[3].matched)
898 Throw<std::runtime_error>(
"XRP must be specified in integral drops.");
903 if (!match[4].matched)
906 beast::lexicalCastThrow<std::uint64_t>(
std::string(match[2]));
912 mantissa = beast::lexicalCastThrow<std::uint64_t>(match[2] + match[4]);
913 exponent = -(match[4].length());
916 if (match[5].matched)
919 if (match[6].matched && (match[6] ==
"-"))
920 exponent -= beast::lexicalCastThrow<int>(
std::string(match[7]));
922 exponent += beast::lexicalCastThrow<int>(
std::string(match[7]));
925 return {issue, mantissa, exponent, negative};
933 bool negative =
false;
942 Throw<std::runtime_error>(
943 "XRP may not be specified with a null Json value");
947 value = v[jss::value];
948 currency = v[jss::currency];
949 issuer = v[jss::issuer];
961 boost::split(elements, val, boost::is_any_of(
"\t\n\r ,/"));
963 if (elements.
size() > 3)
964 Throw<std::runtime_error>(
"invalid amount string");
968 if (elements.
size() > 1)
969 currency = elements[1];
971 if (elements.
size() > 2)
972 issuer = elements[2];
985 Throw<std::runtime_error>(
"XRP may not be specified as an object");
992 Throw<std::runtime_error>(
"invalid currency");
995 Throw<std::runtime_error>(
"invalid issuer");
998 Throw<std::runtime_error>(
"invalid issuer");
1003 if (value.
asInt() >= 0)
1005 mantissa = value.
asInt();
1009 mantissa = -value.
asInt();
1021 mantissa = ret.mantissa();
1022 exponent = ret.exponent();
1023 negative = ret.negative();
1027 Throw<std::runtime_error>(
"invalid amount type");
1030 return {name, issue, mantissa, exponent, native, negative};
1044 <<
"amountFromJsonNoThrow: caught: " << e.
what();
1066 Throw<std::runtime_error>(
1067 "Can't compare amounts that are't comparable!");
1126 boost::multiprecision::uint128_t ret;
1128 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1133 Throw<std::overflow_error>(
1138 return static_cast<uint64_t
>(ret);
1148 boost::multiprecision::uint128_t ret;
1150 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1156 Throw<std::overflow_error>(
1162 return static_cast<uint64_t
>(ret);
1168 if (den == beast::zero)
1169 Throw<std::runtime_error>(
"division by zero");
1171 if (num == beast::zero)
1206 numOffset - denOffset - 17,
1213 if (v1 == beast::zero || v2 == beast::zero)
1223 if (minV > 3000000000ull)
1224 Throw<std::runtime_error>(
"Native value overflow");
1226 if (((maxV >> 32) * minV) > 2095475792ull)
1227 Throw<std::runtime_error>(
"Native value overflow");
1265 offset1 + offset2 + 14,
1285 value += (loops >= 2) ? 9 : 10;
1311 if (v1 == beast::zero || v2 == beast::zero)
1314 bool const xrp =
isXRP(issue);
1323 if (minV > 3000000000ull)
1324 Throw<std::runtime_error>(
"Native value overflow");
1326 if (((maxV >> 32) * minV) > 2095475792ull)
1327 Throw<std::runtime_error>(
"Native value overflow");
1366 int offset = offset1 + offset2 + 14;
1367 if (resultNegative != roundUp)
1369 STAmount result(issue, amount, offset, resultNegative);
1371 if (roundUp && !resultNegative && !result)
1385 return STAmount(issue, amount, offset, resultNegative);
1397 if (den == beast::zero)
1398 Throw<std::runtime_error>(
"division by zero");
1400 if (num == beast::zero)
1435 numVal,
tenTo17, denVal, (resultNegative != roundUp) ? denVal - 1 : 0);
1437 int offset = numOffset - denOffset - 17;
1439 if (resultNegative != roundUp)
1442 STAmount result(issue, amount, offset, resultNegative);
1443 if (roundUp && !resultNegative && !result)
1457 return STAmount(issue, amount, offset, resultNegative);