export const metamaskUtils = { registerToken : async ( tokenAddress , tokenSymbol , tokenDecimals , tokenImage ) => { const tokenAdded = await window . ethereum . request ( { method : 'wallet_watchAsset' , params : { type : 'ERC20' , options : { address : tokenAddress , symbol : tokenSymbol , decimals : tokenDecimals , image : tokenImage } } } ) ; return tokenAdded ; } , setupNetwork : async ( { chainId , chainName , rpcUrls , blockExplorerUrls , nativeCurrency } ) => { return new Promise ( async ( resolve , reject ) => { const provider = window . ethereum ; if ( provider ) { try { await provider . request ( { method : 'wallet_switchEthereumChain' , params : [ { chainId : `0x${ chainId . toString ( 16 ) }` } ] } ) ; resolve ( true ) ; } catch ( switchError ) { if ( switchError . code === 4902 ) { try { await provider . request ( { method : 'wallet_addEthereumChain' , params : [ { chainId : `0x${ chainId . toString ( 16 ) }` , chainName , nativeCurrency , rpcUrls , blockExplorerUrls } ] } ) ; resolve ( true ) ; return true ; } catch ( error ) { reject ( error ) ; return false ; } } else { reject ( switchError ) ; } } } else { console . error ( "Can't setup the BSC network on metamask because window.ethereum is undefined" ) ; reject ( false ) ; return false ; } } ) ; } } ;