-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.ts
More file actions
76 lines (65 loc) · 2.09 KB
/
index.ts
File metadata and controls
76 lines (65 loc) · 2.09 KB
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
import * as ensResolver from './ens';
import * as lensResolver from './lens';
import * as unstoppableDomainResolver from './unstoppableDomains';
import * as starknetResolver from './starknet';
import cache from './cache';
import {
Address,
Handle,
normalizeAddresses,
normalizeHandles,
withoutEmptyValues,
mapOriginalInput
} from './utils';
import { timeAddressResolverResponse as timeResponse } from '../helpers/metrics';
const RESOLVERS = [ensResolver, unstoppableDomainResolver, lensResolver, starknetResolver];
const MAX_LOOKUP_ADDRESSES = 50;
const MAX_RESOLVE_NAMES = 5;
async function _call(fnName: string, input: string[], maxInputLength: number) {
if (input.length > maxInputLength) {
return Promise.reject({
error: `params must contains less than ${maxInputLength} items`,
code: 400
});
}
if (input.length === 0) return {};
return withoutEmptyValues(
await cache(input, async (_input: string[]) => {
const results = await Promise.all(
RESOLVERS.map(async r => {
const end = timeResponse.startTimer({
provider: r.NAME,
method: fnName
});
let result = {};
let status = 0;
try {
result = await r[fnName](_input);
status = 1;
} catch (e) {}
end({ status });
return result;
})
);
return Object.fromEntries(
_input.map(item => [item, results.map(r => r[item]).filter(i => !!i)[0] || ''])
);
})
);
}
export async function lookupAddresses(addresses: Address[]): Promise<Record<Address, Handle>> {
const result = await _call(
'lookupAddresses',
Array.from(new Set(normalizeAddresses(addresses))),
MAX_LOOKUP_ADDRESSES
);
return mapOriginalInput(addresses, result) as Record<Address, Handle>;
}
export async function resolveNames(handles: Handle[]): Promise<Record<Handle, Address>> {
const result = await _call(
'resolveNames',
Array.from(new Set(normalizeHandles(handles))),
MAX_RESOLVE_NAMES
);
return mapOriginalInput(handles, result) as Record<Handle, Address>;
}