mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-12 04:42:36 -03:00
Merge getreceivedby tally into GetReceived function
This commit is contained in:
parent
210b533a11
commit
a1d5b12ec0
1 changed files with 48 additions and 58 deletions
|
@ -576,6 +576,52 @@ static UniValue signmessage(const JSONRPCRequest& request)
|
|||
return signature;
|
||||
}
|
||||
|
||||
static CAmount GetReceived(interfaces::Chain::Lock& locked_chain, const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
|
||||
{
|
||||
std::set<CTxDestination> address_set;
|
||||
|
||||
if (by_label) {
|
||||
// Get the set of addresses assigned to label
|
||||
std::string label = LabelFromValue(params[0]);
|
||||
address_set = wallet.GetLabelAddresses(label);
|
||||
} else {
|
||||
// Get the address
|
||||
CTxDestination dest = DecodeDestination(params[0].get_str());
|
||||
if (!IsValidDestination(dest)) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
|
||||
}
|
||||
CScript script_pub_key = GetScriptForDestination(dest);
|
||||
if (!wallet.IsMine(script_pub_key)) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet");
|
||||
}
|
||||
address_set.insert(dest);
|
||||
}
|
||||
|
||||
// Minimum confirmations
|
||||
int min_depth = 1;
|
||||
if (!params[1].isNull())
|
||||
min_depth = params[1].get_int();
|
||||
|
||||
// Tally
|
||||
CAmount amount = 0;
|
||||
for (const std::pair<const uint256, CWalletTx>& wtx_pair : wallet.mapWallet) {
|
||||
const CWalletTx& wtx = wtx_pair.second;
|
||||
if (wtx.IsCoinBase() || !locked_chain.checkFinalTx(*wtx.tx) || wtx.GetDepthInMainChain() < min_depth) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const CTxOut& txout : wtx.tx->vout) {
|
||||
CTxDestination address;
|
||||
if (ExtractDestination(txout.scriptPubKey, address) && wallet.IsMine(address) && address_set.count(address)) {
|
||||
amount += txout.nValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return amount;
|
||||
}
|
||||
|
||||
|
||||
static UniValue getreceivedbyaddress(const JSONRPCRequest& request)
|
||||
{
|
||||
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
|
||||
|
@ -613,36 +659,7 @@ static UniValue getreceivedbyaddress(const JSONRPCRequest& request)
|
|||
auto locked_chain = pwallet->chain().lock();
|
||||
LOCK(pwallet->cs_wallet);
|
||||
|
||||
// Bitcoin address
|
||||
CTxDestination dest = DecodeDestination(request.params[0].get_str());
|
||||
if (!IsValidDestination(dest)) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
|
||||
}
|
||||
CScript scriptPubKey = GetScriptForDestination(dest);
|
||||
if (!pwallet->IsMine(scriptPubKey)) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet");
|
||||
}
|
||||
|
||||
// Minimum confirmations
|
||||
int nMinDepth = 1;
|
||||
if (!request.params[1].isNull())
|
||||
nMinDepth = request.params[1].get_int();
|
||||
|
||||
// Tally
|
||||
CAmount nAmount = 0;
|
||||
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
|
||||
const CWalletTx& wtx = pairWtx.second;
|
||||
if (wtx.IsCoinBase() || !locked_chain->checkFinalTx(*wtx.tx)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const CTxOut& txout : wtx.tx->vout)
|
||||
if (txout.scriptPubKey == scriptPubKey)
|
||||
if (wtx.GetDepthInMainChain() >= nMinDepth)
|
||||
nAmount += txout.nValue;
|
||||
}
|
||||
|
||||
return ValueFromAmount(nAmount);
|
||||
return ValueFromAmount(GetReceived(*locked_chain, *pwallet, request.params, /* by_label */ false));
|
||||
}
|
||||
|
||||
|
||||
|
@ -683,34 +700,7 @@ static UniValue getreceivedbylabel(const JSONRPCRequest& request)
|
|||
auto locked_chain = pwallet->chain().lock();
|
||||
LOCK(pwallet->cs_wallet);
|
||||
|
||||
// Minimum confirmations
|
||||
int nMinDepth = 1;
|
||||
if (!request.params[1].isNull())
|
||||
nMinDepth = request.params[1].get_int();
|
||||
|
||||
// Get the set of pub keys assigned to label
|
||||
std::string label = LabelFromValue(request.params[0]);
|
||||
std::set<CTxDestination> setAddress = pwallet->GetLabelAddresses(label);
|
||||
|
||||
// Tally
|
||||
CAmount nAmount = 0;
|
||||
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
|
||||
const CWalletTx& wtx = pairWtx.second;
|
||||
if (wtx.IsCoinBase() || !locked_chain->checkFinalTx(*wtx.tx)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const CTxOut& txout : wtx.tx->vout)
|
||||
{
|
||||
CTxDestination address;
|
||||
if (ExtractDestination(txout.scriptPubKey, address) && pwallet->IsMine(address) && setAddress.count(address)) {
|
||||
if (wtx.GetDepthInMainChain() >= nMinDepth)
|
||||
nAmount += txout.nValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ValueFromAmount(nAmount);
|
||||
return ValueFromAmount(GetReceived(*locked_chain, *pwallet, request.params, /* by_label */ true));
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue