简体   繁体   中英

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);
    });
    ```
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");

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 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM