UNPKG

emmet.sdk

Version:
45 lines 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.switchEvmChain = void 0; const chains_1 = require("../../chains"); const format_1 = require("../format"); const detectEthereumProvider_1 = require("./detectEthereumProvider"); async function switchEvmChain(chainName) { const formattedName = (0, format_1.formatChainName)(chainName); const newChain = chains_1.ALL_CHAINS[formattedName]; const chainId = `0x${newChain.id.toString(16)}`; const ethereum = await (0, detectEthereumProvider_1.detectEthereumProvider)(); try { await ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId }], }); } catch (switchError) { if (switchError && switchError.code === 4902) { try { await ethereum.request({ method: 'wallet_addEthereumChain', params: [ { chainId: newChain.id, chainName: newChain.name, rpcUrls: newChain.rpcUrls, nativeCurrency: newChain.nativeCurrency }, ], }); } catch (addError) { if (addError.code === 4001) { console.error("User rejected chain addition."); } else { console.error("Failed to add new chain:", addError); } } } } } exports.switchEvmChain = switchEvmChain; //# sourceMappingURL=switchEvmChain.js.map