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/Number.h>
24#include <xrpl/basics/base_uint.h>
25#include <xrpl/protocol/Issue.h>
26#include <xrpl/protocol/MPTIssue.h>
27
28namespace ripple {
29
30class Asset;
31class STAmount;
32
33template <typename TIss>
35 std::is_same_v<TIss, Issue> || std::is_same_v<TIss, MPTIssue>;
36
37template <typename A>
38concept AssetType =
39 std::is_convertible_v<A, Asset> || std::is_convertible_v<A, Issue> ||
40 std::is_convertible_v<A, MPTIssue> || std::is_convertible_v<A, MPTID>;
41
42/* Asset is an abstraction of three different issue types: XRP, IOU, MPT.
43 * For historical reasons, two issue types XRP and IOU are wrapped in Issue
44 * type. Many functions and classes there were first written for Issue
45 * have been rewritten for Asset.
46 */
47class Asset
48{
49public:
51
52private:
54
55public:
56 Asset() = default;
57
61 Asset(Issue const& issue) : issue_(issue)
62 {
63 }
64
65 Asset(MPTIssue const& mptIssue) : issue_(mptIssue)
66 {
67 }
68
69 Asset(MPTID const& issuanceID) : issue_(MPTIssue{issuanceID})
70 {
71 }
72
73 AccountID const&
74 getIssuer() const;
75
76 template <ValidIssueType TIss>
77 constexpr TIss const&
78 get() const;
79
80 template <ValidIssueType TIss>
81 TIss&
82 get();
83
84 template <ValidIssueType TIss>
85 constexpr bool
86 holds() const;
87
89 getText() const;
90
91 constexpr value_type const&
92 value() const;
93
94 void
95 setJson(Json::Value& jv) const;
96
98 operator()(Number const&) const;
99
100 bool
101 native() const
102 {
103 return holds<Issue>() && get<Issue>().native();
104 }
105
106 friend constexpr bool
107 operator==(Asset const& lhs, Asset const& rhs);
108
109 friend constexpr std::weak_ordering
110 operator<=>(Asset const& lhs, Asset const& rhs);
111
112 friend constexpr bool
113 operator==(Currency const& lhs, Asset const& rhs);
114
118 friend constexpr bool
119 equalTokens(Asset const& lhs, Asset const& rhs);
120};
121
122inline Json::Value
123to_json(Asset const& asset)
124{
125 Json::Value jv;
126 asset.setJson(jv);
127 return jv;
128}
129
130template <ValidIssueType TIss>
131constexpr bool
133{
134 return std::holds_alternative<TIss>(issue_);
135}
136
137template <ValidIssueType TIss>
138constexpr TIss const&
140{
141 if (!std::holds_alternative<TIss>(issue_))
142 Throw<std::logic_error>("Asset is not a requested issue");
143 return std::get<TIss>(issue_);
144}
145
146template <ValidIssueType TIss>
147TIss&
149{
150 if (!std::holds_alternative<TIss>(issue_))
151 Throw<std::logic_error>("Asset is not a requested issue");
152 return std::get<TIss>(issue_);
153}
154
155constexpr Asset::value_type const&
157{
158 return issue_;
159}
160
161constexpr bool
162operator==(Asset const& lhs, Asset const& rhs)
163{
164 return std::visit(
165 [&]<typename TLhs, typename TRhs>(
166 TLhs const& issLhs, TRhs const& issRhs) {
167 if constexpr (std::is_same_v<TLhs, TRhs>)
168 return issLhs == issRhs;
169 else
170 return false;
171 },
172 lhs.issue_,
173 rhs.issue_);
174}
175
177operator<=>(Asset const& lhs, Asset const& rhs)
178{
179 return std::visit(
180 []<ValidIssueType TLhs, ValidIssueType TRhs>(
181 TLhs const& lhs_, TRhs const& rhs_) {
182 if constexpr (std::is_same_v<TLhs, TRhs>)
183 return std::weak_ordering(lhs_ <=> rhs_);
184 else if constexpr (
185 std::is_same_v<TLhs, Issue> && std::is_same_v<TRhs, MPTIssue>)
186 return std::weak_ordering::greater;
187 else
188 return std::weak_ordering::less;
189 },
190 lhs.issue_,
191 rhs.issue_);
192}
193
194constexpr bool
195operator==(Currency const& lhs, Asset const& rhs)
196{
197 return rhs.holds<Issue>() && rhs.get<Issue>().currency == lhs;
198}
199
200constexpr bool
201equalTokens(Asset const& lhs, Asset const& rhs)
202{
203 return std::visit(
204 [&]<typename TLhs, typename TRhs>(
205 TLhs const& issLhs, TRhs const& issRhs) {
206 if constexpr (
207 std::is_same_v<TLhs, Issue> && std::is_same_v<TRhs, Issue>)
208 return issLhs.currency == issRhs.currency;
209 else if constexpr (
210 std::is_same_v<TLhs, MPTIssue> &&
211 std::is_same_v<TRhs, MPTIssue>)
212 return issLhs.getMptID() == issRhs.getMptID();
213 else
214 return false;
215 },
216 lhs.issue_,
217 rhs.issue_);
218}
219
220inline bool
221isXRP(Asset const& asset)
222{
223 return asset.native();
224}
225
227to_string(Asset const& asset);
228
229bool
230validJSONAsset(Json::Value const& jv);
231
232Asset
233assetFromJson(Json::Value const& jv);
234
235} // namespace ripple
236
237#endif // RIPPLE_PROTOCOL_ASSET_H_INCLUDED
Represents a JSON value.
Definition: json_value.h:149
STAmount operator()(Number const &) const
Definition: Asset.cpp:56
bool native() const
Definition: Asset.h:101
Asset()=default
Asset(MPTIssue const &mptIssue)
Definition: Asset.h:65
Asset(Issue const &issue)
Conversions to Asset are implicit and conversions to specific issue type are explicit.
Definition: Asset.h:61
void setJson(Json::Value &jv) const
Definition: Asset.cpp:50
Asset(MPTID const &issuanceID)
Definition: Asset.h:69
constexpr value_type const & value() const
Definition: Asset.h:156
friend constexpr std::weak_ordering operator<=>(Asset const &lhs, Asset const &rhs)
Definition: Asset.h:177
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:201
constexpr TIss const & get() const
AccountID const & getIssuer() const
Definition: Asset.cpp:36
value_type issue_
Definition: Asset.h:53
constexpr bool holds() const
Definition: Asset.h:132
std::string getText() const
Definition: Asset.cpp:44
friend constexpr bool operator==(Asset const &lhs, Asset const &rhs)
Definition: Asset.h:162
A currency issued by an account.
Definition: Issue.h:33
Currency currency
Definition: Issue.h:35
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
constexpr bool equalTokens(Asset const &lhs, Asset const &rhs)
Definition: Asset.h:201
bool isXRP(AccountID const &c)
Definition: AccountID.h:90
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:77
Json::Value to_json(Asset const &asset)
Definition: Asset.h:123
bool validJSONAsset(Json::Value const &jv)
Definition: Asset.cpp:69
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)