0

I'm trying to put together a simple application using Truffle, Metamask and ASP.NET.

I've created a bunch of test accounts in Metamask, I then start testrpc explicitly specifying a couple of those accounts and their initial balance, like so:

testrpc --account="0xa3........18, 1000000000000000000000" --account="0x80.......08, 1000000000000000000000"

In the testrpc log I can see these are started correctly:

Available Accounts
==================
(0) 0x27.....d5
(1) 0x1b.....24

I then deploy my contract, log in to Metamask and then spin up my web application.

In my js I can see that it's retrieving the web3 instance from metamask:

            if (typeof web3 !== 'undefined') { // This evaluates to true
                App.web3Provider = web3.currentProvider;
                web3 = new Web3(web3.currentProvider);
                console.log("Already have a web3 instance");
            } else {

I try to get the list of accounts like this:

            App.contracts.JmTest.deployed().then(function(instance) {

                web3.eth.getAccounts(function (error, accounts) {
                    if (error) {
                        console.log(error);
                    }

                    console.dir(accounts);

However it only shows one of the two accounts that I started testrpc with...

Array(1)
0: "0x27.....d5"

It turns out that this is the currently selected account in Metamask. If I start testrpc without specifying any accounts and restart Chrome, the same thing happens - again I only get the the currently selected account in Metamask.

What am I missing?

1

Ok, so it looks like it's a case of RTFM....

When a user selects an account in MetaMask, that account silently becomes the web3.eth.accounts[0] in your JS context, the only member of the web3.eth.accounts array.

  • So this means we can not loop through all the test accounts in metamask? as when we use web3.eth.accounts it silently takes accounts[0]. – urjit on rails Jun 10 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.