简体   繁体   中英

Deploy a smart contract with constructor in Truffle

I want to deploy my contract which contain a constructor (2 account address and a value P), in Remix, I put manually the address of both account and P value, but in truffle, I edited manually the 2_deploy_contracts.js file as follow:

Contract:

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);};

Thanks in advance for help.

You have to declare and initialize these parameters:
var account1='0x...';
var account2='0x...';
var P=...;
deployer.deploy(contract, account1, account2, P);

so i was facing the similar problem. And there are few things to be noted.

  1. In your contract, you should declare the contracts, constructer something like this: Here Coin and Reward are my contracts.
constructor(Coin _coinContractsAddress, Reward _rewardContractAddress){
    coin_ = _coinContractsAddress;
    reward_ = _rewardContractAddress;
}
  1. Now this was for the contract. Changes in Deployment file should be made like this:

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. Now run truffle migrations and it will be successfully deployed. truffle migrate --reset

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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