soubhagya
soubhagya

Reputation: 806

web3 get name from address

import Web3 from "web3";


useEffect(() => {
const web3 = new Web3(window.ethereum);

web3.eth.ens.getAddress("ethereum.eth").then(function (address) {
    console.log(address);
});
}, []);

here using this code I am getting address from ether name is there any way I can get name from address

web3.eth.ens.getOwner("0xc74E8eFaFE54481bD109f97422AeBca607499f57").then(function (address) {
    console.log(address);
});

I am trying above piece of code but it is not working if I input 0xc74E8eFaFE54481bD109f97422AeBca607499f57

I should get ethereum.eth

Upvotes: 3

Views: 2369

Answers (2)

Danziger
Danziger

Reputation: 21171

ENS now supports reverse resolution, as explained in the docs:

Viem solution:

import { publicClient } from './client';

const ensName = await publicClient.getEnsName({
    address: '0xc74E8eFaFE54481bD109f97422AeBca607499f57',
});

Wagmi solution:

import { useEnsName } from 'wagmi';

const { data: ensName, chainId } = useEnsName({
    address: '0xc74E8eFaFE54481bD109f97422AeBca607499f57',
});

Upvotes: 0

Benjamin Azoulay
Benjamin Azoulay

Reputation: 418

As you can see from the ENS documentation, web3.js can't perform "reversed resolution", i.e. getting a ENS name from an address. However, you can do this from the ensjs API like this:

const address = '0xc74E8eFaFE54481bD109f97422AeBca607499f57';
var name = await ens.getName(address)
// Check to be sure the reverse record is correct.
if(address != await ens.name(name).getAddress()) {
  name = null;
}

Upvotes: 2

Related Questions