I'm using web3, truffle and testrpc.
In my smart contract, some of the functions can only be called by the owner (or creator) of the contract (i.e. onlyOwner functions). So, I've defined some modifier for that. Also, in the smart contract, I have defined a variable called owner that can be called by anyone to see who the owner is.
I'm designing a UI. I'd like to alert the users, in UI, if they call the onlyOwner functions (e.g. you're not the owner). In my .js file I can call owner.call(); to get the owner address. But I don't know how to get the users address to compare it with the owner address.
Question: How can I get the user's address via web3?
Also, when we switch the default account (i.e. the first account) in MetaMask to 2nd, 3rd, .. account, how the .js file can recognize it and use that account as the default one?
Best Answer
If you are using web(version 0.19.0: latest stable), you can do
1. Download Web3
2. Algo to load Web3, check the connection and retrieve the accounts
3. Auto-reload after switching account
The solution right now is a basic
setInterval
function