2

I am using Truffle Development (or Ganache at times) and the problem is web3.eth.accounts is only one account and not the list of all 10 accounts

I want to get hold of at least first two so that I can do token transfer (ERC721) using safeTransferFrom

For now I have just hardcoded the second parameter (to account) and that works, but every time I switch Development to Ganche I have to recode the second account, which is not nice.

I have tried all kinds of ways for getAccounts (callback or promise) but no use.

For example:

web3.eth.getAccounts((error, accounts) => {
  this.setState( {accounts : accounts} );
}
  • 1
    Is it possible that this is the same problem from here: ethereum.stackexchange.com/questions/41283/… – Shawn Tabrizi Jun 25 '18 at 18:34
  • Shawn, yes I had seen the thread you are pointing to. But that thread is about using HDWalletProvider and says only 1 account is returned. I am not sure if HDWalletProvider is used internally, and hoping that there is some better way to get around this limitation of 1 account. If HDWalletProvider is the only way, then YES this is same thread. But I want some authority to say if HDWalletProvider is the only way. – atul kumthekar Jul 3 '18 at 2:41
3

You can get the accounts by writing an async function, and using await, like this:

async function yourFunc() {
    const provider = ganache.provider();
    const web3 = new Web3(provider);
    const accounts = await web3.eth.getAccounts();

    for (let i = 0; i < 10; i++)
        console.log('account['+i+']: '+accounts[i]);
}

yourFunc().then();

FYI: If what you're trying to do is deploy a contract to the ganache provider for local testing, I've posted a fully working code sample here. Please upvote if this helps you.

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.