javascript - 如何使用 --constructor-args 参数运行 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);
});
```
解决方案
const Token = await ethers.getContractFactory("Test01");
const token = await Token.deploy();
Token
(大写 T)是ContractFactory
. 根据文档,您可以将构造函数参数传递给deploy()
方法。
例如,如果您的 Solidity 构造函数采用 abool
和 astring
constructor(bool _foo, string memory _hello) {
}
这将是 JS 片段:
const token = await Token.deploy(true, "hello");
推荐阅读
- python - 如何在 tkinter 中根据不同大小的计算机屏幕放置小部件
- python - BeautifulSoup 没有读取相同的 HTML 源代码
- java - 选择 FromDate 时出错
- angular - Jasmine 单元测试用例中的模拟窗口对象
- python - Wagtail“预取”多个页面的图像网址
- python - Pandas 系列在系列对象的开头添加索引和值
- android - Android Studio BroadcastReceiver 在 Android 6.0 中不工作
- python - django - 尽管声明了默认值,但此字段是必需的错误
- if-statement - Azure 数据工厂中的 IF 表达式
- c - 如何在 for 循环中使用 scanf 以使其在第一次尝试时不会停止循环?