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>(
87 "issueFromJson can only be specified with an 'object' Json value");
88 }
89
90 if (v.isMember(jss::mpt_issuance_id))
91 {
92 Throw<std::runtime_error>(
93 "issueFromJson, Issue should not have mpt_issuance_id");
94 }
95
96 Json::Value const curStr = v[jss::currency];
97 Json::Value const issStr = v[jss::issuer];
98
99 if (!curStr.isString())
100 {
101 Throw<Json::error>(
102 "issueFromJson currency must be a string Json value");
103 }
104
105 auto const currency = to_currency(curStr.asString());
106 if (currency == badCurrency() || currency == noCurrency())
107 {
108 Throw<Json::error>("issueFromJson currency must be a valid currency");
109 }
110
111 if (isXRP(currency))
112 {
113 if (!issStr.isNull())
114 {
115 Throw<Json::error>("Issue, XRP should not have issuer");
116 }
117 return xrpIssue();
118 }
119
120 if (!issStr.isString())
121 {
122 Throw<Json::error>("issueFromJson issuer must be a string Json value");
123 }
124 auto const issuer = parseBase58<AccountID>(issStr.asString());
125
126 if (!issuer)
127 {
128 Throw<Json::error>("issueFromJson issuer must be a valid account");
129 }
130
131 return Issue{currency, *issuer};
132}
133
135operator<<(std::ostream& os, Issue const& x)
136{
137 os << to_string(x);
138 return os;
139}
140
141} // 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:99
bool isXRP(AccountID const &c)
Definition AccountID.h:71
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:628
Issue issueFromJson(Json::Value const &v)
Definition Issue.cpp:82
Json::Value to_json(Asset const &asset)
Definition Asset.h:124
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:65
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
T reserve(T... args)