Metamask Contract Deployment – How to Deploy Contract Using Second Address in Wallet with Hardhat

contract-deploymenthardhat-deploymetamask

I'm trying to deploy a contract using the second address in my wallet (Metamask), how can I achieve such thing from hardhat?
This is my hardhat config:

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
require("@nomiclabs/hardhat-ethers");
require('@openzeppelin/hardhat-upgrades');
require("@nomiclabs/hardhat-etherscan");


module.exports = {
  solidity: "0.8.2",
  networks: {
    rinkeby: {
      url: `https://eth-rinkeby.alchemyapi.io/v2/${process.env.ALCHEMY_KEY}`,
      accounts: {mnemonic: process.env.MNEMONIC_WALLET},
      gas: 2100000,
      gasPrice: 80000000000
    }
  },
  etherscan: {
    apiKey: process.env.ETHERSCAN_API_KEY
  }
};

In this way I deploy my contract using the first address of my Metamask wallet, how can I deploy the contract using the second one?

Best Answer

This is actually a question about ethers.js, not hardhat.

Let's use my Greeter deployer as a base to start from:

task("deploy:Greeter")
  .addParam("greeting", "Say hello, be nice")
  .setAction(async function (taskArguments: TaskArguments, { ethers }) {
    const signers: SignerWithAddress[] = await ethers.getSigners();
    const greeterFactory: Greeter__factory = <Greeter__factory>await ethers.getContractFactory("Greeter");
    const greeter: Greeter = <Greeter>await greeterFactory.connect(signers[0]).deploy(taskArguments.greeting);
    await greeter.deployed();
    console.log("Greeter deployed to: ", greeter.address);
  });

So to change the deployer account, you use the connect method and then call the deploy function.

Related Topic