From 2f609eaa1f47f1c7df20a88ccd58276397f0aeae Mon Sep 17 00:00:00 2001 From: MarcusWentz Date: Wed, 28 Jun 2023 16:35:31 -0400 Subject: [PATCH] Website docs RPC connect Metamask click button --- .../ConnectToMetamask.tsx | 87 +++++++++++++++++++ ...configuration.md => rpc-configuration.mdx} | 6 ++ 2 files changed, 93 insertions(+) create mode 100644 packages/website/components/MetamaskNetworkButton/ConnectToMetamask.tsx rename packages/website/pages/docs/reference/{rpc-configuration.md => rpc-configuration.mdx} (79%) diff --git a/packages/website/components/MetamaskNetworkButton/ConnectToMetamask.tsx b/packages/website/components/MetamaskNetworkButton/ConnectToMetamask.tsx new file mode 100644 index 00000000000..deb02162032 --- /dev/null +++ b/packages/website/components/MetamaskNetworkButton/ConnectToMetamask.tsx @@ -0,0 +1,87 @@ +type Props = { + buttonText: string; +}; + +async function ConnectToMetamask(props: Props) { + if (!(window as any).ethereum) { + alert("Wallet not detected! Install Metamask then try again."); + return; + } + if(props.buttonText == "Connect to Sepolia"){ + ConnectToSepolia(); + } + if(props.buttonText == "Connect to Taiko"){ + ConnectToTaiko(); + } +} + +async function ConnectToSepolia() { + if (!(window as any).ethereum) { + alert("Metamask not detected! Install Metamask then try again.") + return; + } + if ((window as any).ethereum.networkVersion == "11155111") { + alert("You are already connected to Sepolia (chainId 11155111).", ) + return; + } + try{ + await (window as any).ethereum.request({ + method: "wallet_switchEthereumChain", + params: [{ + chainId: "0xaa36a7" + }] + }); + } catch (error) { + alert("Failed to add the network with wallet_addEthereumChain request. Add the network with https://chainlist.org/ or do it manually. Error log: " + error.message) + } +} + +async function ConnectToTaiko() { + if ((window as any).ethereum.networkVersion == "167005") { + alert("You are already connected to Taiko Alpha 3 (chainId 167005).", ) + return; + } + interface AddEthereumChainParameter { + chainId: string; // A 0x-prefixed hexadecimal string + chainName: string; + nativeCurrency: { + name: string; + symbol: string; // 2-6 characters long + decimals: 18; + }; + rpcUrls: string[]; + blockExplorerUrls?: string[]; + iconUrls?: string[]; // Currently ignored. + } + const taikoParams: AddEthereumChainParameter = { + chainId: "0x28c5d", + chainName: "Taiko (Alpha-3 Testnet)", + nativeCurrency: { + name: "ETH", + symbol: "eth", + decimals: 18, + }, + rpcUrls: ["https://rpc.test.taiko.xyz"], + blockExplorerUrls: ["https://explorer.test.taiko.xyz/"], + iconUrls: [], + }; + try{ + await (window as any).ethereum.request({ + method: "wallet_addEthereumChain", + params: [taikoParams], + }); + } catch (error) { + alert("Failed to add the network with wallet_addEthereumChain request. Add the network with https://chainlist.org/ or do it manually. Error log: " + error.message) + } +} + +export default function ConnectToMetamaskButton(props: Props) { + return ( +
ConnectToMetamask(props)} + className="hover:cursor-pointer text-neutral-900 bg-neutral-100 hover:bg-neutral-200 border-solid border-neutral-200 focus:ring-4 focus:outline-none focus:ring-neutral-100 font-medium rounded-lg text-sm px-3 py-2 text-center inline-flex items-center dark:focus:ring-neutral-600 dark:bg-neutral-800 dark:border-neutral-700 dark:text-white dark:hover:bg-neutral-700" + > + {props.buttonText} +
+ ); +} \ No newline at end of file diff --git a/packages/website/pages/docs/reference/rpc-configuration.md b/packages/website/pages/docs/reference/rpc-configuration.mdx similarity index 79% rename from packages/website/pages/docs/reference/rpc-configuration.md rename to packages/website/pages/docs/reference/rpc-configuration.mdx index 2339dc555cd..82ce9b9d785 100644 --- a/packages/website/pages/docs/reference/rpc-configuration.md +++ b/packages/website/pages/docs/reference/rpc-configuration.mdx @@ -1,7 +1,11 @@ +import ConnectToMetamask from 'components/MetamaskNetworkButton/ConnectToMetamask'; + # RPC configuration ## Sepolia + + | Name | Value | | ------------------ | ---------------------------- | | Chain ID | 11155111 | @@ -11,6 +15,8 @@ ## Taiko + + | Name | Value | | ------------------ | ------------------------------- | | Chain ID | 167005 |