Fix balances cache (#4890)

This commit is contained in:
Roman Volosovskyi 2024-03-11 14:48:40 +01:00 committed by GitHub
parent 2dfbe3099a
commit 2be9bfc304
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 46 deletions

View File

@ -33,7 +33,7 @@ func (p *Persistence) SaveTokens(tokens map[common.Address][]Token) (err error)
for address, addressTokens := range tokens { for address, addressTokens := range tokens {
for _, t := range addressTokens { for _, t := range addressTokens {
for chainID, b := range t.BalancesPerChain { for chainID, b := range t.BalancesPerChain {
if b.HasError || b.Balance.Cmp(big.NewFloat(0)) == 0 { if b.HasError {
continue continue
} }
_, err = tx.Exec(`INSERT INTO token_balances(user_address,token_name,token_symbol,token_address,token_decimals,token_description,token_url,balance,raw_balance,chain_id) VALUES (?,?,?,?,?,?,?,?,?,?)`, address.Hex(), t.Name, t.Symbol, b.Address.Hex(), t.Decimals, t.Description, t.AssetWebsiteURL, b.Balance.String(), b.RawBalance, chainID) _, err = tx.Exec(`INSERT INTO token_balances(user_address,token_name,token_symbol,token_address,token_decimals,token_description,token_url,balance,raw_balance,chain_id) VALUES (?,?,?,?,?,?,?,?,?,?)`, address.Hex(), t.Name, t.Symbol, b.Address.Hex(), t.Decimals, t.Description, t.AssetWebsiteURL, b.Balance.String(), b.RawBalance, chainID)

View File

@ -277,20 +277,6 @@ func (r *Reader) FetchOrGetCachedWalletBalances(ctx context.Context, addresses [
} }
tokens, err := r.getWalletTokenBalances(ctx, addresses, false) tokens, err := r.getWalletTokenBalances(ctx, addresses, false)
addressWithoutCachedBalances := false
for _, address := range addresses {
if _, ok := tokens[address]; !ok {
addressWithoutCachedBalances = true
break
}
}
// there should be at least ETH balance
if addressWithoutCachedBalances {
return r.GetWalletTokenBalances(ctx, addresses)
}
return tokens, err return tokens, err
} }
@ -340,36 +326,31 @@ func (r *Reader) getWalletTokenBalances(ctx context.Context, addresses []common.
verifiedTokens, unverifiedTokens := splitVerifiedTokens(allTokens) verifiedTokens, unverifiedTokens := splitVerifiedTokens(allTokens)
cachedBalancesPerChain := map[common.Address]map[common.Address]map[uint64]string{}
updateAnyway := false updateAnyway := false
cachedBalancesPerChain := map[common.Address]map[common.Address]map[uint64]ChainBalance{}
if !updateBalances { 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 _, address := range addresses {
for _, tokenList := range [][]*token.Token{verifiedTokens, unverifiedTokens} { if _, ok := cachedTokens[address]; !ok {
for _, tokens := range getTokenBySymbols(tokenList) {
for _, token := range tokens {
if _, ok := cachedBalancesPerChain[address][token.Address][token.ChainID]; !ok {
updateAnyway = true updateAnyway = true
break break
} }
} }
} }
if !updateBalances && !updateAnyway {
for address, tokens := range cachedTokens {
for _, token := range tokens {
for _, balance := range token.BalancesPerChain {
if _, ok := cachedBalancesPerChain[address]; !ok {
cachedBalancesPerChain[address] = map[common.Address]map[uint64]string{}
} }
if _, ok := cachedBalancesPerChain[address][balance.Address]; !ok {
cachedBalancesPerChain[address][balance.Address] = map[uint64]string{}
}
cachedBalancesPerChain[address][balance.Address][balance.ChainID] = balance.RawBalance
}
}
} }
} }
@ -397,19 +378,22 @@ func (r *Reader) getWalletTokenBalances(ctx context.Context, addresses []common.
isVisible := false isVisible := false
for _, token := range tokens { for _, token := range tokens {
var balance *big.Float var balance *big.Float
hexBalance := &hexutil.Big{} hexBalance := &big.Int{}
if latestBalances != nil { if latestBalances != nil {
hexBalance = latestBalances[token.ChainID][address][token.Address] hexBalance = latestBalances[token.ChainID][address][token.Address].ToInt()
} else {
if cachedRawBalance, ok := cachedBalancesPerChain[address][token.Address][token.ChainID]; ok {
hexBalance, _ = new(big.Int).SetString(cachedRawBalance, 10)
}
}
balance = big.NewFloat(0.0) balance = big.NewFloat(0.0)
if hexBalance != nil { if hexBalance != nil {
balance = new(big.Float).Quo( balance = new(big.Float).Quo(
new(big.Float).SetInt(hexBalance.ToInt()), new(big.Float).SetInt(hexBalance),
big.NewFloat(math.Pow(10, float64(decimals))), 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 {
hasError = err != nil || !client.GetIsConnected() hasError = err != nil || !client.GetIsConnected()
@ -426,7 +410,7 @@ func (r *Reader) getWalletTokenBalances(ctx context.Context, addresses []common.
balance1DayAgoStr = balance1DayAgo.String() balance1DayAgoStr = balance1DayAgo.String()
} }
balancesPerChain[token.ChainID] = ChainBalance{ balancesPerChain[token.ChainID] = ChainBalance{
RawBalance: hexBalance.ToInt().String(), RawBalance: hexBalance.String(),
Balance: balance, Balance: balance,
Balance1DayAgo: balance1DayAgoStr, Balance1DayAgo: balance1DayAgoStr,
Address: token.Address, Address: token.Address,

View File

@ -67,7 +67,7 @@ func (c *findNewBlocksCommand) detectTransfers(parent context.Context, accounts
tokenAddresses = append(tokenAddresses, token.Address) tokenAddresses = append(tokenAddresses, token.Address)
} }
} }
log.Info("findNewBlocksCommand detectTransfers", "cnt", len(tokenAddresses), "addresses", tokenAddresses) log.Info("findNewBlocksCommand detectTransfers", "cnt", len(tokenAddresses))
ctx, cancel := context.WithTimeout(parent, requestTimeout) ctx, cancel := context.WithTimeout(parent, requestTimeout)
defer cancel() defer cancel()