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 {
29 ret += "0";
30 }
31 else if (account == noAccount())
32 {
33 ret += "1";
34 }
35 else
36 {
37 ret += to_string(account);
38 }
39 }
40
41 return ret;
42}
43
44void
46{
47 jv[jss::currency] = to_string(currency);
48 if (!isXRP(currency))
49 jv[jss::issuer] = toBase58(account);
50}
51
52bool
54{
55 return *this == xrpIssue();
56}
57
58bool
60{
61 return native();
62}
63
64bool
66{
67 return isXRP(ac.currency) == isXRP(ac.account);
68}
69
71to_string(Issue const& ac)
72{
73 if (isXRP(ac.account))
74 return to_string(ac.currency);
75
76 return to_string(ac.account) + "/" + to_string(ac.currency);
77}
78
80to_json(Issue const& is)
81{
82 Json::Value jv;
83 is.setJson(jv);
84 return jv;
85}
86
87Issue
89{
90 if (!v.isObject())
91 {
92 Throw<std::runtime_error>(
93 "issueFromJson can only be specified with an 'object' Json value");
94 }
95
96 if (v.isMember(jss::mpt_issuance_id))
97 {
98 Throw<std::runtime_error>("issueFromJson, Issue should not have mpt_issuance_id");
99 }
100
101 Json::Value const curStr = v[jss::currency];
102 Json::Value const issStr = v[jss::issuer];
103
104 if (!curStr.isString())
105 {
106 Throw<Json::error>("issueFromJson currency must be a string Json value");
107 }
108
109 auto const currency = to_currency(curStr.asString());
110 if (currency == badCurrency() || currency == noCurrency())
111 {
112 Throw<Json::error>("issueFromJson currency must be a valid currency");
113 }
114
115 if (isXRP(currency))
116 {
117 if (!issStr.isNull())
118 {
119 Throw<Json::error>("Issue, XRP should not have issuer");
120 }
121 return xrpIssue();
122 }
123
124 if (!issStr.isString())
125 {
126 Throw<Json::error>("issueFromJson issuer must be a string Json value");
127 }
128 auto const issuer = parseBase58<AccountID>(issStr.asString());
129
130 if (!issuer)
131 {
132 Throw<Json::error>("issueFromJson issuer must be a valid account");
133 }
134
135 return Issue{currency, *issuer};
136}
137
139operator<<(std::ostream& os, Issue const& x)
140{
141 os << to_string(x);
142 return os;
143}
144
145} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
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:13
Currency currency
Definition Issue.h:15
AccountID account
Definition Issue.h:16
bool native() const
Definition Issue.cpp:53
bool integral() const
Definition Issue.cpp:59
void setJson(Json::Value &jv) const
Definition Issue.cpp:45
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:5
bool isConsistent(Book const &book)
Definition Book.cpp:10
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:97
bool isXRP(AccountID const &c)
Definition AccountID.h:70
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
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:617
Issue issueFromJson(Json::Value const &v)
Definition Issue.cpp:88
Json::Value to_json(Asset const &asset)
Definition Asset.h:121
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:64
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
T reserve(T... args)