fix_: Token cache usage

When fetch or cache, always return the cache
Do not save if the token is not being returns
Resolve connection map AFTER making the call
This commit is contained in:
Anthony Laibe 2024-08-13 10:49:16 +02:00 committed by Anthony Laibe
parent 7ed92531c2
commit 1287e5af14
1 changed files with 12 additions and 8 deletions

View File

@ -264,9 +264,9 @@ func (r *Reader) invalidateBalanceCache() {
func (r *Reader) FetchOrGetCachedWalletBalances(ctx context.Context, clients map[uint64]chain.ClientInterface, addresses []common.Address) (map[common.Address][]token.StorageToken, error) { func (r *Reader) FetchOrGetCachedWalletBalances(ctx context.Context, clients map[uint64]chain.ClientInterface, addresses []common.Address) (map[common.Address][]token.StorageToken, error) {
needFetch := !r.isBalanceCacheValid(addresses) || r.isBalanceUpdateNeededAnyway(clients, addresses) needFetch := !r.isBalanceCacheValid(addresses) || r.isBalanceUpdateNeededAnyway(clients, addresses)
if needFetch { if needFetch {
fetchedBalances, err := r.FetchBalances(ctx, clients, addresses) _, err := r.FetchBalances(ctx, clients, addresses)
if err == nil { if err != nil {
return fetchedBalances, nil log.Error("FetchOrGetCachedWalletBalances error", "err", err)
} }
} }
@ -438,6 +438,10 @@ func (r *Reader) createBalancePerChainPerSymbol(
hasError = !connected hasError = !connected
} }
if _, ok := balances[tok.ChainID][address][tok.Address]; !ok {
hasError = true
}
// TODO: Avoid passing the entire balances map to toChainBalance. Iterate over the balances map once and pass the balance per address per token to toChainBalance // TODO: Avoid passing the entire balances map to toChainBalance. Iterate over the balances map once and pass the balance per address per token to toChainBalance
balance := toChainBalance(balances, tok, address, decimals, cachedTokens, hasError, isMandatoryToken) balance := toChainBalance(balances, tok, address, decimals, cachedTokens, hasError, isMandatoryToken)
if balance != nil { if balance != nil {
@ -654,11 +658,6 @@ func (r *Reader) FetchBalances(ctx context.Context, clients map[uint64]chain.Cli
return nil, err return nil, err
} }
connectedPerChain := map[uint64]bool{}
for chainID, client := range clients {
connectedPerChain[chainID] = client.IsConnected()
}
tokenAddresses := getTokenAddresses(allTokens) tokenAddresses := getTokenAddresses(allTokens)
balances, err := r.fetchBalances(ctx, clients, addresses, tokenAddresses) balances, err := r.fetchBalances(ctx, clients, addresses, tokenAddresses)
if err != nil { if err != nil {
@ -666,6 +665,11 @@ func (r *Reader) FetchBalances(ctx context.Context, clients map[uint64]chain.Cli
return nil, err return nil, err
} }
connectedPerChain := map[uint64]bool{}
for chainID, client := range clients {
connectedPerChain[chainID] = client.IsConnected()
}
tokens := r.balancesToTokensByAddress(connectedPerChain, addresses, allTokens, balances, cachedTokens) tokens := r.balancesToTokensByAddress(connectedPerChain, addresses, allTokens, balances, cachedTokens)
err = r.persistence.SaveTokens(tokens) err = r.persistence.SaveTokens(tokens)