enableWeb3 with specific chainId in browser


I’m building a multi-chain dApp and would like to fetch data in similar smart contracts but on different chains. One is one Goerli, the other is on Mumbai, and the last one is on Bsc Testnet.

I have been digging within the moralis SDK repository and the only clue I’ve found to achieve this is doing something like :

// chainIdDecimal is just the decimal value of a chainId, I also printed this to be sure I sent the right value
const web3 = await Moralis.enableWeb3({ chainId: chainIdDecimal });

I’ve also tried

const web3 = await Moralis.enableWeb3(chainIdDecimal);

But when I do the following

console.log(await web3.eth.net.getId()); 

It always prints the one from the Metamask… Any solution?

Thanks :wink:

if you want to read data from different chains for a smart contract, you can use runContractFunction where you don’t need to initialise any web3 instance: https://docs.moralis.io/moralis-server/web3-sdk/native#runcontractfunction

Hey are there any other solutions except from using web3 module directly, because from what I remember, MoralisWeb3Api is not yet usable on a local dev chain?

web3api is not available on local dev chain, you can test directly on a testnet

you could check what chain is in metamask, try to add a new chain, try to force a change in what chain is selected in metamask

or you could use a cloud function that can also have access to a web3 instance

Ok I will use Moralis speedy nodes and just instantiate a custom web3 :wink: Thanks for the help!