From 53630f43a431ed09341cc5a4e5c6ceaf1f37e51d Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Mon, 29 Nov 2021 14:28:35 -0300 Subject: [PATCH 1/2] Silence catch error on purpose --- src/usePriceOracle.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/usePriceOracle.ts b/src/usePriceOracle.ts index f41a1be..2e49afd 100644 --- a/src/usePriceOracle.ts +++ b/src/usePriceOracle.ts @@ -50,7 +50,8 @@ export const useMultipleETHUSDOracle = ( const priceData = await ethFeed.latestRoundData({ blockTag }); return BigNumber.from(priceData.answer); } catch (err) { - console.error(err); + // Silently ignore on purpose; it means the network or block number does + // not contain the chainlink feed contract return undefined; } })() From 832f5f435f00d0276c27f9dba8c5e0dab18f4f5b Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Mon, 29 Nov 2021 14:45:14 -0300 Subject: [PATCH 2/2] Reduce the amount of network calls by probing first only name() and on success probe other erc20 methods --- src/api/address-resolver/ERCTokenResolver.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/api/address-resolver/ERCTokenResolver.ts b/src/api/address-resolver/ERCTokenResolver.ts index f087ec6..49301bd 100644 --- a/src/api/address-resolver/ERCTokenResolver.ts +++ b/src/api/address-resolver/ERCTokenResolver.ts @@ -1,24 +1,31 @@ import { BaseProvider } from "@ethersproject/providers"; import { Contract } from "@ethersproject/contracts"; +import { Interface } from "@ethersproject/abi"; import { IAddressResolver } from "./address-resolver"; import erc20 from "../../erc20.json"; import { TokenMeta } from "../../types"; +const erc20Interface = new Interface(erc20); + export class ERCTokenResolver implements IAddressResolver { async resolveAddress( provider: BaseProvider, address: string ): Promise { - const erc20Contract = new Contract(address, erc20, provider); + const erc20Contract = new Contract(address, erc20Interface, provider); try { - const [name, symbol, decimals] = (await Promise.all([ - erc20Contract.name(), + const name = (await erc20Contract.name()) as string; + if (!name.trim()) { + return undefined; + } + + const [symbol, decimals] = (await Promise.all([ erc20Contract.symbol(), erc20Contract.decimals(), - ])) as [string, string, number]; + ])) as [string, number]; // Prevent faulty tokens with empty name/symbol - if (!name.trim() || !symbol.trim()) { + if (!symbol.trim()) { return undefined; }