【问题标题】:adding a constructor argument to migration script in truffle在松露中向迁移脚本添加构造函数参数
【发布时间】:2021-12-11 02:58:58
【问题描述】:

我在这里得到了一份非常简单的 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}))
}

我只想添加参数以使其成为动态发行合同,但我不确定我缺少什么,我查看了有关迁移脚本及其参数的 truffle 文档here,看来我正在做documentation dictates 是什么,但我显然错过了一个关键部分。对于向构造函数添加参数的任何帮助表示赞赏。

【问题讨论】:

    标签: ethereum solidity truffle


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      这实际上是我的一个语法错误,与您可能认为将参数传递给这样的合同委托人的语法相反

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

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

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

      来源:here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-21
        相关资源
        最近更新 更多