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

feat(website): RPC connect Metamask Networks #14068

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { MetaMaskInpageProvider } from "@metamask/providers";

declare global {
interface Window{
ethereum?:MetaMaskInpageProvider
}
}

async function ConnectToSepolia() {
if (!window.ethereum) {
alert("Metamask not detected! Install Metamask then try again.")
return;
}
if (window.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)
}
}

type Props = {
buttonText: string;
};

export default function ConnectToSepoliaButton(props: Props) {
return (
<div
onClick={() => ConnectToSepolia()}
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}
Click to Connect to Sepolia
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { MetaMaskInpageProvider } from "@metamask/providers";

declare global {
interface Window{
ethereum?:MetaMaskInpageProvider
}
}

async function ConnectToTaikoAlpha3() {
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: [],
};

if (!window.ethereum) {
alert("Metamask not detected! Install Metamask then try again.")
return;
}
if (window.ethereum.networkVersion == "167005") {
alert("You are already connected to Taiko Alpha 3 (chainId 167005).", )
return;
}
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)
}
}

type Props = {
buttonText: string;
};

export default function ConnectToTaikoAlpha3Button(props: Props) {
return (
<div
onClick={() => ConnectToTaikoAlpha3()}
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}
Click to Connect to Taiko Alpha 3
</div>
);
}
1 change: 1 addition & 0 deletions packages/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"start": "pnpm next start"
},
"dependencies": {
"@metamask/providers": "^11.1.0",
"@vercel/analytics": "^1.0.1",
"next": "^13.4.7",
"next-themes": "^0.2.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Sepolia

import ConnectToSepoliaButton from 'components/MetamaskNetworkButton/ConnectToSepolia';

<ConnectToSepoliaButton />

| Name | Value |
| ------------------ | ---------------------------- |
| Chain ID | 11155111 |
Expand All @@ -11,6 +15,10 @@

## Taiko

import ConnectToTaikoAlpha3Button from 'components/MetamaskNetworkButton/ConnectToTaikoAlpha3';

<ConnectToTaikoAlpha3Button />

| Name | Value |
| ------------------ | ------------------------------- |
| Chain ID | 167005 |
Expand Down
Loading