5

When I run this code in web3

web3.eth.getAccounts().then(e => console.log(e));

The console returns this:

 [ '0x772f437b0f15e1F205C8BD923b5C8357e9c0c429',
    '0x5738d9100A6CB64FB78730746ab61472c7808fD9',
    '0x7086D052EAaD359c7aCD9B993c6169aE0dEC0725',
    '0x9fA18751b024FDC55cC85A484fF4261351Dd4666',
    '0x95a29de4A8cc4697E627922d270Aa74C2bd7494a',
    '0x332c22e9c7F02e092b18C6cc4D9Bfd46d36Dd7D9',
    '0xaED2F48c0d06a13D1FE2853E7Ee3B7d0aA328d3B',
    '0x88A0941f0dcb501C20b7BaE5a41608EB981f1209',
    '0xad5d1cB78e80518b596A814340826F36B89660Fc',
    '0x67E23e936C9d22eDCf9ebC9A989911b313c24BC1' ]

But what if I only want the first account in the array and assign it to a variable?

In this case, it's 0x772f437b0f15e1F205C8BD923b5C8357e9c0c429

EDIT:

I have got the correct output after following the instruction of István András Seres, below is my code:

var firstAccount;

web3.eth.getAccounts().then(e => { 
 firstAccount = e[0];
 console.log("A: " + firstAccount);
}) 

console.log("B: " + firstAccount);

Strangely, the output is:

A: [correct testrpc account]

B: undefined

It seems that a variable is not allowed to carry the same value once it exits getAccounts() function.

7 Answers 7

10

Since web3.eth.getAccounts() returns a list of addresses you can access the first accounts's address easily just by e[0] in your example.

So just try this: web3.eth.getAccounts().then(e => let firstAcc=e[0]; console.log(firstAcc));

Have a look at the official documentation of the getAccounts() web3js function here.

4
  • Hi Istvan, I got the desired output on the terminal. However, it seems that the variable firstAcc resets its own value once the program exits the getAccounts() function (See the EDIT for reference). The web3.js documentation doesn't seem to explain why that is the case. Is there a way to overcome this problem? Sorry for asking such basic question, I really appreciate the help.
    – potatoguy
    Commented Nov 29, 2017 at 11:24
  • 1
    This is not a web3.js related issue rather just a regular javascript phenomena. The function which body is after the .then is called the callback function. In your edited question first the second console.log is executed and at the time of its execution indeed firstAccount is undefined, this is no surprise! However, when the callback function is returned and executed, obviously we will have the correct value in the firstAccount variable. I guess you should just get to know better callback functions. You could start at thousand places, there are plenty of good resources for Javascript Commented Nov 29, 2017 at 12:08
  • A more interesting question is, how do you get the 11th account address if you have 11+ accounts? Commented Aug 1, 2018 at 20:01
  • web3.eth.getAccounts((err, res) => {accounts = res[0]}) Commented Aug 31, 2018 at 20:30
3

you can do:

var address = web3.eth.accounts[0];

to get the first address in in curret account

6
  • 1
    I believe "web3.eth.accounts" has been deprecated, no? In any case, it did not return the first account. When I print it on the console, it says "undefined." Thanks for answering though.
    – potatoguy
    Commented Nov 29, 2017 at 10:06
  • 1
    web3.eth.accounts[0]; definitively return the first account address.
    – btc4cash
    Commented Nov 29, 2017 at 10:08
  • @btc4cash No it doesn't. Commented Jul 2, 2018 at 7:18
  • Prove it please!
    – btc4cash
    Commented Jul 2, 2018 at 13:35
  • @btc4cash, John is saying right so don't argu if you are not sure about your answer. Commented Aug 31, 2018 at 20:27
2

It is very important to take note of the version of web3 you are using because there are changes to be reckoned with!

CASE 1: web3.js 0.2x.x (Stable)

For web3 0.2x.x https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethaccounts

var address = web3.eth.accounts[0]; 

and the async (asynchronous) version can be used,

web3.eth.getAccounts(callback(error, result){ ... });

They are both read-only and return a list (i.e. Array) of accounts the node controls.

Here is the Problem:

web3.eth.getAccounts().then(e => console.log(e));

will fail because getAccounts implements a callback not a promise.

CASE 2: web3.js 1.0 ( A work in Progress)

getAccounts returns Promise which returns Array - An array of addresses controlled by node. https://web3js.readthedocs.io/en/1.0/web3-eth.html#getaccounts Supports both Promise and Callback

web3.eth.getAccounts(callback(error, result){ ... }); // callback
web3.eth.getAccounts().then(e => console.log(e)); // Promise
2

In Truffle Console

web3.eth.getAccounts().then( function(s){FirstA=s[0]})

then You can use FirstA variable

FirstA

1

You can use

web3.eth.accounts[0]

style in geth command window to get the account.

You should use

web3.eth.getAccounts()

style in Node JS to get the account.

1
  • 1
    first one works in web3 0.20.x, second one is used in web3 1.0.x
    – sensei
    Commented Dec 10, 2018 at 11:17
1

For those who are still looking for an easy answer:

let accounts = await web3.eth.getAccounts()

will assign whole array to accounts. Then you can access as accounts[0] to the first one

0
web3.eth.getAccounts().then(function(acc){accounts=acc;});
accounts[0];

Note: I'm using truffle console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.