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 @@ -52,14 +52,9 @@ async getNameForAddress(ethAddress: string): Promise { const normalizedETHAddress = ethAddress.toLowerCase(); - const cacheResult = this.nameQueryCache.get(normalizedETHAddress); + const cacheResult = this.getCachedNameForAddress(normalizedETHAddress); if (cacheResult) { - const { cacheInsertionTime, normalizedENSName } = cacheResult; - if (cacheInsertionTime + cacheTimeout > Date.now()) { - return normalizedENSName; - } else { - this.nameQueryCache.delete(normalizedETHAddress); - } + return cacheResult; } const cacheAndReturnResult = (result: ?string) => { @@ -85,20 +80,32 @@ return cacheAndReturnResult(normalizedENSName); } + getCachedNameForAddress(ethAddress: string): ?string { + const normalizedETHAddress = ethAddress.toLowerCase(); + + const cacheResult = this.nameQueryCache.get(normalizedETHAddress); + if (!cacheResult) { + return undefined; + } + + const { cacheInsertionTime, normalizedENSName } = cacheResult; + if (cacheInsertionTime + cacheTimeout <= Date.now()) { + this.nameQueryCache.delete(normalizedETHAddress); + return undefined; + } + + return normalizedENSName; + } + async getAddressForName(ensName: string): Promise { const normalizedENSName = namehash.normalize(ensName); if (normalizedENSName !== ensName) { return undefined; } - const cacheResult = this.addressQueryCache.get(normalizedENSName); + const cacheResult = this.getCachedAddressForName(normalizedENSName); if (cacheResult) { - const { cacheInsertionTime, normalizedETHAddress } = cacheResult; - if (cacheInsertionTime + cacheTimeout > Date.now()) { - return normalizedETHAddress; - } else { - this.addressQueryCache.delete(normalizedENSName); - } + return cacheResult; } const cacheAndReturnResult = (result: ?string) => { @@ -116,6 +123,26 @@ } return cacheAndReturnResult(ethAddress.toLowerCase()); } + + getCachedAddressForName(ensName: string): ?string { + const normalizedENSName = namehash.normalize(ensName); + if (normalizedENSName !== ensName) { + return undefined; + } + + const cacheResult = this.addressQueryCache.get(normalizedENSName); + if (!cacheResult) { + return undefined; + } + + const { cacheInsertionTime, normalizedETHAddress } = cacheResult; + if (cacheInsertionTime + cacheTimeout <= Date.now()) { + this.addressQueryCache.delete(normalizedENSName); + return undefined; + } + + return normalizedETHAddress; + } } export { ENSCache }; 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 @@ -33,6 +33,15 @@ } describe('getNameForAddress', () => { + it('should fail to return ashoat.eth if not in cache', async () => { + if (!process.env.ALCHEMY_API_KEY) { + return; + } + const obviouslyAshoatEth = ensCache.getCachedNameForAddress( + '0x911413ef4127910d79303483f7470d095f399ca9', + ); + expect(obviouslyAshoatEth).toBe(undefined); + }); it('should return ashoat.eth', async () => { if (!process.env.ALCHEMY_API_KEY) { return; @@ -42,6 +51,15 @@ ); expect(obviouslyAshoatEth).toBe('ashoat.eth'); }); + it('should return ashoat.eth if in cache', async () => { + if (!process.env.ALCHEMY_API_KEY) { + return; + } + const obviouslyAshoatEth = ensCache.getCachedNameForAddress( + '0x911413ef4127910d79303483f7470d095f399ca9', + ); + expect(obviouslyAshoatEth).toBe('ashoat.eth'); + }); it('should have ashoat.eth cached', async () => { if (!process.env.ALCHEMY_API_KEY) { return; @@ -56,6 +74,13 @@ }); describe('getAddressForName', () => { + it("should fail to return ashoat.eth's address if not in cache", async () => { + if (!process.env.ALCHEMY_API_KEY) { + return; + } + const ashoatAddr = ensCache.getCachedAddressForName('ashoat.eth'); + expect(ashoatAddr).toBe(undefined); + }); it("should return ashoat.eth's address", async () => { if (!process.env.ALCHEMY_API_KEY) { return; @@ -63,6 +88,13 @@ const ashoatAddr = await ensCache.getAddressForName('ashoat.eth'); expect(ashoatAddr).toBe('0x911413ef4127910d79303483f7470d095f399ca9'); }); + it("should return ashoat.eth's address if in cache", async () => { + if (!process.env.ALCHEMY_API_KEY) { + return; + } + const ashoatAddr = ensCache.getCachedAddressForName('ashoat.eth'); + expect(ashoatAddr).toBe('0x911413ef4127910d79303483f7470d095f399ca9'); + }); it("should have ashoat.eth's address cached", async () => { if (!process.env.ALCHEMY_API_KEY) { return;