Skip to content

Commit 0d12c98

Browse files
Fix/switch to correct network (#312)
* test: `useWallet()` update test to check that the chain ID is converted to hexadecimal before requesting a network change * fix: `switchToCorrectNetwork()` change chain ID to hexadecimal before requesting network switch * changeset Co-authored-by: Dhaiwat Pandya <[email protected]>
1 parent 1be5a6d commit 0d12c98

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

.changeset/spotty-lamps-compete.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web3-ui/hooks': minor
3+
---
4+
5+
The network/chain ID was not being converted to hexadecimal before requesting a switch from MetaMask. This was only a problem for networks with chain IDs greater than 9, like the Mumbai Testnet (80001).

packages/hooks/src/hooks/useWallet.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('useWallet tests', () => {
5454
expect(requestArgs).toStrictEqual({
5555
method: 'wallet_switchEthereumChain',
5656
// this passes (for high number networks)
57-
params: [{ chainId: `0x${testNetwork}` }],
57+
params: [{ chainId: `0x${testNetwork.toString(16)}` }],
5858
// The test should probably look like this
5959
// if we are in fact converting network numbers to hex
6060
// but fails with the current implementation

packages/hooks/src/hooks/useWallet.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ export function useWallet() {
4040
try {
4141
console.log('chainId', { chainId });
4242
console.log('requiredNetwork', { network });
43+
const hexNetwork = network?.toString(16);
4344
// check if the chain to connect to is installed
4445
await window.ethereum.request({
4546
method: 'wallet_switchEthereumChain',
46-
params: [{ chainId: `0x${network}` }], // chainId must be in hexadecimal numbers
47+
params: [{ chainId: `0x${hexNetwork}` }], // chainId must be in hexadecimal numbers
4748
});
4849
} catch (error) {
4950
console.error(error);

0 commit comments

Comments
 (0)