rippled
Loading...
Searching...
No Matches
Asset.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_ASSET_H_INCLUDED
21#define RIPPLE_PROTOCOL_ASSET_H_INCLUDED
22
23#include <xrpl/basics/base_uint.h>
24#include <xrpl/protocol/Issue.h>
25#include <xrpl/protocol/MPTIssue.h>
26
27namespace ripple {
28
29class Asset;
30
31template <typename TIss>
33 std::is_same_v<TIss, Issue> || std::is_same_v<TIss, MPTIssue>;
34
35template <typename A>
36concept AssetType =
37 std::is_convertible_v<A, Asset> || std::is_convertible_v<A, Issue> ||
38 std::is_convertible_v<A, MPTIssue> || std::is_convertible_v<A, MPTID>;
39
40/* Asset is an abstraction of three different issue types: XRP, IOU, MPT.
41 * For historical reasons, two issue types XRP and IOU are wrapped in Issue
42 * type. Many functions and classes there were first written for Issue
43 * have been rewritten for Asset.
44 */
45class Asset
46{
47public:
49
50private:
52
53public:
54 Asset() = default;
55
59 Asset(Issue const& issue) : issue_(issue)
60 {
61 }
62
63 Asset(MPTIssue const& mptIssue) : issue_(mptIssue)
64 {
65 }
66
67 Asset(MPTID const& issuanceID) : issue_(MPTIssue{issuanceID})
68 {
69 }
70
71 AccountID const&
72 getIssuer() const;
73
74 template <ValidIssueType TIss>
75 constexpr TIss const&
76 get() const;
77
78 template <ValidIssueType TIss>
79 TIss&
80 get();
81
82 template <ValidIssueType TIss>
83 constexpr bool
84 holds() const;
85
87 getText() const;
88
89 constexpr value_type const&
90 value() const;
91
92 void
93 setJson(Json::Value& jv) const;
94
95 bool
96 native() const
97 {
98 return holds<Issue>() && get<Issue>().native();
99 }
100
101 friend constexpr bool
102 operator==(Asset const& lhs, Asset const& rhs);
103
104 friend constexpr std::weak_ordering
105 operator<=>(Asset const& lhs, Asset const& rhs);
106
107 friend constexpr bool
108 operator==(Currency const& lhs, Asset const& rhs);
109
113 friend constexpr bool
114 equalTokens(Asset const& lhs, Asset const& rhs);
115};
116
117template <ValidIssueType TIss>
118constexpr bool
120{
121 return std::holds_alternative<TIss>(issue_);
122}
123
124template <ValidIssueType TIss>
125constexpr TIss const&
127{
128 if (!std::holds_alternative<TIss>(issue_))
129 Throw<std::logic_error>("Asset is not a requested issue");
130 return std::get<TIss>(issue_);
131}
132
133template <ValidIssueType TIss>
134TIss&
136{
137 if (!std::holds_alternative<TIss>(issue_))
138 Throw<std::logic_error>("Asset is not a requested issue");
139 return std::get<TIss>(issue_);
140}
141
142constexpr Asset::value_type const&
144{
145 return issue_;
146}
147
148constexpr bool
149operator==(Asset const& lhs, Asset const& rhs)
150{
151 return std::visit(
152 [&]<typename TLhs, typename TRhs>(
153 TLhs const& issLhs, TRhs const& issRhs) {
154 if constexpr (std::is_same_v<TLhs, TRhs>)
155 return issLhs == issRhs;
156 else
157 return false;
158 },
159 lhs.issue_,
160 rhs.issue_);
161}
162
164operator<=>(Asset const& lhs, Asset const& rhs)
165{
166 return std::visit(
167 []<ValidIssueType TLhs, ValidIssueType TRhs>(
168 TLhs const& lhs_, TRhs const& rhs_) {
169 if constexpr (std::is_same_v<TLhs, TRhs>)
170 return std::weak_ordering(lhs_ <=> rhs_);
171 else if constexpr (
172 std::is_same_v<TLhs, Issue> && std::is_same_v<TRhs, MPTIssue>)
173 return std::weak_ordering::greater;
174 else
175 return std::weak_ordering::less;
176 },
177 lhs.issue_,
178 rhs.issue_);
179}
180
181constexpr bool
182operator==(Currency const& lhs, Asset const& rhs)
183{
184 return rhs.holds<Issue>() && rhs.get<Issue>().currency == lhs;
185}
186
187constexpr bool
188equalTokens(Asset const& lhs, Asset const& rhs)
189{
190 return std::visit(
191 [&]<typename TLhs, typename TRhs>(
192 TLhs const& issLhs, TRhs const& issRhs) {
193 if constexpr (
194 std::is_same_v<TLhs, Issue> && std::is_same_v<TRhs, Issue>)
195 return issLhs.currency == issRhs.currency;
196 else if constexpr (
197 std::is_same_v<TLhs, MPTIssue> &&
198 std::is_same_v<TRhs, MPTIssue>)
199 return issLhs.getMptID() == issRhs.getMptID();
200 else
201 return false;
202 },
203 lhs.issue_,
204 rhs.issue_);
205}
206
207inline bool
208isXRP(Asset const& asset)
209{
210 return asset.native();
211}
212
214to_string(Asset const& asset);
215
216bool
217validJSONAsset(Json::Value const& jv);
218
219Asset
220assetFromJson(Json::Value const& jv);
221
223to_json(Asset const& asset);
224
225} // namespace ripple
226
227#endif // RIPPLE_PROTOCOL_ASSET_H_INCLUDED
Represents a JSON value.
Definition: json_value.h:148
bool native() const
Definition: Asset.h:96
Asset()=default
Asset(MPTIssue const &mptIssue)
Definition: Asset.h:63
Asset(Issue const &issue)
Conversions to Asset are implicit and conversions to specific issue type are explicit.
Definition: Asset.h:59
void setJson(Json::Value &jv) const
Definition: Asset.cpp:49
Asset(MPTID const &issuanceID)
Definition: Asset.h:67
constexpr value_type const & value() const
Definition: Asset.h:143
friend constexpr std::weak_ordering operator<=>(Asset const &lhs, Asset const &rhs)
Definition: Asset.h:164
friend constexpr bool equalTokens(Asset const &lhs, Asset const &rhs)
Return true if both assets refer to the same currency (regardless of issuer) or MPT issuance.
Definition: Asset.h:188
constexpr TIss const & get() const
AccountID const & getIssuer() const
Definition: Asset.cpp:35
value_type issue_
Definition: Asset.h:51
constexpr bool holds() const
Definition: Asset.h:119
std::string getText() const
Definition: Asset.cpp:43
friend constexpr bool operator==(Asset const &lhs, Asset const &rhs)
Definition: Asset.h:149
A currency issued by an account.
Definition: Issue.h:36
Currency currency
Definition: Issue.h:38
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
constexpr bool equalTokens(Asset const &lhs, Asset const &rhs)
Definition: Asset.h:188
bool isXRP(AccountID const &c)
Definition: AccountID.h:91
constexpr std::strong_ordering operator<=>(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition: base_uint.h:563
Asset assetFromJson(Json::Value const &jv)
Definition: Asset.cpp:70
Json::Value to_json(Asset const &asset)
Definition: Asset.cpp:82
bool validJSONAsset(Json::Value const &jv)
Definition: Asset.cpp:62
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition: base_uint.h:585
T visit(T... args)