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>
@@ -70,14 +70,14 @@ std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& reques
70
70
return wallets.size () == 1 || (request.fHelp && wallets.size () > 0 ) ? wallets[0 ] : nullptr ;
71
71
}
72
72
73
- std::string HelpRequiringPassphrase (CWallet * const pwallet)
73
+ std::string HelpRequiringPassphrase (const CWallet* pwallet)
74
74
{
75
75
return pwallet && pwallet->IsCrypted ()
76
76
? " \n Requires wallet passphrase to be set with walletpassphrase call."
77
77
: " " ;
78
78
}
79
79
80
- bool EnsureWalletIsAvailable (CWallet * const pwallet, bool avoidException)
80
+ bool EnsureWalletIsAvailable (const CWallet* pwallet, bool avoidException)
81
81
{
82
82
if (pwallet) return true ;
83
83
if (avoidException) return false ;
@@ -89,7 +89,7 @@ bool EnsureWalletIsAvailable(CWallet * const pwallet, bool avoidException)
89
89
" Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path)." );
90
90
}
91
91
92
- void EnsureWalletIsUnlocked (CWallet * const pwallet)
92
+ void EnsureWalletIsUnlocked (const CWallet* pwallet)
93
93
{
94
94
if (pwallet->IsLocked ()) {
95
95
throw JSONRPCError (RPC_WALLET_UNLOCK_NEEDED, " Error: Please enter the wallet passphrase with walletpassphrase first." );
@@ -785,7 +785,7 @@ static UniValue getunconfirmedbalance(const JSONRPCRequest &request)
785
785
if (request.fHelp || request.params .size () > 0 )
786
786
throw std::runtime_error (
787
787
RPCHelpMan{" getunconfirmedbalance" ,
788
- " Returns the server's total unconfirmed balance \n " ,
788
+ " DEPRECATED \n Identical to getbalances().mine.untrusted_pending \n " ,
789
789
{},
790
790
RPCResults{},
791
791
RPCExamples{" " },
@@ -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);
@@ -2382,18 +2444,16 @@ static UniValue getwalletinfo(const JSONRPCRequest& request)
2382
2444
return NullUniValue;
2383
2445
}
2384
2446
2385
- if (request.fHelp || request.params .size () != 0 )
2386
- throw std::runtime_error (
2387
- RPCHelpMan{" getwalletinfo" ,
2447
+ const RPCHelpMan help{" getwalletinfo" ,
2388
2448
" Returns an object containing various wallet state info.\n " ,
2389
2449
{},
2390
2450
RPCResult{
2391
2451
" {\n "
2392
2452
" \" walletname\" : xxxxx, (string) the wallet name\n "
2393
2453
" \" walletversion\" : xxxxx, (numeric) the wallet version\n "
2394
- " \" balance\" : xxxxxxx, (numeric) the total confirmed balance of the wallet in " + CURRENCY_UNIT + " \n "
2395
- " \" unconfirmed_balance\" : xxx, (numeric) the total unconfirmed balance of the wallet in " + CURRENCY_UNIT + " \n "
2396
- " \" immature_balance\" : xxxxxx, (numeric) the total immature balance of the wallet in " + CURRENCY_UNIT + " \n "
2454
+ " \" balance\" : xxxxxxx, (numeric) DEPRECATED. Identical to getbalances().mine.trusted \n "
2455
+ " \" unconfirmed_balance\" : xxx, (numeric) DEPRECATED. Identical to getbalances().mine.untrusted_pending \n "
2456
+ " \" immature_balance\" : xxxxxx, (numeric) DEPRECATED. Identical to getbalances().mine.immature \n "
2397
2457
" \" txcount\" : xxxxxxx, (numeric) the total number of transactions in the wallet\n "
2398
2458
" \" keypoololdest\" : xxxxxx, (numeric) the timestamp (seconds since Unix epoch) of the oldest pre-generated key in the key pool\n "
2399
2459
" \" keypoolsize\" : xxxx, (numeric) how many new keys are pre-generated (only counts external keys)\n "
@@ -2408,7 +2468,11 @@ static UniValue getwalletinfo(const JSONRPCRequest& request)
2408
2468
HelpExampleCli (" getwalletinfo" , " " )
2409
2469
+ HelpExampleRpc (" getwalletinfo" , " " )
2410
2470
},
2411
- }.ToString ());
2471
+ };
2472
+
2473
+ if (request.fHelp || !help.IsValidNumArgs (request.params .size ())) {
2474
+ throw std::runtime_error (help.ToString ());
2475
+ }
2412
2476
2413
2477
// Make sure the results are valid at least up to the most recent block
2414
2478
// the user could have gotten from another RPC command prior to now
@@ -4073,6 +4137,7 @@ static const CRPCCommand commands[] =
4073
4137
{ " wallet" , " getreceivedbylabel" , &getreceivedbylabel, {" label" ," minconf" } },
4074
4138
{ " wallet" , " gettransaction" , &gettransaction, {" txid" ," include_watchonly" } },
4075
4139
{ " wallet" , " getunconfirmedbalance" , &getunconfirmedbalance, {} },
4140
+ { " wallet" , " getbalances" , &getbalances, {} },
4076
4141
{ " wallet" , " getwalletinfo" , &getwalletinfo, {} },
4077
4142
{ " wallet" , " importaddress" , &importaddress, {" address" ," label" ," rescan" ," p2sh" } },
4078
4143
{ " wallet" , " importmulti" , &importmulti, {" requests" ," options" } },
0 commit comments