Skip to content

Commit

Permalink
MPT integration into DEX (merge with develop)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Dec 8, 2024
1 parent 6d58065 commit bb8dd72
Show file tree
Hide file tree
Showing 114 changed files with 6,117 additions and 2,338 deletions.
27 changes: 27 additions & 0 deletions include/xrpl/basics/MPTAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class MPTAmount : private boost::totally_ordered<MPTAmount>,
public:
MPTAmount() = default;
constexpr MPTAmount(MPTAmount const& other) = default;
constexpr MPTAmount(beast::Zero);
constexpr MPTAmount&
operator=(MPTAmount const& other) = default;

Expand Down Expand Up @@ -82,13 +83,19 @@ class MPTAmount : private boost::totally_ordered<MPTAmount>,
constexpr int
signum() const noexcept;

Json::Value
jsonClipped() const;

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

friend std::istream&
operator>>(std::istream& s, MPTAmount& val);

static MPTAmount
minPositiveAmount();
};
Expand All @@ -97,6 +104,11 @@ constexpr MPTAmount::MPTAmount(value_type value) : value_(value)
{
}

constexpr MPTAmount::MPTAmount(beast::Zero)
{
*this = beast::zero;
}

constexpr MPTAmount&
MPTAmount::operator=(beast::Zero)
{
Expand Down Expand Up @@ -127,6 +139,21 @@ MPTAmount::value() const
return value_;
}

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

// 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.value();
}

