How is it possible to set an event listener for changes to web3.eth.accounts
or web3.eth.getAccounts()
?
3 Answers
If you want to see when the selected address changes, and if you're using web3, then Web3.currentProvider.publicConfigStore has some events which you'll want to watch:
Subscribe to the update function with
Web3.currentProvider.publicConfigStore.on('update',callback);
Where callback is a function which takes the form
function callback(error,data){
The new selected address can be accessed within that function as
data.selectedAddress
But be aware that the event will fire in cases other than when selectedAddress
has been updated, so you'll need an if statement in there.
-
The callback function signature is actually
function callback(data,error)
, or maybeerror
is never used. Are there reference docs for this somewhere?– cmikeb1Commented Nov 10, 2018 at 22:36
So I'm not entirely sure but I think no. The closest thing I can think of for getting info within Solidity on what is stored in eth.accounts
would be calling msg.sender
in your code. That would return the contract address of the sender, but only the sender.
You could have an event in the code that takes msg.sender
as an indexed argument (see Solidity in Depth on wiki), and then do something with that value using filters in the UI. Best I can think of, good luck.
EDIT:
I think if you wanted to check if there were any changes to accounts it would go in the code you write for the user interface to the dapp.
Event listeners use to listen - as the name tells - to events in smart contracts. Nevertheless you can create a NodeJS Application, that uses a setInterval() function to call these values, compare the outcomes to the previous ones.