Fix balances cache (#4890)
This commit is contained in:
parent
2dfbe3099a
commit
2be9bfc304
|
@ -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)
|
||||||
|
|
|
@ -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,37 +326,32 @@ 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 {
|
for _, address := range addresses {
|
||||||
if _, ok := cachedBalancesPerChain[address]; !ok {
|
if _, ok := cachedTokens[address]; !ok {
|
||||||
cachedBalancesPerChain[address] = map[common.Address]map[uint64]ChainBalance{}
|
updateAnyway = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !updateBalances && !updateAnyway {
|
||||||
|
for address, tokens := range cachedTokens {
|
||||||
for _, token := range tokens {
|
for _, token := range tokens {
|
||||||
for _, balance := range token.BalancesPerChain {
|
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 {
|
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
|
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
|
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()
|
||||||
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))),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else {
|
} 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
|
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,
|
||||||
|
|
|
@ -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()
|
||||||
|
|
Loading…
Reference in New Issue