How to most easily keep track of “selectedAddres” with EIP1193 wallet providers (e.g. MetaMask)

Is there a simple, straightforward way to *guarantee* some code is called when `window.ethereum.selectedAddres` is set, unset, or changed?

I am using `ethers.js`, but it would be nice to know a “general” `window.ethereum.` solution without having to involve ethers.

The value passed to the success branch of

window.ethereum.request({ method: ‘eth_requestAccounts’ })

is an *array* of addresses. And the value that `window.ethereum.selectedAddres` gets set to upon calling `eth_requestAccounts` doesn’t necessarily have a straightforward relationship to the returned address array.

I can (and do) use

window.ethereum.on(‘accountsChanged’, callback);

but that doesn’t fire on the first page load. And it’s not completely clear to me that the `address` passed to the above callback will *always* be the same as the value of `window.ethereum.selectedAddres`.

While we are on the subject, it would be nice to achieve the same simplification for `window.ethereum.chainId` changes too.

This stuff gets all kinds of confusing. MM can have multiple wallets, has its *own* sense of the “current” wallet (which may disagree with `selectedAddres` in some circumstances) . . .

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

In-game currency conversion mechanism to cryptocurrency – Patent Pending held by the Tap Project | by Tap Project (TTT)

Weekly options with a 30% correction. The single greatest transfer of wealth in history.