mirror of
https://github.com/LBRYFoundation/lbcwallet.git
synced 2025-08-23 17:47:29 +00:00
calculate each account's balance individually
This commit is contained in:
parent
2bb45752e1
commit
bf86ccf5b4
1 changed files with 48 additions and 12 deletions
|
@ -1514,23 +1514,59 @@ func (w *Wallet) AccountBalances(requiredConfs int32) ([]AccountBalanceResult, e
|
||||||
|
|
||||||
syncBlock := w.Manager.SyncedTo()
|
syncBlock := w.Manager.SyncedTo()
|
||||||
|
|
||||||
return w.Manager.ForEachAccount(addrmgrNs, func(account uint32) error {
|
// Fill out all account info except for the balances.
|
||||||
accountName, err := w.Manager.AccountName(addrmgrNs, account)
|
lastAcct, err := w.Manager.LastAccount(addrmgrNs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
balance, err := w.TxStore.Balance(txmgrNs, requiredConfs, syncBlock.Height)
|
results = make([]AccountBalanceResult, lastAcct+2)
|
||||||
|
for i := range results[:len(results)-1] {
|
||||||
|
accountName, err := w.Manager.AccountName(addrmgrNs, uint32(i))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
results = append(results, AccountBalanceResult{
|
results[i].AccountNumber = uint32(i)
|
||||||
AccountNumber: account,
|
results[i].AccountName = accountName
|
||||||
AccountName: accountName,
|
}
|
||||||
AccountBalance: balance,
|
results[len(results)-1].AccountNumber = waddrmgr.ImportedAddrAccount
|
||||||
})
|
results[len(results)-1].AccountName = waddrmgr.ImportedAddrAccountName
|
||||||
|
|
||||||
|
// Fetch all unspent outputs, and iterate over them tallying each
|
||||||
|
// account's balance where the output script pays to an account address
|
||||||
|
// and the required number of confirmations is met.
|
||||||
|
unspentOutputs, err := w.TxStore.UnspentOutputs(txmgrNs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for i := range unspentOutputs {
|
||||||
|
output := &unspentOutputs[i]
|
||||||
|
if !confirmed(requiredConfs, output.Height, syncBlock.Height) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if output.FromCoinBase && !confirmed(int32(w.ChainParams().CoinbaseMaturity),
|
||||||
|
output.Height, syncBlock.Height) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_, addrs, _, err := txscript.ExtractPkScriptAddrs(output.PkScript, w.chainParams)
|
||||||
|
if err != nil || len(addrs) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
outputAcct, err := w.Manager.AddrAccount(addrmgrNs, addrs[0])
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case outputAcct == waddrmgr.ImportedAddrAccount:
|
||||||
|
results[len(results)-1].AccountBalance += output.Amount
|
||||||
|
case outputAcct > lastAcct:
|
||||||
|
return errors.New("waddrmgr.Manager.AddrAccount returned account " +
|
||||||
|
"beyond recorded last account")
|
||||||
|
default:
|
||||||
|
results[outputAcct].AccountBalance += output.Amount
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
})
|
|
||||||
return results, err
|
return results, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue