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