import 'regenerator-runtime/runtime' import 'react-app-polyfill/ie11' import * as React from 'react' import * as ReactDOM from 'react-dom' import { AbstractConnectorInterface } from '@web3-react/types' import { Web3ReactProvider, useWeb3React, UnsupportedChainIdError } from '@web3-react/core' import { NoEthereumProviderError, UserRejectedRequestError as UserRejectedRequestErrorInjected } from '@web3-react/injected-connector' import { URI_AVAILABLE, UserRejectedRequestError as UserRejectedRequestErrorWalletConnect } from '@web3-react/walletconnect-connector' import { UserRejectedRequestError as UserRejectedRequestErrorFrame } from '@web3-react/frame-connector' import { Web3Provider } from '@ethersproject/providers' import { formatEther } from '@ethersproject/units' import { injected, network, walletconnect, walletlink, ledger, trezor, frame, authereum, fortmatic, portis, squarelink, torus } from './connectors' import { useEagerConnect, useInactiveListener } from './hooks' import { Spinner } from './Spinner' const connectorsByName: { [name: string]: AbstractConnectorInterface } = { Injected: injected, Network: network, WalletConnect: walletconnect, WalletLink: walletlink, Ledger: ledger, Trezor: trezor, Frame: frame, Authereum: authereum, Fortmatic: fortmatic, Portis: portis, Squarelink: squarelink, Torus: torus } function getErrorMessage(error: Error) { if (error instanceof NoEthereumProviderError) { return 'No Ethereum browser extension detected, install MetaMask on desktop or visit from a dApp browser on mobile.' } else if (error instanceof UnsupportedChainIdError) { return "You're connected to an unsupported network." } else if ( error instanceof UserRejectedRequestErrorInjected || error instanceof UserRejectedRequestErrorWalletConnect || error instanceof UserRejectedRequestErrorFrame ) { return 'Please authorize this website to access your Ethereum account.' } else { console.error(error) return 'An unknown error occurred. Check the console for more details.' } } function getLibrary(provider: any): Web3Provider { const library = new Web3Provider(provider) library.pollingInterval = 8000 return library } function App() { return ( ) } function MyComponent() { const context = useWeb3React() const { connector, library, chainId, account, activate, deactivate, active, error } = context // handle logic to recognize the connector currently being activated const [activatingConnector, setActivatingConnector] = React.useState() React.useEffect(() => { if (activatingConnector && activatingConnector === connector) { setActivatingConnector(undefined) } }, [activatingConnector, connector]) // handle logic to eagerly connect to the injected ethereum provider, if it exists and has granted access already const triedEager = useEagerConnect() // handle logic to connect in reaction to certain events on the injected ethereum provider, if it exists useInactiveListener(!triedEager || !!activatingConnector) // set up block listener const [blockNumber, setBlockNumber] = React.useState() React.useEffect((): any => { if (library) { let stale = false library .getBlockNumber() .then((blockNumber: number) => { if (!stale) { setBlockNumber(blockNumber) } }) .catch(() => { if (!stale) { setBlockNumber(null) } }) const updateBlockNumber = (blockNumber: number) => { setBlockNumber(blockNumber) } library.on('block', updateBlockNumber) return () => { library.removeListener('block', updateBlockNumber) stale = true setBlockNumber(undefined) } } }, [library, chainId]) // fetch eth balance of the connected account const [ethBalance, setEthBalance] = React.useState() React.useEffect((): any => { if (library && account) { let stale = false library .getBalance(account) .then((balance: any) => { if (!stale) { setEthBalance(balance) } }) .catch(() => { if (!stale) { setEthBalance(null) } }) return () => { stale = true setEthBalance(undefined) } } }, [library, account, chainId]) // log the walletconnect URI React.useEffect(() => { const logURI = (uri: any) => { console.log('WalletConnect URI', uri) } walletconnect.on(URI_AVAILABLE, logURI) return () => { walletconnect.off(URI_AVAILABLE, logURI) } }, []) return (

{active ? '🟢' : error ? '🔴' : '🟠'}

Chain Id ⛓ {chainId === undefined ? '...' : chainId} Block Number 🔢 {blockNumber === undefined ? '...' : blockNumber === null ? 'Error' : blockNumber.toLocaleString()} Account 🤖 {account === undefined ? '...' : account === null ? 'None' : `${account.substring(0, 6)}...${account.substring(account.length - 4)}`} Balance 💰 {ethBalance === undefined ? '...' : ethBalance === null ? 'Error' : `Ξ${parseFloat(formatEther(ethBalance)).toPrecision(4)}`}


{Object.keys(connectorsByName).map(name => { const currentConnector = connectorsByName[name] const activating = currentConnector === activatingConnector const connected = currentConnector === connector const disabled = !triedEager || !!activatingConnector || connected || !!error return ( ) })}
{(active || error) && ( )} {!!error &&

{getErrorMessage(error)}

}

{!!(library && account) && ( )} {!!(connector === network && chainId) && ( )} {connector === walletconnect && ( )} {connector === fortmatic && ( )} {connector === portis && ( <> {chainId !== undefined && ( )} )} {connector === torus && ( )}
) } ReactDOM.render(, document.getElementById('root'))