2022-07-12 16:30:31 +02:00
|
|
|
import { ethers } from 'ethers';
|
2021-02-04 19:15:23 +01:00
|
|
|
import ensNetworkMap from 'ethereum-ens-network-map';
|
2022-07-12 16:30:31 +02:00
|
|
|
import { NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP } from '../../../../shared/constants/network';
|
2019-11-01 18:54:00 +01:00
|
|
|
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class Ens {
|
2020-11-03 00:41:28 +01:00
|
|
|
static getNetworkEnsSupport(network) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return Boolean(ensNetworkMap[network]);
|
2019-11-01 18:54:00 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor({ network, provider } = {}) {
|
2022-07-12 16:30:31 +02:00
|
|
|
const networkName = NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP[network];
|
|
|
|
const ensAddress = ensNetworkMap[network];
|
|
|
|
const ethProvider = new ethers.providers.Web3Provider(provider, {
|
|
|
|
chainId: parseInt(network, 10),
|
|
|
|
name: networkName,
|
|
|
|
ensAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2022-07-12 16:30:31 +02:00
|
|
|
this._ethProvider = ethProvider;
|
2019-11-01 18:54:00 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
lookup(ensName) {
|
2022-07-12 16:30:31 +02:00
|
|
|
return this._ethProvider.resolveName(ensName);
|
2019-11-01 18:54:00 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
reverse(address) {
|
2022-07-12 16:30:31 +02:00
|
|
|
return this._ethProvider.lookupAddress(address);
|
2019-11-01 18:54:00 +01:00
|
|
|
}
|
|
|
|
}
|