Merge pull request #796 from letolabs/coinsupply

Docs for coinsupply RPC and improved error-checking on block height
This commit is contained in:
jl777
2018-07-30 22:47:53 -11:00
committed by GitHub

View File

@@ -229,12 +229,31 @@ public:
UniValue coinsupply(const UniValue& params, bool fHelp) UniValue coinsupply(const UniValue& params, bool fHelp)
{ {
int32_t height = 0; int64_t zfunds,supply = 0; UniValue result(UniValue::VOBJ); int32_t height = 0; int32_t currentHeight; int64_t zfunds,supply = 0; UniValue result(UniValue::VOBJ);
if (fHelp || params.size() > 1) if (fHelp || params.size() > 1)
throw runtime_error("coinsupply <height>\n"); throw runtime_error("coinsupply <height>\n"
"\nReturn coin supply information at a given block height. If no height is given, the current height is used.\n"
"\nArguments:\n"
"1. \"height\" (integer, optional) Block height\n"
"\nResult:\n"
"{\n"
" \"result\" : \"success\", (string) If the request was successful.\n"
" \"coin\" : \"KMD\", (string) The currency symbol of the coin for asset chains, otherwise KMD.\n"
" \"height\" : 420, (integer) The height of this coin supply data\n"
" \"supply\" : \"777.0\", (float) The transparent coin supply\n"
" \"zfunds\" : \"0.777\", (float) The shielded coin supply (in zaddrs)\n"
" \"total\" : \"777.777\", (float) The total coin supply, i.e. sum of supply + zfunds\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("coinsupply", "420")
+ HelpExampleRpc("coinsupply", "420")
);
if ( params.size() == 0 ) if ( params.size() == 0 )
height = chainActive.Height(); height = chainActive.Height();
else height = atoi(params[0].get_str()); else height = atoi(params[0].get_str());
currentHeight = chainActive.Height();
if (height >= 0 && height <= currentHeight) {
if ( (supply= komodo_coinsupply(&zfunds,height)) > 0 ) if ( (supply= komodo_coinsupply(&zfunds,height)) > 0 )
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
@@ -244,6 +263,9 @@ UniValue coinsupply(const UniValue& params, bool fHelp)
result.push_back(Pair("zfunds", ValueFromAmount(zfunds))); result.push_back(Pair("zfunds", ValueFromAmount(zfunds)));
result.push_back(Pair("total", ValueFromAmount(zfunds + supply))); result.push_back(Pair("total", ValueFromAmount(zfunds + supply)));
} else result.push_back(Pair("error", "couldnt calculate supply")); } else result.push_back(Pair("error", "couldnt calculate supply"));
} else {
result.push_back(Pair("error", "invalid height"));
}
return(result); return(result);
} }