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

Add switchToCorrectNetwork method #125

Merged
merged 5 commits into from
Dec 16, 2021
Merged

Add switchToCorrectNetwork method #125

merged 5 commits into from
Dec 16, 2021

Conversation

naz3eh
Copy link
Member

@naz3eh naz3eh commented Dec 15, 2021

Closes #119

Description

This PR adds a method switchToCorrectNetwork to the useWallet() hook. This method allows the user to connect to the required network without manually heading over to the MetaMask and switching the network.

Users can use this method by directly assigning this switchToCorrectNetwork function to the onClick handler of any button and then the magic will happen and the MetaMask network will get changed to the required network.

In case, any user doesn't have MetaMask installed then it will show an alert to the user and ask them to install MetaMask.

@naz3eh naz3eh requested a review from Dhaiwat10 December 15, 2021 18:11
@changeset-bot
Copy link

changeset-bot bot commented Dec 15, 2021

🦋 Changeset detected

Latest commit: 0fd8c23

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@web3-ui/hooks Minor
@web3-ui/core Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Dhaiwat10
Copy link
Member

@Nazeeh21 can you check the failing test?

add `any` type to error in catch handler

add any type to error in catch handler
@Pponwili1998

This comment has been minimized.

@Pponwili1998

This comment has been minimized.

Comment on lines 3 to 5
import Authereum from 'authereum';
import React, { useEffect } from 'react';
import { NETWORKS, Provider, useWallet } from '..';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change the order of the imports? We should try to keep the changes to the minimum. Only introduce relevant changes so that it's easier for anyone to grasp the PR when re-visiting

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is not any specific reason for that. I'll change it back. 😅

} else {
// if no window.ethereum then MetaMask is not installed
alert(
'MetaMask is not installed. Please consider installing it: https://metamask.io/download.html'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'MetaMask is not installed. Please consider installing it: https://metamask.io/download.html'
'Switching networks automatically is only supported in MetaMask. https://metamask.io/download.html'

Can you change the error message?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll update it.

console.error(error);
// This error code indicates that the chain has not been added to MetaMask.
// If it is not, then install it into the user MetaMask
// @ts-ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of @ts-ignore, did you try using something @ts-expect-error? I think that would be more appropriate.

Copy link
Member Author

@naz3eh naz3eh Dec 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I haven't tried it, I'll try using it.

} catch (error) {
console.error(error);
// This error code indicates that the chain has not been added to MetaMask.
// If it is not, then install it into the user MetaMask
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// If it is not, then install it into the user MetaMask
// If it is not, then install it into the user's MetaMask

}
}
} else {
// if no window.ethereum then MetaMask is not installed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// if no window.ethereum then MetaMask is not installed
// if window.ethereum is undefined then MetaMask is not installed

@Dhaiwat10
Copy link
Member

You need to also run yarn changeset and do a minor bump to the hooks package

@naz3eh naz3eh requested a review from Dhaiwat10 December 16, 2021 10:55
@Dhaiwat10 Dhaiwat10 merged commit 3f0bc64 into main Dec 16, 2021
@Dhaiwat10 Dhaiwat10 deleted the fix/issue-119 branch December 16, 2021 11:34
@github-actions github-actions bot mentioned this pull request Dec 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch to correct network
3 participants