Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  159] [ 4]  / answers: 1 / hits: 10121  / 3 Years ago, thu, october 28, 2021, 12:00:00

I'm trying to run a script with Hardhat to deploy a contract which has constructor arguments. When I run npx hardhat run scriptsdeploy.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);
});
```

More From » solidity

 Answers
7
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");

[#727] Tuesday, October 19, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pranavrorys

Total Points: 466
Total Questions: 87
Total Answers: 115

Location: Barbados
Member since Sun, Nov 27, 2022
2 Years ago
pranavrorys questions
Fri, May 27, 22, 00:00, 2 Years ago
Sat, May 30, 20, 00:00, 4 Years ago
Fri, Dec 20, 19, 00:00, 5 Years ago
Fri, Oct 11, 19, 00:00, 5 Years ago
;