From 1287e5af14dbf9bb53f3085bdb03277e65d2f31a Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 13 Aug 2024 10:49:16 +0200 Subject: [PATCH] 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 --- services/wallet/reader.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/services/wallet/reader.go b/services/wallet/reader.go index 58ff0eefd..2a34667db 100644 --- a/services/wallet/reader.go +++ b/services/wallet/reader.go @@ -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)