繁体   English   中英

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

[英]Deploy a smart contract with constructor in Truffle

我想部署包含构造函数(2 个帐户地址和一个值 P)的合同,在 Remix 中,我手动输入了帐户和 P 值的地址,但在松露中,我手动编辑了 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. 现在这是为了合同。 部署文件的更改应该是这样的:

部署合同.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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM