fix(@desktop/wallet): guarded access to tables of token service

This commit is contained in:
Sale Djenic 2023-04-06 13:55:55 +02:00 committed by saledjenic
parent 2d3ff3ebc7
commit e02b1befd8
1 changed files with 11 additions and 7 deletions

View File

@ -154,14 +154,15 @@ QtObject:
return self.tokensToAddressesMap[symbol].addresses return self.tokensToAddressesMap[symbol].addresses
proc findTokenBySymbol*(self: Service, network: NetworkDto, symbol: string): TokenDto = proc findTokenBySymbol*(self: Service, network: NetworkDto, symbol: string): TokenDto =
try: if not self.tokens.hasKey(network.chainId):
for token in self.tokens[network.chainId]: return
if token.symbol == symbol: for token in self.tokens[network.chainId]:
return token if token.symbol == symbol:
except Exception as e: return token
error "Error finding token by symbol", msg = e.msg
proc findTokenByAddress*(self: Service, network: NetworkDto, address: Address): TokenDto = proc findTokenByAddress*(self: Service, network: NetworkDto, address: Address): TokenDto =
if not self.tokens.hasKey(network.chainId):
return
for token in self.tokens[network.chainId]: for token in self.tokens[network.chainId]:
if token.address == address: if token.address == address:
return token return token
@ -217,7 +218,8 @@ QtObject:
try: try:
let response = backend.fetchPrices(@[cryptoKey], @[fiat]) let response = backend.fetchPrices(@[cryptoKey], @[fiat])
let prices = jsonToPricesMap(response.result) let prices = jsonToPricesMap(response.result)
if not prices.hasKey(cryptoKey) or not prices[cryptoKey].hasKey(fiat):
return 0.0
self.updateCachedTokenPrice(cryptoKey, fiat, prices[cryptoKey][fiat]) self.updateCachedTokenPrice(cryptoKey, fiat, prices[cryptoKey][fiat])
return prices[cryptoKey][fiat] * factor return prices[cryptoKey][fiat] * factor
except Exception as e: except Exception as e:
@ -282,6 +284,8 @@ QtObject:
if network.nativeCurrencySymbol == tokenSymbol: if network.nativeCurrencySymbol == tokenSymbol:
chainIds.add(network.chainId) chainIds.add(network.chainId)
else: else:
if not self.tokens.hasKey(network.chainId):
continue
for token in self.tokens[network.chainId]: for token in self.tokens[network.chainId]:
if token.symbol == tokenSymbol: if token.symbol == tokenSymbol:
chainIds.add(network.chainId) chainIds.add(network.chainId)