From d54e539700b2bbfd812eae5a45d36c9ac94622cd Mon Sep 17 00:00:00 2001 From: Roy Lee Date: Mon, 22 Aug 2022 11:41:26 -0700 Subject: [PATCH] multi-scope: update getbalance --- internal/rpchelp/methods.go | 2 +- rpc/legacyrpc/methods.go | 32 ++++++++++++-------------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/internal/rpchelp/methods.go b/internal/rpchelp/methods.go index 7e2c136ca2..3d362c94ee 100644 --- a/internal/rpchelp/methods.go +++ b/internal/rpchelp/methods.go @@ -31,7 +31,7 @@ var Methods = []struct { {"getaccountaddress", returnsString}, {"getaddressesbyaccount", returnsStringArray}, {"getaddressinfo", []interface{}{(*btcjson.GetAddressInfoResult)(nil)}}, - {"getbalance", append(returnsNumber, returnsNumber[0])}, + {"getbalance", returnsNumber}, {"getbestblockhash", returnsString}, {"getblockcount", returnsNumber}, {"getinfo", []interface{}{(*btcjson.InfoWalletResult)(nil)}}, diff --git a/rpc/legacyrpc/methods.go b/rpc/legacyrpc/methods.go index a187dab5f3..494201fe03 100644 --- a/rpc/legacyrpc/methods.go +++ b/rpc/legacyrpc/methods.go @@ -582,30 +582,22 @@ func getAddressInfo(icmd interface{}, w *wallet.Wallet) (interface{}, error) { func getBalance(icmd interface{}, w *wallet.Wallet) (interface{}, error) { cmd := icmd.(*btcjson.GetBalanceCmd) - var balance btcutil.Amount - var err error - accountName := "*" - if cmd.Account != nil { - accountName = *cmd.Account - } - if accountName == "*" { - balance, _, err = w.CalculateBalance(int32(*cmd.MinConf)) - if err != nil { - return nil, err - } - } else { - var account uint32 - account, err = w.AccountNumber(waddrmgr.KeyScopeBIP0044, accountName) - if err != nil { - return nil, err - } - bals, err := w.CalculateAccountBalances(account, int32(*cmd.MinConf)) + if *cmd.Account == "*" { + balance, _, err := w.CalculateBalance(int32(*cmd.MinConf)) if err != nil { return nil, err } - balance = bals.Spendable + return balance.ToBTC(), nil + } + + account, err := w.AccountNumber(*cmd.Account) + if err != nil { + return nil, err } - return balance.ToBTC(), nil + + bals, err := w.CalculateAccountBalances(account, int32(*cmd.MinConf)) + + return bals.Spendable.ToBTC(), err } // getBestBlock handles a getbestblock request by returning a JSON object