Skip to content

Commit

Permalink
multi-scope: update getreceivedbyaccount
Browse files Browse the repository at this point in the history
  • Loading branch information
roylee17 committed Sep 26, 2022
1 parent ffd0e0f commit b1d25b1
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions rpc/legacyrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,27 +928,38 @@ func getRawChangeAddress(icmd interface{}, w *wallet.Wallet) (interface{}, error
// getReceivedByAccount handles a getreceivedbyaccount request by returning
// the total amount received by addresses of an account.
func getReceivedByAccount(icmd interface{}, w *wallet.Wallet) (interface{}, error) {

cmd := icmd.(*btcjson.GetReceivedByAccountCmd)

account, err := w.AccountNumber(waddrmgr.KeyScopeBIP0044, cmd.Account)
account, err := w.AccountNumber(*cmd.Account)
if err != nil {
return nil, err
}

// TODO: This is more inefficient that it could be, but the entire
// algorithm is already dominated by reading every transaction in the
// wallet's history.
results, err := w.TotalReceivedForAccounts(
waddrmgr.KeyScopeBIP0044, int32(*cmd.MinConf),
)
if err != nil {
return nil, err
}
acctIndex := int(account)
if account == waddrmgr.ImportedAddrAccount {
acctIndex = len(results) - 1
totalReceived := 0.0

var results []wallet.AccountTotalReceivedResult

fn := func(scope waddrmgr.KeyScope) error {

results, err = w.TotalReceivedForAccounts(
scope, int32(*cmd.MinConf))
if err != nil {
return err
}

acctIndex := int(account)
if account == waddrmgr.ImportedAddrAccount {
acctIndex = len(results) - 1
}

totalReceived += results[acctIndex].TotalReceived.ToBTC()

return nil
}
return results[acctIndex].TotalReceived.ToBTC(), nil
err = forEachKeyScope(fn)

return totalReceived, err
}

// getReceivedByAddress handles a getreceivedbyaddress request by returning
Expand Down

0 comments on commit b1d25b1

Please sign in to comment.