This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1867 - Add network if nonexistent (#2424)
* mod switch/addNetworks util * fix chain info object
- Loading branch information
Showing
3 changed files
with
85 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { BigNumber } from '@ethersproject/bignumber' | ||
import { hexStripZeros } from '@ethersproject/bytes' | ||
import { Web3Provider } from '@ethersproject/providers' | ||
import { L1ChainInfo, L2ChainInfo, SupportedChainId } from 'constants/chains' | ||
|
||
interface AddNetworkArguments { | ||
library: Web3Provider | ||
chainId: SupportedChainId | ||
info: L1ChainInfo | L2ChainInfo | ||
} | ||
|
||
// provider.request returns Promise<any>, but wallet_switchEthereumChain must return null or throw | ||
// see https://github.com/rekmarks/EIPs/blob/3326-create/EIPS/eip-3326.md for more info on wallet_switchEthereumChain | ||
export async function addNetwork({ library, chainId, info }: AddNetworkArguments): Promise<null | void> { | ||
if (!library?.provider?.request) { | ||
return | ||
} | ||
const formattedChainId = hexStripZeros(BigNumber.from(chainId).toHexString()) | ||
try { | ||
await library?.provider.request({ | ||
method: 'wallet_addEthereumChain', | ||
params: [ | ||
{ | ||
chainId: formattedChainId, | ||
chainName: info.label, | ||
rpcUrls: info.rpcUrls, | ||
nativeCurrency: info.nativeCurrency, | ||
blockExplorerUrls: [info.explorer], | ||
}, | ||
], | ||
}) | ||
} catch (error) { | ||
console.error('error adding eth network: ', chainId, info, error) | ||
throw new Error(error?.message) | ||
} | ||
} |
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,47 @@ | ||
import { BigNumber } from '@ethersproject/bignumber' | ||
import { hexStripZeros } from '@ethersproject/bytes' | ||
import { Web3Provider } from '@ethersproject/providers' | ||
import { CHAIN_INFO, SupportedChainId } from 'constants/chains' | ||
|
||
import { addNetwork } from 'utils/addNetwork' | ||
|
||
interface SwitchNetworkArguments { | ||
library: Web3Provider | ||
chainId?: SupportedChainId | ||
} | ||
|
||
// provider.request returns Promise<any>, but wallet_switchEthereumChain must return null or throw | ||
// see https://github.com/rekmarks/EIPs/blob/3326-create/EIPS/eip-3326.md for more info on wallet_switchEthereumChain | ||
export async function switchToNetwork({ library, chainId }: SwitchNetworkArguments): Promise<null | void> { | ||
if (!library?.provider?.request) { | ||
return | ||
} | ||
if (!chainId && library?.getNetwork) { | ||
;({ chainId } = await library.getNetwork()) | ||
} | ||
const formattedChainId = hexStripZeros(BigNumber.from(chainId).toHexString()) | ||
try { | ||
await library?.provider.request({ | ||
method: 'wallet_switchEthereumChain', | ||
params: [{ chainId: formattedChainId }], | ||
}) | ||
} catch (error) { | ||
// 4902 is the error code for attempting to switch to an unrecognized chainId | ||
if (error.code === 4902 && chainId !== undefined) { | ||
const info = CHAIN_INFO[chainId] | ||
|
||
// MOD - need to handle these errors, else loops | ||
try { | ||
// metamask (only known implementer) automatically switches after a network is added | ||
// the second call is done here because that behavior is not a part of the spec and cannot be relied upon in the future | ||
// metamask's behavior when switching to the current network is just to return null (a no-op) | ||
await addNetwork({ library, chainId, info }) | ||
await switchToNetwork({ library, chainId }) | ||
} catch (error) { | ||
console.error(`Error in ADDING/SWITCHING ${chainId}:`, error) | ||
} | ||
} else { | ||
throw error | ||
} | ||
} | ||
} |