-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c3ff24
commit 0e2506a
Showing
1 changed file
with
59 additions
and
0 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,59 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>MetaMask Chain Switch</title> | ||
<script> | ||
const provider = window.ethereum; | ||
|
||
async function switchChain(chainId, chainName, rpcUrl) { | ||
if (!provider) { | ||
alert("MetaMask is not installed!"); | ||
return; | ||
} | ||
|
||
try { | ||
await provider.request({ | ||
method: "wallet_switchEthereumChain", | ||
params: [{ chainId: chainId }] | ||
}); | ||
} catch (switchError) { | ||
console.error(switchError); | ||
if (switchError.code === 4902) { | ||
addChain(chainId, chainName, rpcUrl); | ||
} else { | ||
console.error("Unable to switch to the chain."); | ||
} | ||
} | ||
} | ||
|
||
async function addChain(chainId, chainName, rpcUrl,explorer,currencySymbol) { | ||
const hexChainId = '0x' + chainId.toString(16); // Convert decimal to hexadecimal | ||
try { | ||
await provider.request({ | ||
method: "wallet_addEthereumChain", | ||
params: [{ | ||
chainId: chainId, | ||
chainName: chainName, | ||
nativeCurrency: { | ||
name: currencySymbol, // Assuming the symbol is also used as the name | ||
symbol: currencySymbol, // Actual currency symbol | ||
decimals: 18 // Standard decimal places for Ethereum-like networks | ||
}, | ||
rpcUrls: [rpcUrl], | ||
blockExplorerUrls: [explorer] | ||
}] | ||
}); | ||
} catch (addError) { | ||
console.error(addError); | ||
} | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<h1>MetaMask Chain Management</h1> | ||
<button onclick="switchChain(17000, 'Holesky', 'https://ethereum-holesky-rpc.publicnode.com','https://holesky.etherscan.io','ETH')">Holesky</button> | ||
<button onclick="switchChain(9659, 'Parallel Testnet', 'https://rpc-accused-coffee-koala-b9fn1dik76.t.conduit.xyz','https://explorerl2new-accused-coffee-koala-b9fn1dik76.t.conduit.xyz','ETH')">Switch to Custom Network 2</button> | ||
</body> | ||
</html> |