bridge_ui: MetaMask wallet_addEthereumChain support (#1085)
* bridge_ui: MetaMask wallet_addEthereumChain support * update karura and acala urls Co-authored-by: Evan Gray <battledingo@gmail.com>
This commit is contained in:
parent
0f6c1e2d98
commit
275fcb2a36
|
@ -10,6 +10,7 @@ import { useCallback, useMemo } from "react";
|
|||
import { useEthereumProvider } from "../contexts/EthereumProviderContext";
|
||||
import { useSolanaWallet } from "../contexts/SolanaWalletContext";
|
||||
import { CLUSTER, getEvmChainId } from "../utils/consts";
|
||||
import { METAMASK_CHAIN_PARAMETERS } from "../utils/metaMaskChainParameters";
|
||||
|
||||
const createWalletStatus = (
|
||||
isReady: boolean,
|
||||
|
@ -46,16 +47,31 @@ function useIsWalletReady(
|
|||
const correctEvmNetwork = getEvmChainId(chainId);
|
||||
const hasCorrectEvmNetwork = evmChainId === correctEvmNetwork;
|
||||
|
||||
const forceNetworkSwitch = useCallback(() => {
|
||||
const forceNetworkSwitch = useCallback(async () => {
|
||||
if (provider && correctEvmNetwork) {
|
||||
if (!isEVMChain(chainId)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
provider.send("wallet_switchEthereumChain", [
|
||||
await provider.send("wallet_switchEthereumChain", [
|
||||
{ chainId: hexStripZeros(hexlify(correctEvmNetwork)) },
|
||||
]);
|
||||
} catch (e) {}
|
||||
} catch (switchError: any) {
|
||||
// This error code indicates that the chain has not been added to MetaMask.
|
||||
if (switchError.code === 4902) {
|
||||
const addChainParameter =
|
||||
METAMASK_CHAIN_PARAMETERS[correctEvmNetwork];
|
||||
if (addChainParameter !== undefined) {
|
||||
try {
|
||||
await provider.send("wallet_addEthereumChain", [
|
||||
addChainParameter,
|
||||
]);
|
||||
} catch (addError) {
|
||||
console.error(addError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [provider, correctEvmNetwork, chainId]);
|
||||
|
||||
|
|
|
@ -0,0 +1,146 @@
|
|||
// https://docs.metamask.io/guide/rpc-api.html#wallet-addethereumchain as per EIP-3085
|
||||
export 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.
|
||||
}
|
||||
|
||||
// https://chainid.network/chains.json for chain info
|
||||
export const METAMASK_CHAIN_PARAMETERS: {
|
||||
[evmChainId: number]: AddEthereumChainParameter;
|
||||
} = {
|
||||
1: {
|
||||
chainId: "0x1",
|
||||
chainName: "Ethereum Mainnet",
|
||||
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
||||
rpcUrls: ["https://rpc.ankr.com/eth"],
|
||||
blockExplorerUrls: ["https://etherscan.io"],
|
||||
},
|
||||
3: {
|
||||
chainId: "0x3",
|
||||
chainName: "Ropsten",
|
||||
nativeCurrency: { name: "Ropsten Ether", symbol: "ROP", decimals: 18 },
|
||||
rpcUrls: ["https://ropsten.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161"],
|
||||
blockExplorerUrls: ["https://ropsten.etherscan.io"],
|
||||
},
|
||||
5: {
|
||||
chainId: "0x5",
|
||||
chainName: "Görli",
|
||||
nativeCurrency: { name: "Görli Ether", symbol: "GOR", decimals: 18 },
|
||||
rpcUrls: ["https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161"],
|
||||
blockExplorerUrls: ["https://goerli.etherscan.io"],
|
||||
},
|
||||
56: {
|
||||
chainId: "0x38",
|
||||
chainName: "Binance Smart Chain Mainnet",
|
||||
nativeCurrency: {
|
||||
name: "Binance Chain Native Token",
|
||||
symbol: "BNB",
|
||||
decimals: 18,
|
||||
},
|
||||
rpcUrls: ["https://bsc-dataseed.binance.org"],
|
||||
blockExplorerUrls: ["https://bscscan.com"],
|
||||
},
|
||||
97: {
|
||||
chainId: "0x61",
|
||||
chainName: "Binance Smart Chain Testnet",
|
||||
nativeCurrency: {
|
||||
name: "Binance Chain Native Token",
|
||||
symbol: "BNB",
|
||||
decimals: 18,
|
||||
},
|
||||
rpcUrls: ["https://data-seed-prebsc-1-s1.binance.org:8545"],
|
||||
blockExplorerUrls: ["https://testnet.bscscan.com"],
|
||||
},
|
||||
137: {
|
||||
chainId: "0x89",
|
||||
chainName: "Polygon Mainnet",
|
||||
nativeCurrency: { name: "MATIC", symbol: "MATIC", decimals: 18 },
|
||||
rpcUrls: ["https://polygon-rpc.com"],
|
||||
blockExplorerUrls: ["https://polygonscan.com"],
|
||||
},
|
||||
250: {
|
||||
chainId: "0xfa",
|
||||
chainName: "Fantom Opera",
|
||||
nativeCurrency: { name: "Fantom", symbol: "FTM", decimals: 18 },
|
||||
rpcUrls: ["https://rpc.ftm.tools"],
|
||||
blockExplorerUrls: ["https://ftmscan.com"],
|
||||
},
|
||||
686: {
|
||||
chainId: "0x2ae",
|
||||
chainName: "Karura Testnet",
|
||||
nativeCurrency: { name: "Karura Token", symbol: "KAR", decimals: 18 },
|
||||
rpcUrls: ["https://karura-dev.aca-dev.network/eth/http"],
|
||||
blockExplorerUrls: ["https://blockscout.karura-dev.aca-dev.network"],
|
||||
},
|
||||
787: {
|
||||
chainId: "0x313",
|
||||
chainName: "Acala Testnet",
|
||||
nativeCurrency: { name: "Acala Token", symbol: "ACA", decimals: 18 },
|
||||
rpcUrls: ["https://acala-dev.aca-dev.network/eth/http"],
|
||||
blockExplorerUrls: ["https://blockscout.acala-dev.aca-dev.network"],
|
||||
},
|
||||
4002: {
|
||||
chainId: "0xfa2",
|
||||
chainName: "Fantom Testnet",
|
||||
nativeCurrency: { name: "Fantom", symbol: "FTM", decimals: 18 },
|
||||
rpcUrls: ["https://rpc.testnet.fantom.network"],
|
||||
blockExplorerUrls: ["https://testnet.ftmscan.com"],
|
||||
},
|
||||
42261: {
|
||||
chainId: "0xa515",
|
||||
chainName: "Emerald Paratime Testnet",
|
||||
nativeCurrency: { name: "Emerald Rose", symbol: "ROSE", decimals: 18 },
|
||||
rpcUrls: ["https://testnet.emerald.oasis.dev"],
|
||||
blockExplorerUrls: ["https://testnet.explorer.emerald.oasis.dev"],
|
||||
},
|
||||
42262: {
|
||||
chainId: "0xa516",
|
||||
chainName: "Emerald Paratime Mainnet",
|
||||
nativeCurrency: { name: "Emerald Rose", symbol: "ROSE", decimals: 18 },
|
||||
rpcUrls: ["https://emerald.oasis.dev"],
|
||||
blockExplorerUrls: ["https://explorer.emerald.oasis.dev"],
|
||||
},
|
||||
43113: {
|
||||
chainId: "0xa869",
|
||||
chainName: "Avalanche Fuji Testnet",
|
||||
nativeCurrency: { name: "Avalanche", symbol: "AVAX", decimals: 18 },
|
||||
rpcUrls: ["https://api.avax-test.network/ext/bc/C/rpc"],
|
||||
blockExplorerUrls: ["https://testnet.snowtrace.io"],
|
||||
},
|
||||
43114: {
|
||||
chainId: "0xa86a",
|
||||
chainName: "Avalanche C-Chain",
|
||||
nativeCurrency: { name: "Avalanche", symbol: "AVAX", decimals: 18 },
|
||||
rpcUrls: ["https://api.avax.network/ext/bc/C/rpc"],
|
||||
blockExplorerUrls: ["https://snowtrace.io"],
|
||||
},
|
||||
80001: {
|
||||
chainId: "0x13881",
|
||||
chainName: "Mumbai",
|
||||
nativeCurrency: { name: "MATIC", symbol: "MATIC", decimals: 18 },
|
||||
rpcUrls: ["https://rpc-mumbai.maticvigil.com"],
|
||||
blockExplorerUrls: ["https://mumbai.polygonscan.com"],
|
||||
},
|
||||
1313161554: {
|
||||
chainId: "0x4e454152",
|
||||
chainName: "Aurora Mainnet",
|
||||
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
||||
rpcUrls: ["https://mainnet.aurora.dev"],
|
||||
blockExplorerUrls: ["https://aurorascan.dev"],
|
||||
},
|
||||
1313161555: {
|
||||
chainId: "0x4e454153",
|
||||
chainName: "Aurora Testnet",
|
||||
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
||||
rpcUrls: ["https://testnet.aurora.dev"],
|
||||
blockExplorerUrls: ["https://testnet.aurorascan.dev"],
|
||||
},
|
||||
};
|
Loading…
Reference in New Issue