diff --git a/packages/bridge-ui/src/utils/switchEthereumChain.spec.ts b/packages/bridge-ui/src/utils/switchEthereumChain.spec.ts deleted file mode 100644 index 23588194451..00000000000 --- a/packages/bridge-ui/src/utils/switchEthereumChain.spec.ts +++ /dev/null @@ -1,63 +0,0 @@ -import type { Ethereum } from '@wagmi/core'; -import type { Chain } from '../domain/chain'; -import { ethers } from 'ethers'; -import { switchEthereumChain } from './switchEthereumChain'; - -const ethereum = { - request: jest.fn(), -}; - -const chain = { - id: 1, - name: 'Ethereum Mainnet', - rpc: 'rpc', - explorerUrl: '', - bridgeAddress: '', - headerSyncAddress: '', -} as Chain; - -describe('switchEthereumChain()', () => { - beforeEach(() => { - jest.resetAllMocks(); - }); - - it('should switchEthereumChain switches to the correct chain', async () => { - await switchEthereumChain(ethereum as unknown as Ethereum, chain); - expect(ethereum.request).toHaveBeenCalledWith({ - method: 'wallet_switchEthereumChain', - params: [{ chainId: '0x1' }], - }); - }); - - it('should addChain when ethereum thros chain not exist error', async () => { - class EthereumError extends Error { - code: number; - constructor(code) { - super(); - this.code = code; - } - } - - ethereum.request.mockImplementationOnce(() => { - const error = new EthereumError(4902); - throw error; - }); - - await switchEthereumChain(ethereum as unknown as Ethereum, chain); - expect(ethereum.request).toHaveBeenCalledWith({ - method: 'wallet_addEthereumChain', - params: [ - { - chainId: ethers.utils.hexValue(chain.id), - chainName: chain.name, - rpcUrls: [chain.rpc], - nativeCurrency: { - symbol: 'ETH', - decimals: 18, - name: 'Ethereum', - }, - }, - ], - }); - }); -}); diff --git a/packages/bridge-ui/src/utils/switchEthereumChain.ts b/packages/bridge-ui/src/utils/switchEthereumChain.ts deleted file mode 100644 index 554a86b2404..00000000000 --- a/packages/bridge-ui/src/utils/switchEthereumChain.ts +++ /dev/null @@ -1,36 +0,0 @@ -import type { Ethereum } from '@wagmi/core'; -import { ethers } from 'ethers'; -import type { Chain } from '../domain/chain'; - -export const switchEthereumChain = async (ethereum: Ethereum, chain: Chain) => { - try { - await ethereum.request({ - method: 'wallet_switchEthereumChain', - params: [{ chainId: ethers.utils.hexValue(chain.id) }], - }); - } catch (switchError) { - // This error code indicates that the chain has not been added to MetaMask. - if ( - switchError.code === 4902 || - switchError?.data?.originalError?.code === 4902 - ) { - await ethereum.request({ - method: 'wallet_addEthereumChain', - params: [ - { - chainId: ethers.utils.hexValue(chain.id), - chainName: chain.name, - rpcUrls: [chain.rpc], - nativeCurrency: { - symbol: 'ETH', - decimals: 18, - name: 'Ethereum', - }, - }, - ], - }); - } else { - throw switchError; - } - } -};