get bitcoin addresses concurrently
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
Steven Polley 2024-09-01 09:05:21 -06:00
parent 647f9a8f7b
commit c119f1f57c

View File

@ -2,7 +2,9 @@ package bitcoin
import (
"fmt"
"log"
"os"
"sync"
)
type Provider struct {
@ -50,15 +52,22 @@ func (p *Provider) GetBalances() ([]int, []string, error) {
balances := make([]int, 0)
ynabAccountIDs := make([]string, 0)
var satoshiBalance int
for _, bitcoinAddress := range p.bitcoinAddresses {
addressResponse, err := p.client.getAddress(bitcoinAddress)
if err != nil {
return balances, ynabAccountIDs, fmt.Errorf("failed to get bitcoin address '%s': %v", bitcoinAddress, err)
}
wg := sync.WaitGroup{}
satoshiBalance += addressResponse.ChainStats.FundedTxoSum - addressResponse.ChainStats.SpentTxoSum
for _, bitcoinAddress := range p.bitcoinAddresses {
wg.Add(1)
go func() {
defer wg.Done()
addressResponse, err := p.client.getAddress(bitcoinAddress)
if err != nil {
log.Printf("failed to get bitcoin address '%s': %v", bitcoinAddress, err)
}
satoshiBalance += addressResponse.ChainStats.FundedTxoSum - addressResponse.ChainStats.SpentTxoSum
}()
}
wg.Wait()
fiatBalance, err := p.client.convertBTCToCAD(satoshiBalance)
if err != nil {
return balances, ynabAccountIDs, fmt.Errorf("failed to convert satoshi balance to fiat balance: %v", err)