5

What is the difference between web3.eth.getCoinbase() and web3.eth.getAccounts()[0]. In both cases, I always get the value, which is the currently selected account in Metamask.

Note: The question is relative to a public-facing dApp. i.e. UI that uses web3.js & interacts with a user's crypto wallet Metamask. It is not for local blockchain development.

2 Answers 2

8

web3.eth.getCoinbase() returns the account that your mining rewards go to.

web3.eth.getAccounts()[0] returns the first account you created (index 0)

Web3.js doesnt expose this API (probably because from an application level , there aren't strong use cases for it ), but it is possible to set your Coinbase (Etherbase) to any of your accounts using from the geth console

miner.setEtherbase(eth.accounts[n]) //Where n is the index number

Update:

  • Metamask doesnt support web3.eth.getCoinbase() as it is a light client

https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md#dizzy-all-async---think-of-metamask-as-a-light-client

  • The current pattern for dealing with accounts in metamask:

    var account = web3.eth.accounts[0];
    var accountInterval = setInterval(function() {
      if (web3.eth.accounts[0] !== account) {
        account = web3.eth.accounts[0];
        updateInterface();
      }
    }, 100);
    

you could probably use web3.eth.getAccounts()[] but this will return an array that you still have to parse.

Example Taken from documentation:

web3.eth.accounts
 web3.eth.accounts
// or async
web3.eth.getAccounts(callback(error, result){ ... })
This property is read only and returns a list of accounts the node controls.

Returns
Array - An array of addresses controlled by client.

Example
var accounts = web3.eth.accounts;
console.log(accounts); // ["0x407d73d8a49eeb85d32cf465507dd71d507100c1"] 

References:

1
  • updated question. pls see note.
    – Vishal
    Commented Jan 11, 2019 at 21:44
0

Those two commands will produce the same result.

web3.eth.getCoinbase() is meant to be used to get your primary (first) account.

web3.eth.getAccounts() is meant to be used to interact with all of your accounts. You can specify the first account by adding the [0].

In general, if you are dealing with more than just your first account, you will want to use getAccounts() and access your first account with [0]. However, it is up the the implementer to decide.

6
  • 3
    It is possible to configure the coinbase address to be arbitrary. It might be that it is not betweeen the accounts returned by web3.eth.getAccounts().
    – Ismael
    Commented Jan 11, 2019 at 21:16
  • updated question.
    – Vishal
    Commented Jan 11, 2019 at 21:44
  • web3.eth.getAccounts() always returns only 1 account, the currently selected account, when using web3.js & metamask. So web3.eth.getAccounts()[0] is your only option anyway.
    – Vishal
    Commented Jan 11, 2019 at 21:46
  • Also, when the user switches account in metamask, getCoinBase() returns the currently selected account. Hence both seem to be giving the exact same account, i.e. the currently selected account.
    – Vishal
    Commented Jan 11, 2019 at 21:54
  • @Vishal metamask doesnt expose getCoinBase() and if you that node has only created one account, this is true. I will be reluctant to use that call for the purposes of selecting the user account through metamask.
    – 0xsegfault
    Commented Jan 11, 2019 at 22:10

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.