8

I'm trying to run a script with Hardhat to deploy a contract which has constructor arguments. When I run npx hardhat run scripts\deploy.js --network rinkeby I get the error:

Error: missing argument: in Contract constructor (count=0, expectedCount=7, code=MISSING_ARGUMENT, version=contracts/5.5.0)

I've tried to use the --constructor-args parameter but get another error:

Error HH305: Unrecognized param --constructor-args

All the references I've found to constructor-args suggests that it's only available as part of hardhat verify, not hardhat run but if that's the case how can I pass arguments when deploying?

Updated to include deploy script

// deploy.js

async function main() {
    const [deployer] = await ethers.getSigners();

    console.log('%c \n Deploying contracts with the account:', 'color:', deployer.address );

    console.log('%c \n Account balance:', 'color:', (await deployer.getBalance()).toString() );

    const Token = await ethers.getContractFactory("Test01");
    const token = await Token.deploy();

    console.log('%c \n Token address:', 'color:', token.address );
    
    
}

main()
    .then( () => process.exit(0) )
    .catch( (error) => {
        console.error(error);
        process.exit(1);
    });
    ```
Frank Furter
  • 508
  • 1
  • 6
  • 15

2 Answers2

11
const Token = await ethers.getContractFactory("Test01");
const token = await Token.deploy();

Token (capital T) is an instance of the ContractFactory. As per the docs, you can pass the constructor arguments to the deploy() method.

For example, if your Solidity constructor takes a bool and a string

constructor(bool _foo, string memory _hello) {
}

this would be the JS snippet:

const token = await Token.deploy(true, "hello");
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • I'm running into the same issue, my constructor does have all parameters in the deploy function but I don't know how to pass the constructor parameters in the command line. Any help? – Samuel Villegas Nov 10 '21 at 13:39
  • If my constructor contained a uint256, how would I pass that parameter to .deploy() ? – SocaBlood Dec 08 '21 at 03:48
  • 1
    @SocaBlood You can pass its `string` value - `deploy("1")`. Or [BigNumber](https://docs.ethers.io/v5/api/utils/bignumber/) object - `deploy(BigNumber.from(1))` – Petr Hejda Dec 08 '21 at 10:09
  • What happens if the smart contract takes constructor params and also an initializer? I am using Hardhat to deploy and I don't know how to do it. – Falcon Stakepool Nov 11 '22 at 06:14
  • 1
    @FalconStakepool Initializer is commonly used in implementation contracts that are used by proxy contracts - in that case it's often not a good practice to declare a constructor in the implementation contract. When you execute the constructor, the storage changes are stored in the implementation contract directly - but when you execute the initializer through a proxy, the storage changes are stored in the proxy contract... Please post a separate question with your code example and description of the goal you're trying to achieve, maybe there's an easier way to achieve the goal. – Petr Hejda Nov 11 '22 at 08:50
2

This happened when i had arguments in the constructor and i did not include them in the deploy function.

const token = await deploy("Token", {
        from: deployer,
        args: [],
        log: true,
*//list all the arguments here ie. adresses*
    });
vimuth
  • 5,064
  • 33
  • 79
  • 116