-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(website): RPC connect Metamask Networks (#14070)
Co-authored-by: dave | d1onys1us <[email protected]>
- Loading branch information
1 parent
99c3980
commit 1ef2126
Showing
4 changed files
with
86 additions
and
1 deletion.
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,50 @@ | ||
import { taikoChainConfig } from "../constants/taikoChainConfig"; | ||
|
||
type ConnectButtonProps = { | ||
network: "Sepolia" | "Taiko"; | ||
}; | ||
|
||
async function ConnectToMetamask(network: ConnectButtonProps["network"]) { | ||
if (!(window as any).ethereum) { | ||
alert("Metamask not detected! Install Metamask then try again."); | ||
} | ||
if ( | ||
(window as any).ethereum.networkVersion == | ||
(network === "Sepolia" ? 11155111 : 167005) | ||
) { | ||
alert(`You are already connected to ${network}.`); | ||
} | ||
try { | ||
if (network === "Sepolia") { | ||
await (window as any).ethereum.request({ | ||
method: "wallet_switchEthereumChain", | ||
params: [ | ||
{ | ||
chainId: "0xaa36a7", | ||
}, | ||
], | ||
}); | ||
} else { | ||
await (window as any).ethereum.request({ | ||
method: "wallet_addEthereumChain", | ||
params: [taikoChainConfig], | ||
}); | ||
} | ||
} 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: ConnectButtonProps) { | ||
return ( | ||
<div | ||
onClick={() => ConnectToMetamask(props.network)} | ||
className="hover:cursor-pointer text-neutral-100 bg-[#E81899] hover:bg-[#d1168a] 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" | ||
> | ||
Connect to {props.network} | ||
</div> | ||
); | ||
} |
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,25 @@ | ||
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. | ||
} | ||
|
||
export const taikoChainConfig: 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: [], | ||
}; |
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