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 _, t := range addressTokens {
for chainID, b := range t.BalancesPerChain {
if b.HasError || b.Balance.Cmp(big.NewFloat(0)) == 0 {
if b.HasError {
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)

View File

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

View File

@ -67,7 +67,7 @@ func (c *findNewBlocksCommand) detectTransfers(parent context.Context, accounts
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)
defer cancel()