I made a dApp that allows users to collect token rewards on multiple different chains. For the sake of an example say it is Rinkeby
and Mumbai
.
In order to get signer and provider to execute my contract functions with ethers.js
I utilise this code:
const provider = new ethers.providers.Web3Provider(web3.currentProvider);
const wallet = new ethers.Wallet(privateKey, provider);
const signer = wallet.provider.getSigner(wallet.address);
It gets the current provider and using a contract function allows to claim the rewards on user's current chain, Rinkeby
. Then if the user wants to collect rewards on the other chain Mumbai
, he has to change the network in Metamask. I want it to be done automatically when user presses "Claim"
button.
How do I change the user's network programmatically?