Add tokens metadata on FetchOrGetCachedWalletBalances call
This commit is contained in:
parent
b206e24c79
commit
7f6f8b3f7c
|
@ -9,6 +9,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/ethereum/go-ethereum/event"
|
"github.com/ethereum/go-ethereum/event"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/status-im/status-go/multiaccounts/accounts"
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
||||||
|
@ -274,7 +275,7 @@ func (r *Reader) FetchOrGetCachedWalletBalances(ctx context.Context, addresses [
|
||||||
return balances, nil
|
return balances, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tokens, err := r.persistence.GetTokens()
|
tokens, err := r.getWalletTokenBalances(ctx, addresses, false)
|
||||||
|
|
||||||
addressWithoutCachedBalances := false
|
addressWithoutCachedBalances := false
|
||||||
for _, address := range addresses {
|
for _, address := range addresses {
|
||||||
|
@ -292,7 +293,7 @@ func (r *Reader) FetchOrGetCachedWalletBalances(ctx context.Context, addresses [
|
||||||
return tokens, err
|
return tokens, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) GetWalletTokenBalances(ctx context.Context, addresses []common.Address) (map[common.Address][]Token, error) {
|
func (r *Reader) getWalletTokenBalances(ctx context.Context, addresses []common.Address, updateBalances bool) (map[common.Address][]Token, error) {
|
||||||
areTestNetworksEnabled, err := r.accountsDB.GetTestNetworksEnabled()
|
areTestNetworksEnabled, err := r.accountsDB.GetTestNetworksEnabled()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -336,16 +337,53 @@ func (r *Reader) GetWalletTokenBalances(ctx context.Context, addresses []common.
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
balances, err := r.tokenManager.GetBalancesByChain(ctx, clients, addresses, tokenAddresses)
|
verifiedTokens, unverifiedTokens := splitVerifiedTokens(allTokens)
|
||||||
if err != nil {
|
|
||||||
for _, client := range clients {
|
updateAnyway := false
|
||||||
client.SetIsConnected(false)
|
cachedBalancesPerChain := map[common.Address]map[common.Address]map[uint64]ChainBalance{}
|
||||||
|
if !updateBalances {
|
||||||
|
for address, tokens := range cachedTokens {
|
||||||
|
if _, ok := cachedBalancesPerChain[address]; !ok {
|
||||||
|
cachedBalancesPerChain[address] = map[common.Address]map[uint64]ChainBalance{}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, token := range tokens {
|
||||||
|
for _, balance := range token.BalancesPerChain {
|
||||||
|
if _, ok := cachedBalancesPerChain[address][balance.Address]; !ok {
|
||||||
|
cachedBalancesPerChain[address][balance.Address] = map[uint64]ChainBalance{}
|
||||||
|
}
|
||||||
|
cachedBalancesPerChain[address][balance.Address][balance.ChainID] = balance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, address := range addresses {
|
||||||
|
for _, tokenList := range [][]*token.Token{verifiedTokens, unverifiedTokens} {
|
||||||
|
for _, tokens := range getTokenBySymbols(tokenList) {
|
||||||
|
for _, token := range tokens {
|
||||||
|
if _, ok := cachedBalancesPerChain[address][token.Address][token.ChainID]; !ok {
|
||||||
|
updateAnyway = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var latestBalances map[uint64]map[common.Address]map[common.Address]*hexutil.Big
|
||||||
|
if updateBalances || updateAnyway {
|
||||||
|
latestBalances, err = r.tokenManager.GetBalancesByChain(ctx, clients, addresses, tokenAddresses)
|
||||||
|
if err != nil {
|
||||||
|
for _, client := range clients {
|
||||||
|
client.SetIsConnected(false)
|
||||||
|
}
|
||||||
|
log.Info("tokenManager.GetBalancesByChain error", "err", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Info("tokenManager.GetBalancesByChain error", "err", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
verifiedTokens, unverifiedTokens := splitVerifiedTokens(allTokens)
|
|
||||||
result := make(map[common.Address][]Token)
|
result := make(map[common.Address][]Token)
|
||||||
communities := make(map[string]bool)
|
communities := make(map[string]bool)
|
||||||
|
|
||||||
|
@ -356,13 +394,19 @@ func (r *Reader) GetWalletTokenBalances(ctx context.Context, addresses []common.
|
||||||
decimals := tokens[0].Decimals
|
decimals := tokens[0].Decimals
|
||||||
isVisible := false
|
isVisible := false
|
||||||
for _, token := range tokens {
|
for _, token := range tokens {
|
||||||
hexBalance := balances[token.ChainID][address][token.Address]
|
var balance *big.Float
|
||||||
balance := big.NewFloat(0.0)
|
hexBalance := &hexutil.Big{}
|
||||||
if hexBalance != nil {
|
if latestBalances != nil {
|
||||||
balance = new(big.Float).Quo(
|
hexBalance = latestBalances[token.ChainID][address][token.Address]
|
||||||
new(big.Float).SetInt(hexBalance.ToInt()),
|
balance = big.NewFloat(0.0)
|
||||||
big.NewFloat(math.Pow(10, float64(decimals))),
|
if hexBalance != nil {
|
||||||
)
|
balance = new(big.Float).Quo(
|
||||||
|
new(big.Float).SetInt(hexBalance.ToInt()),
|
||||||
|
big.NewFloat(math.Pow(10, float64(decimals))),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
balance = cachedBalancesPerChain[address][token.Address][token.ChainID].Balance
|
||||||
}
|
}
|
||||||
hasError := false
|
hasError := false
|
||||||
if client, ok := clients[token.ChainID]; ok {
|
if client, ok := clients[token.ChainID]; ok {
|
||||||
|
@ -413,6 +457,10 @@ func (r *Reader) GetWalletTokenBalances(ctx context.Context, addresses []common.
|
||||||
return result, r.persistence.SaveTokens(result)
|
return result, r.persistence.SaveTokens(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Reader) GetWalletTokenBalances(ctx context.Context, addresses []common.Address) (map[common.Address][]Token, error) {
|
||||||
|
return r.getWalletTokenBalances(ctx, addresses, true)
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Reader) GetWalletToken(ctx context.Context, addresses []common.Address) (map[common.Address][]Token, error) {
|
func (r *Reader) GetWalletToken(ctx context.Context, addresses []common.Address) (map[common.Address][]Token, error) {
|
||||||
areTestNetworksEnabled, err := r.accountsDB.GetTestNetworksEnabled()
|
areTestNetworksEnabled, err := r.accountsDB.GetTestNetworksEnabled()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue