Skip to content

Commit

Permalink
Introduce MPT support (XLS-33d):
Browse files Browse the repository at this point in the history
New Transactions:
- MPTokenIssuanceCreate
- MPTokenIssuanceDestory
- MPTokenIssuanceSet
- MPTokenAuthorize

Modified Transactions:
- Payment
- Clawback

New Objects:
- MPTokenIssuance
- MPTokenAuthorize

API updates:
- ledger_entry
- account_objects
- ledger_data

Read full spec: https://github.com/XRPLF/XRPL-Standards/tree/master/XLS-0033d-multi-purpose-tokens

---------

Co-authored-by: Shawn Xie <[email protected]>
Co-authored-by: Howard Hinnant <[email protected]>
  • Loading branch information
3 people committed Aug 27, 2024
1 parent d9bd75e commit 57c21a6
Show file tree
Hide file tree
Showing 124 changed files with 7,155 additions and 855 deletions.
2 changes: 1 addition & 1 deletion Builds/levelization/results/loops.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Loop: test.jtx test.unit_test
test.unit_test == test.jtx

Loop: xrpl.basics xrpl.json
xrpl.json ~= xrpl.basics
xrpl.json == xrpl.basics

Loop: xrpld.app xrpld.core
xrpld.app > xrpld.core
Expand Down
185 changes: 185 additions & 0 deletions include/xrpl/basics/MPTAmount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
//------------------------------------------------------------------------------
/*
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 value_type = std::int64_t;

protected:
value_type value_;

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

constexpr explicit MPTAmount(value_type value);

constexpr MPTAmount& operator=(beast::Zero);

MPTAmount&
operator+=(MPTAmount const& other);

MPTAmount&
operator-=(MPTAmount const& other);

MPTAmount
operator-() const;

bool
operator==(MPTAmount const& other) const;

bool
operator==(value_type other) const;

bool
operator<(MPTAmount const& other) const;

/** Returns true if the amount is not zero */
explicit constexpr operator bool() const noexcept;

/** Return the sign of the amount */
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();
};

constexpr MPTAmount::MPTAmount(value_type value) : value_(value)
{
}

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

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

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

/** Returns the underlying value. Code SHOULD NOT call this
function unless the type has been abstracted away,
e.g. in a templated function.
*/
constexpr MPTAmount::value_type
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)
{
return std::to_string(amount.value());
}

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.value());
auto const neg = amt.value() < 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::value_type>::max())
Throw<std::overflow_error>("XRP mulRatio overflow");
return MPTAmount(r.convert_to<MPTAmount::value_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.value()}
{
}

inline constexpr Number::rep
Number::mantissa() const noexcept
{
Expand Down
2 changes: 2 additions & 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 Expand Up @@ -633,6 +634,7 @@ operator<<(std::ostream& out, base_uint<Bits, Tag> const& u)
#ifndef __INTELLISENSE__
static_assert(sizeof(uint128) == 128 / 8, "There should be no padding bytes");
static_assert(sizeof(uint160) == 160 / 8, "There should be no padding bytes");
static_assert(sizeof(uint192) == 192 / 8, "There should be no padding bytes");
static_assert(sizeof(uint256) == 256 / 8, "There should be no padding bytes");
#endif

Expand Down
3 changes: 2 additions & 1 deletion include/xrpl/protocol/Feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace detail {
// Feature.cpp. Because it's only used to reserve storage, and determine how
// large to make the FeatureBitset, it MAY be larger. It MUST NOT be less than
// the actual number of amendments. A LogicError on startup will verify this.
static constexpr std::size_t numFeatures = 79;
static constexpr std::size_t numFeatures = 80;

/** Amendments that this server supports and the default voting behavior.
Whether they are enabled depends on the Rules defined in the validated
Expand Down Expand Up @@ -372,6 +372,7 @@ extern uint256 const fixEnforceNFTokenTrustline;
extern uint256 const fixInnerObjTemplate2;
extern uint256 const featureInvariantsV1_1;
extern uint256 const fixNFTokenPageLinks;
extern uint256 const featureMPTokensV1;

} // namespace ripple

Expand Down
27 changes: 27 additions & 0 deletions include/xrpl/protocol/Indexes.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,30 @@ did(AccountID const& account) noexcept;
Keylet
oracle(AccountID const& account, std::uint32_t const& documentID) noexcept;

Keylet
mptIssuance(AccountID const& issuer, std::uint32_t seq) noexcept;

Keylet
mptIssuance(MPTID const& mpt) noexcept;

inline Keylet
mptIssuance(uint256 const& issuance)
{
return {ltMPTOKEN_ISSUANCE, issuance};
}

Keylet
mptoken(MPTID const& issuanceID, AccountID const& holder) noexcept;

inline Keylet
mptoken(uint256 const& mptokenKey)
{
return {ltMPTOKEN, mptokenKey};
}

Keylet
mptoken(uint256 const& issuanceKey, AccountID const& holder) noexcept;

} // namespace keylet

// Everything below is deprecated and should be removed in favor of keylets:
Expand Down Expand Up @@ -327,6 +351,9 @@ std::array<keyletDesc<AccountID const&>, 6> const directAccountKeylets{
{&keylet::nftpage_max, jss::NFTokenPage, true},
{&keylet::did, jss::DID, true}}};

MPTID
getMptID(AccountID const& account, std::uint32_t sequence);

} // namespace ripple

#endif
3 changes: 3 additions & 0 deletions include/xrpl/protocol/Issue.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Issue
{
}

AccountID const&
getIssuer() const;

std::string
getText() const;
};
Expand Down
25 changes: 25 additions & 0 deletions include/xrpl/protocol/LedgerFormats.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ enum LedgerEntryType : std::uint16_t
\sa keylet::oracle
*/
ltORACLE = 0x0080,

/** A ledger object representing an individual MPToken asset type, but not
* any balances of that asset itself.
\sa keylet::mptIssuance
*/
ltMPTOKEN_ISSUANCE = 0x007e,

/** A ledger object representing an individual MPToken balance.
\sa keylet::mptoken
*/
ltMPTOKEN = 0x007f,

//---------------------------------------------------------------------------
/** A special type, matching any ledger entry type.
Expand Down Expand Up @@ -308,6 +321,18 @@ enum LedgerSpecificFlags {

// ltNFTOKEN_OFFER
lsfSellNFToken = 0x00000001,

// ltMPTOKEN_ISSUANCE
lsfMPTLocked = 0x00000001, // Also used in ltMPTOKEN
lsfMPTCanLock = 0x00000002,
lsfMPTRequireAuth = 0x00000004,
lsfMPTCanEscrow = 0x00000008,
lsfMPTCanTrade = 0x00000010,
lsfMPTCanTransfer = 0x00000020,
lsfMPTCanClawback = 0x00000040,

// ltMPTOKEN
lsfMPTAuthorized = 0x00000002,
};

//------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 57c21a6

Please sign in to comment.