[Ethereum] Fetching all the accounts from Genache

testrpcweb3js

I'm using Ganache to generate dummy accounts but every time I try to access the list of accounts using web3.eth.accounts or the web3.eth.getAccounts method, I only get access to the first account in the array. Can anyone advise on how I can get an array of all the accounts?

Best Answer

For version 0.x of web3:

try this:

beforeEach(()=>{
web3.eth.getAccounts((err,acc)=>{
    if(err){
        console.log('error.............',err);
    }
    console.log(acc);
});
});

describe('contract',()=>{
it('deployes a contract',()=>{

});
});

or can simply try this :

 beforeEach(()=>{
 web3.eth.getAccounts(console.log);
 });

for version 1.x of web3:

beforeEach(()=>{
 web3.eth.getAccounts()
  .then(fetchedAccounts=>{
     console.log(fetchedAccounts);
  });
});

describe('Inbox',()=>{
it('deployes a contract',()=>{

  });
});

version 1.0.0 of web3 uses promises while 0.x doesn't work on promises it works on callbacks.

hope it helps!