【问题标题】:Why runs my SmartContract out of gas when I add a simple function当我添加一个简单的函数时,为什么我的 SmartContract 会耗尽燃料
【发布时间】:2019-02-14 04:34:20
【问题描述】:

我有一个从 openzeppelin 的 MintableToken 派生的简单令牌。

但是,当我添加一个构造函数或另一个函数时,我总是会用尽gas。但是当我只添加两者之一时,无论是构造函数还是函数,一切正常。

我的问题是:如何将多个函数与构造函数一起添加到我的 SmartContract 中?

令牌代码:

pragma solidity ^0.4.22;

import "openzeppelin-solidity/contracts/token/ERC20/MintableToken.sol";

contract HaioToken is MintableToken {
    string public name = "Haio Token";
    string public symbol = "HAI";
    uint8 public decimals = 18;

    uint256 public cap;

    constructor(uint256 _cap) public {
        cap = _cap;
    }

    function test(address _to) public returns (bool) {
        return true;    
    }

}

迁移:

2_deploy_contracts.js:

var HaioToken = artifacts.require("HaioToken");

module.exports = function(deployer, network, accounts) {
  const hardCap = 25000000;

  return deployer
      .then(() => {
          return deployer.deploy(HaioToken, hardCap);
      })
};

当我想部署代码时,我收到以下错误消息:

错误:处理事务时出现 VM 异常:气体不足

如果我删除构造函数或测试函数,一切正常。

【问题讨论】:

    标签: ethereum solidity smartcontracts truffle openzeppelin


    【解决方案1】:

    我猜您正在使用运行“truffle init”后开箱即用的松露默认设置运行迁移,不是吗?

    您应该在truffle.js(或Windows 上的truffle-config.js)中以这种方式提高要在合约部署中发送的gas:

    module.exports = {
      networks: {
        development: {
          host: "localhost",
          port: 7545,
          network_id: "*",
          gas: 5000000
        }
      }
    };
    

    (5000000 的值是一个开箱即用的示例,如果您不必关心,因为在本地测试网上开发:))

    【讨论】:

      猜你喜欢
      • 2019-08-17
      • 2021-11-03
      • 2021-12-15
      • 2011-07-06
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 2013-06-09
      • 2012-04-17
      相关资源
      最近更新 更多