status-go/services/wallet/balance.go

66 lines
1.7 KiB
Go
Raw Normal View History

2019-07-02 07:28:57 +00:00
package wallet
import (
"context"
"sync"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
2019-07-02 07:28:57 +00:00
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
2020-01-02 09:10:19 +00:00
2019-07-02 07:28:57 +00:00
"github.com/status-im/status-go/services/wallet/ierc20"
)
var requestTimeout = 20 * time.Second
2019-07-02 07:28:57 +00:00
// GetTokensBalances takes list of accounts and tokens and returns mapping of token balances for each account.
2021-03-08 11:18:43 +00:00
func GetTokensBalances(parent context.Context, client *walletClient, accounts, tokens []common.Address) (map[common.Address]map[common.Address]*hexutil.Big, error) {
2019-07-02 07:28:57 +00:00
var (
group = NewAtomicGroup(parent)
mu sync.Mutex
response = map[common.Address]map[common.Address]*hexutil.Big{}
2019-07-02 07:28:57 +00:00
)
for _, token := range tokens {
caller, err := ierc20.NewIERC20Caller(token, client)
token := token
if err != nil {
return nil, err
}
for _, account := range accounts {
// Why we are doing this?
2019-07-02 07:28:57 +00:00
account := account
group.Add(func(parent context.Context) error {
ctx, cancel := context.WithTimeout(parent, requestTimeout)
2019-07-02 07:28:57 +00:00
balance, err := caller.BalanceOf(&bind.CallOpts{
Context: ctx,
2019-07-02 07:28:57 +00:00
}, account)
cancel()
// We don't want to return an error here and prevent
// the rest from completing
2019-07-02 07:28:57 +00:00
if err != nil {
log.Error("can't fetch erc20 token balance", "account", account, "token", token, "error", err)
return nil
2019-07-02 07:28:57 +00:00
}
mu.Lock()
_, exist := response[account]
if !exist {
response[account] = map[common.Address]*hexutil.Big{}
2019-07-02 07:28:57 +00:00
}
response[account][token] = balance
mu.Unlock()
return nil
})
}
}
select {
case <-group.WaitAsync():
case <-parent.Done():
return nil, parent.Err()
}
return response, group.Error()
}