import { Networks } from "../constants/blockchain" ; const switchRequest = ( ) => { return window . ethereum . request ( { method : "wallet_switchEthereumChain" , params : [ { chainId : "0xa86a" } ] , } ) ; } ; const addChainRequest = ( ) => { return window . ethereum . request ( { method : "wallet_addEthereumChain" , params : [ { chainId : "0xa86a" , chainName : "Avalanche Mainnet" , rpcUrls : [ "https://api.avax.network/ext/bc/C/rpc" ] , blockExplorerUrls : [ "https://cchain.explorer.avax.network/" ] , nativeCurrency : { name : "AVAX" , symbol : "AVAX" , decimals : 18 , } , } , ] , } ) ; } ; export const swithNetwork = async ( ) => { if ( window . ethereum ) { try { await switchRequest ( ) ; } catch ( error ) { if ( error . code === 4902 ) { try { await addChainRequest ( ) ; await switchRequest ( ) ; } catch ( addError ) { console . log ( error ) ; } } console . log ( error ) ; } } } ;