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 @@ -58,7 +58,7 @@ class ENSCache { provider: EthersProvider; batchReverseResolverSmartContract: ?ReverseRecordsEthersSmartContract; - batchReverseResolverSmartContractPromise: Promise; + batchReverseResolverSmartContractPromise: Promise; // Maps from normalized ETH address to a cache entry for its name nameQueryCache: Map = new Map(); @@ -72,15 +72,15 @@ this.batchReverseResolverSmartContractPromise = (async () => { const { chainId } = await provider.getNetwork(); const reverseRecordsAddress = resolverAddresses[chainId]; - invariant( - reverseRecordsAddress, - `no ReverseRecords smart contract address for chaind ID ${chainId}!`, - ); - this.batchReverseResolverSmartContract = new Contract( - reverseRecordsAddress, - resolverABI, - provider, - ); + if (reverseRecordsAddress) { + this.batchReverseResolverSmartContract = new Contract( + reverseRecordsAddress, + resolverABI, + provider, + ); + } else { + this.batchReverseResolverSmartContract = null; + } return this.batchReverseResolverSmartContract; })(); } @@ -182,25 +182,31 @@ let smartContract; if (batchReverseResolverSmartContract) { smartContract = batchReverseResolverSmartContract; - } else { + } else if (batchReverseResolverSmartContract !== null) { smartContract = await batchReverseResolverSmartContractPromise; } // ReverseRecords smart contract handles checking forward resolution let ensNames: $ReadOnlyArray; - try { - const raceResult = await Promise.race([ - smartContract['getNames(address[])'](needFetch), - throwOnTimeout(`names for ${JSON.stringify(needFetch)}`), - ]); - invariant( - Array.isArray(raceResult), - 'ReverseRecords smart contract should return array', + if (smartContract) { + try { + const raceResult = await Promise.race([ + smartContract['getNames(address[])'](needFetch), + throwOnTimeout(`names for ${JSON.stringify(needFetch)}`), + ]); + invariant( + Array.isArray(raceResult), + 'ReverseRecords smart contract should return array', + ); + ensNames = raceResult; + } catch (e) { + console.log(e); + ensNames = new Array(needFetch.length).fill(null); + } + } else { + ensNames = await Promise.all( + needFetch.map(ethAddress => this.getNameForAddress(ethAddress)), ); - ensNames = raceResult; - } catch (e) { - console.log(e); - ensNames = new Array(needFetch.length).fill(null); } const resultMap = new Map(); diff --git a/lib/utils/ens-cache.test.js b/lib/utils/ens-cache.test.js --- a/lib/utils/ens-cache.test.js +++ b/lib/utils/ens-cache.test.js @@ -183,7 +183,14 @@ const timesLookupAddressCalledAfter = timesLookupAddressCalled; const timesLookupAddressCalledDuringTest = timesLookupAddressCalledAfter - timesLookupAddressCalledBefore; - expect(timesLookupAddressCalledDuringTest).toBe(0); + + // These tests are run on the Sepolia testnet, where the ReverseRecords + // smart contract is not deployed. As a result, we end up needing to call + // the lookupAddress method (single lookup) once for each address. On + // mainnet (outside of these tests) this is 0, since the ReverseRecords + // smart contract lets us batch up our requests, and avoid calling + // lookupAddress entirely. + expect(timesLookupAddressCalledDuringTest).toBe(3); expect(ashoatEthResult1).toBe(ashoatDotEth); expect(commalphaEthResult1).toBe(commalphaDotEth);