Add tokens metadata on FetchOrGetCachedWalletBalances call

This commit is contained in:
Roman Volosovskyi 2024-02-15 12:01:49 +01:00 committed by Jakub Sokołowski
parent b206e24c79
commit 7f6f8b3f7c
No known key found for this signature in database
GPG Key ID: FE65CD384D5BF7B4
2 changed files with 65 additions and 17 deletions

View File

@ -1 +1 @@
0.174.6
0.174.7

View File

@ -9,6 +9,7 @@ import (
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/multiaccounts/accounts"
@ -274,7 +275,7 @@ func (r *Reader) FetchOrGetCachedWalletBalances(ctx context.Context, addresses [
return balances, nil
}
tokens, err := r.persistence.GetTokens()
tokens, err := r.getWalletTokenBalances(ctx, addresses, false)
addressWithoutCachedBalances := false
for _, address := range addresses {
@ -292,7 +293,7 @@ func (r *Reader) FetchOrGetCachedWalletBalances(ctx context.Context, addresses [
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()
if err != nil {
return nil, err
@ -336,16 +337,53 @@ func (r *Reader) GetWalletTokenBalances(ctx context.Context, addresses []common.
return nil, err
}
balances, err := r.tokenManager.GetBalancesByChain(ctx, clients, addresses, tokenAddresses)
if err != nil {
for _, client := range clients {
client.SetIsConnected(false)
verifiedTokens, unverifiedTokens := splitVerifiedTokens(allTokens)
updateAnyway := 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)
communities := make(map[string]bool)
@ -356,13 +394,19 @@ func (r *Reader) GetWalletTokenBalances(ctx context.Context, addresses []common.
decimals := tokens[0].Decimals
isVisible := false
for _, token := range tokens {
hexBalance := balances[token.ChainID][address][token.Address]
balance := big.NewFloat(0.0)
if hexBalance != nil {
balance = new(big.Float).Quo(
new(big.Float).SetInt(hexBalance.ToInt()),
big.NewFloat(math.Pow(10, float64(decimals))),
)
var balance *big.Float
hexBalance := &hexutil.Big{}
if latestBalances != nil {
hexBalance = latestBalances[token.ChainID][address][token.Address]
balance = big.NewFloat(0.0)
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
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)
}
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) {
areTestNetworksEnabled, err := r.accountsDB.GetTestNetworksEnabled()
if err != nil {