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