3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/LocalValue.h>
5#include <xrpl/basics/Number.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/protocol/Asset.h>
8#include <xrpl/protocol/IOUAmount.h>
9#include <xrpl/protocol/Issue.h>
10#include <xrpl/protocol/MPTAmount.h>
11#include <xrpl/protocol/SField.h>
12#include <xrpl/protocol/STBase.h>
13#include <xrpl/protocol/Serializer.h>
14#include <xrpl/protocol/XRPAmount.h>
15#include <xrpl/protocol/json_get_or_throw.h>
52 static_assert(
cMaxValue == 9'999'999'999'999'999ull);
73 template <AssetType A>
82 template <AssetType A>
86 template <AssetType A>
102 template <AssetType A>
110 template <AssetType A>
113 template <AssetType A>
116 template <AssetType A>
119 template <AssetType A>
147 holds() const noexcept;
159 constexpr TIss const&
183 value() const noexcept;
192 operator
bool() const noexcept;
277 copy(
std::
size_t n,
void* buf) const override;
279 move(
std::
size_t n,
void* buf) override;
302template <AssetType A>
308template <AssetType A>
310 :
STBase(name), mAsset(asset), mValue(mantissa), mOffset(exponent), mIsNegative(negative)
317 "xrpl::STAmount::STAmount(SField, A, std::uint64_t, int, bool) : "
318 "maximum mantissa input");
328template <AssetType A>
335template <AssetType A>
341template <AssetType A>
349 : mAsset(issue), mOffset(amount.exponent()), mIsNegative(amount <
beast::zero)
352 mValue = unsafe_cast<std::uint64_t>(-amount.mantissa());
354 mValue = unsafe_cast<std::uint64_t>(amount.mantissa());
360 : mAsset(mptIssue), mOffset(0), mIsNegative(amount <
beast::zero)
363 mValue = unsafe_cast<std::uint64_t>(-amount.value());
365 mValue = unsafe_cast<std::uint64_t>(amount.value());
421template <Val
idIssueType TIss>
446template <Val
idIssueType TIss>
483inline STAmount::operator bool() const noexcept
485 return *
this != beast::zero;
511template <AssetType A>
532 if (*
this != beast::zero)
578operator==(STAmount
const& lhs, STAmount
const& rhs);
580operator<(STAmount
const& lhs, STAmount
const& rhs);
585 return !(lhs == rhs);
616operator+(STAmount
const& v1, STAmount
const& v2);
618operator-(STAmount
const& v1, STAmount
const& v2);
621divide(STAmount
const& v1, STAmount
const& v2, Asset
const& asset);
624multiply(STAmount
const& v1, STAmount
const& v2, Asset
const& asset);
628mulRound(STAmount
const& v1, STAmount
const& v2, Asset
const& asset,
bool roundUp);
632mulRoundStrict(STAmount
const& v1, STAmount
const& v2, Asset
const& asset,
bool roundUp);
636divRound(STAmount
const& v1, STAmount
const& v2, Asset
const& asset,
bool roundUp);
640divRoundStrict(STAmount
const& v1, STAmount
const& v2, Asset
const& asset,
bool roundUp);
646getRate(STAmount
const& offerOut, STAmount
const& offerIn);
660[[nodiscard]] STAmount
672template <AssetType A>
690template <AssetType A>
712 return amount.native();
716canAdd(STAmount
const& amt1, STAmount
const& amt2);
719canSubtract(STAmount
const& amt1, STAmount
const& amt2);
729 using namespace xrpl;
732 Throw<JsonMissingKeyError>(key);
734 return amountFromJson(field, inner);
Lightweight wrapper to tag static string.
bool isMember(char const *key) const
Return true if the object has a member named key.
constexpr TIss const & get() const
AccountID const & getIssuer() const
constexpr bool holds() const
Tracks the number of instances of an object.
Floating point representation of amounts with high dynamic range.
A currency issued by an account.
Number is a floating point type that can represent a wide range of values.
static rounding_mode getround()
constexpr rep mantissa() const noexcept
Returns the mantissa of the external view of the Number.
static MantissaRange::mantissa_scale getMantissaScale()
Returns which mantissa scale is currently in use for normalization.
constexpr bool holds() const noexcept
Json::Value getJson(JsonOptions=JsonOptions::none) const override
constexpr TIss const & get() const
void setIssue(Asset const &asset)
Set the Issue for this amount.
std::string getFullText() const override
static constexpr std::uint64_t cMaxValue
Issue const & issue() const
static std::uint64_t const uRateOne
static STAmount fromNumber(A const &asset, Number const &number)
void add(Serializer &s) const override
void setIssuer(AccountID const &uIssuer)
std::uint64_t mantissa() const noexcept
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
static constexpr std::uint64_t cValueMask
std::string getText() const override
void setJson(Json::Value &) const
SerializedTypeID getSType() const override
bool negative() const noexcept
STAmount zeroed() const
Returns a zero value with the same issuer and currency.
static int const cMaxOffset
bool isDefault() const override
bool integral() const noexcept
STAmount & operator=(beast::Zero)
Currency const & getCurrency() const
static std::unique_ptr< STAmount > construct(SerialIter &, SField const &name)
static constexpr std::uint64_t cIssuedCurrency
bool native() const noexcept
static constexpr std::uint64_t cMinValue
Asset const & asset() const
static int const cMinOffset
static constexpr std::uint64_t cMaxNative
STAmount(A const &asset, Number const &number)
static constexpr std::uint64_t cMPToken
int exponent() const noexcept
AccountID const & getIssuer() const
static constexpr std::uint64_t cMaxNativeN
STBase * move(std::size_t n, void *buf) override
static constexpr std::uint64_t cPositive
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)
A type which can be exported to a well known binary format.
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.
STAmount divide(STAmount const &amount, Rate const &rate)
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
bool operator>=(STAmount const &lhs, STAmount const &rhs)
bool isXRP(AccountID const &c)
constexpr base_uint< Bits, Tag > operator+(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Number operator-(Number const &x, Number const &y)
STAmount amountFromJson(SField const &name, Json::Value const &v)
STAmount mulRoundStrict(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
bool isLegalNet(STAmount const &value)
STAmount amountFromString(Asset const &asset, std::string const &amount)
STAmount divRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
STAmount multiply(STAmount const &amount, Rate const &rate)
STAmount roundToScale(STAmount const &value, std::int32_t scale, Number::rounding_mode rounding=Number::getround())
Round an arbitrary precision Amount to the precision of an STAmount that has a given exponent.
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
bool amountFromJsonNoThrow(STAmount &result, Json::Value const &jvSource)
bool canAdd(STAmount const &amt1, STAmount const &amt2)
Safely checks if two STAmount values can be added without overflow, underflow, or precision loss.
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
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)
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)
bool operator>(STAmount const &lhs, STAmount const &rhs)
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safe_cast(Src s) noexcept
Zero allows classes to offer efficient comparisons to zero.
Note, should be treated as flags that can be | and &.