1#include <xrpl/protocol/STAmount.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/MathUtilities.h>
5#include <xrpl/basics/Number.h>
6#include <xrpl/basics/contract.h>
7#include <xrpl/basics/safe_cast.h>
8#include <xrpl/beast/utility/Zero.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/json/json_forwards.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/Asset.h>
14#include <xrpl/protocol/Concepts.h>
15#include <xrpl/protocol/Feature.h>
16#include <xrpl/protocol/IOUAmount.h>
17#include <xrpl/protocol/Issue.h>
18#include <xrpl/protocol/MPTAmount.h>
19#include <xrpl/protocol/MPTIssue.h>
20#include <xrpl/protocol/Protocol.h>
21#include <xrpl/protocol/Rules.h>
22#include <xrpl/protocol/SField.h>
23#include <xrpl/protocol/STArray.h>
24#include <xrpl/protocol/STBase.h>
25#include <xrpl/protocol/STNumber.h>
26#include <xrpl/protocol/STObject.h>
27#include <xrpl/protocol/Serializer.h>
28#include <xrpl/protocol/SystemParameters.h>
29#include <xrpl/protocol/UintTypes.h>
30#include <xrpl/protocol/XRPAmount.h>
31#include <xrpl/protocol/jss.h>
33#include <boost/algorithm/string/classification.hpp>
34#include <boost/algorithm/string/split.hpp>
35#include <boost/multiprecision/detail/default_ops.hpp>
64 XRPL_ASSERT(amount.
exponent() == 0,
"xrpl::getInt64Value : exponent is zero");
70 "xrpl::getInt64Value : mantissa must roundtrip");
97 return v1.
native() == v2.
native() && issue1.currency == issue2.currency;
101 return issue1 == issue2;
163 int offset =
static_cast<int>(
value >> (64 - 10));
165 value &= ~(1023ull << (64 - 10));
169 bool const isNegative = (offset & 256) == 0;
170 offset = (offset & 255) - 97;
206 "xrpl::STAmount::STAmount(SField, std::uint64_t, bool) : maximum "
219 "xrpl::STAmount::STAmount(SField, STAmount) : maximum input");
230 "xrpl::STAmount::STAmount(std::uint64_t, bool) : maximum mantissa "
264 return emplace(n, buf, std::move(*
this));
279 XRPL_ASSERT(
offset_ == 0,
"xrpl::STAmount::xrp : amount is canonical");
309 XRPL_ASSERT(
offset_ == 0,
"xrpl::STAmount::mpt : amount is canonical");
320 XRPL_ASSERT(
integral() ==
false,
"xrpl::STAmount::operator=(IOUAmount) : is not integral");
344 auto const originalMantissa = number.
mantissa();
437 "xrpl::getRate : exponent inside range");
439 return (ret << (64 - 8)) | r.
mantissa();
455 [[maybe_unused]]
STAmount const amount{asset, value};
507 return ((rhs.
negative() ? -rhs : rhs) + (lhs.
negative() ? -lhs : lhs)) <= kMaxLoss;
520 UNREACHABLE(
"STAmount::canAdd : unexpected STAmount type");
595 UNREACHABLE(
"STAmount::canSubtract : unexpected STAmount type");
668 XRPL_ASSERT(
offset_ + 43 > 0,
"xrpl::STAmount::getText : minimum offset");
670 size_t const padPrefix = 27;
671 size_t const padSuffix = 23;
675 val.
append(padPrefix,
'0');
677 val.
append(padSuffix,
'0');
679 size_t const offset(
offset_ + 43);
681 auto preFrom(val.
begin());
682 auto const preTo(val.
begin() + offset);
684 auto const postFrom(val.
begin() + offset);
685 auto postTo(val.
end());
690 preFrom += padPrefix;
692 XRPL_ASSERT(postTo >= postFrom,
"xrpl::STAmount::getText : first distance check");
694 preFrom =
std::find_if(preFrom, preTo, [](
char c) {
return c !=
'0'; });
701 XRPL_ASSERT(postTo >= postFrom,
"xrpl::STAmount::getText : second distance check");
706 [](
char c) {
return c !=
'0'; })
710 if (preFrom == preTo)
716 ret.
append(preFrom, preTo);
719 if (postTo != postFrom)
722 ret.
append(postFrom, postTo);
741 auto u8 =
static_cast<unsigned char>(
kMpToken >> 56);
743 u8 |=
static_cast<unsigned char>(
kPositive >> 56);
748 [&](
Issue const& issue) {
751 XRPL_ASSERT(
offset_ == 0,
"xrpl::STAmount::add : zero offset");
787 auto const* v =
dynamic_cast<STAmount const*
>(&t);
788 return (v !=
nullptr) && (*v == *
this);
838 auto const set = [&](
auto const& val) {
839 auto const value = val.value();
896 int const exponent =
static_cast<int>(rate >> (64 - 8)) - 100;
907 return {asset, parts.mantissa, parts.exponent, parts.negative};
929 value = v[jss::value];
930 if (v.
isMember(jss::mpt_issuance_id))
933 currencyOrMPTID = v[jss::mpt_issuance_id];
937 currencyOrMPTID = v[jss::currency];
938 issuer = v[jss::issuer];
951 boost::split(elements, val, boost::is_any_of(
"\t\n\r ,/"));
953 if (elements.
size() > 3)
958 if (elements.
size() > 1)
959 currencyOrMPTID = elements[1];
961 if (elements.
size() > 2)
962 issuer = elements[2];
1005 if (value.asInt() >= 0)
1011 parts.
mantissa = value.asAbsUInt();
1015 else if (value.isUInt())
1019 else if (value.isString())
1044 JLOG(
debugLog().warn()) <<
"amountFromJsonNoThrow: caught: " << e.
what();
1099 if (value.mantissa() == 0)
1132 JLOG(j.
error()) <<
"hasInvalidAmount: depth exceeds 10";
1150 case STI_LEDGERENTRY:
1151 case STI_TRANSACTION:
1159 dynamic_cast<STObject const*
>(&field) ==
nullptr,
1160 "xrpl::hasInvalidAmount : unhandled STObject type");
1183 boost::multiprecision::uint128_t ret;
1185 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1191 std::format(
"overflow: ({} * {}) / {}", multiplier, multiplicand, divisor));
1194 return static_cast<uint64_t
>(ret);
1204 boost::multiprecision::uint128_t ret;
1206 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1213 "overflow: (({} * {}) + {}) / {}", multiplier, multiplicand, rounding, divisor));
1216 return static_cast<uint64_t
>(ret);
1271 int offset = numOffset - denOffset - 17;
1298 if (minV > 3000000000ull)
1301 if (((maxV >> 32) * minV) > 2095475792ull)
1311 if (minV > 3037000499ull)
1314 if (((maxV >> 32) * minV) > 2147483648ull)
1317 return STAmount(asset, minV * maxV);
1360 value += (loops >= 2) ? 9 : 10;
1390 bool hadRemainder =
false;
1398 hadRemainder |= (value != (newValue * 10));
1402 value += (hadRemainder && roundUp) ? 10 : 9;
1424 if (value.integral())
1434 if (value.exponent() >=
scale)
1444 return (value + referenceValue) - referenceValue;
1451class DontAffectNumberRoundMode
1458 DontAffectNumberRoundMode(DontAffectNumberRoundMode
const&) =
delete;
1460 DontAffectNumberRoundMode&
1461 operator=(DontAffectNumberRoundMode
const&) =
delete;
1465roundMode(
bool const resultNegative,
bool const roundUp)
1476 return roundUp ^ resultNegative ? Upward : Downward;
1482 bool const resultNegative,
1489 auto result =
STAmount{asset, number};
1494 [[maybe_unused]]
bool const nonzeroPositiveRoundUp =
1497 !nonzeroPositiveRoundUp || !asset.integral() || result !=
beast::kZero,
1498 "xrpl::roundNumberResult : positive rounded-up integral result is representable");
1500 if (roundUp && !resultNegative && !result)
1509 if (asset.integral())
1523template <
void (*CanonicalizeFunc)(
bool, std::u
int64_t&,
int&,
bool),
typename MightSaveRound>
1535 if (minV > 3000000000ull)
1538 if (((maxV >> 32) * minV) > 2095475792ull)
1549 if (minV > 3037000499ull)
1552 if (((maxV >> 32) * minV) > 2147483648ull)
1555 return STAmount(asset, minV * maxV);
1579 return roundNumberResult(asset, resultNegative, roundUp, result);
1613 int offset = offset1 + offset2 + 14;
1614 if (resultNegative != roundUp)
1616 CanonicalizeFunc(asset.
integral(), amount, offset, roundUp);
1622 return STAmount(asset, amount, offset, resultNegative);
1625 if (roundUp && !resultNegative && !result)
1639 return STAmount(asset, amount, offset, resultNegative);
1658template <
typename MightSaveRound>
1686 return roundNumberResult(asset, resultNegative, roundUp, result);
1721 int offset = numOffset - denOffset - 17;
1723 if (resultNegative != roundUp)
1731 MightSaveRound
const savedRound(roundUp ^ resultNegative ? Upward : Downward);
1732 return STAmount(asset, amount, offset, resultNegative);
1735 if (roundUp && !resultNegative && !result)
1749 return STAmount(asset, amount, offset, resultNegative);
1771 "xrpl::mulRatio(STAmount) : rounding is Upward or Downward");
1777 return {
mulRatio(amt.
iou(), num, den, roundUp), issue};
1780 return {
mulRatio(amt.
mpt(), num, den, roundUp), issue};
A generic endpoint for log messages.
bool isNull() const
isNull() tests to see if this field is null.
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
std::string asString() const
Returns the unquoted string value.
bool isObjectOrNull() const
bool isMember(char const *key) const
Return true if the object has a member named key.
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
constexpr bool native() const
constexpr bool holds() const
constexpr value_type const & value() const
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Floating point representation of amounts with high dynamic range.
A currency issued by an account.
constexpr value_type value() const
Returns the underlying value.
constexpr MPTID const & getMptID() const
Number is a floating point type that can represent a wide range of values.
constexpr rep mantissa() const noexcept
Returns the mantissa of the external view of the Number.
constexpr int exponent() const noexcept
Returns the exponent of the external view of the Number.
constexpr bool holds() const noexcept
void setIssue(Asset const &asset)
Set the Issue for this amount.
static constexpr std::uint64_t kIssuedCurrency
std::string getFullText() const override
static STAmount fromNumber(A const &asset, Number const &number)
void add(Serializer &s) const override
static std::uint64_t const kURateOne
std::uint64_t mantissa() const noexcept
static constexpr std::uint64_t kPositive
int signum() const noexcept
bool isEquivalent(STBase const &t) const override
STBase * copy(std::size_t n, void *buf) const override
std::string getText() const override
static constexpr int kMinOffset
SerializedTypeID getSType() const override
bool negative() const noexcept
static constexpr std::uint64_t kMaxNativeN
bool isDefault() const override
bool integral() const noexcept
static constexpr std::uint64_t kValueMask
STAmount & operator=(beast::Zero)
static std::unique_ptr< STAmount > construct(SerialIter &, SField const &name)
bool native() const noexcept
Asset const & asset() const
STAmount & operator+=(STAmount const &)
void setJson(json::Value &) const
static constexpr std::uint64_t kMpToken
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
STAmount & operator-=(STAmount const &)
int exponent() const noexcept
bool isZeroAtScale(int scale) const
Checks if this amount evaluates to zero when constrained to a specific accounting scale.
static constexpr std::uint64_t kMinValue
static constexpr std::uint64_t kMaxValue
STBase * move(std::size_t n, void *buf) override
static constexpr int kMaxOffset
STAmount const & value() const noexcept
STAmount(SerialIter &sit, SField const &name)
A type which can be exported to a well known binary format.
SField const & getFName() const
static STBase * emplace(std::size_t n, void *buf, T &&val)
virtual SerializedTypeID getSType() const
int addBitString(BaseUInt< Bits, Tag > const &v)
int add8(unsigned char byteValue)
constexpr value_type drops() const
Returns the number of drops.
T make_reverse_iterator(T... args)
@ Object
object value (collection of name/value pairs).
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr BaseUInt< Bits, Tag > operator+(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
static std::int64_t getMPTValue(STAmount const &amount)
constexpr std::optional< std::int64_t > checkedSub(std::int64_t a, std::int64_t b) noexcept
Subtract two signed 64-bit integers, returning std::nullopt when the exact mathematical difference is...
STAmount divide(STAmount const &amount, Rate const &rate)
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
static STAmount mulRoundImpl(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
constexpr std::optional< std::int64_t > checkedAdd(std::int64_t a, std::int64_t b) noexcept
Add two signed 64-bit integers, returning std::nullopt when the exact mathematical sum is not represe...
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
bool isLegalMPT(STAmount const &value)
bool isXRP(AccountID const &c)
beast::Journal debugLog()
Returns a debug journal.
NumberParts partsFromString(std::string const &number)
Number operator-(Number const &x, Number const &y)
STAmount mulRoundStrict(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
bool isFeatureEnabled(UInt256 const &feature, bool resultIfNoRules)
Check whether a feature is enabled in the current ledger rules.
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
bool isLegalNet(STAmount const &value)
bool validJSONAsset(json::Value const &jv)
STAmount amountFromString(Asset const &asset, std::string const &amount)
std::optional< Rules > const & getCurrentTransactionRules()
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
static std::uint64_t const kTenTO14
STAmount divRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
constexpr Dest unsafeCast(Src s) noexcept
STAmount roundToScale(STAmount const &value, std::int32_t scale, Number::RoundingMode rounding=Number::getround())
Round an arbitrary precision Amount to the precision of an STAmount that has a given exponent.
bool canAdd(STAmount const &amt1, STAmount const &amt2)
Safely checks if two STAmount values can be added without overflow, underflow, or precision loss.
bool amountFromJsonNoThrow(STAmount &result, json::Value const &jvSource)
STAmount amountFromQuality(std::uint64_t rate)
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
BaseUInt< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
static std::int64_t getSNValue(STAmount const &amount)
static bool areComparable(STAmount const &v1, STAmount const &v2)
STAmount amountFromJson(SField const &name, json::Value const &v)
bool hasInvalidAmount(STBase const &field, beast::Journal j)
bool isRepresentable(Asset const &asset, Number const &value)
Check whether an arbitrary precision Number can be converted to an STAmount of a given Asset without ...
static std::uint64_t const kTenTO17
STAmount mulRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
static std::int64_t getInt64Value(STAmount const &amount, bool valid, char const *error)
bool canSubtract(STAmount const &amt1, STAmount const &amt2)
Determines if it is safe to subtract one STAmount from another.
Dest safeDowncast(Src *s) noexcept
static std::uint64_t muldivRound(std::uint64_t multiplier, std::uint64_t multiplicand, std::uint64_t divisor, std::uint64_t rounding)
STAmount divRoundStrict(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
constexpr bool kIsMptissueV
static std::uint64_t muldiv(std::uint64_t multiplier, std::uint64_t multiplicand, std::uint64_t divisor)
static std::string const & systemCurrencyCode()
STAmount multiply(STAmount const &amount, Number const &frac, Number::RoundingMode rm)
static STAmount divRoundImpl(STAmount const &num, STAmount const &den, Asset const &asset, bool roundUp)
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
constexpr XRPAmount kInitialXrp
Configure the native currency.
static void canonicalizeRoundStrict(bool integral, std::uint64_t &value, int &offset, bool roundUp)
bool toIssuer(AccountID &, std::string const &)
Convert hex or base58 string to AccountID.
static std::uint64_t const kTenTO14M1
static void canonicalizeRound(bool integral, std::uint64_t &value, int &offset, bool)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Note, should be treated as flags that can be | and &.