-
Notifications
You must be signed in to change notification settings - Fork 30
/
ens-cache.test.js
421 lines (388 loc) · 14 KB
/
ens-cache.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// @flow
import { addEnsContracts } from '@ensdomains/ensjs';
import { AlchemyProvider } from 'ethers';
import { createClient } from 'viem';
// eslint-disable-next-line import/extensions
import { mainnet, sepolia } from 'viem/chains';
import { ENSCache } from './ens-cache.js';
import { ENSWrapper } from './ens-wrapper.js';
import {
getAlchemyMainnetViemTransport,
getAlchemySepoliaViemTransport,
} from './viem-utils.js';
const sepoliaEthersProvider = new AlchemyProvider(
'sepolia',
process.env.ALCHEMY_API_KEY,
);
const sepoliaViemTransport = getAlchemySepoliaViemTransport(
process.env.ALCHEMY_API_KEY,
);
const sepoliaViemClient = createClient({
chain: addEnsContracts(sepolia),
transport: sepoliaViemTransport,
});
const baseSepoliaENSWrapper = new ENSWrapper(
sepoliaViemClient,
sepoliaEthersProvider,
);
let timesGetAddressForNameCalled = 0;
let timesGetNameForAddressCalled = 0;
let timesGetAvatarURIForNameCalled = 0;
const sepoliaENSWrapper: ENSWrapper = ({
...baseSepoliaENSWrapper,
getAddressForName: (ethAddress: string) => {
timesGetAddressForNameCalled++;
return baseSepoliaENSWrapper.getAddressForName(ethAddress);
},
getNameForAddress: (ensName: string) => {
timesGetNameForAddressCalled++;
return baseSepoliaENSWrapper.getNameForAddress(ensName);
},
getAvatarURIForName: (ensName: string) => {
timesGetAvatarURIForNameCalled++;
return baseSepoliaENSWrapper.getAvatarURIForName(ensName);
},
}: any);
const ensCache = new ENSCache(sepoliaENSWrapper);
const mainnetEthersProvider = new AlchemyProvider(
'mainnet',
process.env.ALCHEMY_API_KEY,
);
const mainnetViemTransport = getAlchemyMainnetViemTransport(
process.env.ALCHEMY_API_KEY,
);
const mainnetViemClient = createClient({
chain: addEnsContracts(mainnet),
transport: mainnetViemTransport,
});
const mainnetENSWrapper = new ENSWrapper(
mainnetViemClient,
mainnetEthersProvider,
);
const mainnetENSCache = new ENSCache(mainnetENSWrapper);
if (!process.env.ALCHEMY_API_KEY) {
// Test only works if we can query blockchain
console.log(
'skipped running ENSCache tests because of missing ALCHEMY_API_KEY ' +
'environmental variable',
);
}
const ashoatDotEth = 'ashoat.eth';
const ashoatAddr = '0x911413ef4127910d79303483f7470d095f399ca9';
const ashoatAvatar = 'https://ashoat.com/small_searching.png';
const commalphaDotEth = 'commalpha.eth';
const commalphaEthAddr = '0x727ad7F5134C03e88087a8019b80388b22aaD24d';
const commalphaEthAvatar =
'https://gateway.ipfs.io/ipfs/Qmb6CCsr5Hvv1DKr9Yt9ucbaK8Fz9MUP1kW9NTqAJhk7o8';
const commbetaDotEth = 'commbeta.eth';
const commbetaEthAddr = '0x07124c3b6687e78aec8f13a2312cba72a0bed387';
const commbetaEthAvatar =
'https://altlayer-image-store.alt.technology/msnft.png';
const noENSNameAddr = '0xcF986104d869967381dFfAb3A4127bCe6a404362';
const mintBaseName = 'mint.base.eth';
const mintBaseAddr = '0x9652721d02b9db43f4311102820158aBb4ecc95B';
describe('getNameForAddress', () => {
beforeAll(() => {
ensCache.clearCache();
mainnetENSCache.clearCache();
});
it('should fail to return ashoat.eth if not in cache', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const ashoatEthResult = ensCache.getCachedNameForAddress(ashoatAddr);
expect(ashoatEthResult).toBe(undefined);
});
it('should return ashoat.eth', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const ashoatEthResult = await ensCache.getNameForAddress(ashoatAddr);
expect(ashoatEthResult).toBe(ashoatDotEth);
});
it('should return mint.base.eth', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const mintBaseResult =
await mainnetENSCache.getNameForAddress(mintBaseAddr);
expect(mintBaseResult).toBe(mintBaseName);
}, 10000);
it('should return ashoat.eth if in cache', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const ashoatEthResult = ensCache.getCachedNameForAddress(ashoatAddr);
expect(ashoatEthResult).toBe(ashoatDotEth);
});
it('should have ashoat.eth cached', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const timesLookupAddressCalledBefore = timesGetAddressForNameCalled;
const ashoatEthResult = await ensCache.getNameForAddress(
ashoatAddr.toUpperCase(),
);
expect(ashoatEthResult).toBe(ashoatDotEth);
expect(timesGetAddressForNameCalled).toBe(timesLookupAddressCalledBefore);
});
it('should dedup simultaneous fetches', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
ensCache.clearCache();
const timesLookupAddressCalledBeforeSingleFetch =
timesGetAddressForNameCalled;
const ashoatEthResult1 = await ensCache.getNameForAddress(ashoatAddr);
expect(ashoatEthResult1).toBe(ashoatDotEth);
const timesLookupAddressCalledForSingleFetch =
timesGetAddressForNameCalled - timesLookupAddressCalledBeforeSingleFetch;
ensCache.clearCache();
const timesLookupAddressCalledBeforeDoubleFetch =
timesGetAddressForNameCalled;
const [ashoatEthResult2, ashoatEthResult3] = await Promise.all([
ensCache.getNameForAddress(ashoatAddr),
ensCache.getNameForAddress(ashoatAddr.toUpperCase()),
]);
expect(ashoatEthResult2).toBe(ashoatDotEth);
expect(ashoatEthResult3).toBe(ashoatDotEth);
const timesLookupAddressCalledForDoubleFetch =
timesGetAddressForNameCalled - timesLookupAddressCalledBeforeDoubleFetch;
expect(timesLookupAddressCalledForDoubleFetch).toBe(
timesLookupAddressCalledForSingleFetch,
);
});
});
describe('getNamesForAddresses', () => {
beforeAll(() => {
ensCache.clearCache();
mainnetENSCache.clearCache();
});
it('should fail to return ashoat.eth if not in cache', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const ashoatEthResult = ensCache.getCachedNameForAddress(ashoatAddr);
expect(ashoatEthResult).toBe(undefined);
});
it('should return ashoat.eth', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const [ashoatEthResult] = await ensCache.getNamesForAddresses([ashoatAddr]);
expect(ashoatEthResult).toBe(ashoatDotEth);
});
it('should return mint.base.eth', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const [mintBaseResult] = await mainnetENSCache.getNamesForAddresses([
mintBaseAddr,
]);
expect(mintBaseResult).toBe(mintBaseName);
}, 10000);
it('should return ashoat.eth if in cache', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const ashoatEthResult = ensCache.getCachedNameForAddress(ashoatAddr);
expect(ashoatEthResult).toBe(ashoatDotEth);
});
it('should fetch multiple at a time', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const [ashoatEthResult, commalphaEthResult, commbetaEthResult] =
await ensCache.getNamesForAddresses([
ashoatAddr,
commalphaEthAddr,
commbetaEthAddr,
]);
expect(ashoatEthResult).toBe(ashoatDotEth);
expect(commalphaEthResult).toBe(commalphaDotEth);
expect(commbetaEthResult).toBe(commbetaDotEth);
});
it('should dedup simultaneous fetches', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
ensCache.clearCache();
const timesLookupAddressCalledBefore = timesGetAddressForNameCalled;
const [
[ashoatEthResult1, commalphaEthResult1, commbetaEthResult1],
ashoatEthResult2,
commalphaEthResult2,
commbetaEthResult2,
] = await Promise.all([
ensCache.getNamesForAddresses([
ashoatAddr,
commalphaEthAddr,
commbetaEthAddr,
]),
ensCache.getNameForAddress(ashoatAddr),
ensCache.getNameForAddress(commalphaEthAddr),
ensCache.getNameForAddress(commbetaEthAddr),
]);
const timesLookupAddressCalledAfter = timesGetAddressForNameCalled;
const timesLookupAddressCalledDuringTest =
timesLookupAddressCalledAfter - timesLookupAddressCalledBefore;
expect(timesLookupAddressCalledDuringTest).toBe(0);
expect(ashoatEthResult1).toBe(ashoatDotEth);
expect(commalphaEthResult1).toBe(commalphaDotEth);
expect(commbetaEthResult1).toBe(commbetaDotEth);
expect(ashoatEthResult2).toBe(ashoatDotEth);
expect(commalphaEthResult2).toBe(commalphaDotEth);
expect(commbetaEthResult2).toBe(commbetaDotEth);
});
it('should return undefined if no ENS name', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const [noNameResult] = await ensCache.getNamesForAddresses([noENSNameAddr]);
expect(noNameResult).toBe(undefined);
});
});
describe('getAddressForName', () => {
beforeAll(() => {
ensCache.clearCache();
mainnetENSCache.clearCache();
});
it("should fail to return ashoat.eth's address if not in cache", async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const ashoatAddrResult = ensCache.getCachedAddressForName(ashoatDotEth);
expect(ashoatAddrResult).toBe(undefined);
});
it("should return ashoat.eth's address", async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const ashoatAddrResult = await ensCache.getAddressForName(ashoatDotEth);
expect(ashoatAddrResult).toBe(ashoatAddr);
});
it("should return ashoat.eth's address if in cache", async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const ashoatAddrResult = ensCache.getCachedAddressForName(ashoatDotEth);
expect(ashoatAddrResult).toBe(ashoatAddr);
});
it("should have ashoat.eth's address cached", async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const timesResolveNameCalledBefore = timesGetNameForAddressCalled;
const ashoatAddrResult = await ensCache.getAddressForName(ashoatDotEth);
expect(ashoatAddrResult).toBe(ashoatAddr);
expect(timesGetNameForAddressCalled).toBe(timesResolveNameCalledBefore);
});
it('should dedup simultaneous fetches', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
ensCache.clearCache();
const timesResolveNameCalledBeforeSingleFetch =
timesGetNameForAddressCalled;
const ashoatAddrResult1 = await ensCache.getAddressForName(ashoatDotEth);
expect(ashoatAddrResult1).toBe(ashoatAddr);
const timesResolveNameCalledForSingleFetch =
timesGetNameForAddressCalled - timesResolveNameCalledBeforeSingleFetch;
ensCache.clearCache();
const timesResolveNameCalledBeforeDoubleFetch =
timesGetNameForAddressCalled;
const [ashoatAddrResult2, ashoatAddrResult3] = await Promise.all([
ensCache.getAddressForName(ashoatDotEth),
ensCache.getAddressForName(ashoatDotEth),
]);
expect(ashoatAddrResult2).toBe(ashoatAddr);
expect(ashoatAddrResult3).toBe(ashoatAddr);
const timesResolveNamesCalledForDoubleFetch =
timesGetNameForAddressCalled - timesResolveNameCalledBeforeDoubleFetch;
expect(timesResolveNamesCalledForDoubleFetch).toBe(
timesResolveNameCalledForSingleFetch,
);
});
});
describe('getAvatarURIForAddress', () => {
beforeAll(() => {
ensCache.clearCache();
});
it("should fail to return ashoat.eth's avatar if not in cache", async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const ashoatAvatarResult =
ensCache.getCachedAvatarURIForAddress(ashoatAddr);
expect(ashoatAvatarResult).toBe(undefined);
});
it("should return ashoat.eth's avatar, an HTTP URI pointing to a PNG", async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const ashoatAvatarResult =
await ensCache.getAvatarURIForAddress(ashoatAddr);
expect(ashoatAvatarResult).toBe(ashoatAvatar);
});
it("should return ashoat.eth's avatar if in cache", async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const ashoatAvatarResult =
ensCache.getCachedAvatarURIForAddress(ashoatAddr);
expect(ashoatAvatarResult).toBe(ashoatAvatar);
});
it("should have ashoat.eth's avatar cached", async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const timesGetAvatarCalledBefore = timesGetAvatarURIForNameCalled;
const ashoatAvatarResult =
await ensCache.getAvatarURIForAddress(ashoatAddr);
expect(ashoatAvatarResult).toBe(ashoatAvatar);
expect(timesGetAvatarURIForNameCalled).toBe(timesGetAvatarCalledBefore);
});
it('should dedup simultaneous fetches', async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
ensCache.clearCache();
const timesGetAvatarCalledBeforeSingleFetch =
timesGetAvatarURIForNameCalled;
const ashoatAvatarResult1 =
await ensCache.getAvatarURIForAddress(ashoatAddr);
expect(ashoatAvatarResult1).toBe(ashoatAvatar);
const timesGetAvatarCalledForSingleFetch =
timesGetAvatarURIForNameCalled - timesGetAvatarCalledBeforeSingleFetch;
ensCache.clearCache();
const timesGetAvatarCalledBeforeDoubleFetch =
timesGetAvatarURIForNameCalled;
const [ashoatAvatarResult2, ashoatAvatarResult3] = await Promise.all([
ensCache.getAvatarURIForAddress(ashoatAddr),
ensCache.getAvatarURIForAddress(ashoatAddr),
]);
expect(ashoatAvatarResult2).toBe(ashoatAvatar);
expect(ashoatAvatarResult3).toBe(ashoatAvatar);
const timesGetAvatarCalledForDoubleFetch =
timesGetAvatarURIForNameCalled - timesGetAvatarCalledBeforeDoubleFetch;
expect(timesGetAvatarCalledForDoubleFetch).toBe(
timesGetAvatarCalledForSingleFetch,
);
});
it("should return commalpha.eth's avatar, an IPFS URI pointing to a JPEG", async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const commalphaAvatarResult =
await ensCache.getAvatarURIForAddress(commalphaEthAddr);
expect(commalphaAvatarResult).toBe(commalphaEthAvatar);
});
it("should return commbeta.eth's avatar, an eip155:1/erc721 URI pointing to an NFT with an HTTP URL", async () => {
if (!process.env.ALCHEMY_API_KEY) {
return;
}
const commbetaAvatarResult =
await ensCache.getAvatarURIForAddress(commbetaEthAddr);
expect(commbetaAvatarResult).toBe(commbetaEthAvatar);
});
});