'ENS: NameChanged() event not triggered
Intro
I'm creating a dapp that uses ens. The ens should be automatically displayed for every wallet that owns it.
For this I'm using the provider.lookupAddress(address)
from ethers.js
. Link to ethers lookupAddress docs.
The users' ens domains are cached (to not to overload the Ethereum node with lots of lookup requests) in a self-hosted database as it's described in the ENS docs: ENS docs "Caching and Updating ENS Names ".
The way to keep the ens actual is needed. I would like to listen to "reverse record" change event and update the record in database at this moment.
The flow I see is the following:
- Some ens reverse record changes - this event is triggered
- I check that the node from the event exists in my databse
- Perform the forward and reverse lookup for the changed ens
- If these records match, update the "ens" column for this wallet in the database.
The recommended in the ENS docs article way is listening to NameChanged
and AddrChanged
events. The first event refers to changing the "reverse record" - exactly what I need..
The ENS Public Resolver on Etherscan, tab "Events" proves this.
Problem
The NameChanged
event is never triggered.
Code
The code to listen to them is listed below:
import { Contract, ethers } from 'ethers';
const provider = new ethers.providers.JsonRpcProvider(
'https://eth-mainnet.alchemyapi.io/v2/ElnwwkJ52xC41WZRYhkd5mm6PjvnFmry'
);
const ensAbi = [
// "ens -> address" record changes event
'event AddrChanged(bytes32 indexed node, address a)',
// "address -> ens" record changes event
'event NameChanged(bytes32 indexed node, string name)'
];
// ENS default resolver address (ENS Public Reolver)
const ensContractAddress = '0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41';
export const startEnsListener = async () => {
const ensContract = new Contract(ensContractAddress, ensAbi, provider);
// Listen to AddrChanged event
const addrChangedFilter = ensContract.filters.AddrChanged();
ensContract.on(addrChangedFilter, (hashedEns, walletAddress, _event) => {
console.log(
`--- addr changed
node: ${hashedEns},
walletAddress: ${walletAddress}`
);
});
// Listen to NameChanged event
const nameChangedFilter = ensContract.filters.NameChanged();
ensContract.on(nameChangedFilter, (hashedAddress, ens, _event) => {
console.log(
`--- name changed
node: ${hashedAddress},
ens: ${ens}`
);
// this call back is never executed - no output in the console for a long-long time
});
console.log('--- EnsListener started');
};
Console output
...
--- addr changed
node: 0x4a48366ecd45c7a40e712b95ec1bb86b1f7e818fef3eb6ce94f02305da2b848e,
walletAddress: 0x8E9bC97761B7b764cc1ba3D7e1b71d1a40BE0853
--- addr changed
node: 0x89f8775f5ab65afd6d8a5116ad2dcedc5642a6946c1b37f7c27f60a43400fb59,
walletAddress: 0x64252bB403D0e2F835e57B5b78a06b2F9F09C2F6
--- addr changed
node: 0x830dfc954f8a63effe700d6d368069d1cc4d428df506378fcbc55f7fee5c6d40,
walletAddress: 0x051a14Cf1cE7F8C99aE3dCdF014Eea65e764FeeD
...
Question
How to listen to NameChanged()
event correctly?
Or are there other ways to keep the cached ens record up to date?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|