Fra*_*ter 8 javascript solidity hardhat
我正在尝试使用 Hardhat 运行脚本来部署具有构造函数参数的合约。当我运行时,npx hardhat run scripts\deploy.js --network rinkeby我收到错误:
Error: missing argument: in Contract constructor (count=0, expectedCount=7, code=MISSING_ARGUMENT, version=contracts/5.5.0)
我尝试使用 --constructor-args 参数,但出现另一个错误:
Error HH305: Unrecognized param --constructor-args
我发现的对 constructor-args 的所有引用都表明它只能作为Hardhat verify 的一部分,而不是Hardhat run 的一部分,但如果是这种情况,我如何在部署时传递参数?
更新以包含部署脚本
// 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);
});
```
Run Code Online (Sandbox Code Playgroud)
Pet*_*jda 11
const Token = await ethers.getContractFactory("Test01");
const token = await Token.deploy();
Run Code Online (Sandbox Code Playgroud)
Token(大写 T)是 的一个实例ContractFactory。根据文档,您可以将构造函数参数传递给该deploy()方法。
例如,如果您的 Solidity 构造函数采用 abool和 astring
constructor(bool _foo, string memory _hello) {
}
Run Code Online (Sandbox Code Playgroud)
这将是 JS 片段:
const token = await Token.deploy(true, "hello");
Run Code Online (Sandbox Code Playgroud)