1
1
// Copyright (c) 2010 Satoshi Nakamoto
2
- // Copyright (c) 2009-2018 The Bitcoin Core developers
2
+ // Copyright (c) 2009-2019 The Bitcoin Core developers
3
3
// Distributed under the MIT software license, see the accompanying
4
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
9
9
#include < core_io.h>
10
10
#include < init.h>
11
11
#include < interfaces/chain.h>
12
- #include < validation.h>
13
12
#include < key_io.h>
14
13
#include < net.h>
15
14
#include < node/transaction.h>
27
26
#include < timedata.h>
28
27
#include < util/bip32.h>
29
28
#include < util/fees.h>
30
- #include < util/system.h>
31
29
#include < util/moneystr.h>
30
+ #include < util/system.h>
32
31
#include < util/url.h>
33
32
#include < util/validation.h>
33
+ #include < validation.h>
34
34
#include < wallet/coincontrol.h>
35
35
#include < wallet/feebumper.h>
36
36
#include < wallet/psbtwallet.h>
@@ -2373,6 +2373,68 @@ static UniValue settxfee(const JSONRPCRequest& request)
2373
2373
return true ;
2374
2374
}
2375
2375
2376
+ static UniValue getbalances (const JSONRPCRequest& request)
2377
+ {
2378
+ std::shared_ptr<CWallet> const rpc_wallet = GetWalletForJSONRPCRequest (request);
2379
+ if (!EnsureWalletIsAvailable (rpc_wallet.get (), request.fHelp )) {
2380
+ return NullUniValue;
2381
+ }
2382
+ CWallet& wallet = *rpc_wallet;
2383
+
2384
+ const RPCHelpMan help{
2385
+ " getbalances" ,
2386
+ " Returns an object with all balances in " + CURRENCY_UNIT + " .\n " ,
2387
+ {},
2388
+ RPCResult{
2389
+ " {\n "
2390
+ " \" mine\" : { (object) balances from outputs that the wallet can sign\n "
2391
+ " \" trusted\" : xxx (numeric) trusted balance (outputs created by the wallet or confirmed outputs)\n "
2392
+ " \" untrusted_pending\" : xxx (numeric) untrusted pending balance (outputs created by others that are in the mempool)\n "
2393
+ " \" immature\" : xxx (numeric) balance from immature coinbase outputs\n "
2394
+ " },\n "
2395
+ " \" watchonly\" : { (object) watchonly balances (not present if wallet does not watch anything)\n "
2396
+ " \" trusted\" : xxx (numeric) trusted balance (outputs created by the wallet or confirmed outputs)\n "
2397
+ " \" untrusted_pending\" : xxx (numeric) untrusted pending balance (outputs created by others that are in the mempool)\n "
2398
+ " \" immature\" : xxx (numeric) balance from immature coinbase outputs\n "
2399
+ " },\n "
2400
+ " }\n " },
2401
+ RPCExamples{
2402
+ HelpExampleCli (" getbalances" , " " ) +
2403
+ HelpExampleRpc (" getbalances" , " " )},
2404
+ };
2405
+
2406
+ if (request.fHelp || !help.IsValidNumArgs (request.params .size ())) {
2407
+ throw std::runtime_error (help.ToString ());
2408
+ }
2409
+
2410
+ // Make sure the results are valid at least up to the most recent block
2411
+ // the user could have gotten from another RPC command prior to now
2412
+ wallet.BlockUntilSyncedToCurrentChain ();
2413
+
2414
+ auto locked_chain = wallet.chain ().lock ();
2415
+ LOCK (wallet.cs_wallet );
2416
+
2417
+ UniValue obj (UniValue::VOBJ);
2418
+
2419
+ const auto bal = wallet.GetBalance ();
2420
+ UniValue balances{UniValue::VOBJ};
2421
+ {
2422
+ UniValue balances_mine{UniValue::VOBJ};
2423
+ balances_mine.pushKV (" trusted" , ValueFromAmount (bal.m_mine_trusted ));
2424
+ balances_mine.pushKV (" untrusted_pending" , ValueFromAmount (bal.m_mine_untrusted_pending ));
2425
+ balances_mine.pushKV (" immature" , ValueFromAmount (bal.m_mine_immature ));
2426
+ balances.pushKV (" mine" , balances_mine);
2427
+ }
2428
+ if (wallet.HaveWatchOnly ()) {
2429
+ UniValue balances_watchonly{UniValue::VOBJ};
2430
+ balances_watchonly.pushKV (" trusted" , ValueFromAmount (bal.m_watchonly_trusted ));
2431
+ balances_watchonly.pushKV (" untrusted_pending" , ValueFromAmount (bal.m_watchonly_untrusted_pending ));
2432
+ balances_watchonly.pushKV (" immature" , ValueFromAmount (bal.m_watchonly_immature ));
2433
+ balances.pushKV (" watchonly" , balances_watchonly);
2434
+ }
2435
+ return balances;
2436
+ }
2437
+
2376
2438
static UniValue getwalletinfo (const JSONRPCRequest& request)
2377
2439
{
2378
2440
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest (request);
@@ -2389,9 +2451,9 @@ static UniValue getwalletinfo(const JSONRPCRequest& request)
2389
2451
" {\n "
2390
2452
" \" walletname\" : xxxxx, (string) the wallet name\n "
2391
2453
" \" walletversion\" : xxxxx, (numeric) the wallet version\n "
2392
- " \" balance\" : xxxxxxx, (numeric) the total confirmed balance of the wallet in " + CURRENCY_UNIT + " \n "
2393
- " \" unconfirmed_balance\" : xxx, (numeric) the total unconfirmed balance of the wallet in " + CURRENCY_UNIT + " \n "
2394
- " \" immature_balance\" : xxxxxx, (numeric) the total immature balance of the wallet in " + CURRENCY_UNIT + " \n "
2454
+ " \" balance\" : xxxxxxx, (numeric) Identical to getbalances().mine.trusted \n "
2455
+ " \" unconfirmed_balance\" : xxx, (numeric) Identical to getbalances().mine.untrusted_pending \n "
2456
+ " \" immature_balance\" : xxxxxx, (numeric) Identical to getbalances().mine. immature\n "
2395
2457
" \" txcount\" : xxxxxxx, (numeric) the total number of transactions in the wallet\n "
2396
2458
" \" keypoololdest\" : xxxxxx, (numeric) the timestamp (seconds since Unix epoch) of the oldest pre-generated key in the key pool\n "
2397
2459
" \" keypoolsize\" : xxxx, (numeric) how many new keys are pre-generated (only counts external keys)\n "
@@ -4075,6 +4137,7 @@ static const CRPCCommand commands[] =
4075
4137
{ " wallet" , " getreceivedbylabel" , &getreceivedbylabel, {" label" ," minconf" } },
4076
4138
{ " wallet" , " gettransaction" , &gettransaction, {" txid" ," include_watchonly" } },
4077
4139
{ " wallet" , " getunconfirmedbalance" , &getunconfirmedbalance, {} },
4140
+ { " wallet" , " getbalances" , &getbalances, {} },
4078
4141
{ " wallet" , " getwalletinfo" , &getwalletinfo, {} },
4079
4142
{ " wallet" , " importaddress" , &importaddress, {" address" ," label" ," rescan" ," p2sh" } },
4080
4143
{ " wallet" , " importmulti" , &importmulti, {" requests" ," options" } },
0 commit comments