web3.eth.accounts returning a function

3.3k views Asked by At

I'm following a tutorial here that uses testrpc with web3.js. After installing the packages ethereumjs-testrpc and web3js, testrpc is started which gives 10 available accounts and their private keys.

web3 is at 1.0.0-beta.18 and ethereumjs-testrpc at 4.1.1.

When the following code is run

Web3 = require('web3');
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
web3.eth.accounts

I get the following output instead of the 10 accounts as shown in the tutorial. What went wrong?

Accounts {
  currentProvider: [Getter/Setter],
  _requestManager:
   RequestManager {
     provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
     providers:
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     subscriptions: {} },
  givenProvider: null,
  providers:
   { WebsocketProvider: [Function: WebsocketProvider],
     HttpProvider: [Function: HttpProvider],
     IpcProvider: [Function: IpcProvider] },
  _provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
  setProvider: [Function],
  _ethereumCall:
   { getId:
      { [Function: send]
        method: [Object],
        request: [Function: bound ],
        call: 'net_version' },
     getGasPrice:
      { [Function: send]
        method: [Object],
        request: [Function: bound ],
        call: 'eth_gasPrice' },
     getTransactionCount:
      { [Function: send]
        method: [Object],
        request: [Function: bound ],
        call: 'eth_getTransactionCount' } },
  wallet:
   Wallet {
     length: 0,
     _accounts: [Circular],
     defaultKeyName: 'web3js_wallet' } }

Later in the tutorial, web3.eth.accounts is needed when deploying the contract

deployedContract = VotingContract.new(['Rama','Nick','Jose'],
    {data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
2

There are 2 answers

2
carver On

That tutorial was written before web3.js v1 came out. The API has changed significantly in v1, including eth.accounts. You can either pin to an older version of web3.js, like 0.19.0, or find the equivalent method in the new v1 docs.

Retrieving the accounts is now done asynchronously, like many other calls in the new API. So you can call it with a callback or using promises. Printing the list of accounts to your console would look like:

web3.eth.getAccounts(console.log);
// or
web3.eth.getAccounts().then(console.log);

From the web3.eth.getAccounts v1 documentation

So specifically rewriting the section you referenced at the end:

web3.eth.getAccounts()
.then(function (accounts) {
  return VotingContract.new(['Rama','Nick','Jose'],
    {data: byteCode, from: accounts[0], gas: 4700000});
})
.then(function (deployedContract) {
  // whatever you want to do with deployedContract...
})
0
Mohit Rakhade On

If web3.eth.accounts is not showing the expected result then, follow this two simple step (Inside the truffle console)

  1. web3.eth.getAccounts().then(function(acc){ accounts = acc })

  2. accounts

That's is... if you want specific account then accounts[0/1/2...9]