rippled
Loading...
Searching...
No Matches
Issue.cpp
1#include <xrpl/basics/contract.h>
2#include <xrpl/json/json_errors.h>
3#include <xrpl/json/json_value.h>
4#include <xrpl/protocol/AccountID.h>
5#include <xrpl/protocol/Issue.h>
6#include <xrpl/protocol/UintTypes.h>
7#include <xrpl/protocol/jss.h>
8
9#include <ostream>
10#include <stdexcept>
11#include <string>
12
13namespace xrpl {
14
17{
18 std::string ret;
19
20 ret.reserve(64);
21 ret = to_string(currency);
22
23 if (!isXRP(currency))
24 {
25 ret += "/";
26
27 if (isXRP(account))
28 ret += "0";
29 else if (account == noAccount())
30 ret += "1";
31 else
32 ret += to_string(account);
33 }
34
35 return ret;
36}
37
38void
40{
41 jv[jss::currency] = to_string(currency);
42 if (!isXRP(currency))
43 jv[jss::issuer] = toBase58(account);
44}
45
46bool
48{
49 return *this == xrpIssue();
50}
51
52bool
54{
55 return native();
56}
57
58bool
60{
61 return isXRP(ac.currency) == isXRP(ac.account);
62}
63
65to_string(Issue const& ac)
66{
67 if (isXRP(ac.account))
68 return to_string(ac.currency);
69
70 return to_string(ac.account) + "/" + to_string(ac.currency);
71}
72
74to_json(Issue const& is)
75{
76 Json::Value jv;
77 is.setJson(jv);
78 return jv;
79}
80
81Issue
83{
84 if (!v.isObject())
85 {
86 Throw<std::runtime_error>("issueFromJson can only be specified with an 'object' Json value");
87 }
88
89 if (v.isMember(jss::mpt_issuance_id))
90 {
91 Throw<std::runtime_error>("issueFromJson, Issue should not have mpt_issuance_id");
92 }
93
94 Json::Value const curStr = v[jss::currency];
95 Json::Value const issStr = v[jss::issuer];
96
97 if (!curStr.isString())
98 {
99 Throw<Json::error>("issueFromJson currency must be a string Json value");
100 }
101
102 auto const currency = to_currency(curStr.asString());
103 if (currency == badCurrency() || currency == noCurrency())
104 {
105 Throw<Json::error>("issueFromJson currency must be a valid currency");
106 }
107
108 if (isXRP(currency))
109 {
110 if (!issStr.isNull())
111 {
112 Throw<Json::error>("Issue, XRP should not have issuer");
113 }
114 return xrpIssue();
115 }
116
117 if (!issStr.isString())
118 {
119 Throw<Json::error>("issueFromJson issuer must be a string Json value");
120 }
121 auto const issuer = parseBase58<AccountID>(issStr.asString());
122
123 if (!issuer)
124 {
125 Throw<Json::error>("issueFromJson issuer must be a valid account");
126 }
127
128 return Issue{currency, *issuer};
129}
130
132operator<<(std::ostream& os, Issue const& x)
133{
134 os << to_string(x);
135 return os;
136}
137
138} // namespace xrpl
Represents a JSON value.
Definition json_value.h:131
bool isString() const
bool isObject() const
std::string asString() const
Returns the unquoted string value.
bool isNull() const
isNull() tests to see if this field is null.
bool isMember(char const *key) const
Return true if the object has a member named key.
A currency issued by an account.
Definition Issue.h:14
Currency currency
Definition Issue.h:16
AccountID account
Definition Issue.h:17
bool native() const
Definition Issue.cpp:47
bool integral() const
Definition Issue.cpp:53
void setJson(Json::Value &jv) const
Definition Issue.cpp:39
std::string getText() const
Definition Issue.cpp:16
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
bool isConsistent(Book const &book)
Definition Book.cpp:10
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:98
bool isXRP(AccountID const &c)
Definition AccountID.h:71
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:92
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:613
Issue issueFromJson(Json::Value const &v)
Definition Issue.cpp:82
Json::Value to_json(Asset const &asset)
Definition Asset.h:122
AccountID const & noAccount()
A placeholder for empty accounts.
Currency const & noCurrency()
A placeholder for empty currencies.
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:62
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
T reserve(T... args)