Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: switchNetwork - invalid chain ID error parsing #3847

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .changeset/nervous-lions-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-utils': patch
'@reown/appkit': patch
'@reown/appkit-adapter-bitcoin': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-cdn': patch
'@reown/appkit-cli': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
'@reown/appkit-wallet-button': patch
---

Fixes issue where switching to an unrecognized chain in MM mobile resulted in INVALID_CHAIN error which was not parsed for wallet_addEthereumChain request
1 change: 1 addition & 0 deletions packages/adapters/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ export class EthersAdapter extends AdapterBlueprint {
} catch (switchError: any) {
if (
switchError.code === WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID ||
switchError.code === WcConstantsUtil.ERROR_INVALID_CHAIN_ID ||
switchError.code === WcConstantsUtil.ERROR_CODE_DEFAULT ||
switchError?.data?.originalError?.code === WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID
) {
Expand Down
91 changes: 65 additions & 26 deletions packages/adapters/ethers/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ describe('EthersAdapter', () => {
})

describe('EthersAdapter - switchNetwork', () => {
const errorCodes = [
WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID,
WcConstantsUtil.ERROR_INVALID_CHAIN_ID,
WcConstantsUtil.ERROR_CODE_DEFAULT,
{ data: { originalError: { code: WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID } } }
]

it('should switch network with Auth provider', async () => {
await adapter.switchNetwork({
caipNetwork: mockCaipNetworks[0],
Expand All @@ -354,42 +361,74 @@ describe('EthersAdapter', () => {
})
})

it('should add Ethereum chain with external provider and use chain default', async () => {
// Mock request so that switchEthereumChain throws with code: WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID
errorCodes.forEach(errorCode => {
it(`should add Ethereum chain when switch fails with error code: ${JSON.stringify(
errorCode
)}`, async () => {
// Mock request so that switchEthereumChain throws with the current error code
vi.mocked(mockProvider.request).mockImplementation(request => {
if (request.method === 'wallet_switchEthereumChain') {
if (typeof errorCode === 'object') {
const error = new Error('Chain switch failed')
Object.assign(error, errorCode)
throw error
}
throw new ErrorWithCode('Chain switch failed', errorCode)
}
return Promise.resolve(null)
})

const mockCaipNetwork = mockCaipNetworks[0]
await adapter.switchNetwork({
caipNetwork: mockCaipNetwork,
provider: mockProvider,
providerType: 'EXTERNAL'
})

expect(mockProvider.request).toHaveBeenCalledWith({
method: 'wallet_addEthereumChain',
params: [
{
chainId: '0x1',
rpcUrls: [mockCaipNetwork.rpcUrls['chainDefault']?.http[0]],
chainName: 'Ethereum',
iconUrls: ['ba0ba0cd-17c6-4806-ad93-f9d174f17900'],
nativeCurrency: {
name: 'Ether',
decimals: 18,
symbol: 'ETH'
},
blockExplorerUrls: ['https://etherscan.io']
}
]
})
})
})

it('should throw error when adding chain fails', async () => {
// Mock switch request to fail
vi.mocked(mockProvider.request).mockImplementation(request => {
if (request.method === 'wallet_switchEthereumChain') {
throw new ErrorWithCode(
'Unrecognized chain ID',
'Chain switch failed',
WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID
)
}
// Mock add chain to also fail
if (request.method === 'wallet_addEthereumChain') {
throw new Error('Chain is not supported')
}
return Promise.resolve(null)
})

const mockCaipNetwork = mockCaipNetworks[0]
await adapter.switchNetwork({
caipNetwork: mockCaipNetwork,
provider: mockProvider,
providerType: 'EXTERNAL'
})

expect(mockProvider.request).toHaveBeenCalledWith({
method: 'wallet_addEthereumChain',
params: [
{
chainId: '0x1',
rpcUrls: [mockCaipNetwork.rpcUrls['chainDefault']?.http[0]],
chainName: 'Ethereum',
iconUrls: ['ba0ba0cd-17c6-4806-ad93-f9d174f17900'],
nativeCurrency: {
name: 'Ether',
decimals: 18,
symbol: 'ETH'
},
blockExplorerUrls: ['https://etherscan.io']
}
]
})
await expect(
adapter.switchNetwork({
caipNetwork: mockCaipNetwork,
provider: mockProvider,
providerType: 'EXTERNAL'
})
).rejects.toThrow('Chain is not supported')
})

it('should call setDefaultChain and request from provider for WALLET_CONNECT', async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/adapters/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ export class Ethers5Adapter extends AdapterBlueprint {
} catch (switchError: any) {
if (
switchError.code === WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID ||
switchError.code === WcConstantsUtil.ERROR_INVALID_CHAIN_ID ||
switchError.code === WcConstantsUtil.ERROR_CODE_DEFAULT ||
switchError?.data?.originalError?.code === WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID
) {
Expand Down
1 change: 0 additions & 1 deletion packages/appkit-utils/exports/ethers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from '../src/ethers/EthersConstantsUtil.js'
export * from '../src/ethers/EthersHelpersUtil.js'

export type * from '../src/ethers/EthersTypesUtil.js'
4 changes: 0 additions & 4 deletions packages/appkit-utils/src/ethers/EthersConstantsUtil.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/appkit-utils/src/solana/SolanaConstantsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export const SolConstantsUtil = {
/**
* Mainnet program ID
*/
ERROR_CODE_UNRECOGNIZED_CHAIN_ID: 4902,
ERROR_CODE_DEFAULT: 5000,
DEFAULT_CHAIN: {
id: '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
caipNetworkId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
Expand Down
1 change: 1 addition & 0 deletions packages/appkit/src/universal-adapter/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export class UniversalAdapter extends AdapterBlueprint {
} catch (switchError: any) {
if (
switchError.code === WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID ||
switchError.code === WcConstantsUtil.ERROR_INVALID_CHAIN_ID ||
switchError.code === WcConstantsUtil.ERROR_CODE_DEFAULT ||
switchError?.data?.originalError?.code ===
WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID
Expand Down
3 changes: 2 additions & 1 deletion packages/appkit/src/utils/ConstantsUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const WcConstantsUtil = {
ERROR_CODE_UNRECOGNIZED_CHAIN_ID: 4902,
ERROR_CODE_DEFAULT: 5000
ERROR_CODE_DEFAULT: 5000,
ERROR_INVALID_CHAIN_ID: 32603
}
22 changes: 16 additions & 6 deletions packages/appkit/src/utils/HelpersUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,31 @@ export const WcHelpersUtil = {
]
case 'eip155':
return [
'personal_sign',
'eth_accounts',
'eth_requestAccounts',
'eth_sendRawTransaction',
'eth_sign',
'eth_signTransaction',
'eth_signTypedData',
'eth_signTypedData_v3',
'eth_signTypedData_v4',
'eth_sendRawTransaction',
'eth_sendTransaction',
'wallet_getCapabilities',
'wallet_sendCalls',
'wallet_showCallsStatus',
'personal_sign',
'wallet_switchEthereumChain',
'wallet_addEthereumChain',
'wallet_getPermissions',
'wallet_requestPermissions',
'wallet_registerOnboarding',
'wallet_watchAsset',
'wallet_scanQRCode',
// EIP-5792
'wallet_getCallsStatus',
'wallet_showCallsStatus',
'wallet_sendCalls',
'wallet_getCapabilities',
// EIP-7715
'wallet_grantPermissions',
'wallet_revokePermissions',
'wallet_switchEthereumChain',
//EIP-7811
'wallet_getAssets'
]
Expand Down
Loading