Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single Asset Vault #5224

Draft
wants to merge 21 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions include/xrpl/json/json_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
#ifndef RIPPLE_JSON_JSON_VALUE_H_INCLUDED
#define RIPPLE_JSON_JSON_VALUE_H_INCLUDED

#include <xrpl/basics/Number.h>
#include <xrpl/json/json_forwards.h>
#include <cstring>
#include <map>
#include <string>
#include <utility>
#include <vector>

/** \brief JSON (JavaScript Object Notation).
Expand Down Expand Up @@ -237,6 +239,11 @@ class Value
Value&
operator=(Value&& other);

template <typename T>
requires(!std::convertible_to<T, Value>)
Value&
operator=(T const& rhs);

Value(Value&& other) noexcept;

/// Swap values.
Expand Down Expand Up @@ -364,6 +371,8 @@ class Value
*/
Value&
operator[](const StaticString& key);
Value const&
operator[](const StaticString& key) const;

/// Return the member named key if it exist, defaultValue otherwise.
Value
Expand Down Expand Up @@ -680,6 +689,51 @@ class ValueIterator : public ValueIteratorBase
}
};

// https://ericniebler.com/2014/10/21/customization-point-design-in-c11-and-beyond/
namespace detail {

inline Value
to_json(ripple::Number const& number)
{
return to_string(number);
}

struct to_json_fn
{
template <typename T>
constexpr Value
operator()(T&& t) const
{
return to_json(std::forward<T>(t));
}
};

template <typename T>
struct static_const
{
static constexpr T value{};
};

template <typename T>
constexpr T static_const<T>::value;

} // namespace detail

namespace {

constexpr auto const& to_json = detail::static_const<detail::to_json_fn>::value;

}

template <typename T>
requires(!std::convertible_to<T, Value>)
Value&
Value::operator=(T const& rhs)
{
*this = to_json(rhs);
return *this;
}

} // namespace Json

#endif // CPPTL_JSON_H_INCLUDED
8 changes: 0 additions & 8 deletions include/xrpl/protocol/AMMCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ class STObject;
class STAmount;
class Rules;

/** Calculate AMM account ID.
*/
AccountID
ammAccountID(
std::uint16_t prefix,
uint256 const& parentHash,
uint256 const& ammID);

/** Calculate Liquidity Provider Token (LPT) Currency.
*/
Currency
Expand Down
16 changes: 13 additions & 3 deletions include/xrpl/protocol/Asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
#ifndef RIPPLE_PROTOCOL_ASSET_H_INCLUDED
#define RIPPLE_PROTOCOL_ASSET_H_INCLUDED

#include <xrpl/basics/Number.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/protocol/Issue.h>
#include <xrpl/protocol/MPTIssue.h>

namespace ripple {

class Asset;
class STAmount;

template <typename TIss>
concept ValidIssueType =
Expand Down Expand Up @@ -92,6 +94,9 @@ class Asset
void
setJson(Json::Value& jv) const;

STAmount
operator()(Number const&) const;

bool
native() const
{
Expand All @@ -114,6 +119,14 @@ class Asset
equalTokens(Asset const& lhs, Asset const& rhs);
};

inline Json::Value
to_json(Asset const& asset)
{
Json::Value jv;
asset.setJson(jv);
return jv;
}

template <ValidIssueType TIss>
constexpr bool
Asset::holds() const
Expand Down Expand Up @@ -219,9 +232,6 @@ validJSONAsset(Json::Value const& jv);
Asset
assetFromJson(Json::Value const& jv);

Json::Value
to_json(Asset const& asset);

} // namespace ripple

#endif // RIPPLE_PROTOCOL_ASSET_H_INCLUDED
2 changes: 1 addition & 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 = 83;
static constexpr std::size_t numFeatures = 84;

/** Amendments that this server supports and the default voting behavior.
Whether they are enabled depends on the Rules defined in the validated
Expand Down
1 change: 0 additions & 1 deletion include/xrpl/protocol/IOUAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <boost/operators.hpp>
#include <cstdint>
#include <string>
#include <utility>

namespace ripple {

Expand Down
9 changes: 9 additions & 0 deletions include/xrpl/protocol/Indexes.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ mptoken(uint256 const& mptokenKey)
Keylet
mptoken(uint256 const& issuanceKey, AccountID const& holder) noexcept;

Keylet
vault(AccountID const& owner, std::uint32_t seq) noexcept;

inline Keylet
vault(uint256 const& vaultKey)
{
return {ltVAULT, vaultKey};
}

} // namespace keylet

// Everything below is deprecated and should be removed in favor of keylets:
Expand Down
3 changes: 3 additions & 0 deletions include/xrpl/protocol/LedgerFormats.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ enum LedgerSpecificFlags {

// ltCREDENTIAL
lsfAccepted = 0x00010000,

// ltVAULT
lsfVaultPrivate = 0x00010000,
};

//------------------------------------------------------------------------------
Expand Down
3 changes: 0 additions & 3 deletions include/xrpl/protocol/MPTAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@
#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 {

Expand Down
7 changes: 5 additions & 2 deletions include/xrpl/protocol/MPTIssue.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ class MPTIssue
AccountID const&
getIssuer() const;

MPTID const&
getMptID() const;
constexpr MPTID const&
getMptID() const
{
return mptID_;
}

std::string
getText() const;
Expand Down
3 changes: 3 additions & 0 deletions include/xrpl/protocol/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ std::size_t constexpr maxMPTokenMetadataLength = 1024;
/** The maximum amount of MPTokenIssuance */
std::uint64_t constexpr maxMPTokenAmount = 0x7FFF'FFFF'FFFF'FFFFull;

