question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not starting with status "connecting" when refreshing page and MetaMask has a pending request to connect

See original GitHub issue

First, I want to say how easy it was to plug and play metamask-react. Excellent library to hide the implementation details of interacting with MetaMask. Thank you! 🙌

Summary

When MetaMask has a pending request to connect and the user refreshes the page, metamask-react doesn’t start with “connecting” status but with “notConnected”, and calling connect() fails.

Detailed steps

Testing different edge cases, I came up with one that I’m not sure that it’s accounted for. For context, I’m making a demo project to learn with a test token called “KikiriCoin” (repo). Given a page that offers the user a button “Connect” that, when clicked, executes the connect function of useMetaMask:

Screen Shot showing web interface with connect button Screenshot showing web interface with connect button

  1. As a user, open the web page and click Connect. Don’t perform any action on the MetaMask popup, leave the request unresolved.

Screen Shot of meta mask connect request Screenshot of meta mask connect request

  1. Refresh the web page. The MetaMask extension continues having the unresolved request.

Expected (ideally): the status given by useMetaMask is connecting. Actual: the status given by useMetaMask is notConnected.

Screen Shot of web interface after refreshing Screenshot of web interface after refreshing

Screen Shot of metamask extension icon Screenshot of metamask extension icon, with its pending request

When the user clicks the “Connect” button again, an error is thrown “Request of type ‘wallet_requestPermissions’ already pending for origin http://localhost:3000. Please wait.” with code -32002. At this point, the user is blocked from connecting unless they click the MetaMask extension icon in the browser to approve/cancel the request, or restart the browser.

Screen Shot of console log error Screenshot of console log error

Solutions

Fortunately, though, if the user opens the MetaMask extension, accepts the connection request, and refreshes the web page, the status returned by useMetaMask will be connected and things will work seamlessly.

I’m not sure if anything can be done from code to check if there’s a pending request, in order to avoid this situation and inform the user appropriately. Ideally, we’d be able to call a function in window.ethereum to check if there was a pending request, and if that was the case, update the status to “connecting”.

If not, it isn’t the end of the world. I would just inform users to check their MetaMask extension for a pending connection request if I receive the error code -32002. Does that sound like the right approach?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
github-actions[bot]commented, Jan 23, 2022

🎉 This issue has been resolved in version 2.1.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

0reactions
guillermodlpacommented, Jan 10, 2022

Hi @VGLoic. This is great, thanks for looking into it and proposing this solution! Like you explained in the comments, it’s a complex solution to handle this edge case that hopefully would be addressed by MetaMask directly. I’ll spend some time later today manually verifying those changes in my test application, and I’ll confirm it’s all good then.

Thanks for suggesting to push the changes in my branch! No worries, I’ll find another way to contribute 😃 Love this project!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solving a MetaMask connection issue - Ledger Support
If the issue persists, try resetting your Metamask account by going to Settings > Advanced > Reset Account. Once your account is reset,...
Read more >
Unable to connect MetaMask extension to Web3 site
Hi, I have problem connecting to my METAMASK wallet (extention in Chrome / brave). I just get this spinning wheel. I have tried...
Read more >
How to check if Metamask is connected after page refreshing
This way we can check if metamask is still connected (if there are any accounts) and avoid auto requesting or need of manually...
Read more >
How to Fix a Pending / Stuck Transaction on Ethereum
It's almost always due to setting a low gas price on MetaMask transactions ... This is not financial advice and you should approach...
Read more >
RPC Error: Request of type 'wallet_requestPermissions ...
If refresh the browser, the button can be clicked, but it will rejects with the error Request of type 'wallet_requestPermissions' already ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found