Skip to content

Commit

Permalink
Merge branch 'main' of github.com:WalletConnect/web3modal into feat/d…
Browse files Browse the repository at this point in the history
…efault-account-type-option
  • Loading branch information
zoruka committed Jan 10, 2025
2 parents 55879a7 + 427dde3 commit 5e140fa
Show file tree
Hide file tree
Showing 28 changed files with 573 additions and 621 deletions.
23 changes: 23 additions & 0 deletions .changeset/breezy-candles-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@reown/appkit-adapter-solana': patch
'@reown/appkit-utils': patch
'@reown/appkit-adapter-bitcoin': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': 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
---

Abstracts Connectors management in Solana Adapter
63 changes: 29 additions & 34 deletions packages/adapters/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,43 +528,38 @@ export class EthersAdapter extends AdapterBlueprint {
}
}

public async switchNetwork(params: AdapterBlueprint.SwitchNetworkParams): Promise<void> {
public override async switchNetwork(params: AdapterBlueprint.SwitchNetworkParams): Promise<void> {
const { caipNetwork, provider, providerType } = params
if (providerType === 'WALLET_CONNECT') {
;(provider as UniversalProvider).setDefaultChain(String(`eip155:${String(caipNetwork.id)}`))
} else if (providerType === 'AUTH') {
const authProvider = provider as W3mFrameProvider
await authProvider.switchNetwork(caipNetwork.id)
await authProvider.connect({
chainId: caipNetwork.id,
preferredAccountType: OptionsController.state.defaultAccountTypes.eip155

if (providerType === 'AUTH' || providerType === 'WALLET_CONNECT') {
await super.switchNetwork(params)

return
}

try {
await (provider as Provider).request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: EthersHelpersUtil.numberToHexString(caipNetwork.id) }]
})
} else {
try {
await (provider as Provider).request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: EthersHelpersUtil.numberToHexString(caipNetwork.id) }]
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (switchError: any) {
if (
switchError.code === WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID ||
switchError.code === WcConstantsUtil.ERROR_CODE_DEFAULT ||
switchError?.data?.originalError?.code ===
WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID
) {
try {
await EthersHelpersUtil.addEthereumChain(provider as Provider, caipNetwork)
} catch (e) {
console.warn('Could not add chain to wallet', e)
}
} else if (
providerType === 'ANNOUNCED' ||
providerType === 'EXTERNAL' ||
providerType === 'INJECTED'
) {
throw new Error('Chain is not supported')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (switchError: any) {
if (
switchError.code === WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID ||
switchError.code === WcConstantsUtil.ERROR_CODE_DEFAULT ||
switchError?.data?.originalError?.code === WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID
) {
try {
await EthersHelpersUtil.addEthereumChain(provider as Provider, caipNetwork)
} catch (e) {
console.warn('Could not add chain to wallet', e)
}
} else if (
providerType === 'ANNOUNCED' ||
providerType === 'EXTERNAL' ||
providerType === 'INJECTED'
) {
throw new Error('Chain is not supported')
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/adapters/ethers/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ const mockWalletConnectProvider = {
const mockAuthProvider = {
connect: vi.fn(),
disconnect: vi.fn(),
switchNetwork: vi.fn()
switchNetwork: vi.fn(),
getUser: vi.fn()
} as unknown as W3mFrameProvider

const mockNetworks = [mainnet]
Expand Down Expand Up @@ -312,8 +313,8 @@ describe('EthersAdapter', () => {
providerType: 'AUTH'
})

expect(mockAuthProvider.switchNetwork).toHaveBeenCalledWith(1)
expect(mockAuthProvider.connect).toHaveBeenCalledWith({ chainId: 1 })
expect(mockAuthProvider.switchNetwork).toHaveBeenCalledWith('eip155:1')
expect(mockAuthProvider.getUser).toHaveBeenCalledWith({ chainId: 'eip155:1' })

Check failure on line 317 in packages/adapters/ethers/src/tests/client.test.ts

View workflow job for this annotation

GitHub Actions / test

src/tests/client.test.ts > EthersAdapter > EthersAdapter - switchNetwork > should switch network with Auth provider

AssertionError: expected "spy" to be called with arguments: [ { chainId: 'eip155:1' } ] Received: 1st spy call: Array [ Object { "chainId": "eip155:1", + "preferredAccountType": "smartAccount", }, ] Number of calls: 1 ❯ src/tests/client.test.ts:317:40
})

it('should add Ethereum chain with external provider and use chain default', async () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/adapters/ethers/src/tests/mocks/AuthConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@ export const mockAuthConnector = {
disconnect: vi.fn(),
switchNetwork: vi.fn(),
rejectRpcRequests: vi.fn(),
request: vi.fn()
request: vi.fn(),
getUser: vi.fn().mockResolvedValue({
address: '0x1234567890123456789012345678901234567890',
chainId: 1,
smartAccountDeployed: true,
preferredAccountType: 'eoa',
accounts: []
})
}
57 changes: 26 additions & 31 deletions packages/adapters/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,39 +531,34 @@ export class Ethers5Adapter extends AdapterBlueprint {
}
}

public async switchNetwork(params: AdapterBlueprint.SwitchNetworkParams): Promise<void> {
public override async switchNetwork(params: AdapterBlueprint.SwitchNetworkParams): Promise<void> {
const { caipNetwork, provider, providerType } = params
if (providerType === 'WALLET_CONNECT') {
;(provider as UniversalProvider).setDefaultChain(String(`eip155:${String(caipNetwork.id)}`))
} else if (providerType === 'AUTH') {
const authProvider = provider as W3mFrameProvider
await authProvider.switchNetwork(caipNetwork.id)
await authProvider.connect({
chainId: caipNetwork.id,
preferredAccountType: OptionsController.state.defaultAccountTypes.eip155

if (providerType === 'AUTH' || providerType === 'WALLET_CONNECT') {
await super.switchNetwork(params)

return
}

try {
await (provider as Provider).request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: EthersHelpersUtil.numberToHexString(caipNetwork.id) }]
})
} else {
try {
await (provider as Provider).request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: EthersHelpersUtil.numberToHexString(caipNetwork.id) }]
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (switchError: any) {
if (
switchError.code === WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID ||
switchError.code === WcConstantsUtil.ERROR_CODE_DEFAULT ||
switchError?.data?.originalError?.code ===
WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID
) {
await EthersHelpersUtil.addEthereumChain(provider as Provider, caipNetwork)
} else if (
providerType === 'ANNOUNCED' ||
providerType === 'EXTERNAL' ||
providerType === 'INJECTED'
) {
throw new Error('Chain is not supported')
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (switchError: any) {
if (
switchError.code === WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID ||
switchError.code === WcConstantsUtil.ERROR_CODE_DEFAULT ||
switchError?.data?.originalError?.code === WcConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID
) {
await EthersHelpersUtil.addEthereumChain(provider as Provider, caipNetwork)
} else if (
providerType === 'ANNOUNCED' ||
providerType === 'EXTERNAL' ||
providerType === 'INJECTED'
) {
throw new Error('Chain is not supported')
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/adapters/ethers5/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ const mockWalletConnectProvider = {
const mockAuthProvider = {
connect: vi.fn(),
disconnect: vi.fn(),
switchNetwork: vi.fn()
switchNetwork: vi.fn(),
getUser: vi.fn()
} as unknown as W3mFrameProvider

const mockNetworks = [mainnet]
Expand Down Expand Up @@ -304,8 +305,8 @@ describe('Ethers5Adapter', () => {
providerType: 'AUTH'
})

expect(mockAuthProvider.switchNetwork).toHaveBeenCalledWith(1)
expect(mockAuthProvider.connect).toHaveBeenCalledWith({ chainId: 1 })
expect(mockAuthProvider.switchNetwork).toHaveBeenCalledWith('eip155:1')
expect(mockAuthProvider.getUser).toHaveBeenCalledWith({ chainId: 'eip155:1' })

Check failure on line 309 in packages/adapters/ethers5/src/tests/client.test.ts

View workflow job for this annotation

GitHub Actions / test

src/tests/client.test.ts > Ethers5Adapter > Ethers5Adapter - switchNetwork > should switch network with Auth provider

AssertionError: expected "spy" to be called with arguments: [ { chainId: 'eip155:1' } ] Received: 1st spy call: Array [ Object { "chainId": "eip155:1", + "preferredAccountType": "smartAccount", }, ] Number of calls: 1 ❯ src/tests/client.test.ts:309:40
})
})

Expand Down
Loading

0 comments on commit 5e140fa

Please sign in to comment.