diff --git a/faucet-ui/src/App.js b/faucet-ui/src/App.js index 873774d..dff7e63 100644 --- a/faucet-ui/src/App.js +++ b/faucet-ui/src/App.js @@ -9,70 +9,102 @@ function App() { const [fcContract, setFcContract] = useState(); const [withdrawError, setWithdrawError] = useState(""); const [withdrawSuccess, setWithdrawSuccess] = useState(""); - const [transactionData, setTransactionData] = useState(""); + const [transactionData, setTransactionData] = useState( + localStorage.getItem("transactionData") || "" + ); + const [isConnected, setIsConnected] = useState(false); + const [isCorrectNetwork, setIsCorrectNetwork] = useState(true); + const [isSwitchingChain, setIsSwitchingChain] = useState(false); useEffect(() => { getCurrentWalletConnected(); addWalletListener(); }, [walletAddress]); + useEffect(() => { + localStorage.setItem("transactionData", transactionData); + }, [transactionData]); + const connectWallet = async () => { - if (typeof window != "undefined" && typeof window.ethereum != "undefined") { + if (typeof window !== "undefined" && typeof window.ethereum !== "undefined") { try { - /* get provider */ - const provider = new ethers.providers.Web3Provider(window.ethereum); - /* get accounts */ - const accounts = await provider.send("eth_requestAccounts", []); - /* get signer */ - setSigner(provider.getSigner()); - /* local contract instance */ - setFcContract(faucetContract(provider)); - /* set active wallet address */ - setWalletAddress(accounts[0]); - } catch (err) { - console.error(err.message); + if (isSwitchingChain) { + setIsSwitchingChain(false); + await window.ethereum.request({ + method: "wallet_switchEthereumChain", + params: [{ chainId: "0x28c60" }], + }); + } else { + await window.ethereum.request({ + method: "wallet_addEthereumChain", + params: [ + { + chainId: "0x28c60", + chainName: "Katla", + nativeCurrency: { + name: "Ethereum", + symbol: "ETH", + decimals: 18, + }, + rpcUrls: ["https://rpc.katla.taiko.xyz"], + blockExplorerUrls: ["https://explorer.katla.taiko.xyz/"], + }, + ], + }); + const provider = new ethers.providers.Web3Provider(window.ethereum); + const accounts = await provider.send("eth_requestAccounts", []); + setSigner(provider.getSigner()); + setFcContract(faucetContract(provider)); + setWalletAddress(accounts[0]); + setIsConnected(true); + } + } catch (error) { + console.error(error); + setIsConnected(false); } } else { - /* MetaMask is not installed */ - console.log("Please install MetaMask"); + console.log("MetaMask is not installed"); } }; const getCurrentWalletConnected = async () => { - if (typeof window != "undefined" && typeof window.ethereum != "undefined") { + if (typeof window !== "undefined" && typeof window.ethereum !== "undefined") { try { - /* get provider */ const provider = new ethers.providers.Web3Provider(window.ethereum); - /* get accounts */ - const accounts = await provider.send("eth_accounts", []); - if (accounts.length > 0) { - /* get signer */ - setSigner(provider.getSigner()); - /* local contract instance */ - setFcContract(faucetContract(provider)); - /* set active wallet address */ - setWalletAddress(accounts[0]); + const chainId = await provider.send("eth_chainId"); + if (chainId === "0x28c60") { + setIsCorrectNetwork(true); + const accounts = await provider.send("eth_accounts", []); + if (accounts.length > 0) { + setSigner(provider.getSigner()); + setFcContract(faucetContract(provider)); + setWalletAddress(accounts[0]); + setIsConnected(true); + } } else { - console.log("Connect to MetaMask using the Connect Wallet button"); + setIsCorrectNetwork(false); } - } catch (err) { - console.error(err.message); + } catch (error) { + console.error(error); + setIsConnected(false); } } else { - /* MetaMask is not installed */ - console.log("Please install MetaMask"); + console.log("MetaMask is not installed"); } }; const addWalletListener = async () => { - if (typeof window != "undefined" && typeof window.ethereum != "undefined") { + if (typeof window !== "undefined" && typeof window.ethereum !== "undefined") { window.ethereum.on("accountsChanged", (accounts) => { setWalletAddress(accounts[0]); }); + window.ethereum.on("chainChanged", () => { + window.location.reload(); + }); } else { - /* MetaMask is not installed */ setWalletAddress(""); - console.log("Please install MetaMask"); + setIsConnected(false); + console.log("MetaMask is not installed"); } }; @@ -89,6 +121,11 @@ function App() { } }; + const handleSwitchChain = () => { + setIsSwitchingChain(true); + connectWallet(); + }; + return (