Hey guys,
I’m trying to set up my dapp such that it connects to the Polygon Mainnet by default, and if the user is not connected to the mainnet, then I’d like to prompt them to do so.
This is my current setup:
try {
setAuthError(null);
setIsAuthenticating(true);
// Enable web3 to get user address and chain
await enableWeb3({ throwOnError: true, provider });
const { account, chainId } = Moralis;
if (!account) {
throw new Error('Connecting to chain failed, as no connected account was found');
}
if (!chainId) {
throw new Error('Connecting to chain failed, as no connected chain was found');
}
// Get message to sign from the auth api
const { message } = await Moralis.Cloud.run('requestMessage', {
address: account,
chain: parseInt(chainId, 16),
networkType: 'evm',
});
// Authenticate and login via parse
await authenticate({
provider,
signingMessage: message,
throwOnError: true,
});
onClose();
I tried replacing chain: parseInt(chainId, 16),
with await authenticate({ chainId: 137 })
as suggested here: https://v1docs.moralis.io/moralis-dapp/users/web3-login/walletconnect however when I do this I’m not able to authenticate at all.
How can I set default chainID, and prompt users to switch networks if they are on the wrong chain?
Thanks!