使用继承时如何将松露合约部署到开发网络? [英] How to deploy truffle contract to dev network when using inheritance?

查看:168
本文介绍了使用继承时如何将松露合约部署到开发网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been attempting to deploy a contract using the Truffle framework, I have recently been testing these contracts on the development network.

我的合同非常大,当我尝试将其部署到测试网时,系统指示我将其拆分,以使合同不会超出汽油限额.不过,请记住,该合同确实以默认的燃气限额部署到了开发网络上.

My contract was very large and when I attempted to deploy it to a test net, I was instructed to split it up so that the contract wouldn't exceed the gas limit. Although, bearing in mind this contract did deploy onto the development network with the default gas limit.

因此,我取出了合同的一部分,并从基础中导出了另一份合同,并在其中添加了已删除的部分.我尝试将其部署到开发网络中以再次对其进行测试,现在出现错误:

So I took out parts of the contract and derived another contract from the base and added the deleted sections in there. I attempted to deploy it to the development network in order to test it again, I now get the error:

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

所以我采取的步骤是:

  1. 将我的gasLimit更改为1亿个无法解决的问题
  2. 检查我的合同是否为抽象"

  1. Change my gasLimit to 100,000,000 which didn't solve it
  2. Check to see if my contract is 'abstract'

  • 我对此的理解是,如果合同或其合同是抽象的, 父级没有任何实现,但具有任何功能.我的不.
  • My understanding of this is that a contract is abstract if it or its parent has any functions without an implementation. Mine don't.

然后我从删除了除构造函数以外的所有代码. 衍生合同,但仍然出现此错误

I then deleted all of the code other than the constructor from the derived contract and I still get this error

我删除了文件,并且部署仅像以前一样使用我的基本合同,因此父合同一定不能具有任何未实现的功能,并且当我尝试从中获取空合同​​时,它仍然不起作用(确保派生合同中没有抽象内容).

I deleted the file and the deployment worked with just my base contract as before, therefore the parent contract must not have any non-implemented functions AND it still doesn't work when I attempt to derive an empty contract from it (making sure nothing was abstract in the derived contract).

  1. 然后我将迁移文件分割开,以便进行迁移 分别,仍然没有运气.
  1. I then split my migration files up so that the migrations happen separately, still no luck.

我的父合同长约300行,所以没有必要将其全部张贴在这里-我知道有些人会说它可能太大了",但是,当它在开发网络上有500行长时就部署了,现在它只有250条线长,而衍生合同却有275条线长,因此没有部署.

My parent contract is around 300 lines long so no point posting it all in here - I know some people will say 'it may just be too large' however, it deployed when it was 500 lines long on the dev network and now it is only 250 lines long with a deriving contract of 275 lines long, it does not deploy.

错误:

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)

我的基本合同太大了,无法发布,它可以按自身的要求很好地部署,而不是抽象的.

My base contract is too large to post, and it deploys fine on its own meaning its not abstract.

我的衍生合同是:

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

我的衍生合同的迁移文件:

My migration file for the derived contract:

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

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

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

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

I have posted this question here as I fear this may be a common issue with Truffle deployment.

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

To conclude, I don't believe this has anything to do with the actual gas limits and is instead, failing for some unknown reason and printing this error message anyway.

推荐答案

我已经找到了解决方案,基本上,如果您要从基本合同继承,则必须在继承的合同构造函数中部署基本合同,如下所示:

I've found a fix to this, basically if you are inheriting from a base contract, you must deploy the base contract within the inherited contracts constructor like so:

旧版本:

仅部署基础,然后部署继承合同,并在类名中引用"is Crowdsale"

Simply deployed the base, then deployed the inheriting contract with a reference to 'is Crowdsale' in class name

  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实例

Only deploy the inheriting contract and create a new instance of base within that constructor

  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
}

注意:基础合同已在构造函数的方括号之前初始化.

NOTE: That the base contract is initialised BEFORE the opening brackets to the constructor function.

我不再遇到断气"错误,我的合同照常运行.

I no longer get the 'out of gas' error and my contract runs as before.

这篇关于使用继承时如何将松露合约部署到开发网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