Hardhat Scripts – How to Use Multiple Networks in a Single Script

hardhathardhat-deploy

I have a piece of code that deploys a contract. I would like to deploy this contract to two networks (lets say Goerli and MainnetEthereum). Inside a loop in hardhat script I can change the network using the following code:

for (const currNetwork of networksToDeploy) {
   hre.changeNetwork(currNetwork);
   const DToken = await DToken.deploy(...constructorArgs)
}

The network doesn't change, it double deploys it in Goerli (the first network). The loop is changing correctly but the provider on DToken.deploy stays the same. Any ideas on how to change that?

Best Answer

You need to instantiate two Wallets.

https://docs.ethers.org/v5/api/signer/#Wallet-constructor

To create a Wallet you need to provide a private key and a provider (if desired) to instantiate a wallet.

new ethers.Wallet( privateKey [ , provider ] )

Here is an example code to instantiate two wallets:

const walletGoerli = new ethers.Wallet(privateKey, goerliProvider);
const walletMumbai = new ethers.Wallet(privateKey, mumbaiProvider);
Related Topic