在 Truffle 中使用构造函数部署智能合约

Deploy a smart contract with constructor in Truffle

我想部署我的合约,它包含一个构造函数(2 个账户地址和一个值 P),在 Remix 中,我手动输入了账户地址和 P 值,但在 truffle 中,我手动编辑了 2_deploy_contracts.js 文件如下:

合同:

constructor(address payable _account1, address payable _account2, uint _P) public {account1 = _account1;account2 = _account2; P = _P;}

2_deploy_contracts.js:

var contract = artifacts.require("contract");
module.exports = function(deployer)  {
deployer.deploy(contract, account1, account2, P);};

在此先感谢您的帮助。

您必须声明并初始化这些参数:
var account1='0x...';
var account2='0x...';
var P=...;
deployer.deploy(contract, account1, account2, P);

所以我遇到了类似的问题。还有一些需要注意的地方。

  1. 在你的合同中,你应该像这样声明合同,构造函数: 这里 CoinReward 是我的合同。
constructor(Coin _coinContractsAddress, Reward _rewardContractAddress){
    coin_ = _coinContractsAddress;
    reward_ = _rewardContractAddress;
}
  1. 现在这是为了合同。 应像这样更改部署文件:

deploy-contracts.js

module.exports = async function() {
    await deployer.deploy(Coin);
    const coin_ = await Coin.deployed();

    await deployer.deploy(Reward);
    const reward_ = await Reward.deployed();
    //Now we are going to deploy these contracts, address in the 3rd Contract address named Bank.
    await deployer.deploy(Bank, coin_.address, reward_.address);
    //Point to note is we are deploying the Bank contract and in its constructor at same time we are passing the coin_.address which is the deployed contract address and reward_.address.
}
  1. 现在 运行 truffle 迁移,它将成功部署。 truffle migrate --reset