get bitcoin addresses concurrently
This commit is contained in:
parent
647f9a8f7b
commit
c119f1f57c
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user