Skip to content

Commit

Permalink
MPT Support
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Jun 29, 2024
1 parent 9fec615 commit bf60716
Show file tree
Hide file tree
Showing 80 changed files with 5,367 additions and 480 deletions.
246 changes: 246 additions & 0 deletions include/xrpl/basics/MPTAmount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#ifndef RIPPLE_BASICS_INTEGRALAMOUNT_H_INCLUDED
#define RIPPLE_BASICS_INTEGRALAMOUNT_H_INCLUDED

#include <xrpl/basics/contract.h>
#include <xrpl/basics/safe_cast.h>
#include <xrpl/beast/utility/Zero.h>
#include <xrpl/json/json_value.h>

#include <boost/multiprecision/cpp_int.hpp>
#include <boost/operators.hpp>

#include <cstdint>
#include <optional>
#include <string>
#include <type_traits>

namespace ripple {

class MPTAmount : private boost::totally_ordered<MPTAmount>,
private boost::additive<MPTAmount>,
private boost::equality_comparable<MPTAmount, std::int64_t>,
private boost::additive<MPTAmount, std::int64_t>
{
public:
using mpt_type = std::int64_t;

protected:
mpt_type mpt_;

public:
MPTAmount() = default;
constexpr MPTAmount(MPTAmount const& other) = default;
constexpr MPTAmount&
operator=(MPTAmount const& other) = default;

constexpr MPTAmount(beast::Zero) : mpt_(0)
{
}

constexpr explicit MPTAmount(mpt_type value) : mpt_(value)
{
}

constexpr MPTAmount& operator=(beast::Zero)
{
mpt_ = 0;
return *this;
}

MPTAmount&
operator=(mpt_type value)
{
mpt_ = value;
return *this;
}

constexpr MPTAmount
operator*(mpt_type const& rhs) const
{
return MPTAmount{mpt_ * rhs};
}

friend constexpr MPTAmount
operator*(mpt_type lhs, MPTAmount const& rhs)
{
// multiplication is commutative
return rhs * lhs;
}

MPTAmount&
operator+=(MPTAmount const& other)
{
mpt_ += other.mpt();
return *this;
}

MPTAmount&
operator-=(MPTAmount const& other)
{
mpt_ -= other.mpt();
return *this;
}

MPTAmount&
operator+=(mpt_type const& rhs)
{
mpt_ += rhs;
return *this;
}

MPTAmount&
operator-=(mpt_type const& rhs)
{
mpt_ -= rhs;
return *this;
}

MPTAmount&
operator*=(mpt_type const& rhs)
{
mpt_ *= rhs;
return *this;
}

MPTAmount
operator-() const
{
return MPTAmount{-mpt_};
}

bool
operator==(MPTAmount const& other) const
{
return mpt_ == other.mpt_;
}

bool
operator==(mpt_type other) const
{
return mpt_ == other;
}

bool
operator<(MPTAmount const& other) const
{
return mpt_ < other.mpt_;
}

/** Returns true if the amount is not zero */
explicit constexpr operator bool() const noexcept
{
return mpt_ != 0;
}

/** Return the sign of the amount */
constexpr int
signum() const noexcept
{
return (mpt_ < 0) ? -1 : (mpt_ ? 1 : 0);
}

Json::Value
jsonClipped() const
{
static_assert(
std::is_signed_v<mpt_type> && std::is_integral_v<mpt_type>,
"Expected MPTAmount to be a signed integral type");

constexpr auto min = std::numeric_limits<Json::Int>::min();
constexpr auto max = std::numeric_limits<Json::Int>::max();

if (mpt_ < min)
return min;
if (mpt_ > max)
return max;
return static_cast<Json::Int>(mpt_);
}

/** Returns the underlying value. Code SHOULD NOT call this
function unless the type has been abstracted away,
e.g. in a templated function.
*/
constexpr mpt_type
mpt() const
{
return mpt_;
}

friend std::istream&
operator>>(std::istream& s, MPTAmount& val)
{
s >> val.mpt_;
return s;
}

static MPTAmount
minPositiveAmount()
{
return MPTAmount{1};
}
};

// Output MPTAmount as just the value.
template <class Char, class Traits>
std::basic_ostream<Char, Traits>&
operator<<(std::basic_ostream<Char, Traits>& os, const MPTAmount& q)
{
return os << q.mpt();
}

inline std::string
to_string(MPTAmount const& amount)
{
return std::to_string(amount.mpt());
}

inline MPTAmount
mulRatio(
MPTAmount const& amt,
std::uint32_t num,
std::uint32_t den,
bool roundUp)
{
using namespace boost::multiprecision;

if (!den)
Throw<std::runtime_error>("division by zero");

int128_t const amt128(amt.mpt());
auto const neg = amt.mpt() < 0;
auto const m = amt128 * num;
auto r = m / den;
if (m % den)
{
if (!neg && roundUp)
r += 1;
if (neg && !roundUp)
r -= 1;
}
if (r > std::numeric_limits<MPTAmount::mpt_type>::max())
Throw<std::overflow_error>("XRP mulRatio overflow");
return MPTAmount(r.convert_to<MPTAmount::mpt_type>());
}

} // namespace ripple

#endif // RIPPLE_BASICS_INTEGRALAMOUNT_H_INCLUDED
7 changes: 7 additions & 0 deletions include/xrpl/basics/Number.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef RIPPLE_BASICS_NUMBER_H_INCLUDED
#define RIPPLE_BASICS_NUMBER_H_INCLUDED

#include <xrpl/basics/MPTAmount.h>
#include <xrpl/basics/XRPAmount.h>
#include <cstdint>
#include <limits>
Expand Down Expand Up @@ -52,6 +53,7 @@ class Number
explicit constexpr Number(rep mantissa, int exponent, unchecked) noexcept;

Number(XRPAmount const& x);
Number(MPTAmount const& x);

constexpr rep
mantissa() const noexcept;
Expand Down Expand Up @@ -89,6 +91,7 @@ class Number
lowest() noexcept;

explicit operator XRPAmount() const; // round to nearest, even on tie
explicit operator MPTAmount() const; // round to nearest, even on tie
explicit operator rep() const; // round to nearest, even on tie

friend constexpr bool
Expand Down Expand Up @@ -210,6 +213,10 @@ inline Number::Number(XRPAmount const& x) : Number{x.drops()}
{
}

inline Number::Number(MPTAmount const& x) : Number{x.mpt()}
{
}

inline constexpr Number::rep
Number::mantissa() const noexcept
{
Expand Down
1 change: 1 addition & 0 deletions include/xrpl/basics/base_uint.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ class base_uint
using uint128 = base_uint<128>;
using uint160 = base_uint<160>;
using uint256 = base_uint<256>;
using uint192 = base_uint<192>;

template <std::size_t Bits, class Tag>
[[nodiscard]] inline constexpr std::strong_ordering
Expand Down
Loading

0 comments on commit bf60716

Please sign in to comment.