-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f24bf8d
commit 0fe8f2a
Showing
7 changed files
with
46 additions
and
109 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
71 changes: 19 additions & 52 deletions
71
apps/subbridge/hooks/useEthereumProviderInitialization.ts
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,75 +1,42 @@ | ||
import { | ||
ethereumProviderAtom, | ||
ethersWeb3ProviderAtom, | ||
evmAccountAtom, | ||
evmChainIdAtom, | ||
isEvmWalletAuthorizedAtom, | ||
} from '@/store/ethers' | ||
import type {ethers} from 'ethers' | ||
import detectEthereumProvider from '@metamask/detect-provider' | ||
import {ethers} from 'ethers' | ||
import {useAtom} from 'jotai' | ||
import {useEffect} from 'react' | ||
import {useEffect, useRef} from 'react' | ||
|
||
export const useEthereumProviderInitialization = (): void => { | ||
const [evmAccount] = useAtom(evmAccountAtom) | ||
const [ethereumProvider, setEthereumProvider] = useAtom(ethereumProviderAtom) | ||
const [, setEthersWeb3Provider] = useAtom(ethersWeb3ProviderAtom) | ||
const [, setEvmAccount] = useAtom(evmAccountAtom) | ||
const [, setEvmChainId] = useAtom(evmChainIdAtom) | ||
const [isEvmWalletAuthorized, setIsEvmWalletAuthorized] = useAtom( | ||
isEvmWalletAuthorizedAtom, | ||
) | ||
const runRef = useRef(false) | ||
|
||
useEffect(() => { | ||
if (ethereumProvider != null) return | ||
if (runRef.current) return | ||
runRef.current = true | ||
const init = async (): Promise<void> => { | ||
const {default: detectEthereumProvider} = await import( | ||
'@metamask/detect-provider' | ||
) | ||
const provider = await detectEthereumProvider({silent: true}) | ||
if (provider == null) return | ||
const ethereum = provider as ethers.providers.ExternalProvider | ||
setEthereumProvider(ethereum) | ||
const ethereum = await detectEthereumProvider({silent: true}) | ||
if (ethereum == null) return | ||
const provider = new ethers.providers.Web3Provider(ethereum) | ||
setEthersWeb3Provider(provider) | ||
const updateAccounts = (accounts: unknown): void => { | ||
const account = (accounts as string[])[0] | ||
setEvmAccount(account ?? null) | ||
} | ||
const updateChainId = (chainId: unknown): void => { | ||
setEvmChainId(Number.parseInt(chainId as string, 16)) | ||
} | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
;(ethereum as any).on('accountsChanged', updateAccounts) | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
;(ethereum as any).on('chainChanged', updateChainId) | ||
void ethereum.request?.({method: 'eth_chainId'}).then(updateChainId) | ||
if (isEvmWalletAuthorized) { | ||
ethereum | ||
.request?.({method: 'eth_requestAccounts'}) | ||
.then((accounts) => { | ||
const account = (accounts as string[])[0] | ||
setEvmAccount(account ?? null) | ||
}) | ||
.catch((error) => { | ||
// User rejected | ||
if (error.code === 4001) { | ||
setIsEvmWalletAuthorized(false) | ||
} else { | ||
throw error | ||
} | ||
}) | ||
} | ||
await provider.listAccounts().then(updateAccounts) | ||
ethereum.on('accountsChanged', updateAccounts) | ||
ethereum.on('chainChanged', updateChainId) | ||
await provider.provider | ||
.request?.({method: 'eth_chainId'}) | ||
.then(updateChainId) | ||
} | ||
|
||
void init() | ||
}, [ | ||
ethereumProvider, | ||
isEvmWalletAuthorized, | ||
setEthereumProvider, | ||
setIsEvmWalletAuthorized, | ||
setEvmAccount, | ||
setEvmChainId, | ||
]) | ||
|
||
useEffect(() => { | ||
if (evmAccount != null) { | ||
setIsEvmWalletAuthorized(true) | ||
} | ||
}, [evmAccount, setIsEvmWalletAuthorized]) | ||
}, [setEthersWeb3Provider, setEvmAccount, setEvmChainId]) | ||
} |
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,42 +1,16 @@ | ||
import {fromChainAtom} from '@/store/bridge' | ||
import {ethereumProviderAtom, evmChainIdAtom} from '@/store/ethers' | ||
import {ethers} from 'ethers' | ||
import {ethersWeb3ProviderAtom} from '@/store/ethers' | ||
import type {ethers} from 'ethers' | ||
import {useAtomValue} from 'jotai' | ||
import useSWRImmutable from 'swr/immutable' | ||
|
||
const ethersWeb3ProviderFetcher = async ([ethereumProvider, _]: [ | ||
ethers.providers.ExternalProvider, | ||
number, | ||
]): Promise<ethers.providers.Web3Provider> => { | ||
const provider = new ethers.providers.Web3Provider(ethereumProvider) | ||
return provider | ||
} | ||
export const useEthersWeb3Provider = | ||
(): ethers.providers.Web3Provider | null => { | ||
const fromChain = useAtomValue(fromChainAtom) | ||
const ethersWeb3Provider = useAtomValue(ethersWeb3ProviderAtom) | ||
|
||
export const useEthersWeb3Provider = (): | ||
| ethers.providers.Web3Provider | ||
| undefined => { | ||
const fromChain = useAtomValue(fromChainAtom) | ||
const evmChainId = useAtomValue(evmChainIdAtom) | ||
const ethereumProvider = useAtomValue(ethereumProviderAtom) | ||
const {data} = useSWRImmutable( | ||
fromChain.kind === 'evm' && | ||
fromChain.evmChainId === evmChainId && | ||
ethereumProvider != null && [ethereumProvider, evmChainId], | ||
ethersWeb3ProviderFetcher, | ||
) | ||
if (fromChain.kind === 'evm') { | ||
return ethersWeb3Provider | ||
} | ||
|
||
return data | ||
} | ||
|
||
const ethersJsonRpcProviderFetcher = async ( | ||
url: string, | ||
): Promise<ethers.providers.StaticJsonRpcProvider> => { | ||
return new ethers.providers.StaticJsonRpcProvider(url) | ||
} | ||
|
||
export const useEthersJsonRpcProvider = ( | ||
url?: string, | ||
): ethers.providers.StaticJsonRpcProvider | undefined => { | ||
const {data} = useSWRImmutable(url, ethersJsonRpcProviderFetcher) | ||
return data | ||
} | ||
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
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