Skip to content

Commit

Permalink
fix: Fixed switching network failure in coinbaseWallet.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenty22 committed Dec 18, 2023
2 parents fa46d6a + 53b65ec commit f2257ee
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 87 deletions.
11 changes: 11 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"mode": "pre",
"tag": "alpha",
"initialVersions": {
"example-nextjs": "0.0.1",
"example-vite": "0.0.1",
"@totejs/walletkit": "0.2.7",
"website": "0.0.1"
},
"changesets": ["stale-pumas-float"]
}
5 changes: 5 additions & 0 deletions .changeset/stale-pumas-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@totejs/walletkit': patch
---

Fixed switching network failure in coinbaseWallet.
1 change: 0 additions & 1 deletion packages/walletkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"rollup-plugin-peer-deps-external": "^2.2.4",
"typescript": "^5.3.2",
"vconsole": "^3.15.1",
"viem": "^1.19.9",
"vite": "^4.5.0",
"vite-plugin-dts": "^3.6.4",
"wagmi": "^0.12.19"
Expand Down
59 changes: 59 additions & 0 deletions packages/walletkit/src/wallets/coinbaseWallet/connector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { CoinbaseWalletConnector as WagmiCoinbaseWalletConnector } from 'wagmi/connectors/coinbaseWallet';
import { hexValue } from 'ethers/lib/utils.js';
import { ProviderRpcError, SwitchChainError, UserRejectedRequestError } from 'wagmi';

export class CoinbaseWalletConnector extends WagmiCoinbaseWalletConnector {
async switchChain(chainId: number) {
const provider = await this.getProvider();
const id = hexValue(chainId);

try {
await provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: id }],
});
return (
this.chains.find((x) => x.id === chainId) ?? {
id: chainId,
name: `Chain ${id}`,
network: `${id}`,
nativeCurrency: { name: 'Ether', decimals: 18, symbol: 'ETH' },
rpcUrls: { default: { http: [''] }, public: { http: [''] } },
}
);
} catch (error) {
const chain = this.chains.find((x) => x.id === chainId);
if (!chain) {
throw new Error(`Chain "${chainId}" not configured for connector "${this.id}".`);
}

// fix:
// We found if there is no corresponding network in coinbaseWallet, error code is -32603
// Indicates chain is not added to provider
if (
(error as ProviderRpcError).code === 4902 ||
(error as ProviderRpcError).code === -32603
) {
try {
await provider.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: id,
chainName: chain.name,
nativeCurrency: chain.nativeCurrency,
rpcUrls: [chain.rpcUrls.public?.http[0] ?? ''],
blockExplorerUrls: this.getBlockExplorerUrls(chain),
},
],
});
return chain;
} catch (error) {
throw new UserRejectedRequestError(error as Error);
}
}

throw new SwitchChainError(error as Error);
}
}
}
2 changes: 1 addition & 1 deletion packages/walletkit/src/wallets/coinbaseWallet/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getGlobalData } from '@/globalData';
import { Chain } from 'wagmi';
import { CoinbaseWalletConnector } from 'wagmi/connectors/coinbaseWallet';
import { CoinbaseWalletConnector } from './connector';
import { PartialWalletProps, WalletProps } from '..';
import { hasInjectedProvider } from '../utils';
import { CoinbaseWalletIcon, CoinbaseWalletTransparentIcon } from './icon';
Expand Down
1 change: 1 addition & 0 deletions packages/walletkit/src/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from './okxWallet';
export * from './mathWallet';
export * from './binanceWeb3Wallet';
export * from './coinbaseWallet';
export * from './coinbaseWallet/connector';
2 changes: 1 addition & 1 deletion packages/walletkit/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default defineConfig({
},
},
rollupOptions: {
external: ['react', 'react-dom', 'viem', 'wagmi', 'qrcode'],
external: ['react', 'react-dom', 'ethers', 'wagmi', 'qrcode'],
plugins: [peerDepsExternal(), nodeResolve()],
output: {
chunkFileNames: 'common.js',
Expand Down
84 changes: 0 additions & 84 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f2257ee

Please sign in to comment.