Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run Hardhat with the --constructor-args parameter?

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);
    });
    ```
like image 973
Frank Furter Avatar asked Jun 16 '26 23:06

Frank Furter


1 Answers

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");
like image 149
Petr Hejda Avatar answered Jun 18 '26 14:06

Petr Hejda