From 0e51b8912b7d0ed17ee04b47983cf311a613cb05 Mon Sep 17 00:00:00 2001 From: DeFiDev M Date: Thu, 22 Oct 2020 11:58:58 +0700 Subject: [PATCH 1/2] rpc gettokenbalances fix --- src/masternodes/mn_rpc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masternodes/mn_rpc.cpp b/src/masternodes/mn_rpc.cpp index 9197ab6f72..894b4e4340 100644 --- a/src/masternodes/mn_rpc.cpp +++ b/src/masternodes/mn_rpc.cpp @@ -1475,7 +1475,7 @@ UniValue gettokenbalances(const JSONRPCRequest& request) { } return true; }, BalanceKey{}); - auto it = totalBalances.balances.find(start); + auto it = totalBalances.balances.lower_bound(start); for (int i = 0; it != totalBalances.balances.end() && i < limit; it++, i++) { CTokenAmount bal = CTokenAmount{(*it).first, (*it).second}; if (indexed_amounts) From 429b79b984b1ef01df81a8c70e3c9d5b822cca32 Mon Sep 17 00:00:00 2001 From: DeFiDev M Date: Thu, 22 Oct 2020 12:00:23 +0700 Subject: [PATCH 2/2] rpc: token symbol lookup for gettokenbalances --- src/masternodes/mn_rpc.cpp | 19 +++++++++++++++---- src/rpc/client.cpp | 1 + 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/masternodes/mn_rpc.cpp b/src/masternodes/mn_rpc.cpp index 894b4e4340..88df7fb2c5 100644 --- a/src/masternodes/mn_rpc.cpp +++ b/src/masternodes/mn_rpc.cpp @@ -1419,6 +1419,8 @@ UniValue gettokenbalances(const JSONRPCRequest& request) { }, {"indexed_amounts", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED, "Format of amounts output (default = false): (true: obj = {tokenid:amount,...}, false: array = [\"amount@tokenid\"...])"}, + {"symbol_lookup", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED, + "Use token symbols in output (default = false)"}, }, RPCResult{ "{...} (array) Json object with balances information\n" @@ -1455,7 +1457,11 @@ UniValue gettokenbalances(const JSONRPCRequest& request) { } bool indexed_amounts = false; if (request.params.size() > 1) { - indexed_amounts = request.params[1].get_bool(); + indexed_amounts = request.params[1].getBool(); + } + bool symbol_lookup = false; + if (request.params.size() > 2) { + symbol_lookup = request.params[2].getBool(); } UniValue ret(UniValue::VARR); @@ -1478,10 +1484,15 @@ UniValue gettokenbalances(const JSONRPCRequest& request) { auto it = totalBalances.balances.lower_bound(start); for (int i = 0; it != totalBalances.balances.end() && i < limit; it++, i++) { CTokenAmount bal = CTokenAmount{(*it).first, (*it).second}; + std::string tokenIdStr = bal.nTokenId.ToString(); + if (symbol_lookup) { + auto token = pcustomcsview->GetToken(bal.nTokenId); + tokenIdStr = token->CreateSymbolKey(bal.nTokenId); + } if (indexed_amounts) - ret.pushKV(bal.nTokenId.ToString(), ValueFromAmount(bal.nValue)); + ret.pushKV(tokenIdStr, ValueFromAmount(bal.nValue)); else - ret.push_back(bal.ToString()); + ret.push_back(ValueFromAmount(bal.nValue).getValStr() + "@" + tokenIdStr); } return ret; } @@ -2869,7 +2880,7 @@ static const CRPCCommand commands[] = {"poolpair", "getpoolpair", &getpoolpair, {"key", "verbose" }}, {"poolpair", "addpoolliquidity", &addpoolliquidity, {"from", "shareAddress", "inputs"}}, {"poolpair", "removepoolliquidity",&removepoolliquidity,{"from", "amount", "inputs"}}, - {"accounts", "gettokenbalances", &gettokenbalances, {"pagination", "indexed_amounts"}}, + {"accounts", "gettokenbalances", &gettokenbalances, {"pagination", "indexed_amounts", "symbol_lookup"}}, {"accounts", "utxostoaccount", &utxostoaccount, {"amounts", "inputs"}}, {"accounts", "accounttoaccount", &accounttoaccount, {"from", "to", "inputs"}}, {"accounts", "accounttoutxos", &accounttoutxos, {"from", "to", "inputs"}}, diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index f800c814ce..af5cb4461d 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -205,6 +205,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "getaccount", 2, "indexed_amounts" }, { "gettokenbalances", 0, "pagination" }, { "gettokenbalances", 1, "indexed_amounts" }, + { "gettokenbalances", 2, "symbol_lookup" }, { "accounttoaccount", 1, "to" }, { "accounttoaccount", 2, "inputs" }, { "accounttoutxos", 1, "to" },