import { INFURA_API_KEY , TARGET_NETWORK } from '../config' ; import { providers } from 'ethers' ; let provider ; export function getProvider ( ) { if ( provider ) return provider ; if ( window . ethereum ) { if ( window . ethereum . mockingInfuraProvider ) { provider = new providers . InfuraProvider ( TARGET_NETWORK , INFURA_API_KEY ) ; } else { provider = new providers . Web3Provider ( window . ethereum , 'any' ) ; provider . on ( 'network' , ( _ , oldNetwork ) => { if ( oldNetwork ) { window . location . reload ( ) ; } } ) ; } } else { provider = new providers . JsonRpcProvider ( `http://localhost:${ process . env . GANACHE_PORT }` ) ; } return provider ; }