当一个合同的构造函数依赖于另一个合同时,如何部署可靠性智能合约?

时间:2018-04-02 07:03:40

标签: solidity smartcontracts truffle

代码示例为https://github.com/facuspagnuolo/ethereum-spiking/tree/master/5-token-sale-contract

相关文件是:

1。合同\ MyToken.sol

contract MyToken is BasicToken, Ownable {
  uint256 public constant INITIAL_SUPPLY = 10000;

  function MyToken() {
    totalSupply = INITIAL_SUPPLY;
    balances[msg.sender] = INITIAL_SUPPLY;
    Transfer(0x0, msg.sender, INITIAL_SUPPLY);
  }
}

2。合同\ TokenSale.sol

contract TokenSale is Ownable {
  MyToken public token;
  uint256 public priceInWei;
  bool public tokenSaleClosed;

  event TokenPurchase(address buyer, address seller, uint256 price, uint256 amount);

  function TokenSale(MyToken _token, uint256 _price) public {
    if (_price < 0) return;

    token = _token;
    priceInWei = _price;
    tokenSaleClosed = false;
  }

}

第3。迁移\ 2_deploy_contracts.js

const MyToken = artifacts.require("./MyToken.sol");
const TokenSale = artifacts.require("./TokenSale.sol");

module.exports = function(deployer) {
  deployer.deploy(MyToken);
  deployer.deploy(TokenSale);
};

当我通过truffle和testrpc($ truffle migrate)部署它时,它失败如下:

  

使用网络'开发'。

     

正在运行迁移:2_deploy_contracts.js     部署MyToken ......     ... 0x289577d078c8fbc61585127ac123dbef43aa711529bf079c4fd400206c65e0de     MyToken:0x33ddda65330e75e45d3d2e4e270457915883c2fc     部署TokenSale ......   遇到错误,祸了。网络状态未知。手动查看成功的事务。   错误:TokenSale契约构造函数需要2个参数,收到0       在C:\ Users \ zklin \ AppData \ Roaming \ npm \ node_modules \ truffle \ build \ webpack:\〜\ truffle-contract \ contract.js:390:1       在新的承诺()       在C:\ Users \ zklin \ AppData \ Roaming \ npm \ node_modules \ truffle \ build \ webpack:\〜\ truffle-contract \ contract.js:374:1       在       at process._tickCallback(internal / process / next_tick.js:188:7)

2 个答案:

答案 0 :(得分:0)

http://truffleframework.com/docs/getting_started/migrations#deployer

// Deploy A, then deploy B, passing in A's newly deployed address
deployer.deploy(A).then(function() {
  return deployer.deploy(B, A.address);
});

答案 1 :(得分:0)

可能只迁移第二个,因此TokenSale会自动部署MyToken。