Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENS Reverse Record #264

Merged
merged 13 commits into from
Apr 1, 2021
Merged
73 changes: 42 additions & 31 deletions src/helpers/profile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { subgraphRequest } from '@snapshot-labs/snapshot.js/src/utils';
import getProvider from '@snapshot-labs/snapshot.js/src/utils/provider';
import {
subgraphRequest,
multicall
} from '@snapshot-labs/snapshot.js/src/utils';
import namehash from 'eth-ens-namehash';

function get3BoxProfiles(addresses) {
return new Promise((resolove, reject) => {
Expand All @@ -25,37 +30,43 @@ function get3BoxProfiles(addresses) {
});
}

function lookupAddresses(addresses) {
async function lookupAddresses(addresses) {
const network = '1';
const provider = getProvider(network);
const abi = [
{
inputs: [{ internalType: 'contract ENS', name: '_ens', type: 'address' }],
ChaituVR marked this conversation as resolved.
Show resolved Hide resolved
stateMutability: 'nonpayable',
type: 'constructor'
},
{
inputs: [
{ internalType: 'address[]', name: 'addresses', type: 'address[]' }
],
name: 'getNames',
outputs: [{ internalType: 'string[]', name: 'r', type: 'string[]' }],
stateMutability: 'view',
type: 'function'
}
];
return new Promise((resolove, reject) => {
subgraphRequest('https://api.thegraph.com/subgraphs/name/ensdomains/ens', {
accounts: {
__args: {
first: 1000,
where: {
id_in: addresses.map(addresses => addresses.toLowerCase())
}
},
id: true,
registrations: {
__args: {
orderBy: 'registrationDate',
first: 1
},
domain: {
name: true,
labelName: true
}
}
}
})
.then(({ accounts }) => {
const ensNames = {};
accounts.forEach(profile => {
ensNames[profile.id.toLowerCase()] =
(profile?.registrations?.[0]?.domain?.labelName &&
profile?.registrations?.[0]?.domain?.name) ||
'';
});
multicall(
ChaituVR marked this conversation as resolved.
Show resolved Hide resolved
network,
provider,
abi,
[['0x3671aE578E63FdF66ad4F3E12CC0c0d71Ac7510C', 'getNames', [addresses]]],
{ blockTag: 'latest' }
)
.then(response => {
const validNames = response[0].r.map(n =>
namehash.normalize(n) === n ? n : ''
);
const ensNames = Object.fromEntries(
addresses.map((address, index) => [
address.toLowerCase(),
validNames[index] || ''
])
);
resolove(ensNames);
})
.catch(error => {
Expand Down