This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
generated from moonshotcollective/scaffold-eth
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AlertDialog for wrong network message and fix chainChanged callback
- Loading branch information
1 parent
77311ee
commit 3a98910
Showing
3 changed files
with
123 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React, { useContext, useEffect, useState } from "react"; | ||
import { | ||
Button, | ||
AlertDialog, | ||
AlertDialogBody, | ||
AlertDialogFooter, | ||
AlertDialogHeader, | ||
AlertDialogContent, | ||
AlertDialogOverlay, | ||
} from "@chakra-ui/react"; | ||
|
||
import { Web3Context } from "../helpers/Web3Context"; | ||
|
||
function WrongNetworkAlertDialog() { | ||
const [isAlertOpen, setIsAlertOpen] = useState(false); | ||
const context = useContext(Web3Context); | ||
const cancelRef = React.useRef(); | ||
|
||
useEffect(() => { | ||
if (context) { | ||
setIsAlertOpen(!context.rightNetwork); | ||
} | ||
}, [context && context.rightNetwork]); | ||
|
||
const onNetworkSwitch = async () => { | ||
const data = [ | ||
{ | ||
chainId: "0x" + context.targetNetwork.chainId.toString(16), | ||
chainName: context.targetNetwork.name, | ||
nativeCurrency: context.targetNetwork.nativeCurrency, | ||
rpcUrls: [context.targetNetwork.rpcUrl], | ||
blockExplorerUrls: [context.targetNetwork.blockExplorer], | ||
}, | ||
]; | ||
try { | ||
await ethereum.request({ | ||
method: "wallet_switchEthereumChain", | ||
params: [{ chainId: data[0].chainId }], | ||
}); | ||
setIsAlertOpen(false); | ||
} catch (switchError) { | ||
// This error code indicates that the chain has not been added to MetaMask. | ||
if (switchError.code === 4902) { | ||
try { | ||
await ethereum.request({ | ||
method: "wallet_addEthereumChain", | ||
params: data, | ||
}); | ||
setIsAlertOpen(false); | ||
} catch (addError) { | ||
console.log(addError); | ||
} | ||
} | ||
// handle other "switch" errors | ||
} | ||
}; | ||
|
||
return ( | ||
<AlertDialog isOpen={isAlertOpen} leastDestructiveRef={cancelRef}> | ||
<AlertDialogOverlay> | ||
<AlertDialogContent> | ||
<AlertDialogHeader fontSize="lg" fontWeight="bold"> | ||
Switch network | ||
</AlertDialogHeader> | ||
|
||
<AlertDialogBody> | ||
To use this app you must switch to the {context.targetNetwork.name} network. | ||
</AlertDialogBody> | ||
|
||
<AlertDialogFooter> | ||
<Button | ||
ref={cancelRef} | ||
style={{ marginRight: 20 }} | ||
onClick={() => { | ||
setIsAlertOpen(false); | ||
}} | ||
> | ||
Close | ||
</Button> | ||
<Button onClick={onNetworkSwitch} colorScheme="blue"> | ||
Switch | ||
</Button> | ||
</AlertDialogFooter> | ||
</AlertDialogContent> | ||
</AlertDialogOverlay> | ||
</AlertDialog> | ||
); | ||
} | ||
|
||
export default WrongNetworkAlertDialog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters