main: get address deltas between range of block heights

This commit is contained in:
Braydon Fuller
2016-03-24 15:44:23 -04:00
committed by Braydon Fuller
parent 206882cd4b
commit cad092aebb
8 changed files with 120 additions and 15 deletions

View File

@@ -435,6 +435,66 @@ bool getAddressesFromParams(const UniValue& params, std::vector<std::pair<uint16
}
UniValue getaddressdeltas(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 1 || !params[0].isObject())
throw runtime_error(
"getaddressdeltas\n"
"\nReturns all changes for an address (requires addressindex to be enabled).\n"
"\nResult\n"
"[\n"
" {\n"
" \"satoshis\" (number) The difference of satoshis\n"
" \"txid\" (string) The related txid\n"
" \"index\" (number) The related input or output index\n"
" \"height\" (number) The block height\n"
" \"hash\" (string) The address hash\n"
" \"type\" (number) The address type 0 for pubkeyhash 1 for scripthash\n"
" }\n"
"]\n"
);
UniValue startValue = find_value(params[0].get_obj(), "start");
UniValue endValue = find_value(params[0].get_obj(), "end");
if (!startValue.isNum() || !endValue.isNum()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Start and end values are expected to be block heights");
}
int start = startValue.get_int();
int end = startValue.get_int();
std::vector<std::pair<uint160, int> > addresses;
if (!getAddressesFromParams(params, addresses)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
}
std::vector<std::pair<CAddressIndexKey, CAmount> > addressIndex;
for (std::vector<std::pair<uint160, int> >::iterator it = addresses.begin(); it != addresses.end(); it++) {
if (!GetAddressIndex((*it).first, (*it).second, addressIndex, start, end)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
}
}
UniValue result(UniValue::VARR);
for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) {
UniValue delta(UniValue::VOBJ);
delta.push_back(Pair("satoshis", it->second));
delta.push_back(Pair("txid", it->first.txhash.GetHex()));
delta.push_back(Pair("index", it->first.index));
delta.push_back(Pair("height", it->first.blockHeight));
delta.push_back(Pair("hash", it->first.hashBytes.GetHex()));
delta.push_back(Pair("type", (int)it->first.type));
result.push_back(delta);
}
return result;
}
UniValue getaddressbalance(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 1)