在松露中向迁移脚本添加构造函数参数

adding a constructor argument to migration script in truffle

我在这里得到了一个非常简单的 ERC-20 合约:

pragma solidity 0.8.1;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract SonnyCoin is ERC20 {
      constructor(uint256 initialValue) public ERC20("SonnyCoin", "SCN") {
             _mint(msg.sender, initialValue);
      }
}

然后我的合约迁移代码如下:

const SonnyCoin = artifacts.require("SonnyCoin");
const web3 = require("web3");
const initialValue = web3.utils.toWei("1", "ether");

module.exports = function(deployer) {
  deployer.deploy(SonnyCoin(initialValue))
  //I've also tried deployer.deploy(SonnyCoin({value: initialValue}))
}

我只想添加参数以使其成为动态发行合同,但我不确定我遗漏了什么,我在这里查看了有关迁移脚本及其参数的松露文档,看来我'我正在做 documentation dictates 的事情,但我显然遗漏了一个关键部分。感谢任何有关向构造函数添加参数的帮助。

这里是document,如何为构造函数传递参数。

myContract.deploy({
   data: '0x12345...',
   arguments: [123, 'My String']
})

在“参数”部分,我们可以传递参数。

这实际上是我的语法错误,与您可能认为的将参数传递给合同构造器的语法相反

module.exports = function(deployer) {
  deployer.deploy(SonnyCoin(arg1))
}

您实际上需要像这样将参数传递给构造函数:

module.exports = function(deployer) {
  deployer.deploy(SonnyCoin, arg1)
}

来源:here