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 ff1df0c492
commit d1d31bd6c0
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) {
needFetch := !r.isBalanceCacheValid(addresses) || r.isBalanceUpdateNeededAnyway(clients, addresses)
if needFetch {
fetchedBalances, err := r.FetchBalances(ctx, clients, addresses)
if err == nil {
return fetchedBalances, nil
_, err := r.FetchBalances(ctx, clients, addresses)
if err != nil {
log.Error("FetchOrGetCachedWalletBalances error", "err", err)
}
}
@ -438,6 +438,10 @@ func (r *Reader) createBalancePerChainPerSymbol(
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
balance := toChainBalance(balances, tok, address, decimals, cachedTokens, hasError, isMandatoryToken)
if balance != nil {
@ -654,11 +658,6 @@ func (r *Reader) FetchBalances(ctx context.Context, clients map[uint64]chain.Cli
return nil, err
}
connectedPerChain := map[uint64]bool{}
for chainID, client := range clients {
connectedPerChain[chainID] = client.IsConnected()
}
tokenAddresses := getTokenAddresses(allTokens)
balances, err := r.fetchBalances(ctx, clients, addresses, tokenAddresses)
if err != nil {
@ -666,6 +665,11 @@ func (r *Reader) FetchBalances(ctx context.Context, clients map[uint64]chain.Cli
return nil, err
}
connectedPerChain := map[uint64]bool{}
for chainID, client := range clients {
connectedPerChain[chainID] = client.IsConnected()
}
tokens := r.balancesToTokensByAddress(connectedPerChain, addresses, allTokens, balances, cachedTokens)
err = r.persistence.SaveTokens(tokens)