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