3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/LocalValue.h>
5#include <xrpl/basics/Number.h>
6#include <xrpl/beast/utility/Journal.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/protocol/Asset.h>
9#include <xrpl/protocol/IOUAmount.h>
10#include <xrpl/protocol/Issue.h>
11#include <xrpl/protocol/MPTAmount.h>
12#include <xrpl/protocol/Protocol.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/STBase.h>
15#include <xrpl/protocol/Serializer.h>
16#include <xrpl/protocol/XRPAmount.h>
17#include <xrpl/protocol/json_get_or_throw.h>
54 static_assert(
kMaxValue == 9'999'999'999'999'999ull);
75 template <AssetType A>
84 template <AssetType A>
93 template <AssetType A>
109 template <AssetType A>
117 template <AssetType A>
120 template <AssetType A>
123 template <AssetType A>
126 template <AssetType A>
153 [[nodiscard]] constexpr
bool
154 holds() const noexcept;
159 [[nodiscard]]
std::uint64_t
162 [[nodiscard]]
Asset const&
166 constexpr TIss const&
187 value() const noexcept;
213 operator
bool() const noexcept;
257 [[nodiscard]]
std::
string
260 [[nodiscard]]
std::
string
295 copy(
std::
size_t n,
void* buf) const override;
297 move(
std::
size_t n,
void* buf) override;
320template <AssetType A>
331template <AssetType A>
345 "xrpl::STAmount::STAmount(SField, A, std::uint64_t, int, bool) : "
346 "maximum mantissa input");
356template <AssetType A>
364template <AssetType A>
370template <AssetType A>
458template <Val
idIssueType TIss>
462 return asset_.holds<TIss>();
483template <Val
idIssueType TIss>
484[[nodiscard]]
constexpr TIss
const&
487 return asset_.get<TIss>();
490template <Val
idIssueType TIss>
494 return asset_.get<TIss>();
500 return asset_.getIssuer();
518operator bool() const noexcept
520 return *
this != beast::kZero;
526 return asset().visit(
549template <AssetType A>
556 if (
asset.integral())
570 if (*
this != beast::kZero)
597[[nodiscard]]
inline bool
603[[nodiscard]]
inline bool
607 (!value.negative() && value.exponent() == 0 && value.mantissa() <=
kMaxMpTokenAmount);
623operator==(STAmount
const& lhs, STAmount
const& rhs);
625operator<(STAmount
const& lhs, STAmount
const& rhs);
630 return !(lhs == rhs);
661operator+(STAmount
const& v1, STAmount
const& v2);
663operator-(STAmount
const& v1, STAmount
const& v2);
666divide(STAmount
const& v1, STAmount
const& v2, Asset
const& asset);
669multiply(STAmount
const& v1, STAmount
const& v2, Asset
const& asset);
673mulRound(STAmount
const& v1, STAmount
const& v2, Asset
const& asset,
bool roundUp);
677mulRoundStrict(STAmount
const& v1, STAmount
const& v2, Asset
const& asset,
bool roundUp);
681divRound(STAmount
const& v1, STAmount
const& v2, Asset
const& asset,
bool roundUp);
685divRoundStrict(STAmount
const& v1, STAmount
const& v2, Asset
const& asset,
bool roundUp);
691getRate(STAmount
const& offerOut, STAmount
const& offerIn);
705[[nodiscard]] STAmount
707 STAmount
const& value,
720template <AssetType A>
738template <AssetType A>
764canAdd(STAmount
const& amt1, STAmount
const& amt2);
767canSubtract(STAmount
const& amt1, STAmount
const& amt2);
792 using namespace xrpl;
A generic endpoint for log messages.
Lightweight wrapper to tag static string.
bool isMember(char const *key) const
Return true if the object has a member named key.
Floating point representation of amounts with high dynamic range.
mantissa_type mantissa() const noexcept
A currency issued by an account.
constexpr value_type value() const
Returns the underlying value.
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.
std::pair< T, int > normalizeToRange() const
static RoundingMode getround()
static MantissaRange::MantissaScale getMantissaScale()
Returns which mantissa scale is currently in use for normalization.
constexpr bool holds() const noexcept
constexpr TIss const & get() const
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
std::pair< mantissa_type, exponent_type > rep
static std::uint64_t const kURateOne
std::uint64_t mantissa() const noexcept
static constexpr std::uint64_t kPositive
int signum() const noexcept
friend class detail::STVar
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
std::uint64_t mantissa_type
SerializedTypeID getSType() const override
bool negative() const noexcept
STAmount zeroed() const
Returns a zero value with the same issuer and currency.
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
void setJson(json::Value &) const
STAmount(A const &asset, Number const &number)
static constexpr std::uint64_t kMpToken
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
int exponent() const noexcept
AccountID const & getIssuer() const
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 std::uint64_t kMaxNative
static constexpr int kMaxOffset
STAmount const & value() const noexcept
STAmount(SerialIter &sit, SField const &name)
STAmount(A const &asset, std::uint64_t mantissa=0, int exponent=0, bool negative=false)
JSON (JavaScript Object Notation).
xrpl::AccountID getOrThrow(json::Value const &v, xrpl::SField const &field)
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)
STAmount divide(STAmount const &amount, Rate const &rate)
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
bool isLegalMPT(STAmount const &value)
bool operator>=(STAmount const &lhs, STAmount const &rhs)
bool isXRP(AccountID const &c)
Number operator-(Number const &x, Number const &y)
STAmount mulRoundStrict(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safeCast(Src s) noexcept
bool isLegalNet(STAmount const &value)
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > unsafeCast(Src s) noexcept
STAmount amountFromString(Asset const &asset, std::string const &amount)
STAmount divRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
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)
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
constexpr bool isPowerOfTen(T value)
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
STAmount amountFromJson(SField const &name, json::Value const &v)
bool hasInvalidAmount(STBase const &field, beast::Journal j)
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
bool operator<=(STAmount const &lhs, STAmount const &rhs)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
STAmount mulRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
bool canSubtract(STAmount const &amt1, STAmount const &amt2)
Determines if it is safe to subtract one STAmount from another.
STAmount divRoundStrict(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
STAmount multiply(STAmount const &amount, Number const &frac, Number::RoundingMode rm)
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
bool operator>(STAmount const &lhs, STAmount const &rhs)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
STAmount toSTAmount(IOUAmount const &iou, Asset const &asset)
Zero allows classes to offer efficient comparisons to zero.
Note, should be treated as flags that can be | and &.