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 @@ -68,27 +68,28 @@ return cacheResult; } - const cacheAndReturnResult = (result: ?string) => { - this.nameQueryCache.set(normalizedETHAddress, { - normalizedETHAddress, - cacheInsertionTime: Date.now(), - normalizedENSName: result, - }); - return result; - }; - - // ethers.js handles checking forward resolution (point 1 above) for us - const ensName = await this.provider.lookupAddress(normalizedETHAddress); - if (!ensName) { - return cacheAndReturnResult(undefined); - } - - const normalizedENSName = normalizeENSName(ensName); - if (normalizedENSName !== ensName) { - return cacheAndReturnResult(undefined); - } - - return cacheAndReturnResult(normalizedENSName); + const fetchENSNamePromise = (async () => { + // ethers.js handles checking forward resolution (point 1 above) for us + const ensName = await this.provider.lookupAddress(normalizedETHAddress); + if (!ensName) { + return undefined; + } + + const normalizedENSName = normalizeENSName(ensName); + if (normalizedENSName !== ensName) { + return undefined; + } + + return normalizedENSName; + })(); + + const normalizedENSName = await fetchENSNamePromise; + this.nameQueryCache.set(normalizedETHAddress, { + normalizedETHAddress, + cacheInsertionTime: Date.now(), + normalizedENSName, + }); + return normalizedENSName; } getCachedNameForAddress(ethAddress: string): ?string { @@ -119,20 +120,21 @@ return cacheResult; } - const cacheAndReturnResult = (result: ?string) => { - this.addressQueryCache.set(normalizedENSName, { - normalizedENSName, - cacheInsertionTime: Date.now(), - normalizedETHAddress: result, - }); - return result; - }; - - const ethAddress = await this.provider.resolveName(normalizedENSName); - if (!ethAddress) { - return cacheAndReturnResult(undefined); - } - return cacheAndReturnResult(normalizeETHAddress(ethAddress)); + const fetchETHAddressPromise = (async () => { + const ethAddress = await this.provider.resolveName(normalizedENSName); + if (!ethAddress) { + return undefined; + } + return normalizeETHAddress(ethAddress); + })(); + + const normalizedETHAddress = await fetchETHAddressPromise; + this.addressQueryCache.set(normalizedENSName, { + normalizedENSName, + cacheInsertionTime: Date.now(), + normalizedETHAddress, + }); + return normalizedETHAddress; } getCachedAddressForName(ensName: string): ?string {