I am new and I am trying to make my first transaction using geth and truffle.

to specify a sender address I type in the truffle console:

src = web3.eth.accounts[0]

but I get "undefined"

if I type accounts = web3.eth.getAccounts() it seems it works and I get an array with 10 accounts

but if I want only the first account of the list and I type src = accounts[0] I get again "undefined"

What am I doing wrong? Many thanks

  • can you show your code? web3.eth.getAccounts is a method, so you need to call it like this web3.eth.getAccounts() – martriay Jan 21 at 2:10
  • sory I forgot the parentesis, what I typed is: accounts = web3.eth.getAccounts(), I wrote it in the truffle console – Alby Jan 21 at 8:32

I think you'll need to do it this way.

const accounts = await web3.eth.getAccounts();
const src = accounts[0];
console.log("1st account: "+src);

I hope this works.

Edit: To provide more info.

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('<your_ganache or geth address>'));
const accounts = await web3.eth.getAccounts();
const src = accounts[0];
console.log("1st account: "+src);

If using ganache-cli or geth, then your URL will be http://localhost:8545 or if using ganache gui, it will be http://localhost:7545 .

Everybody is new at some point. The community is with you.

  • I wrote the first line in the truffle console and I received an "undefined", but if I write: web3.eth.getAccounts() I receive a list of 10 addresses – Alby Jan 21 at 8:34
  • yes you'll get 10 addresses because whatever local provider like geth or ganache you are using will generate 10 addresses by default – vteja711 Jan 21 at 8:36
  • Now that will be in an array so you'll need to get the 1st element of that array – vteja711 Jan 21 at 8:36
  • Also instead of calling the method in console, try writing a simple nodeJS file – vteja711 Jan 21 at 8:37
  • if I write the three suggested lines in an "acccounts.js" file and I launch it with node accounts.js I get the error message "web3 is not defined". sorry I am really new to this :( – Alby Jan 21 at 9:03

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.