web3modal
web3modal copied to clipboard
Callbacks not firing on provider but rather on instance
I think the documentation might be incorrect... I was trying to follow instructions for listening to callbacks, however, when I used provider, none of the events seem to fire. When I use instance, it works as expected. Is that intended or am I doing something wrong?
The below code is working.
const web3Modal = new Web3Modal({
network: 'rinkeby', // optional
cacheProvider: false, // optional
providerOptions: this.getProviderOptions(), // required
});
const instance = await web3Modal.connect();
const provider = new ethers.providers.Web3Provider(instance);
// Subscribe to accounts change
instance.on('accountsChanged', (accounts: string[]) => {
console.log(accounts);
});
// Subscribe to chainId change
instance.on('chainChanged', (chainId: number) => {
console.log(chainId);
});
// Subscribe to provider connection
instance.on('connect', (info: { chainId: number }) => {
console.log(info);
});
// Subscribe to provider disconnection
instance.on('disconnect', (error: { code: number; message: string }) => {
console.log(error);
});