feat: reduce infura call

check if token exists before making the call
This commit is contained in:
Anthony Laibe 2023-01-09 15:54:07 +01:00 committed by Anthony Laibe
parent ec7c0e9c7d
commit 78795ce3e0
1 changed files with 21 additions and 1 deletions

View File

@ -75,6 +75,19 @@ func NewTokenManager(
return tokenManager return tokenManager
} }
func (tm *Manager) inStore(address common.Address, chainID uint64) bool {
if address == nativeChainAddress {
return true
}
tokensMap, ok := tokenStore[chainID]
if !ok {
return false
}
_, ok = tokensMap[address]
return ok
}
func (tm *Manager) FindToken(network *params.Network, tokenSymbol string) *Token { func (tm *Manager) FindToken(network *params.Network, tokenSymbol string) *Token {
if tokenSymbol == network.NativeCurrencySymbol { if tokenSymbol == network.NativeCurrencySymbol {
return tm.ToToken(network) return tm.ToToken(network)
@ -389,10 +402,10 @@ func (tm *Manager) GetBalances(parent context.Context, clients []*chain.Client,
response[account][token] = &sumHex response[account][token] = &sumHex
mu.Unlock() mu.Unlock()
} }
contractMaker := contracts.ContractMaker{RPCClient: tm.RPCClient} contractMaker := contracts.ContractMaker{RPCClient: tm.RPCClient}
for clientIdx := range clients { for clientIdx := range clients {
client := clients[clientIdx] client := clients[clientIdx]
ethScanContract, err := contractMaker.NewEthScan(client.ChainID) ethScanContract, err := contractMaker.NewEthScan(client.ChainID)
if err == nil { if err == nil {
@ -468,6 +481,9 @@ func (tm *Manager) GetBalances(parent context.Context, clients []*chain.Client,
account := accounts[accountIdx] account := accounts[accountIdx]
token := tokens[tokenIdx] token := tokens[tokenIdx]
client := clients[clientIdx] client := clients[clientIdx]
if !tm.inStore(token, client.ChainID) {
continue
}
group.Add(func(parent context.Context) error { group.Add(func(parent context.Context) error {
ctx, cancel := context.WithTimeout(parent, requestTimeout) ctx, cancel := context.WithTimeout(parent, requestTimeout)
defer cancel() defer cancel()
@ -524,6 +540,7 @@ func (tm *Manager) GetBalancesByChain(parent context.Context, clients []*chain.C
contractMaker := contracts.ContractMaker{RPCClient: tm.RPCClient} contractMaker := contracts.ContractMaker{RPCClient: tm.RPCClient}
for clientIdx := range clients { for clientIdx := range clients {
client := clients[clientIdx] client := clients[clientIdx]
ethScanContract, err := contractMaker.NewEthScan(client.ChainID) ethScanContract, err := contractMaker.NewEthScan(client.ChainID)
if err == nil { if err == nil {
fetchChainBalance := false fetchChainBalance := false
@ -597,6 +614,9 @@ func (tm *Manager) GetBalancesByChain(parent context.Context, clients []*chain.C
// Below, we set account, token and client from idx on purpose to avoid override // Below, we set account, token and client from idx on purpose to avoid override
account := accounts[accountIdx] account := accounts[accountIdx]
token := tokens[tokenIdx] token := tokens[tokenIdx]
if !tm.inStore(token, client.ChainID) {
continue
}
client := clients[clientIdx] client := clients[clientIdx]
group.Add(func(parent context.Context) error { group.Add(func(parent context.Context) error {
ctx, cancel := context.WithTimeout(parent, requestTimeout) ctx, cancel := context.WithTimeout(parent, requestTimeout)