dappuniversity / token_sale

Code Your Own Cryptocurrency & ICO on Ethereum | Tutorial
407 stars 389 forks source link

Chrome console showing null account?? Token address is also different #31

Open ChowdhuryRidwan opened 4 years ago

ChowdhuryRidwan commented 4 years ago

Problem is in the connection of metamask to the local blockchain by using its RPC address. I use the private key of the first account to get its information. But in my system the first account's actual address in ganache is: 0x4DF4F75E96D49A1b61a4Cd1800d55444bb39C761 but the address that is displayed in my chrome console is 0xED4Cd7eA80CfdDef5c78F6A02C9352124AA6D835. Also in app.js in the render function where I have to display my account I am getting account as NULL. Please do confirm why it is different. How do I wire up the actual account to app.js.

Screen Shot 2020-04-04 at 7 10 37 PM
megancooper commented 4 years ago

@ChowdhuryRidwan I was able to fix this issue with the following code: (see https://ethereum.stackexchange.com/a/78987)

initWeb3: function( ) {
// Wait for loading completion to avoid race conditions with web3 injection timing.
        if (window.ethereum) {
          const web3 = new Web3(window.ethereum);
          try {
            // Request account access if needed
            window.ethereum.enable()
                .then(web3 => {
                    console.log(web3)
                    App.web3Provider = web3;
                });
          } catch (error) {
            console.error(error);
          }
        }
        // Legacy dapp browsers...
        else if (window.web3) {
          // Use Mist/MetaMask's provider.
          const web3 = window.web3;
          console.log('Injected web3 detected.');
          App.web3Provider = web3;
        }
        // Fallback to localhost; use dev console port by default...
        else {
          const provider = new Web3.providers.HttpProvider('http://127.0.0.1:7545');
          const web3 = new Web3(provider);
          console.log('No web3 instance injected, using Local web3.');
          App.web3Provider = web3;
        }
        return App.initContracts();
    },