Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasBlock authored Apr 28, 2024
1 parent 7c3ff24 commit 0e2506a
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions 4.html
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>

0 comments on commit 0e2506a

Please sign in to comment.