3

I am trying to deploy a contract as per documentation https://hardhat.org/guides/deploying.html#deploying-your-contracts Below is how i am deploying it

hardhat.config.js

require("@nomiclabs/hardhat-ethers");
require("@openzeppelin/hardhat-upgrades");
const fs = require("fs");
module.exports = {
    defaultNetwork: "hardhat",
    networks: {local: {
        url: "http://127.0.0.1:8545",
        from: "0x46b01b2888929537269342ce9454ca33873e33a9" // want this contract owner
    }},
    solidity: "0.8.1",
};


deploy.js

const { ethers } = require("hardhat");
(async () => {
    try {
        let ContractFactory = await ethers.getContractFactory("SomeContract");
        let contract = await ContractFactory.deploy();
        console.log("Contract address:", contract.address);
        console.log("Contract creation transaction:", contract.deployTransaction.hash);
    }catch(err) {
        console.log('Error deploying contract: ', err);
    }
})();

Well, in this way hardhat will deploy contract by using first signer. This is what i dont want... I want to specify a particular Address as singer/owner of the contract.

Is there any way to achieve this other than iterating over all signers() & set the signer i wanted as owner. Any help would be much appreciated.

3 Answers 3

3

Since we have provided from address in our hardhat config so we can easily access that address from network config & use as contract owner as below.

deploy.js

const { network,ethers } = require("hardhat");
(async () => {
    try {
        let ContractFactory = await ethers.getContractFactory("SomeContract");
        let contract_owner = await ethers.getSigner(network.config.from);
        let contract = await ContractFactory.connect(contract_owner).deploy();
        console.log("Contract address:", contract.address);
        console.log("Contract owner:", contract_owner);
        console.log("Contract creation transaction:", contract.deployTransaction.hash);
    }catch(err) {
        console.log('Error deploying contract: ', err);
    }
})();
2
  • 1
    This is not working! could you check?
    – Russo
    Jul 11, 2022 at 15:28
  • @Russo could you please add more context why it not works. I found this way more feasible as i did not need to expose/write private key for the signers. i can see your solution ethereum.stackexchange.com/a/131563/73114
    – Gobi Part
    Jul 13, 2022 at 3:39
0

Where do you keep a private key of your current signer? I have a file .secret with the corresponding private key.

If I want other signer, I simply replace the private key inside the .secret file.

There are better ways, but this is often the fastest when I am doing some tests, etc.

3
  • i don't want to use the private key of my address, i believe its not really needed as i am able to deploy contract via my first signer in signers() list so there could be a way to pass my desired address as contract owner.
    – Gobi Part
    Apr 8, 2022 at 9:11
  • In openZeppelin there is an Ownable.sol contact that your own contract could inherit. It also allows you to transfer ownership to any address you might like.
    – Sky
    Apr 9, 2022 at 9:52
  • Thanks Sky. I found a convenient way to do this. Please have a look at answer.
    – Gobi Part
    Apr 18, 2022 at 2:16
0

Set accounts

localhost: {
      url: "http://127.0.0.1:8545",
      gasPrice: ethers.utils.parseUnits("10", "gwei").toNumber(),
      gas: 7e6,
      accounts: [
        `${DEPLOYER_PRIVATE_KEY_no_0x}`,
        `${USER1PK_no_0x}`
      ],
}

Then send some ETH to your deployer address so you can deploy contracts:

  const accounts = await ethers.getSigners();
  await accounts[1].sendTransaction({ to: accounts[0], value: toWei(1000) });
  const balance0ETH = await accounts[0].getBalance();

But sending ETH is extra work... if you want to change the default Hardhat accounts:

    localhost: {
      url: "http://127.0.0.1:8545",
      gasPrice: ethers.utils.parseUnits("10", "gwei").toNumber(),
      gas: 7e6,
      accounts: {
        mnemonic: "patch alter unable artist hospital prize swear know faith steel frog gesture",
        path: "m/44'/60'/0'/0",
        initialIndex: 0,
        count: 20,
        passphrase: "",
      }
      //accounts: [privateKey1, privateKey2, ...]
      //accounts: { mnemonic: process.env.MNEMONIC_WALLET },
    },
    hardhat: {
      gasPrice: ethers.utils.parseUnits("10", "gwei").toNumber(),
      gas: 7e6,
      accounts: {
        mnemonic: "patch alter unable artist hospital prize swear know faith steel frog gesture",
        path: "m/44'/60'/0'/0",
        initialIndex: 0,
        count: 20,
        passphrase: "",
      }
    },

Then run npx hardhat node to start your local Ethereum network with those default accounts filled with some ETH balances:

Accounts
========
Account #0: 0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b (10000 ETH)

Account #1: 0x735113e044BFce4DebA5da7BfBc96A3e6A96F6Da (10000 ETH)

Account #2: 0x314c6751d8fE2d66b886a37F987F695753498b27 (10000 ETH)
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.