Skip to content

Commit 1b39084

Browse files
Merge pull request #2291 from Web3Auth/feat/wc-chainid-fix
fix chainId format
2 parents 859b0ba + f81d4bd commit 1b39084

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

packages/no-modal/src/connectors/wallet-connect-v2-connector/WalletConnectV2Provider.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
IEthChainSwitchHandlers,
1212
} from "../../providers/ethereum-provider";
1313
import { createSolanaJsonRpcClient as createSolJsonRpcClient, createSolanaMiddleware } from "../../providers/solana-provider";
14+
import { formatChainId } from "./utils";
1415
import { getAccounts, getEthProviderHandlers, getSolProviderHandlers, switchChain } from "./walletConnectV2Utils";
1516

1617
export type WalletConnectV2ProviderConfig = BaseProviderConfig;
@@ -181,14 +182,13 @@ export class WalletConnectV2Provider extends BaseProvider<BaseProviderConfig, Wa
181182

182183
if (event.name === "chainChanged") {
183184
if (!data) return;
184-
const connectedChainId = data as number;
185-
const connectedHexChainId = `0x${connectedChainId.toString(16)}`;
186-
185+
const connectedChainId = data as number | string;
186+
const formattedChainId = formatChainId(connectedChainId);
187187
// Check if chainId changed and trigger event
188188
const { currentChain } = this;
189-
if (connectedHexChainId && currentChain.chainId !== connectedHexChainId) {
189+
if (formattedChainId && currentChain.chainId !== formattedChainId) {
190190
// Handle rpcUrl update
191-
await this.setupEngine(connector, connectedHexChainId);
191+
await this.setupEngine(connector, formattedChainId);
192192
}
193193
}
194194
});

packages/no-modal/src/connectors/wallet-connect-v2-connector/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ export const isChainIdSupported = (chainNamespace: ChainNamespaceType, chainID:
1212
const isSupported = supportedNamespaces[chainNamespace].chains?.includes(wcChainNamespace);
1313
return !!isSupported;
1414
};
15+
16+
export const formatChainId = (chainId: number | string) => {
17+
if (typeof chainId === "number") {
18+
return `0x${chainId.toString(16)}`;
19+
} else if (typeof chainId === "string") {
20+
return chainId.startsWith("0x") ? chainId : `0x${parseInt(chainId, 10).toString(16)}`;
21+
}
22+
throw new Error(`Invalid chainId: ${chainId}`);
23+
};

0 commit comments

Comments
 (0)