inline std::string
to_string(MPTAmount const& amount)
{
Expand Down
20 changes: 10 additions & 10 deletions include/xrpl/protocol/AMMCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <xrpl/basics/Number.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/Issue.h>
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/TER.h>
#include <xrpl/protocol/UintTypes.h>

Expand Down Expand Up @@ -59,14 +59,14 @@ ammAccountID(
/** Calculate Liquidity Provider Token (LPT) Currency.
*/
Currency
ammLPTCurrency(Currency const& cur1, Currency const& cur2);
ammLPTCurrency(Asset const& issue1, Asset const& issue2);

/** Calculate LPT Issue from AMM asset pair.
*/
Issue
ammLPTIssue(
Currency const& cur1,
Currency const& cur2,
Asset const& issue1,
Asset const& issue2,
AccountID const& ammAccountID);

/** Validate the amount.
Expand All @@ -77,19 +77,19 @@ ammLPTIssue(
NotTEC
invalidAMMAmount(
STAmount const& amount,
std::optional<std::pair<Issue, Issue>> const& pair = std::nullopt,
std::optional<std::pair<Asset, Asset>> const& pair = std::nullopt,
bool validZero = false);

NotTEC
invalidAMMAsset(
Issue const& issue,
std::optional<std::pair<Issue, Issue>> const& pair = std::nullopt);
Asset const& issue,
std::optional<std::pair<Asset, Asset>> const& pair = std::nullopt);

NotTEC
invalidAMMAssetPair(
Issue const& issue1,
Issue const& issue2,
std::optional<std::pair<Issue, Issue>> const& pair = std::nullopt);
Asset const& issue1,
Asset const& issue2,
std::optional<std::pair<Asset, Asset>> const& pair = std::nullopt);

/** Get time slot of the auction slot.
*/
Expand Down
87 changes: 72 additions & 15 deletions include/xrpl/protocol/AmountConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@

#include <xrpl/basics/IOUAmount.h>
#include <xrpl/basics/XRPAmount.h>
#include <xrpl/protocol/Protocol.h>
#include <xrpl/protocol/STAmount.h>

#include <type_traits>

namespace ripple {

inline STAmount
toSTAmount(IOUAmount const& iou, Issue const& iss)
toSTAmount(IOUAmount const& iou, Asset const& iss)
{
ASSERT(iss.holds<Issue>(), "ripple::toSTAmount : is Issue");
bool const isNeg = iou.signum() < 0;
std::uint64_t const umant = isNeg ? -iou.mantissa() : iou.mantissa();
return STAmount(iss, umant, iou.exponent(), isNeg, STAmount::unchecked());
Expand All @@ -51,14 +53,25 @@ toSTAmount(XRPAmount const& xrp)
}

inline STAmount
toSTAmount(XRPAmount const& xrp, Issue const& iss)
toSTAmount(XRPAmount const& xrp, Asset const& iss)
{
ASSERT(
isXRP(iss.account) && isXRP(iss.currency),
"ripple::toSTAmount : is XRP");
ASSERT(isXRP(iss), "ripple::toSTAmount : is XRP");
return toSTAmount(xrp);
}

inline STAmount
toSTAmount(MPTAmount const& mpt)
{
return STAmount(mpt, noMPT());
}

inline STAmount
toSTAmount(MPTAmount const& mpt, Asset const& iss)
{
ASSERT(iss.holds<MPTIssue>(), "ripple::toSTAmount : is MPT");
return STAmount(mpt, iss.get<MPTIssue>());
}

template <class T>
T
toAmount(STAmount const& amt) = delete;
Expand Down Expand Up @@ -100,6 +113,20 @@ toAmount<XRPAmount>(STAmount const& amt)
return XRPAmount(sMant);
}

template <>
inline MPTAmount
toAmount<MPTAmount>(STAmount const& amt)
{
ASSERT(
amt.holds<MPTIssue>() && amt.mantissa() <= maxMPTokenAmount,
"ripple::toAmount<MPTAmount> : maximum mantissa");
bool const isNeg = amt.negative();
std::int64_t const sMant =
isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();

return MPTAmount(sMant);
}

template <class T>
T
toAmount(IOUAmount const& amt) = delete;
Expand All @@ -122,10 +149,21 @@ toAmount<XRPAmount>(XRPAmount const& amt)
return amt;
}

template <class T>
T
toAmount(MPTAmount const& amt) = delete;

template <>
inline MPTAmount
toAmount<MPTAmount>(MPTAmount const& amt)
{
return amt;
}

template <typename T>
T
toAmount(
Issue const& issue,
Asset const& issue,
Number const& n,
Number::rounding_mode mode = Number::getround())
{
Expand All @@ -137,6 +175,8 @@ toAmount(
return IOUAmount(n);
else if constexpr (std::is_same_v<XRPAmount, T>)
return XRPAmount(static_cast<std::int64_t>(n));
else if constexpr (std::is_same_v<MPTAmount, T>)
return MPTAmount(static_cast<std::int64_t>(n));
else if constexpr (std::is_same_v<STAmount, T>)
{
if (isXRP(issue))
Expand All @@ -152,18 +192,31 @@ toAmount(

template <typename T>
T
toMaxAmount(Issue const& issue)
toMaxAmount(Asset const& issue)
{
if constexpr (std::is_same_v<IOUAmount, T>)
return IOUAmount(STAmount::cMaxValue, STAmount::cMaxOffset);
else if constexpr (std::is_same_v<XRPAmount, T>)
return XRPAmount(static_cast<std::int64_t>(STAmount::cMaxNativeN));
else if constexpr (std::is_same_v<MPTAmount, T>)
return MPTAmount(maxMPTokenAmount);
else if constexpr (std::is_same_v<STAmount, T>)
{
if (isXRP(issue))
return STAmount(
issue, static_cast<std::int64_t>(STAmount::cMaxNativeN));
return STAmount(issue, STAmount::cMaxValue, STAmount::cMaxOffset);
return std::visit(
[]<ValidIssueType TIss>(TIss const& issue_) {
if constexpr (std::is_same_v<TIss, Issue>)
{
if (isXRP(issue_))
return STAmount(
issue_,
static_cast<std::int64_t>(STAmount::cMaxNativeN));
return STAmount(
issue_, STAmount::cMaxValue, STAmount::cMaxOffset);
}
else
return STAmount(issue_, maxMPTokenAmount);
},
issue.value());
}
else
{
Expand All @@ -174,23 +227,25 @@ toMaxAmount(Issue const& issue)

inline STAmount
toSTAmount(
Issue const& issue,
Asset const& issue,
Number const& n,
Number::rounding_mode mode = Number::getround())
{
return toAmount<STAmount>(issue, n, mode);
}

template <typename T>
Issue
getIssue(T const& amt)
Asset
getAsset(T const& amt)
{
if constexpr (std::is_same_v<IOUAmount, T>)
return noIssue();
else if constexpr (std::is_same_v<XRPAmount, T>)
return xrpIssue();
else if constexpr (std::is_same_v<MPTAmount, T>)
return noMPT();
else if constexpr (std::is_same_v<STAmount, T>)
return amt.issue();
return amt.asset();
else
{
constexpr bool alwaysFalse = !std::is_same_v<T, T>;
Expand All @@ -206,6 +261,8 @@ get(STAmount const& a)
return a.iou();
else if constexpr (std::is_same_v<XRPAmount, T>)
return a.xrp();
else if constexpr (std::is_same_v<MPTAmount, T>)
return a.mpt();
else if constexpr (std::is_same_v<STAmount, T>)
return a;
else
Expand Down
Loading

0 comments on commit bb8dd72

Please sign in to comment.