/** The maximum length of MPTokenMetadata */
std::size_t constexpr maxVaultDataLength = 256;

/** A ledger index. */
using LedgerIndex = std::uint32_t;

Expand Down
1 change: 0 additions & 1 deletion include/xrpl/protocol/SField.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include <cstdint>
#include <map>
#include <utility>

namespace ripple {

Expand Down
22 changes: 20 additions & 2 deletions include/xrpl/protocol/STAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ class STAmount final : public STBase, public CountedObject<STAmount>
template <AssetType A>
STAmount(A const& asset, int mantissa, int exponent = 0);

template <AssetType A>
STAmount(A const& asset, Number const& number)
: STAmount(asset, number.mantissa(), number.exponent())
{
}

// Legacy support for new-style amounts
STAmount(IOUAmount const& amount, Issue const& issue);
STAmount(XRPAmount const& amount);
Expand Down Expand Up @@ -230,6 +236,9 @@ class STAmount final : public STBase, public CountedObject<STAmount>
STAmount&
operator=(XRPAmount const& amount);

STAmount&
operator=(Number const&);

//--------------------------------------------------------------------------
//
// Modification
Expand Down Expand Up @@ -268,7 +277,7 @@ class STAmount final : public STBase, public CountedObject<STAmount>
std::string
getText() const override;

Json::Value getJson(JsonOptions) const override;
Json::Value getJson(JsonOptions = JsonOptions::none) const override;

void
add(Serializer& s) const override;
Expand Down Expand Up @@ -417,7 +426,7 @@ STAmount
amountFromQuality(std::uint64_t rate);

STAmount
amountFromString(Asset const& issue, std::string const& amount);
amountFromString(Asset const& asset, std::string const& amount);

STAmount
amountFromJson(SField const& name, Json::Value const& v);
Expand Down Expand Up @@ -541,6 +550,15 @@ STAmount::operator=(XRPAmount const& amount)
return *this;
}

inline STAmount&
STAmount::operator=(Number const& number)
{
mIsNegative = number.mantissa() < 0;
mValue = mIsNegative ? -number.mantissa() : number.mantissa();
mOffset = number.exponent();
return *this;
}

inline void
STAmount::negate()
{
Expand Down
10 changes: 9 additions & 1 deletion include/xrpl/protocol/STBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ struct JsonOptions
}
};

template <typename T>
requires requires(T const& t) { t.getJson(JsonOptions::none); }
Json::Value
to_json(T const& t)
{
return t.getJson(JsonOptions::none);
}

namespace detail {
class STVar;
}
Expand Down Expand Up @@ -155,7 +163,7 @@ class STBase
virtual std::string
getText() const;

virtual Json::Value getJson(JsonOptions /*options*/) const;
virtual Json::Value getJson(JsonOptions = JsonOptions::none) const;

virtual void
add(Serializer& s) const;
Expand Down
9 changes: 9 additions & 0 deletions include/xrpl/protocol/STIssue.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class STIssue final : public STBase, CountedObject<STIssue>

explicit STIssue(SField const& name);

STIssue&
operator=(STIssue const& rhs) = default;
STIssue&
operator=(Asset const& rhs)
{
asset_ = rhs;
return *this;
}

template <ValidIssueType TIss>
TIss const&
get() const;
Expand Down
20 changes: 20 additions & 0 deletions include/xrpl/protocol/STNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class STNumber : public STBase, public CountedObject<STNumber>
void
setValue(Number const& v);

STNumber&
operator=(Number const& rhs)
{
setValue(rhs);
return *this;
}

bool
isEquivalent(STBase const& t) const override;
bool
Expand All @@ -83,6 +90,19 @@ class STNumber : public STBase, public CountedObject<STNumber>
std::ostream&
operator<<(std::ostream& out, STNumber const& rhs);

struct NumberParts
{
std::uint64_t mantissa = 0;
int exponent = 0;
bool negative = false;
};

NumberParts
partsFromString(std::string const& number);

STNumber
numberFromJson(SField const& field, Json::Value const& value);

} // namespace ripple

#endif
Loading
Loading