[英]How to deploy truffle contract to dev network when using inheritance?
我一直在尝试使用Truffle框架部署合同,最近我在开发网络上测试了这些合同。
我的合同非常大,当我尝试将其部署到测试网时,系统指示我将其拆分,以使合同不会超出汽油限额。 不过,请记住,该合同确实以默认的气体限制部署到了开发网络上。
因此,我取出了合同的一部分,并从基础中导出了另一个合同,并在其中添加了已删除的部分。 我尝试将其部署到开发网络以再次对其进行测试,现在出现错误:
'Error: The contract code couldn't be stored, please check your gas amount.'
因此,我采取的步骤是:
检查我的合同是否“抽象”
然后,我从派生合同中删除了除构造函数以外的所有代码,但仍然出现此错误
我像以前一样删除了文件,并且部署仅与我的基本合同一起使用,因此,父合同一定不能具有任何未实现的功能,并且当我尝试从中获取空合同时,它仍然不起作用(请确保没有任何问题)衍生合同中的摘要)。
我的父合同长约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部署的常见问题。
总而言之,我不认为这与实际的气体限制有关,而是由于某种未知原因而失败,并且无论如何都会打印此错误消息。
我已经找到了解决方法,基本上,如果您要从基本合同继承,则必须在继承的合同构造函数中部署基本合同,如下所示:
旧版本:
只需部署基础,然后部署继承合同,并在类名中引用“ 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
}
注意:基本合同是在构造函数的大括号之前初始化的。
我不再遇到“断气”错误,合同也像以前一样运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.