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 Throw<std::logic_error>(
305 "Cannot return non-native STAmount as XRPAmount");
319 Throw<std::logic_error>(
"Cannot return native STAmount as IOUAmount");
354 Throw<std::runtime_error>(
"Can't add amounts that are't comparable!");
356 if (v2 == beast::zero)
359 if (v1 == beast::zero)
400 if ((fv >= -10) && (fv <= 10))
444 if (offerOut == beast::zero)
449 if (r == beast::zero)
453 return (ret << (64 - 8)) | r.
mantissa();
515 if (*
this == beast::zero)
524 bool const scientific(
542 size_t const pad_prefix = 27;
543 size_t const pad_suffix = 23;
547 val.
append(pad_prefix,
'0');
549 val.
append(pad_suffix,
'0');
551 size_t const offset(
mOffset + 43);
553 auto pre_from(val.
begin());
554 auto const pre_to(val.
begin() + offset);
556 auto const post_from(val.
begin() + offset);
557 auto post_to(val.
end());
562 pre_from += pad_prefix;
564 assert(post_to >= post_from);
566 pre_from =
std::find_if(pre_from, pre_to, [](
char c) {
return c !=
'0'; });
571 post_to -= pad_suffix;
573 assert(post_to >= post_from);
578 [](
char c) {
return c !=
'0'; })
582 if (pre_from == pre_to)
585 ret.
append(pre_from, pre_to);
587 if (post_to != post_from)
590 ret.
append(post_from, post_to);
617 if (*
this == beast::zero)
638 return v && (*v == *
this);
680 Throw<std::runtime_error>(
681 "Native currency amount out of range");
697 Throw<std::runtime_error>(
698 "Native currency amount out of range");
705 Throw<std::runtime_error>(
"Native currency amount out of range");
728 Throw<std::runtime_error>(
"value overflow");
743 Throw<std::runtime_error>(
"value overflow");
775 int exponent =
static_cast<int>(rate >> (64 - 8)) - 100;
783 static boost::regex
const reNumber(
788 "([eE]([+-]?)([0-9]+))?"
790 boost::regex_constants::optimize);
794 if (!boost::regex_match(amount, match, reNumber))
795 Throw<std::runtime_error>(
"Number '" + amount +
"' is not valid");
808 if ((match[2].length() + match[4].length()) > 32)
809 Throw<std::runtime_error>(
"Number '" + amount +
"' is overlong");
811 bool negative = (match[1].matched && (match[1] ==
"-"));
814 if (
isXRP(issue) && match[3].matched)
815 Throw<std::runtime_error>(
"XRP must be specified in integral drops.");
820 if (!match[4].matched)
823 beast::lexicalCastThrow<std::uint64_t>(
std::string(match[2]));
829 mantissa = beast::lexicalCastThrow<std::uint64_t>(match[2] + match[4]);
830 exponent = -(match[4].length());
833 if (match[5].matched)
836 if (match[6].matched && (match[6] ==
"-"))
837 exponent -= beast::lexicalCastThrow<int>(
std::string(match[7]));
839 exponent += beast::lexicalCastThrow<int>(
std::string(match[7]));
842 return {issue, mantissa, exponent, negative};
850 bool negative =
false;
859 Throw<std::runtime_error>(
860 "XRP may not be specified with a null Json value");
864 value = v[jss::value];
865 currency = v[jss::currency];
866 issuer = v[jss::issuer];
878 boost::split(elements, val, boost::is_any_of(
"\t\n\r ,/"));
880 if (elements.
size() > 3)
881 Throw<std::runtime_error>(
"invalid amount string");
885 if (elements.
size() > 1)
886 currency = elements[1];
888 if (elements.
size() > 2)
889 issuer = elements[2];
902 Throw<std::runtime_error>(
"XRP may not be specified as an object");
909 Throw<std::runtime_error>(
"invalid currency");
912 Throw<std::runtime_error>(
"invalid issuer");
915 Throw<std::runtime_error>(
"invalid issuer");
920 if (value.
asInt() >= 0)
922 mantissa = value.
asInt();
926 mantissa = -value.
asInt();
938 mantissa = ret.mantissa();
939 exponent = ret.exponent();
940 negative = ret.negative();
944 Throw<std::runtime_error>(
"invalid amount type");
947 return {name, issue, mantissa, exponent, native, negative};
961 <<
"amountFromJsonNoThrow: caught: " << e.
what();
983 Throw<std::runtime_error>(
984 "Can't compare amounts that are't comparable!");
1043 boost::multiprecision::uint128_t ret;
1045 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1050 Throw<std::overflow_error>(
1055 return static_cast<uint64_t
>(ret);
1065 boost::multiprecision::uint128_t ret;
1067 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1073 Throw<std::overflow_error>(
1079 return static_cast<uint64_t
>(ret);
1085 if (den == beast::zero)
1086 Throw<std::runtime_error>(
"division by zero");
1088 if (num == beast::zero)
1123 numOffset - denOffset - 17,
1130 if (v1 == beast::zero || v2 == beast::zero)
1140 if (minV > 3000000000ull)
1141 Throw<std::runtime_error>(
"Native value overflow");
1143 if (((maxV >> 32) * minV) > 2095475792ull)
1144 Throw<std::runtime_error>(
"Native value overflow");
1179 offset1 + offset2 + 14,
1199 value += (loops >= 2) ? 9 : 10;
1225 if (v1 == beast::zero || v2 == beast::zero)
1228 bool const xrp =
isXRP(issue);
1237 if (minV > 3000000000ull)
1238 Throw<std::runtime_error>(
"Native value overflow");
1240 if (((maxV >> 32) * minV) > 2095475792ull)
1241 Throw<std::runtime_error>(
"Native value overflow");
1280 int offset = offset1 + offset2 + 14;
1281 if (resultNegative != roundUp)
1283 STAmount result(issue, amount, offset, resultNegative);
1285 if (roundUp && !resultNegative && !result)
1299 return STAmount(issue, amount, offset, resultNegative);
1311 if (den == beast::zero)
1312 Throw<std::runtime_error>(
"division by zero");
1314 if (num == beast::zero)
1349 numVal,
tenTo17, denVal, (resultNegative != roundUp) ? denVal - 1 : 0);
1351 int offset = numOffset - denOffset - 17;
1353 if (resultNegative != roundUp)
1356 STAmount result(issue, amount, offset, resultNegative);
1357 if (roundUp && !resultNegative && !result)
1371 return STAmount(issue, amount, offset, resultNegative);