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>
46 Throw<std::runtime_error>(
"amount is not native!");
84 Throw<std::runtime_error>(
"negative zero is not canonical");
97 Throw<std::runtime_error>(
"invalid native currency");
102 Throw<std::runtime_error>(
"invalid native account");
105 int offset =
static_cast<int>(
value >> (64 - 10));
107 value &= ~(1023ull << (64 - 10));
111 bool isNegative = (offset & 256) == 0;
112 offset = (offset & 255) - 97;
117 Throw<std::runtime_error>(
"invalid currency value");
129 Throw<std::runtime_error>(
"invalid currency value");
151 , mIsNegative(negative)
166 , mIsNegative(negative)
182 , mIsNegative(negative)
188 :
STBase(name), mOffset(0), mIsNative(true)
198 , mIsNegative(negative)
213 , mIsNegative(negative)
225 , mIsNegative(mantissa != 0 && negative)
235 : mIssue(issue), mValue(mantissa), mOffset(exponent), mIsNegative(negative)
241 : mIssue(issue), mOffset(exponent)
264 , mOffset(amount.exponent())
266 , mIsNegative(amount <
beast::zero)
277 : mOffset(0), mIsNative(true), mIsNegative(amount <
beast::zero)
280 mValue = unsafe_cast<std::uint64_t>(-amount.
drops());
282 mValue = unsafe_cast<std::uint64_t>(amount.
drops());
290 return std::make_unique<STAmount>(sit, name);
302 Throw<std::logic_error>(
303 "Cannot return non-native STAmount as XRPAmount");
317 Throw<std::logic_error>(
"Cannot return native STAmount as IOUAmount");
352 Throw<std::runtime_error>(
"Can't add amounts that are't comparable!");
354 if (v2 == beast::zero)
357 if (v1 == beast::zero)
398 if ((fv >= -10) && (fv <= 10))
442 if (offerOut == beast::zero)
447 if (r == beast::zero)
451 return (ret << (64 - 8)) | r.
mantissa();
513 if (*
this == beast::zero)
522 bool const scientific(
540 size_t const pad_prefix = 27;
541 size_t const pad_suffix = 23;
545 val.
append(pad_prefix,
'0');
547 val.
append(pad_suffix,
'0');
549 size_t const offset(
mOffset + 43);
551 auto pre_from(val.
begin());
552 auto const pre_to(val.
begin() + offset);
554 auto const post_from(val.
begin() + offset);
555 auto post_to(val.
end());
560 pre_from += pad_prefix;
562 assert(post_to >= post_from);
564 pre_from =
std::find_if(pre_from, pre_to, [](
char c) {
return c !=
'0'; });
569 post_to -= pad_suffix;
571 assert(post_to >= post_from);
576 [](
char c) {
return c !=
'0'; })
580 if (pre_from == pre_to)
583 ret.
append(pre_from, pre_to);
585 if (post_to != post_from)
588 ret.
append(post_from, post_to);
615 if (*
this == beast::zero)
636 return v && (*v == *
this);
685 Throw<std::runtime_error>(
"Native currency amount out of range");
708 Throw<std::runtime_error>(
"value overflow");
723 Throw<std::runtime_error>(
"value overflow");
755 int exponent =
static_cast<int>(rate >> (64 - 8)) - 100;
763 static boost::regex
const reNumber(
768 "([eE]([+-]?)([0-9]+))?"
770 boost::regex_constants::optimize);
774 if (!boost::regex_match(amount, match, reNumber))
775 Throw<std::runtime_error>(
"Number '" + amount +
"' is not valid");
788 if ((match[2].length() + match[4].length()) > 32)
789 Throw<std::runtime_error>(
"Number '" + amount +
"' is overlong");
791 bool negative = (match[1].matched && (match[1] ==
"-"));
794 if (
isXRP(issue) && match[3].matched)
795 Throw<std::runtime_error>(
"XRP must be specified in integral drops.");
800 if (!match[4].matched)
803 beast::lexicalCastThrow<std::uint64_t>(
std::string(match[2]));
809 mantissa = beast::lexicalCastThrow<std::uint64_t>(match[2] + match[4]);
810 exponent = -(match[4].length());
813 if (match[5].matched)
816 if (match[6].matched && (match[6] ==
"-"))
817 exponent -= beast::lexicalCastThrow<int>(
std::string(match[7]));
819 exponent += beast::lexicalCastThrow<int>(
std::string(match[7]));
822 return {issue, mantissa, exponent, negative};
830 bool negative =
false;
839 Throw<std::runtime_error>(
840 "XRP may not be specified with a null Json value");
844 value = v[jss::value];
845 currency = v[jss::currency];
846 issuer = v[jss::issuer];
858 boost::split(elements, val, boost::is_any_of(
"\t\n\r ,/"));
860 if (elements.
size() > 3)
861 Throw<std::runtime_error>(
"invalid amount string");
865 if (elements.
size() > 1)
866 currency = elements[1];
868 if (elements.
size() > 2)
869 issuer = elements[2];
882 Throw<std::runtime_error>(
"XRP may not be specified as an object");
889 Throw<std::runtime_error>(
"invalid currency");
892 Throw<std::runtime_error>(
"invalid issuer");
895 Throw<std::runtime_error>(
"invalid issuer");
900 if (value.
asInt() >= 0)
902 mantissa = value.
asInt();
906 mantissa = -value.
asInt();
918 mantissa = ret.mantissa();
919 exponent = ret.exponent();
920 negative = ret.negative();
924 Throw<std::runtime_error>(
"invalid amount type");
927 return {name, issue, mantissa, exponent, native, negative};
941 <<
"amountFromJsonNoThrow: caught: " << e.
what();
963 Throw<std::runtime_error>(
964 "Can't compare amounts that are't comparable!");
1023 boost::multiprecision::uint128_t ret;
1025 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1030 Throw<std::overflow_error>(
1035 return static_cast<uint64_t
>(ret);
1045 boost::multiprecision::uint128_t ret;
1047 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1053 Throw<std::overflow_error>(
1059 return static_cast<uint64_t
>(ret);
1065 if (den == beast::zero)
1066 Throw<std::runtime_error>(
"division by zero");
1068 if (num == beast::zero)
1103 numOffset - denOffset - 17,
1110 if (v1 == beast::zero || v2 == beast::zero)
1120 if (minV > 3000000000ull)
1121 Throw<std::runtime_error>(
"Native value overflow");
1123 if (((maxV >> 32) * minV) > 2095475792ull)
1124 Throw<std::runtime_error>(
"Native value overflow");
1159 offset1 + offset2 + 14,
1179 value += (loops >= 2) ? 9 : 10;
1205 if (v1 == beast::zero || v2 == beast::zero)
1208 bool const xrp =
isXRP(issue);
1217 if (minV > 3000000000ull)
1218 Throw<std::runtime_error>(
"Native value overflow");
1220 if (((maxV >> 32) * minV) > 2095475792ull)
1221 Throw<std::runtime_error>(
"Native value overflow");
1260 int offset = offset1 + offset2 + 14;
1261 if (resultNegative != roundUp)
1263 STAmount result(issue, amount, offset, resultNegative);
1265 if (roundUp && !resultNegative && !result)
1279 return STAmount(issue, amount, offset, resultNegative);
1291 if (den == beast::zero)
1292 Throw<std::runtime_error>(
"division by zero");
1294 if (num == beast::zero)
1329 numVal,
tenTo17, denVal, (resultNegative != roundUp) ? denVal - 1 : 0);
1331 int offset = numOffset - denOffset - 17;
1333 if (resultNegative != roundUp)
1336 STAmount result(issue, amount, offset, resultNegative);
1337 if (roundUp && !resultNegative && !result)
1351 return STAmount(issue, amount, offset, resultNegative);