Ethers.js: Support new MetaMask (which uses EIP-1102 by default)

Created on 5 Nov 2018  路  13Comments  路  Source: ethers-io/ethers.js

See: https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8

As of a few days ago, looks like ethers.js no longer works with MetaMask for contract calls. I'm getting:

ethers-v4.min.js:1 Uncaught Error: invalid signer or provider (arg="signerOrProvider", value=undefined, version=4.0.4)
    at Object.n [as throwError] (ethers-v4.min.js:1)
    at new i (ethers-v4.min.js:1)
    at window.onload ((index):40)

Example here: http://jsfiddle.net/081cvoan/1/ (using the example in the cookbook)

Once fixed, docs also need to be updated for how to use the new window.ethereum with ethers.js (instead of window.web3.currentProvider).

documentation

Most helpful comment

Yes, I think I may need to modify the Web3Provider to get EIP 1193 happy too. At the very least, this also requires additional test cases.

Thanks! Hopefully I have time this week to catch up.

All 13 comments

Yes, I think I may need to modify the Web3Provider to get EIP 1193 happy too. At the very least, this also requires additional test cases.

Thanks! Hopefully I have time this week to catch up.

Actually looks like ethers.js contract calls no longer work with the new MetaMask. I'll update this issue.

I think I got it to work with the latest version: doing await window.ethereum.enable() and then provider.getSigner() as the provider for the contract seems to do the trick.

Might be sufficient to just update docs (I'm still getting other unrelated errors so I'm not certain yet).

@shazow did you get this working? We've been waiting to see this issue resolved before diving into to the new MetaMask...

Is calling enable not sufficient?

Yea, I got it working. The docs still need to be updated, the order of operations matters, and the errors could use some work too.

Can you elaborate and share the code you used? Thx

Uh the code is kinda littered here: https://github.com/vipnode/vipnode.org/blob/master/components/ContractPool.vue

I don't have a good isolated example right now.

@shazow I think you can close this as ethers works fine with the new metamask

I should update the docs to reflect this change. And perhaps add a check to detect EIP-1102, if possible. We talked about this (MetaMask, Status.im, Web3, et al) during a steering committee during DevCon4, but I do not believe an introspective API has been formalized yet. I will reach out to them this week.

Feel free to edit this issue if that helps, or make a fresh one.

Got stuck on this for a while, but I can confirm that provider.getSigner() DOES in fact resolve the error when connecting to MetaMask.

Also noted in the docs: https://docs.ethers.io/ethers.js/html/cookbook-providers.html?highlight=getsigner#metamask

Closing this now, as I think it has been addressed. If there are still problems though, please feel free to re-open or open a new issue.

Thanks! :)

Was this page helpful?
0 / 5 - 0 ratings