1#ifndef XRPL_PROTOCOL_STAMOUNT_H_INCLUDED
2#define XRPL_PROTOCOL_STAMOUNT_H_INCLUDED
4#include <xrpl/basics/CountedObject.h>
5#include <xrpl/basics/LocalValue.h>
6#include <xrpl/basics/Number.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/SField.h>
13#include <xrpl/protocol/STBase.h>
14#include <xrpl/protocol/Serializer.h>
15#include <xrpl/protocol/XRPAmount.h>
16#include <xrpl/protocol/json_get_or_throw.h>
53 static_assert(
cMaxValue == 9'999'999'999'999'999ull);
74 template <AssetType A>
83 template <AssetType A>
92 template <AssetType A>
111 template <AssetType A>
126 template <AssetType A>
133 template <AssetType A>
136 template <AssetType A>
139 template <AssetType A>
168 holds() const noexcept;
180 constexpr TIss const&
204 value() const noexcept;
213 operator
bool() const noexcept;
294 copy(
std::
size_t n,
void* buf) const override;
296 move(
std::
size_t n,
void* buf) override;
323template <AssetType A>
334template <AssetType A>
345 , mIsNegative(negative)
350 "xrpl::STAmount::STAmount(SField, A, std::uint64_t, int, bool) : "
351 "maximum mantissa input");
355template <AssetType A>
357 : mAsset(asset), mOffset(exponent)
363template <AssetType A>
373template <AssetType A>
382 , mOffset(amount.exponent())
383 , mIsNegative(amount <
beast::zero)
386 mValue = unsafe_cast<std::uint64_t>(-amount.mantissa());
388 mValue = unsafe_cast<std::uint64_t>(amount.mantissa());
394 : mAsset(mptIssue), mOffset(0), mIsNegative(amount <
beast::zero)
397 mValue = unsafe_cast<std::uint64_t>(-amount.value());
399 mValue = unsafe_cast<std::uint64_t>(amount.value());
455template <Val
idIssueType TIss>
480template <Val
idIssueType TIss>
517inline STAmount::operator bool() const noexcept
519 return *
this != beast::zero;
558 if (*
this != beast::zero)
604operator==(STAmount
const& lhs, STAmount
const& rhs);
606operator<(STAmount
const& lhs, STAmount
const& rhs);
611 return !(lhs == rhs);
642operator+(STAmount
const& v1, STAmount
const& v2);
644operator-(STAmount
const& v1, STAmount
const& v2);
647divide(STAmount
const& v1, STAmount
const& v2, Asset
const& asset);
650multiply(STAmount
const& v1, STAmount
const& v2, Asset
const& asset);
688getRate(STAmount
const& offerOut, STAmount
const& offerIn);
704 STAmount
const& value,
720template <AssetType A>
742 return amount.native();
746canAdd(STAmount
const& amt1, STAmount
const& amt2);
749canSubtract(STAmount
const& amt1, STAmount
const& amt2);
759 using namespace xrpl;
762 Throw<JsonMissingKeyError>(key);
764 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.
static rounding_mode getround()
constexpr rep mantissa() const noexcept
constexpr int exponent() const noexcept
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
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)
Number roundToAsset(A const &asset, Number const &value, std::int32_t scale, Number::rounding_mode rounding=Number::getround())
Round an arbitrary precision Number to the precision of a given Asset.
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
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 &.