0

I'm running testrpc on the default port 7545.

I run truffle console and test with: web3.eth.getAccounts().then( function (result) { return result[0] });

This works fine. According to docs the getAccounts() method only works if the account is connected properly with ganache, so it seems I should be all set.

when I enable MetaMask in crome and setup to required accounts, localhost web page is showing loading evin i had properly setup MetaMask as shown in image.

Capture333 But when I dissabled the MetaMask, It is showing proper output with account at index 0 as shown below: Capture444

truffle version
Truffle v5.1.26 (core: 5.1.26)
Solidity - 0.4.25 (solc-js)
Node v12.13.0
Web3.js v1.2.1

Please help me getting out of this.

1 Answer 1

1

In order to display accounts with Metamask first you have to prompt the user to allow the application to have access to the accounts.

Check https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1102.md

Look for window.ethereum and if it's presented call window.ethereum.enable() and after that is resolved, you will be able to access the accounts.

6
  • Do we have to look for window.ethereum in truffle console? Commented May 21, 2020 at 10:37
  • I cannot answer you precisely to that question because I haven't used truffle. You have to ask about that in the code that is executed in the browser.
    – Tek
    Commented May 21, 2020 at 10:39
  • thanks Tek let me check for that Commented May 21, 2020 at 10:41
  • let app = await Election.deployed() , let accounts = web3.eth.getAccounts() and app.vote(1,{ from: accounts[1] }) is working in truffle that means i can vote from different accounts, so I'm having account access Commented May 21, 2020 at 11:04
  • so there is no need for window.ethereum.enable() Commented May 21, 2020 at 11:06

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.