on(eventName, listener)
Feature
Subscribes to wallet events.
Parameters
string eventName,
function listener
Return Value
(void)
example
// Listen for when the user changes their selected account in the wallet
// 'accountsChanged' event occurs also if the permissions of some of the accounts have changed.
provider.on('accountsChanged', (accounts) => {
if (accounts.length === 0) {
// All accounts were disconnected
console.log('User disconnected all accounts.');
} else {
// The new array of permitted accounts.
// accounts[0] is the new selected address.
console.log('New permitted accounts list:', accounts);
console.log('New selected address:', accounts[0]);
}
});
// Listen for when the user fully disconnects the site
provider.on('disconnect', (message) => {
console.log(message);
});
// Listen for network changes
provider.on('chainChanged', (chainId) => {
console.log('Network changed to:', chainId);
// It's recommended to reload the page on chain change
window.location.reload();
});Last updated