I'm trying to fetch the first account using web3.eth.accounts[0] using Truffle, but couldn't seem to make it work.

I'm using version 0.5.0. I read that the command is deprecated and have to use web3.eth.getAccounts() for asynchronous execution. The latter only displays an array of accounts connected to the local network.

How do I get a single account in this case? I wanted to pass { from: web3.eth.accounts[0] } metadata to a public function in my contract.

  • 1
    You probably want to change the tag here from solidity to web3.js. – goodvibration Jan 10 '19 at 15:39

Try this:

async function getAccount(id) {
    let accounts = await web3.eth.getAccounts();
    return accounts[id];

You'll need to execute await getAccount(0) from an async function of course (or use a Promise).

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.