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})
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, like0.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:
From the
web3.eth.getAccounts
v1 documentationSo specifically rewriting the section you referenced at the end: