-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fixed switching network failure in coinbaseWallet.
- Loading branch information
Showing
8 changed files
with
78 additions
and
87 deletions.
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
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"] | ||
} |
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,5 @@ | ||
--- | ||
'@totejs/walletkit': patch | ||
--- | ||
|
||
Fixed switching network failure in coinbaseWallet. |
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
59 changes: 59 additions & 0 deletions
59
packages/walletkit/src/wallets/coinbaseWallet/connector.ts
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,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); | ||
} | ||
} | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.