【问题标题】:How to deploy truffle contract to dev network when using inheritance?使用继承时如何将松露合约部署到开发网络?
【发布时间】:2017-12-19 18:29:56
【问题描述】:

我一直在尝试使用 Truffle 框架部署合约,最近我一直在开发网络上测试这些合约。

我的合约非常大,当我尝试将其部署到测试网时,我被指示将其拆分,这样合约就不会超过 gas 限制。不过,请记住,该合约确实以默认的 gas 限制部署到了开发网络上。

所以我取出合同的一部分,从基础衍生出另一个合同,并在其中添加了已删除的部分。我试图将其部署到开发网络以再次对其进行测试,但现在出现错误:

'Error: The contract code couldn't be stored, please check your gas amount.'

所以我采取的步骤是:

  1. 将我的 gasLimit 更改为 100,000,000 没有解决它
  2. 检查我的合同是否“抽象”

    • 我对此的理解是,如果合同或其 parent 有任何没有实现的功能。我的没有。
  3. 然后我从 派生合同,我仍然收到此错误

我删除了该文件,并且像以前一样仅使用我的基本合同进行部署,因此父合同不得有任何未实现的功能,并且当我尝试从中派生空合同时它仍然无法工作(使确定派生合同中没有什么是抽象的)。

  1. 然后我拆分我的迁移文件,以便进行迁移 分开,仍然没有运气。

我的父合同大约有 300 行长,所以没有必要在这里全部发布 - 我知道有些人会说“它可能太大了”,但是,它在开发网络上的 500 行长时部署,现在它只有 250 行长,派生合约长 275 行,它没有部署。

错误:

Running migration: 2_deploy_contracts.js
Replacing ERC20Token...
 ... 0xcae613274de1aa278e7ae5d1239f43445132a417d98765a4f227ea2439c9e4fc
 ERC20Token: 0xeec918d74c746167564401103096d45bbd494b74
 Replacing Crowdsale...
 ... 0x0ffc7291d84289c1391a81ed9f76d1e165285e3a3eadc065732aa288ea049b3a
 Crowdsale: 0x0d8cc4b8d15d4c3ef1d70af0071376fb26b5669b
 Saving successful migration to network...
 ... 0x7f351d76f61f7b801913f59b808688a2567b64933cdfdcf78bb18b0bf4e4ff69
 Saving artifacts...
 Running migration: 3_more_deployed_contracts.js
   Deploying StagedSale...
   ... 0x216136bb24d317b140a247f10ec4d6791559739111a85932133cd4a66b12a1d9
 Error encountered, bailing. Network state unknown. Review successful 
 transactions manually.
 Error: The contract code couldn't be stored, please check your gas 
 amount.
at Object.callback 
(/usr/local/lib/node_modules/truffle/build/cli.bundled.js:329221:46)
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:39618:25
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:331159:9
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:175492:11
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:314196:9
at XMLHttpRequest.request.onreadystatechange 
(/usr/local/lib/node_modules/truffle/build/cli.bundled.js:329855:7)

我的基础合约太大而无法发布,它本身就可以很好地部署,这意味着它不是抽象的。

我的派生合约是:

pragma solidity ^0.4.16;

import "./SafeMath.sol";
import "./Crowdsale.sol";

contract StagedSale is Crowdsale {
    using SafeMath for uint256;

   /*
    * Setup the contract and transfer ownership to appropriate beneficiary
    */
    function StagedSale
    ( 
        uint256 _stage1Duration, 
        uint256 _stage2Duration
    ) public {
        uint256 stage1duration = _stage1Duration.mul(1 minutes);
        uint256 stage2duration = _stage2Duration.mul(1 minutes);
    }

我的衍生合约迁移文件:

var StagedSale = artifacts.require("./StagedSale.sol");

module.exports = function(deployer) {
  const stage1Duration = 1;
  const stage2Duration = 1;

  deployer.deploy(StagedSale, stage1Duration, stage2Duration);
};

我在这里发布了这个问题,因为我担心这可能是 Truffle 部署的常见问题。

总而言之,我认为这与实际气体限制无关,而是由于某种未知原因而失败并打印此错误消息。

【问题讨论】:

  • 我倾向于从表面上理解错误信息。 web3.eth.estimateGas() 告诉你什么?看看ethereum.stackexchange.com/questions/10974/…
  • 我已经看到了这个问题,这不可能是这个原因,如果你注意到问题的开始,我的合同在开发网络上部署得很好,其中有 500 多行。然后我尝试将它部署到测试网,然后它说它太大了。因此,我将合约拆分为一个 300 行的基本合约,并从大约 30 行的继承合约开始,然后添加更多功能。我现在在尝试部署到开发服务器时遇到此错误,所以我什至无法测试它是什么确实如此,它再次适用于我之前的 500+ 行合同。

标签: ethereum solidity truffle consensys-truffle


【解决方案1】:

我已经找到了解决这个问题的方法,基本上如果你从基础合约继承,你必须像这样在继承的合约构造函数中部署基础合约:

旧版本:

简单地部署基础,然后部署继承合约并在类名中引用“is Crowdsale”

  deployer.deploy(ERC20Token, initialAmount, tokenName, decimalUnits,tokenSymbol).then(function() {
    return deployer.deploy(Crowdsale, softCap, hardCap, etherCostOfEachToken, sendFundsTo, toChecksumAddress(ERC20Token.address), durationInMinutes);
  });

deployer.deploy(FinalizableSale);

新版本

仅部署继承合约并在该构造函数中创建新的 base 实例

  deployer.deploy(ERC20Token, initialAmount, tokenName, decimalUnits,tokenSymbol).then(function() {
    return deployer.deploy(Finalizable, softCap, hardCap, etherCostOfEachToken, sendFundsTo, toChecksumAddress(ERC20Token.address), durationInMinutes);
  });

最终的构造函数:

function FinalizableSale(uint256 _fundingGoalInEthers, uint256 _fundingLimitInEthers,  uint256 _etherCostOfEachToken, address _sendFundsTo, address _tokenAddress, uint256 _durationInMinutes)
    Crowdsale(_fundingGoalInEthers, _fundingLimitInEthers, _etherCostOfEachToken, _sendFundsTo, _tokenAddress, _durationInMinutes)
{
   //do something
}

注意:基本合约在构造函数的左括号之前初始化。

我不再收到“汽油不足”错误,并且我的合同像以前一样运行。

【讨论】:

    猜你喜欢
    • 2021-06-18
    • 2018-06-18
    • 2019-09-09
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2017-10-11
    • 2020-02-29
    • 2020-03-09
    相关资源
    最近更新 更多