diff --git a/lib/utils/ens-cache.js b/lib/utils/ens-cache.js --- a/lib/utils/ens-cache.js +++ b/lib/utils/ens-cache.js @@ -2,8 +2,16 @@ import namehash from 'eth-ens-namehash'; +import sleep from './sleep'; + const cacheTimeout = 24 * 60 * 60 * 1000; // one day const failedQueryCacheTimeout = 5 * 60 * 1000; // five minutes +const queryTimeout = 5 * 1000; // five seconds + +async function throwOnTimeout(ethAddressOrName: string) { + await sleep(queryTimeout); + throw new Error(`ENS fetch for ${ethAddressOrName} timed out`); +} export type EthersProvider = { +lookupAddress: (address: string) => Promise, @@ -73,7 +81,10 @@ // ethers.js handles checking forward resolution (point 1 above) for us let ensName; try { - ensName = await this.provider.lookupAddress(normalizedETHAddress); + ensName = await Promise.race([ + this.provider.lookupAddress(normalizedETHAddress), + throwOnTimeout(normalizedETHAddress), + ]); } catch (e) { console.log(e); return null; @@ -92,7 +103,7 @@ this.nameQueryCache.set(normalizedETHAddress, { normalizedETHAddress, - expirationTime: Date.now() + cacheTimeout, + expirationTime: Date.now() + queryTimeout * 2, normalizedENSName: fetchENSNamePromise, }); @@ -154,7 +165,10 @@ const fetchETHAddressPromise = (async () => { let ethAddress; try { - ethAddress = await this.provider.resolveName(normalizedENSName); + ethAddress = await Promise.race([ + this.provider.resolveName(normalizedENSName), + throwOnTimeout(normalizedENSName), + ]); } catch (e) { console.log(e); return null; @@ -167,7 +181,7 @@ this.addressQueryCache.set(normalizedENSName, { normalizedENSName, - expirationTime: Date.now() + cacheTimeout, + expirationTime: Date.now() + queryTimeout * 2, normalizedETHAddress: fetchETHAddressPromise, });