Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloaded dapp calling eth accounts won't work (Pizza) #392

Open
filippsen opened this issue Mar 17, 2019 · 2 comments
Open

Downloaded dapp calling eth accounts won't work (Pizza) #392

filippsen opened this issue Mar 17, 2019 · 2 comments
Labels
bug Something isn't working

Comments

@filippsen
Copy link
Contributor

filippsen commented Mar 17, 2019

Environment/Browser

1.6.1.
Still valid on version 1.7.0

Description

Downloaded Pizza template Dapp won't work. This appears to be related to the missing enable call to MetaMask, to allow accessing injected MetaMask accounts.

Steps to reproduce

  1. Create a new project from the CryptoPizza template
  2. Set Ropsten network (in Select a Network button) or any other external network
  3. Deploy CryptoPizza.sol
  4. Open Preview panel
  5. Click Download DApp button
  6. Run the downloaded html file locally
  7. Observe Browser Console error

Expected result

Expected it to work just like Preview.

Actual result

jquery.js:3827 Uncaught TypeError: Cannot read property 'eth' of undefined

Line is: window.web3.eth.accounts[0].

Related

@filippsen filippsen added the bug Something isn't working label Mar 17, 2019
@filippsen filippsen mentioned this issue Mar 17, 2019
48 tasks
@filippsen
Copy link
Contributor Author

The same might apply to other templates. Also observed when running a local download copy of ERC-20 Token template.

@filippsen
Copy link
Contributor Author

Problem

In order to retrieve account information when using _MetaMask, it is now required to ask for user's consent before direct access to the account is granted.
See also: ethereum.enable() and https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8

The program propagates the empty account address and fails to call getBalance and related operations.

Possible solution

Add the MetaMask authorization request pop-up with the enable() call. That will provide valid accounts data this.web3.eth.accounts[0], but only after user's consent.

Example with ERC-20 token template

Change MyToken.init to become an async function and add the new MetaMask code (from the referenced blog post) after the this.web3 assignment in that same function.

Example:

MyToken.prototype.init = async function(cb) {
    this.web3 = new Web3(
        (window.web3 && window.web3.currentProvider) ||
        new Web3.providers.HttpProvider(this.Contract.endpoint));
    if (window.ethereum) {
        window.web3 = new Web3(ethereum);
        try {
            await ethereum.enable();
            console.log('Accounts are now accessible!');
        } catch (error) {
            console.error(error);
        }
    } else if (window.web3) {
        window.web3 = new Web3(web3.currentProvider);
        console.log('Accounts are already accessible!');
    } else {
        console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
    }
[...]
}

Extra notes

In order to get MetaMask notification to appear, it is required that the dapp file is served by a web server. In case the dapp is accessed via file:///..., calls to window.web3 or window.ethereum won't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant