import React from 'react'
import { Web3ReactProvider, useWeb3React, UnsupportedChainIdError } from '@web3-react/core'
import {
NoEthereumProviderError,
UserRejectedRequestError as UserRejectedRequestErrorInjected
} from '@web3-react/injected-connector'
import { 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 { useEagerConnect, useInactiveListener } from '../hooks'
import {
injected,
network,
walletconnect,
walletlink,
ledger,
trezor,
lattice,
frame,
authereum,
fortmatic,
magic,
portis,
torus
} from '../connectors'
import { Spinner } from '../components/Spinner'
enum ConnectorNames {
Injected = 'Injected',
Network = 'Network',
WalletConnect = 'WalletConnect',
WalletLink = 'WalletLink',
Ledger = 'Ledger',
Trezor = 'Trezor',
Lattice = 'Lattice',
Frame = 'Frame',
Authereum = 'Authereum',
Fortmatic = 'Fortmatic',
Magic = 'Magic',
Portis = 'Portis',
Torus = 'Torus'
}
const connectorsByName: { [connectorName in ConnectorNames]: any } = {
[ConnectorNames.Injected]: injected,
[ConnectorNames.Network]: network,
[ConnectorNames.WalletConnect]: walletconnect,
[ConnectorNames.WalletLink]: walletlink,
[ConnectorNames.Ledger]: ledger,
[ConnectorNames.Trezor]: trezor,
[ConnectorNames.Lattice]: lattice,
[ConnectorNames.Frame]: frame,
[ConnectorNames.Authereum]: authereum,
[ConnectorNames.Fortmatic]: fortmatic,
[ConnectorNames.Magic]: magic,
[ConnectorNames.Portis]: portis,
[ConnectorNames.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 = 12000
return library
}
export default function() {
return (
)
}
function ChainId() {
const { chainId } = useWeb3React()
return (
<>
Chain Id
⛓
{chainId ?? ''}
>
)
}
function BlockNumber() {
const { chainId, library } = useWeb3React()
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 () => {
stale = true
library.removeListener('block', updateBlockNumber)
setBlockNumber(undefined)
}
}
}, [library, chainId]) // ensures refresh if referential identity of library doesn't change across chainIds
return (
<>
Block Number
🔢
{blockNumber === null ? 'Error' : blockNumber ?? ''}
>
)
}
function Account() {
const { account } = useWeb3React()
return (
<>
Account
🤖
{account === null
? '-'
: account
? `${account.substring(0, 6)}...${account.substring(account.length - 4)}`
: ''}
>
)
}
function Balance() {
const { account, library, chainId } = useWeb3React()
const [balance, setBalance] = React.useState()
React.useEffect((): any => {
if (!!account && !!library) {
let stale = false
library
.getBalance(account)
.then((balance: any) => {
if (!stale) {
setBalance(balance)
}
})
.catch(() => {
if (!stale) {
setBalance(null)
}
})
return () => {
stale = true
setBalance(undefined)
}
}
}, [account, library, chainId]) // ensures refresh if referential identity of library doesn't change across chainIds
return (
<>
Balance
💰
{balance === null ? 'Error' : balance ? `Ξ${formatEther(balance)}` : ''}
>
)
}
function Header() {
const { active, error } = useWeb3React()
return (
<>
{active ? '🟢' : error ? '🔴' : '🟠'}
>
)
}
function App() {
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)
return (
<>