当我添加一个简单的函数时,为什么我的 SmartContract 会耗尽 gas

Why runs my SmartContract out of gas when I add a simple function

我有一个源自 openzeppelin MintableToken 的简单标记。

但是,当我添加一个构造函数或另一个函数时,我经常 运行 没油了。但是当我只添加两者之一时,构造函数或函数,一切正常。

我的问题是:如何将多个函数和构造函数一起添加到我的 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);
      })
};

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

Error: VM Exception while processing transaction: out of gas

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

我猜你是 运行 在 运行“truffle init”之后开箱即用的带有 truffles 默认设置的迁移,不是吗?

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

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

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