Skip to content

Commit 7297306

Browse files
celbalraickti
authored andcommitted
Refactor tokeninfo and add displaying NFT data file
1 parent c7daa3f commit 7297306

File tree

3 files changed

+95
-38
lines changed

3 files changed

+95
-38
lines changed

src/tokens/rpctokens.cpp

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#include <boost/lexical_cast.hpp>
2626

27-
void TokenGroupCreationToJSON(const CTokenGroupID &tgID, const CTokenGroupCreation& tgCreation, UniValue& entry, const bool extended = false) {
27+
void TokenGroupCreationToJSON(const CTokenGroupID &tgID, const CTokenGroupCreation& tgCreation, UniValue& entry, const bool fShowCreation = false, const bool fShowNFTData = false) {
2828
CTxOut creationOutput;
2929
CTxDestination creationDestination;
3030
GetGroupedCreationOutput(*tgCreation.creationTransaction, creationOutput);
@@ -36,15 +36,15 @@ void TokenGroupCreationToJSON(const CTokenGroupID &tgID, const CTokenGroupCreati
3636
entry.push_back(Pair("parent_groupID", EncodeTokenGroup(tgCreation.tokenGroupInfo.associatedGroup)));
3737
entry.push_back(Pair("subgroup_data", std::string(subgroupData.begin(), subgroupData.end())));
3838
}
39-
entry.push_back(Pair("ticker", tgDescGetTicker(*tgCreation.pTokenGroupDescription)));
40-
entry.push_back(Pair("name", tgDescGetName(*tgCreation.pTokenGroupDescription)));
41-
entry.push_back(Pair("decimal_pos", tgDescGetDecimalPos(*tgCreation.pTokenGroupDescription)));
42-
entry.push_back(Pair("metadata_url", tgDescGetDocumentURL(*tgCreation.pTokenGroupDescription)));
43-
entry.push_back(Pair("metadata_hash", tgDescGetDocumentHash(*tgCreation.pTokenGroupDescription).ToString()));
39+
4440
std::string flags = tgID.encodeFlags();
4541
if (flags != "none")
4642
entry.push_back(Pair("flags", flags));
47-
if (extended) {
43+
44+
UniValue specification = tgDescToJson(*tgCreation.pTokenGroupDescription, fShowNFTData);
45+
entry.push_back(Pair("specification", specification));
46+
47+
if (fShowCreation) {
4848
UniValue extendedEntry(UniValue::VOBJ);
4949
extendedEntry.push_back(Pair("txid", tgCreation.creationTransaction->GetHash().GetHex()));
5050
extendedEntry.push_back(Pair("blockhash", tgCreation.creationBlockHash.GetHex()));
@@ -64,19 +64,20 @@ extern UniValue tokeninfo(const JSONRPCRequest& request)
6464

6565
if (request.fHelp || request.params.size() < 1)
6666
throw std::runtime_error(
67-
"tokeninfo [list, all, stats, groupid, ticker, name] ( \"specifier \" ) ( \"extended_info\" ) \n"
67+
"tokeninfo [list, all, stats, groupid, ticker, name] ( \"specifier \" ) ( \"creation_data\" ) ( \"nft_data\" )\n"
6868
"\nReturns information on all tokens configured on the blockchain.\n"
6969
"\nArguments:\n"
7070
"'list' lists all token groupID's and corresponding token tickers\n"
71-
"'all' shows extended information on all tokens\n"
71+
"'all' shows data for all tokens\n"
7272
"'stats' shows statistical information on the management tokens in a specific block.\n"
7373
" Args: block hash (optional)\n"
7474
"'groupid' shows information on the token configuration with the specified grouID\n"
7575
"'ticker' shows information on the token configuration with the specified ticker\n"
7676
"'name' shows information on the token configuration with the specified name'\n"
7777
"\n"
7878
"'specifier' (string, optional) parameter to couple with the main action'\n"
79-
"'extended_info' (bool, optional) show extended information'\n"
79+
"'creation_data' (bool, optional) show token creation data'\n"
80+
"'nft_data' (bool, optional) show base64 encoded data of NFT tokens'\n"
8081
"\n" +
8182
HelpExampleCli("tokeninfo", "ticker \"WAGERR\"") +
8283
"\n"
@@ -105,17 +106,17 @@ extern UniValue tokeninfo(const JSONRPCRequest& request)
105106
if (request.params.size() > 2) {
106107
throw JSONRPCError(RPC_INVALID_PARAMS, "Too many parameters");
107108
}
108-
bool extended = false;
109+
bool fShowCreation = false;
109110
if (request.params.size() > curparam) {
110-
std::string sExtended;
111+
std::string strShowCreation;
111112
std::string p = request.params[curparam].get_str();
112-
std::transform(p.begin(), p.end(), std::back_inserter(sExtended), ::tolower);
113-
extended = (sExtended == "true");
113+
std::transform(p.begin(), p.end(), std::back_inserter(strShowCreation), ::tolower);
114+
fShowCreation = (strShowCreation == "true");
114115
}
115116

116117
for (auto tokenGroupMapping : tokenGroupManager.get()->GetMapTokenGroups()) {
117118
UniValue entry(UniValue::VOBJ);
118-
TokenGroupCreationToJSON(tokenGroupMapping.first, tokenGroupMapping.second, entry, extended);
119+
TokenGroupCreationToJSON(tokenGroupMapping.first, tokenGroupMapping.second, entry, fShowCreation);
119120
ret.push_back(entry);
120121
}
121122
} else if (operation == "stats") {
@@ -147,7 +148,7 @@ extern UniValue tokeninfo(const JSONRPCRequest& request)
147148
ret.push_back(entry);
148149

149150
} else if (operation == "groupid") {
150-
if (request.params.size() > 3) {
151+
if (request.params.size() > 4) {
151152
throw JSONRPCError(RPC_INVALID_PARAMS, "Too many parameters");
152153
}
153154

@@ -158,20 +159,28 @@ extern UniValue tokeninfo(const JSONRPCRequest& request)
158159
throw JSONRPCError(RPC_INVALID_PARAMS, "Invalid parameter: No group specified");
159160
}
160161
curparam++;
161-
bool extended = false;
162+
bool fShowCreation = false;
163+
if (request.params.size() > curparam) {
164+
std::string strShowCreation;
165+
std::string p = request.params[curparam].get_str();
166+
std::transform(p.begin(), p.end(), std::back_inserter(strShowCreation), ::tolower);
167+
fShowCreation = (strShowCreation == "true");
168+
}
169+
curparam++;
170+
bool fShowNFTData = false;
162171
if (request.params.size() > curparam) {
163-
std::string sExtended;
172+
std::string strShowNFTData;
164173
std::string p = request.params[curparam].get_str();
165-
std::transform(p.begin(), p.end(), std::back_inserter(sExtended), ::tolower);
166-
extended = (sExtended == "true");
174+
std::transform(p.begin(), p.end(), std::back_inserter(strShowNFTData), ::tolower);
175+
fShowNFTData = (strShowNFTData == "true");
167176
}
168177
UniValue entry(UniValue::VOBJ);
169178
CTokenGroupCreation tgCreation;
170179
tokenGroupManager.get()->GetTokenGroupCreation(grpID, tgCreation);
171-
TokenGroupCreationToJSON(grpID, tgCreation, entry, extended);
180+
TokenGroupCreationToJSON(grpID, tgCreation, entry, fShowCreation, fShowNFTData);
172181
ret.push_back(entry);
173182
} else if (operation == "ticker") {
174-
if (request.params.size() > 3) {
183+
if (request.params.size() > 4) {
175184
throw JSONRPCError(RPC_INVALID_PARAMS, "Too many parameters");
176185
}
177186

@@ -183,23 +192,31 @@ extern UniValue tokeninfo(const JSONRPCRequest& request)
183192
throw JSONRPCError(RPC_INVALID_PARAMS, "Invalid parameter: could not find token group");
184193
}
185194
curparam++;
186-
bool extended = false;
195+
bool fShowCreation = false;
187196
if (request.params.size() > curparam) {
188-
std::string sExtended;
197+
std::string strShowCreation;
189198
std::string p = request.params[curparam].get_str();
190-
std::transform(p.begin(), p.end(), std::back_inserter(sExtended), ::tolower);
191-
extended = (sExtended == "true");
199+
std::transform(p.begin(), p.end(), std::back_inserter(strShowCreation), ::tolower);
200+
fShowCreation = (strShowCreation == "true");
201+
}
202+
curparam++;
203+
bool fShowNFTData = false;
204+
if (request.params.size() > curparam) {
205+
std::string strShowNFTData;
206+
std::string p = request.params[curparam].get_str();
207+
std::transform(p.begin(), p.end(), std::back_inserter(strShowNFTData), ::tolower);
208+
fShowNFTData = (strShowNFTData == "true");
192209
}
193210

194211
CTokenGroupCreation tgCreation;
195212
tokenGroupManager.get()->GetTokenGroupCreation(grpID, tgCreation);
196213

197214
LogPrint(BCLog::TOKEN, "%s - tokenGroupCreation has [%s] [%s]\n", __func__, tgDescGetTicker(*tgCreation.pTokenGroupDescription), EncodeTokenGroup(tgCreation.tokenGroupInfo.associatedGroup));
198215
UniValue entry(UniValue::VOBJ);
199-
TokenGroupCreationToJSON(grpID, tgCreation, entry, extended);
216+
TokenGroupCreationToJSON(grpID, tgCreation, entry, fShowCreation, fShowNFTData);
200217
ret.push_back(entry);
201218
} else if (operation == "name") {
202-
if (request.params.size() > 3) {
219+
if (request.params.size() > 4) {
203220
throw JSONRPCError(RPC_INVALID_PARAMS, "Too many parameters");
204221
}
205222

@@ -211,20 +228,28 @@ extern UniValue tokeninfo(const JSONRPCRequest& request)
211228
throw JSONRPCError(RPC_INVALID_PARAMS, "Invalid parameter: Could not find token group");
212229
}
213230
curparam++;
214-
bool extended = false;
231+
bool fShowCreation = false;
232+
if (request.params.size() > curparam) {
233+
std::string strShowCreation;
234+
std::string p = request.params[curparam].get_str();
235+
std::transform(p.begin(), p.end(), std::back_inserter(strShowCreation), ::tolower);
236+
fShowCreation = (strShowCreation == "true");
237+
}
238+
curparam++;
239+
bool fShowNFTData = false;
215240
if (request.params.size() > curparam) {
216-
std::string sExtended;
241+
std::string strShowNFTData;
217242
std::string p = request.params[curparam].get_str();
218-
std::transform(p.begin(), p.end(), std::back_inserter(sExtended), ::tolower);
219-
extended = (sExtended == "true");
243+
std::transform(p.begin(), p.end(), std::back_inserter(strShowNFTData), ::tolower);
244+
fShowNFTData = (strShowNFTData == "true");
220245
}
221246

222247
CTokenGroupCreation tgCreation;
223248
tokenGroupManager.get()->GetTokenGroupCreation(grpID, tgCreation);
224249

225250
LogPrint(BCLog::TOKEN, "%s - tokenGroupCreation has [%s] [%s]\n", __func__, tgDescGetTicker(*tgCreation.pTokenGroupDescription), EncodeTokenGroup(tgCreation.tokenGroupInfo.associatedGroup));
226251
UniValue entry(UniValue::VOBJ);
227-
TokenGroupCreationToJSON(grpID, tgCreation, entry, extended);
252+
TokenGroupCreationToJSON(grpID, tgCreation, entry, fShowCreation, fShowNFTData);
228253
ret.push_back(entry);
229254
} else {
230255
throw JSONRPCError(RPC_INVALID_PARAMS, "Invalid parameter: unknown operation");

src/tokens/tokengroupdescription.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "util.h"
99
#include <rpc/protocol.h>
1010
#include <rpc/server.h>
11-
#include <univalue.h>
1211

1312
void CTokenGroupDescriptionRegular::ToJson(UniValue& obj) const
1413
{
@@ -33,14 +32,17 @@ void CTokenGroupDescriptionMGT::ToJson(UniValue& obj) const
3332
obj.pushKV("bls_pubkey", blsPubKey.ToString());
3433
}
3534

36-
void CTokenGroupDescriptionNFT::ToJson(UniValue& obj) const
35+
void CTokenGroupDescriptionNFT::ToJson(UniValue& obj, const bool& fFull) const
3736
{
3837
obj.clear();
3938
obj.setObject();
4039
obj.pushKV("name", strName);
4140
obj.pushKV("metadata_url", strDocumentUrl);
4241
obj.pushKV("metadata_hash", documentHash.ToString());
43-
obj.pushKV("data", EncodeBase64(vchData.data(), vchData.size()));
42+
if (fFull) {
43+
obj.pushKV("data_filename", strDataFilename);
44+
obj.pushKV("data_base64", EncodeBase64(vchData.data(), vchData.size()));
45+
}
4446
}
4547

4648
std::string ConsumeParamTicker(const JSONRPCRequest& request, unsigned int &curparam) {

src/tokens/tokengroupdescription.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#include "script/script.h"
1111
#include <primitives/transaction.h>
1212
#include "uint256.h"
13+
#include <univalue.h>
1314

1415
#include <boost/variant.hpp>
1516

1617
class JSONRPCRequest;
17-
class UniValue;
1818

1919
static CAmount COINFromDecimalPos(const uint8_t& nDecimalPos) {
2020
uint8_t n = nDecimalPos <= 16 ? nDecimalPos : 0;
@@ -197,7 +197,7 @@ class CTokenGroupDescriptionNFT
197197
READWRITE(vchData);
198198
READWRITE(strDataFilename);
199199
}
200-
void ToJson(UniValue& obj) const;
200+
void ToJson(UniValue& obj, const bool& fFull = false) const;
201201

202202
void WriteHashable(CHashWriter& ss) const {
203203
ss << nVersion;
@@ -222,6 +222,36 @@ class CTokenGroupDescriptionNFT
222222

223223
typedef boost::variant<CTokenGroupDescriptionRegular, CTokenGroupDescriptionMGT, CTokenGroupDescriptionNFT> CTokenGroupDescriptionVariant;
224224

225+
class tgdesc_to_json : public boost::static_visitor<UniValue>
226+
{
227+
private:
228+
const bool fFull = false;
229+
public:
230+
tgdesc_to_json(const bool& fFull = false) : fFull(fFull) {}
231+
232+
UniValue operator()(CTokenGroupDescriptionRegular& tgDesc) const
233+
{
234+
UniValue obj(UniValue::VOBJ);
235+
tgDesc.ToJson(obj);
236+
return obj;
237+
}
238+
UniValue operator()(CTokenGroupDescriptionMGT& tgDesc) const
239+
{
240+
UniValue obj(UniValue::VOBJ);
241+
tgDesc.ToJson(obj);
242+
return obj;
243+
}
244+
UniValue operator()(CTokenGroupDescriptionNFT& tgDesc) const
245+
{
246+
UniValue obj(UniValue::VOBJ);
247+
tgDesc.ToJson(obj, fFull);
248+
return obj;
249+
}
250+
};
251+
inline UniValue tgDescToJson(CTokenGroupDescriptionVariant& tgDesc, const bool& fFull = false) {
252+
return boost::apply_visitor(tgdesc_to_json(fFull), tgDesc);
253+
}
254+
225255
class tgdesc_get_ticker : public boost::static_visitor<std::string>
226256
{
227257
public:

0 commit comments

Comments
 (0)