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