Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/rpcwallet.cpp
// Copyright (c) 2010 Satoshi Nakamoto | // Copyright (c) 2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2018 The Bitcoin Core developers | // Copyright (c) 2009-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <amount.h> | #include <amount.h> | ||||
#include <chainparams.h> // for GetConsensus. | #include <chainparams.h> // for GetConsensus. | ||||
#include <coins.h> | #include <coins.h> | ||||
#include <config.h> | #include <config.h> | ||||
#include <consensus/validation.h> | #include <consensus/validation.h> | ||||
Show All 10 Lines | |||||
#include <rpc/util.h> | #include <rpc/util.h> | ||||
#include <script/descriptor.h> | #include <script/descriptor.h> | ||||
#include <util/bip32.h> | #include <util/bip32.h> | ||||
#include <util/error.h> | #include <util/error.h> | ||||
#include <util/moneystr.h> | #include <util/moneystr.h> | ||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <util/url.h> | #include <util/url.h> | ||||
#include <util/validation.h> | #include <util/validation.h> | ||||
#include <validation.h> | |||||
#include <wallet/coincontrol.h> | #include <wallet/coincontrol.h> | ||||
#include <wallet/psbtwallet.h> | #include <wallet/psbtwallet.h> | ||||
#include <wallet/rpcwallet.h> | #include <wallet/rpcwallet.h> | ||||
#include <wallet/wallet.h> | #include <wallet/wallet.h> | ||||
#include <wallet/walletdb.h> | #include <wallet/walletdb.h> | ||||
#include <wallet/walletutil.h> | #include <wallet/walletutil.h> | ||||
#include <univalue.h> | #include <univalue.h> | ||||
▲ Show 20 Lines • Show All 2,841 Lines • ▼ Show 20 Lines | if (tx_fee_rate == CFeeRate()) { | ||||
strprintf("txfee cannot be less than wallet min fee (%s)", | strprintf("txfee cannot be less than wallet min fee (%s)", | ||||
pwallet->m_min_fee.ToString())); | pwallet->m_min_fee.ToString())); | ||||
} | } | ||||
pwallet->m_pay_tx_fee = tx_fee_rate; | pwallet->m_pay_tx_fee = tx_fee_rate; | ||||
return true; | return true; | ||||
} | } | ||||
static UniValue getbalances(const Config &config, | |||||
const JSONRPCRequest &request) { | |||||
std::shared_ptr<CWallet> const rpc_wallet = | |||||
GetWalletForJSONRPCRequest(request); | |||||
if (!EnsureWalletIsAvailable(rpc_wallet.get(), request.fHelp)) { | |||||
return NullUniValue; | |||||
} | |||||
CWallet &wallet = *rpc_wallet; | |||||
const RPCHelpMan help{ | |||||
"getbalances", | |||||
"Returns an object with all balances in " + CURRENCY_UNIT + ".\n", | |||||
{}, | |||||
RPCResult{ | |||||
"{\n" | |||||
" \"mine\": { (object) balances from " | |||||
"outputs that the wallet can sign\n" | |||||
" \"trusted\": xxx (numeric) trusted balance " | |||||
"(outputs created by the wallet or confirmed outputs)\n" | |||||
" \"untrusted_pending\": xxx (numeric) untrusted " | |||||
"pending balance (outputs created by others that are in the " | |||||
"mempool)\n" | |||||
" \"immature\": xxx (numeric) balance from " | |||||
"immature coinbase outputs\n" | |||||
" },\n" | |||||
" \"watchonly\": { (object) watchonly " | |||||
"balances (not present if wallet does not watch anything)\n" | |||||
" \"trusted\": xxx (numeric) trusted balance " | |||||
"(outputs created by the wallet or confirmed outputs)\n" | |||||
" \"untrusted_pending\": xxx (numeric) untrusted " | |||||
"pending balance (outputs created by others that are in the " | |||||
"mempool)\n" | |||||
" \"immature\": xxx (numeric) balance from " | |||||
"immature coinbase outputs\n" | |||||
" },\n" | |||||
"}\n"}, | |||||
RPCExamples{HelpExampleCli("getbalances", "") + | |||||
HelpExampleRpc("getbalances", "")}, | |||||
}; | |||||
if (request.fHelp || !help.IsValidNumArgs(request.params.size())) { | |||||
throw std::runtime_error(help.ToString()); | |||||
} | |||||
// Make sure the results are valid at least up to the most recent block | |||||
// the user could have gotten from another RPC command prior to now | |||||
wallet.BlockUntilSyncedToCurrentChain(); | |||||
auto locked_chain = wallet.chain().lock(); | |||||
LOCK(wallet.cs_wallet); | |||||
UniValue obj(UniValue::VOBJ); | |||||
const auto bal = wallet.GetBalance(); | |||||
UniValue balances{UniValue::VOBJ}; | |||||
{ | |||||
UniValue balances_mine{UniValue::VOBJ}; | |||||
balances_mine.pushKV("trusted", ValueFromAmount(bal.m_mine_trusted)); | |||||
balances_mine.pushKV("untrusted_pending", | |||||
ValueFromAmount(bal.m_mine_untrusted_pending)); | |||||
balances_mine.pushKV("immature", ValueFromAmount(bal.m_mine_immature)); | |||||
balances.pushKV("mine", balances_mine); | |||||
} | |||||
if (wallet.HaveWatchOnly()) { | |||||
UniValue balances_watchonly{UniValue::VOBJ}; | |||||
balances_watchonly.pushKV("trusted", | |||||
ValueFromAmount(bal.m_watchonly_trusted)); | |||||
balances_watchonly.pushKV( | |||||
"untrusted_pending", | |||||
ValueFromAmount(bal.m_watchonly_untrusted_pending)); | |||||
balances_watchonly.pushKV("immature", | |||||
ValueFromAmount(bal.m_watchonly_immature)); | |||||
balances.pushKV("watchonly", balances_watchonly); | |||||
} | |||||
return balances; | |||||
} | |||||
static UniValue getwalletinfo(const Config &config, | static UniValue getwalletinfo(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request); | std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request); | ||||
CWallet *const pwallet = wallet.get(); | CWallet *const pwallet = wallet.get(); | ||||
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) { | if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) { | ||||
return NullUniValue; | return NullUniValue; | ||||
} | } | ||||
const RPCHelpMan help{ | const RPCHelpMan help{ | ||||
"getwalletinfo", | "getwalletinfo", | ||||
"Returns an object containing various wallet state info.\n", | "Returns an object containing various wallet state info.\n", | ||||
{}, | {}, | ||||
RPCResult{ | RPCResult{ | ||||
"{\n" | "{\n" | ||||
" \"walletname\": xxxxx, (string) the wallet name\n" | " \"walletname\": xxxxx, (string) the wallet name\n" | ||||
" \"walletversion\": xxxxx, (numeric) the wallet " | " \"walletversion\": xxxxx, (numeric) the wallet " | ||||
"version\n" | "version\n" | ||||
" \"balance\": xxxxxxx, (numeric) the total " | " \"balance\": xxxxxxx, (numeric) Identical to " | ||||
"confirmed balance of the wallet in " + | "getbalances().mine.trusted\n" | ||||
CURRENCY_UNIT + | " \"unconfirmed_balance\": xxx, (numeric) Identical to " | ||||
"\n" | "getbalances().mine.untrusted_pending\n" | ||||
" \"unconfirmed_balance\": xxx, (numeric) the total " | " \"immature_balance\": xxxxxx, (numeric) Identical to " | ||||
"unconfirmed balance of the wallet in " + | "getbalances().mine.immature\n" | ||||
CURRENCY_UNIT + | |||||
"\n" | |||||
" \"immature_balance\": xxxxxx, (numeric) the total immature " | |||||
"balance of the wallet in " + | |||||
CURRENCY_UNIT + | |||||
"\n" | |||||
" \"txcount\": xxxxxxx, (numeric) the total number " | " \"txcount\": xxxxxxx, (numeric) the total number " | ||||
"of transactions in the wallet\n" | "of transactions in the wallet\n" | ||||
" \"keypoololdest\": xxxxxx, (numeric) the timestamp " | " \"keypoololdest\": xxxxxx, (numeric) the timestamp " | ||||
"(seconds since Unix epoch) of the oldest pre-generated key in the " | "(seconds since Unix epoch) of the oldest pre-generated key in the " | ||||
"key pool\n" | "key pool\n" | ||||
" \"keypoolsize\": xxxx, (numeric) how many new keys " | " \"keypoolsize\": xxxx, (numeric) how many new keys " | ||||
"are pre-generated (only counts external keys)\n" | "are pre-generated (only counts external keys)\n" | ||||
" \"keypoolsize_hd_internal\": xxxx, (numeric) how many new keys " | " \"keypoolsize_hd_internal\": xxxx, (numeric) how many new keys " | ||||
▲ Show 20 Lines • Show All 1,832 Lines • ▼ Show 20 Lines | static const CRPCCommand commands[] = { | ||||
{ "wallet", "getaddressinfo", getaddressinfo, {"address"} }, | { "wallet", "getaddressinfo", getaddressinfo, {"address"} }, | ||||
{ "wallet", "getbalance", getbalance, {"dummy","minconf","include_watchonly"} }, | { "wallet", "getbalance", getbalance, {"dummy","minconf","include_watchonly"} }, | ||||
{ "wallet", "getnewaddress", getnewaddress, {"label", "address_type"} }, | { "wallet", "getnewaddress", getnewaddress, {"label", "address_type"} }, | ||||
{ "wallet", "getrawchangeaddress", getrawchangeaddress, {"address_type"} }, | { "wallet", "getrawchangeaddress", getrawchangeaddress, {"address_type"} }, | ||||
{ "wallet", "getreceivedbyaddress", getreceivedbyaddress, {"address","minconf"} }, | { "wallet", "getreceivedbyaddress", getreceivedbyaddress, {"address","minconf"} }, | ||||
{ "wallet", "getreceivedbylabel", getreceivedbylabel, {"label","minconf"} }, | { "wallet", "getreceivedbylabel", getreceivedbylabel, {"label","minconf"} }, | ||||
{ "wallet", "gettransaction", gettransaction, {"txid","include_watchonly"} }, | { "wallet", "gettransaction", gettransaction, {"txid","include_watchonly"} }, | ||||
{ "wallet", "getunconfirmedbalance", getunconfirmedbalance, {} }, | { "wallet", "getunconfirmedbalance", getunconfirmedbalance, {} }, | ||||
{ "wallet", "getbalances", getbalances, {} }, | |||||
{ "wallet", "getwalletinfo", getwalletinfo, {} }, | { "wallet", "getwalletinfo", getwalletinfo, {} }, | ||||
{ "wallet", "keypoolrefill", keypoolrefill, {"newsize"} }, | { "wallet", "keypoolrefill", keypoolrefill, {"newsize"} }, | ||||
{ "wallet", "listaddressgroupings", listaddressgroupings, {} }, | { "wallet", "listaddressgroupings", listaddressgroupings, {} }, | ||||
{ "wallet", "listlabels", listlabels, {"purpose"} }, | { "wallet", "listlabels", listlabels, {"purpose"} }, | ||||
{ "wallet", "listlockunspent", listlockunspent, {} }, | { "wallet", "listlockunspent", listlockunspent, {} }, | ||||
{ "wallet", "listreceivedbyaddress", listreceivedbyaddress, {"minconf","include_empty","include_watchonly","address_filter"} }, | { "wallet", "listreceivedbyaddress", listreceivedbyaddress, {"minconf","include_empty","include_watchonly","address_filter"} }, | ||||
{ "wallet", "listreceivedbylabel", listreceivedbylabel, {"minconf","include_empty","include_watchonly"} }, | { "wallet", "listreceivedbylabel", listreceivedbylabel, {"minconf","include_empty","include_watchonly"} }, | ||||
{ "wallet", "listsinceblock", listsinceblock, {"blockhash","target_confirmations","include_watchonly","include_removed"} }, | { "wallet", "listsinceblock", listsinceblock, {"blockhash","target_confirmations","include_watchonly","include_removed"} }, | ||||
Show All 32 Lines |