-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🥶 Bump web3-react to 8.0.0-beta, add Web3Modal to example (#479)
- Loading branch information
Showing
31 changed files
with
1,238 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,39 @@ | ||
import { useWeb3React } from '@web3-react/core' | ||
import { Web3Provider } from '@ethersproject/providers' | ||
import { ChainId } from '../constants' | ||
import { ChainId, ConnectorTuple } from '../constants' | ||
import { useConfig, useConnectors } from '../providers' | ||
import { MetaMask } from '@web3-react/metamask' | ||
import { useCallback } from 'react' | ||
import { useConfig } from '../providers/config/context' | ||
import { InjectedConnector } from '@web3-react/injected-connector' | ||
|
||
type ActivateBrowserWallet = (onError?: (error: Error) => void, throwErrors?: boolean) => void | ||
|
||
export type Web3Ethers = ReturnType<typeof useWeb3React> & { | ||
library?: Web3Provider | ||
chainId?: ChainId | ||
activateBrowserWallet: ActivateBrowserWallet | ||
function findActiveConnector(connectors: ConnectorTuple[]) { | ||
return connectors.find(([, , store]) => !!store.getState().accounts) | ||
} | ||
|
||
export function useEthers(): Web3Ethers { | ||
const result = useWeb3React<Web3Provider>() | ||
const { networks } = useConfig() | ||
const activateBrowserWallet = useCallback<ActivateBrowserWallet>( | ||
async (onError, throwErrors) => { | ||
const injected = new InjectedConnector({ supportedChainIds: networks?.map((network) => network.chainId) }) | ||
if (onError instanceof Function) { | ||
await result.activate(injected, onError, throwErrors) | ||
} else { | ||
await result.activate(injected, undefined, throwErrors) | ||
} | ||
}, | ||
[networks] | ||
) | ||
return { ...result, activateBrowserWallet } | ||
export function useEthers() { | ||
const { activeConnector, setConnectors, connectors, setActiveConnector } = useConnectors() | ||
const { defaultConnectors } = useConfig() | ||
const [connector, hooks, store] = activeConnector ?? findActiveConnector(connectors) ?? connectors[0] | ||
const result = hooks.useWeb3React(hooks.useProvider()) | ||
|
||
const deactivate = useCallback(() => { | ||
setConnectors(defaultConnectors) | ||
setActiveConnector(undefined) | ||
return connector.deactivate?.() | ||
}, [connector, defaultConnectors]) | ||
|
||
const activate = useCallback(() => { | ||
setActiveConnector([connector, hooks, store]) | ||
return connector.activate() | ||
}, [connector, hooks, store]) | ||
|
||
const activateBrowserWallet = useCallback(() => { | ||
const metamask = connectors.find(([connector]) => connector instanceof MetaMask) | ||
if (metamask) { | ||
setActiveConnector(metamask) | ||
return metamask[0].activate() | ||
} else { | ||
setActiveConnector([connector, hooks, store]) | ||
return connector.activate() | ||
} | ||
}, [connectors]) | ||
|
||
return { ...result, chainId: result.chainId as ChainId, activate, deactivate, activateBrowserWallet } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,14 @@ | ||
import { NetworkConnector } from '@web3-react/network-connector' | ||
import { useEffect } from 'react' | ||
import { useEthers } from '../hooks' | ||
import { InjectedConnector } from '@web3-react/injected-connector' | ||
import { useConfig } from './config/context' | ||
|
||
export function NetworkActivator() { | ||
const { activate, account, chainId: connectedChainId, active, connector } = useEthers() | ||
const { networks, readOnlyChainId, readOnlyUrls, autoConnect } = useConfig() | ||
const { activateBrowserWallet } = useEthers() | ||
const { autoConnect } = useConfig() | ||
|
||
useEffect(() => { | ||
const eagerConnect = async () => { | ||
const injected = new InjectedConnector({ | ||
supportedChainIds: networks?.map((network) => network?.chainId) || [], | ||
}) | ||
if (await injected.isAuthorized()) { | ||
activate(injected) | ||
} | ||
} | ||
autoConnect && eagerConnect() | ||
autoConnect && activateBrowserWallet() | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (readOnlyChainId && readOnlyUrls) { | ||
if (!active || (connector instanceof NetworkConnector && connectedChainId !== readOnlyChainId)) { | ||
activate(new NetworkConnector({ defaultChainId: readOnlyChainId, urls: readOnlyUrls || [] })) | ||
} | ||
} | ||
}, [readOnlyChainId, readOnlyUrls, active, account, connectedChainId, connector]) | ||
|
||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createContext, useContext } from 'react' | ||
import { Provider } from '@ethersproject/providers' | ||
import { ConnectorTuple } from '../../constants' | ||
|
||
type ConnectorContextProps = { | ||
connectors: ConnectorTuple[] | ||
activeConnector?: ConnectorTuple | ||
setConnectors: (connectors: ConnectorTuple[]) => void | ||
setActiveConnector: (connectors: ConnectorTuple | undefined) => void | ||
setProvider: (provider: Provider) => void | ||
} | ||
export const ConnectorsContext = createContext<ConnectorContextProps>({ | ||
connectors: [], | ||
activeConnector: undefined, | ||
setConnectors: () => undefined, | ||
setActiveConnector: () => undefined, | ||
setProvider: () => undefined, | ||
}) | ||
|
||
export function useConnectors() { | ||
return useContext(ConnectorsContext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './context' | ||
export * from './provider' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ReactNode, useCallback, useState } from 'react' | ||
import { ConnectorsContext } from './context' | ||
import { initializeConnector } from '@web3-react/core' | ||
import { EIP1193 } from '@web3-react/eip1193' | ||
import { Provider } from '@ethersproject/providers' | ||
import { ConnectorTuple } from '../../constants' | ||
|
||
interface ConnectorsProviderProps { | ||
children: ReactNode | ||
defaultConnectors: ConnectorTuple[] | ||
} | ||
|
||
export function ConnectorsProvider({ defaultConnectors, children }: ConnectorsProviderProps) { | ||
const [connectors, setConnectors] = useState(defaultConnectors) | ||
const [activeConnector, setActiveConnector] = useState<ConnectorTuple | undefined>(defaultConnectors[0]) | ||
|
||
const updateConnectors = useCallback( | ||
(_connectors: ConnectorTuple[]) => { | ||
setConnectors(_connectors) | ||
setActiveConnector(_connectors[0]) | ||
}, | ||
[setConnectors, setActiveConnector] | ||
) | ||
|
||
const setProvider = useCallback( | ||
(provider: Provider) => { | ||
const _connectors = [initializeConnector<EIP1193>((actions) => new EIP1193(actions, provider as any))] | ||
setConnectors(_connectors) | ||
setActiveConnector(_connectors[0]) | ||
}, | ||
[setConnectors, setActiveConnector] | ||
) | ||
|
||
return ( | ||
<ConnectorsContext.Provider | ||
value={{ connectors, setConnectors: updateConnectors, activeConnector, setActiveConnector, setProvider }} | ||
children={children} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.