diff --git a/src/useChainInfo.ts b/src/useChainInfo.ts index 0d494b9..5cd0f01 100644 --- a/src/useChainInfo.ts +++ b/src/useChainInfo.ts @@ -25,24 +25,30 @@ export const useChainInfoFromMetadataFile = ( const [chainInfo, setChainInfo] = useState(undefined); useEffect(() => { - if (chainId === undefined) { + if (assetsURLPrefix === undefined || chainId === undefined) { setChainInfo(undefined); return; } const readChainInfo = async () => { - const res = await fetch(chainInfoURL(assetsURLPrefix!, chainId)); - if (!res.ok) { + try { + const res = await fetch(chainInfoURL(assetsURLPrefix, chainId)); + if (!res.ok) { + setChainInfo(defaultChainInfo); + return; + } + const info = await res.json(); + + setChainInfo({ + nativeName: info.nativeCurrency.name, + nativeDecimals: info.nativeCurrency.decimals, + nativeSymbol: info.nativeCurrency.symbol, + }); + } catch (err) { + // ignore setChainInfo(defaultChainInfo); return; } - const info = await res.json(); - - setChainInfo({ - nativeName: info.nativeCurrency.name, - nativeDecimals: info.nativeCurrency.decimals, - nativeSymbol: info.nativeCurrency.symbol, - }); }; readChainInfo(); }, [assetsURLPrefix, chainId]);