Skip to content

Commit c1f0c6a

Browse files
ChaituVRbonustrack
andauthored
ENS Reverse Record (#264)
* ENS Reverse Record * Fixes * Added back ens subgraph and reverse record as priority * refactor Co-authored-by: Fabien <bonustrack@users.noreply.github.com>
1 parent 7ce97d5 commit c1f0c6a

File tree

1 file changed

+59
-13
lines changed

1 file changed

+59
-13
lines changed

src/helpers/profile.ts

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { subgraphRequest } from '@snapshot-labs/snapshot.js/src/utils';
1+
import getProvider from '@snapshot-labs/snapshot.js/src/utils/provider';
2+
import { subgraphRequest, call } from '@snapshot-labs/snapshot.js/src/utils';
3+
import namehash from 'eth-ens-namehash';
24

35
function get3BoxProfiles(addresses) {
46
return new Promise((resolove, reject) => {
@@ -25,9 +27,32 @@ function get3BoxProfiles(addresses) {
2527
});
2628
}
2729

28-
function lookupAddresses(addresses) {
29-
return new Promise((resolove, reject) => {
30-
subgraphRequest('https://api.thegraph.com/subgraphs/name/ensdomains/ens', {
30+
function ensReverseRecordRequest(addresses) {
31+
const network = '1';
32+
const provider = getProvider(network);
33+
const abi = [
34+
{
35+
inputs: [
36+
{ internalType: 'address[]', name: 'addresses', type: 'address[]' }
37+
],
38+
name: 'getNames',
39+
outputs: [{ internalType: 'string[]', name: 'r', type: 'string[]' }],
40+
stateMutability: 'view',
41+
type: 'function'
42+
}
43+
];
44+
return call(
45+
provider,
46+
abi,
47+
['0x3671aE578E63FdF66ad4F3E12CC0c0d71Ac7510C', 'getNames', [addresses]],
48+
{ blockTag: 'latest' }
49+
);
50+
}
51+
52+
function ensSubGraphRequest(addresses) {
53+
return subgraphRequest(
54+
'https://api.thegraph.com/subgraphs/name/ensdomains/ens',
55+
{
3156
accounts: {
3257
__args: {
3358
first: 1000,
@@ -47,15 +72,36 @@ function lookupAddresses(addresses) {
4772
}
4873
}
4974
}
50-
})
51-
.then(({ accounts }) => {
52-
const ensNames = {};
53-
accounts.forEach(profile => {
54-
ensNames[profile.id.toLowerCase()] =
55-
(profile?.registrations?.[0]?.domain?.labelName &&
56-
profile?.registrations?.[0]?.domain?.name) ||
57-
'';
58-
});
75+
}
76+
);
77+
}
78+
79+
function lookupAddresses(addresses) {
80+
return new Promise((resolove, reject) => {
81+
Promise.all([
82+
ensReverseRecordRequest(addresses),
83+
ensSubGraphRequest(addresses)
84+
])
85+
.then(([reverseRecords, { accounts }]) => {
86+
const validNames = reverseRecords.map(n =>
87+
namehash.normalize(n) === n ? n : ''
88+
);
89+
// reverse record will be given preference
90+
const ensNames = Object.fromEntries(
91+
addresses.map((address, index) => {
92+
const account = accounts.find(
93+
account => account.id.toLowerCase() === address.toLowerCase()
94+
);
95+
return [
96+
address.toLowerCase(),
97+
validNames[index] ||
98+
(account?.registrations?.[0]?.domain?.labelName &&
99+
account?.registrations?.[0]?.domain?.name) ||
100+
''
101+
];
102+
})
103+
);
104+
59105
resolove(ensNames);
60106
})
61107
.catch(error => {

0 commit comments

Comments
 (0)