Contract-Development

讓 Truffle 部署一個合約並將其作為參數傳遞給另一個合約的建構子

  • September 2, 2018

我有兩個契約,contractA& contractB。我希望 Truffle 首先 deploy contractA,然後將其作為參數傳遞給contractB的建構子。

這是我目前正在嘗試的,但contractB從未真正部署:

let contractA = artifacts.require("./contractA.sol");
let contractB = artifacts.require("./contractB.sol");

module.exports = async function(deployer, network) {
 await deployer.deploy(contractA);  // this gets deployed fine
 deployer.deploy(contractB, contractA.address); // this *never* gets deployed
};

事實上,即使我將程式碼更改為以下程式碼contractB不會部署:

await deployer.deploy(contractA);    
deployer.deploy(contractB, "0x123");  // does not deploy even if I enter the address manually

我在這裡想念什麼?

我終於能夠通過創建多個 Truffle 部署文件來解決這個問題。

2_deploy_contractA.js:

// 2_deploy_contractA.js

let contractA = artifacts.require("./contractA.sol");

module.exports = function(deployer, network) {
   deployer.deploy(contractA);
};

3_deploy_contractB.js:

// 3_deploy_contractB.js

let contractA = artifacts.require("./contractA.sol");
let contractB = artifacts.require("./contractB.sol");

module.exports = function(deployer, network) {
   deployer.deploy(contractB, contractA.address);
};

試試這個:

deployer.then(async function() {
   let contractA = await artifacts.require("A").new();
   let contractB = await artifacts.require("B").new(contractA._address);
});

引用自:https://ethereum.stackexchange.com/questions/57942