0

I am using web3.eth.getcoinbase() function to get the address of the current account but instead, it returns null. Can someone help me with this? This is the code

web3.eth.getCoinbase(function(err, account) {
  if (err === null) {
    App.account = account;
    $("#accountAddress").html("Your Account: " + account);
  }
});

The entire code is in here

1

This code is outdated. because MetaMask now need to have permission in order or get access to the user's MetaMask.

Change this Code

if (typeof web3 !== 'undefined') {
  // If a web3 instance is already provided by Meta Mask.
  App.web3Provider = web3.currentProvider;
  web3 = new Web3(web3.currentProvider);
} else {
  // Specify default instance if no web3 instance provided
  App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
  web3 = new Web3(App.web3Provider);
}

with this code :

if(typeof window !== 'undefined' && typeof window.ethereum !== 'undefined'){
  //getting Permission to access. This is for when the user has new MetaMask
  window.ethereum.enable();
  App.web3Provider = window.ethereum;
  web3 = new Web3(window.ethereum);

}else if (typeof window !== 'undefined' && typeof window.web3 !== 'undefined') {
  web3 = new Web3(window.web3.currentProvider);
  // Acccounts always exposed. This is those who have old version of MetaMask

} else {
  // Specify default instance if no web3 instance provided
  App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
  web3 = new Web3(App.web3Provider);

}

In the new MetaMask there is no such thing like window.web3 . instead you have window.ethereum

If this doesn't work. try to delete the word "window" from everywhere in the new code.

2
  • thanks a lot, it worked. – Shras299 May 9 '20 at 4:33
  • you're welcome. – Masoud jt May 9 '20 at 10:44

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.