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 ripple {
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 holds<Issue>() && get<Issue>().native();
85 }
86
87 friend constexpr bool
88 operator==(Asset const& lhs, Asset const& rhs);
89
90 friend constexpr std::weak_ordering
91 operator<=>(Asset const& lhs, Asset const& rhs);
92
93 friend constexpr bool
94 operator==(Currency const& lhs, Asset const& rhs);
95
99 friend constexpr bool
100 equalTokens(Asset const& lhs, Asset const& rhs);
101};
102
103inline Json::Value
104to_json(Asset const& asset)
105{
106 Json::Value jv;
107 asset.setJson(jv);
108 return jv;
109}
110
111template <ValidIssueType TIss>
112constexpr bool
114{
116}
117
118template <ValidIssueType TIss>
119constexpr TIss const&
121{
123 Throw<std::logic_error>("Asset is not a requested issue");
124 return std::get<TIss>(issue_);
125}
126
127template <ValidIssueType TIss>
128TIss&
130{
132 Throw<std::logic_error>("Asset is not a requested issue");
133 return std::get<TIss>(issue_);
134}
135
136constexpr Asset::value_type const&
138{
139 return issue_;
140}
141
142constexpr bool
143operator==(Asset const& lhs, Asset const& rhs)
144{
145 return std::visit(
146 [&]<typename TLhs, typename TRhs>(
147 TLhs const& issLhs, TRhs const& issRhs) {
148 if constexpr (std::is_same_v<TLhs, TRhs>)
149 return issLhs == issRhs;
150 else
151 return false;
152 },
153 lhs.issue_,
154 rhs.issue_);
155}
156
158operator<=>(Asset const& lhs, Asset const& rhs)
159{
160 return std::visit(
161 []<ValidIssueType TLhs, ValidIssueType TRhs>(
162 TLhs const& lhs_, TRhs const& rhs_) {
163 if constexpr (std::is_same_v<TLhs, TRhs>)
164 return std::weak_ordering(lhs_ <=> rhs_);
165 else if constexpr (
167 return std::weak_ordering::greater;
168 else
169 return std::weak_ordering::less;
170 },
171 lhs.issue_,
172 rhs.issue_);
173}
174
175constexpr bool
176operator==(Currency const& lhs, Asset const& rhs)
177{
178 return rhs.holds<Issue>() && rhs.get<Issue>().currency == lhs;
179}
180
181constexpr bool
182equalTokens(Asset const& lhs, Asset const& rhs)
183{
184 return std::visit(
185 [&]<typename TLhs, typename TRhs>(
186 TLhs const& issLhs, TRhs const& issRhs) {
187 if constexpr (
189 return issLhs.currency == issRhs.currency;
190 else if constexpr (
193 return issLhs.getMptID() == issRhs.getMptID();
194 else
195 return false;
196 },
197 lhs.issue_,
198 rhs.issue_);
199}
200
201inline bool
202isXRP(Asset const& asset)
203{
204 return asset.native();
205}
206
208to_string(Asset const& asset);
209
210bool
211validJSONAsset(Json::Value const& jv);
212
213Asset
214assetFromJson(Json::Value const& jv);
215
216} // namespace ripple
217
218#endif // XRPL_PROTOCOL_ASSET_H_INCLUDED
Represents a JSON value.
Definition json_value.h:131
STAmount operator()(Number const &) const
Definition Asset.cpp:37
bool native() const
Definition Asset.h:82
Asset()=default
Asset(MPTIssue const &mptIssue)
Definition Asset.h:46
Asset(Issue const &issue)
Conversions to Asset are implicit and conversions to specific issue type are explicit.
Definition Asset.h:42
void setJson(Json::Value &jv) const
Definition Asset.cpp:31
Asset(MPTID const &issuanceID)
Definition Asset.h:50
constexpr value_type const & value() const
Definition Asset.h:137
friend constexpr std::weak_ordering operator<=>(Asset const &lhs, Asset const &rhs)
Definition Asset.h:158
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:182
constexpr TIss const & get() const
AccountID const & getIssuer() const
Definition Asset.cpp:17
value_type issue_
Definition Asset.h:34
constexpr bool holds() const
Definition Asset.h:113
std::string getText() const
Definition Asset.cpp:25
friend constexpr bool operator==(Asset const &lhs, Asset const &rhs)
Definition Asset.h:143
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
constexpr bool equalTokens(Asset const &lhs, Asset const &rhs)
Definition Asset.h:182
bool isXRP(AccountID const &c)
Definition AccountID.h:71
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:104
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
T visit(T... args)