所需气体超过区块气体限制回退功能

Gas required exceeds block gas limit fallback function

我正在研究智能合约并在此处观看了此视频:https://www.youtube.com/watch?v=s677QFT6e4U&t=911s。我完全复制了代码,但是当我尝试调用回退函数时出现以下错误:Gas required exceeds block gas limit: 300000000。即使后备函数如下(它什么也不做):

function () payable {

}

这怎么会耗油太多?

合同代码:

pragma solidity ^0.4.11;

import './IERC20.sol';
import './SafeMath.sol';

contract AToken is IERC20 {
    
    using SafeMath for uint256;
    
    uint256 public _totalSupply = 0;
    uint256 public constant hardLimit = 45000000;
    string public constant symbol = "ABC";
    string public constant name = "Alphabet";
    uint8 public constant decimals = 18;
    
    //1 ETH = 25000 Alphabet
    uint256 public constant RATE = 25000;
    
    address public owner;
    
    mapping(address => uint256) balances;
    mapping(address => mapping(address => uint256)) allowed;
    
    function () payable {
        createTokens();
    }
    
    function SnapToken() {
        owner = msg.sender;
    }
    
    function createTokens() payable {
        //require(msg.value > 0);
        //uint256 tokens = msg.value.mul(RATE);
        //require(tokens.add(_totalSupply) <= hardLimit);
        //balances[msg.sender] = balances[msg.sender].add(tokens);
        //_totalSupply = _totalSupply.add(tokens);
        //owner.transfer(msg.value);
    }
    
    function totalSupply() constant returns (uint256 totalSupply) {
        return _totalSupply;
    }
    
    function balanceOf(address _owner) constant returns (uint256 balance) {
        return balances[_owner];
    }
    
    function transfer(address _to, uint256 _value) returns (bool success) {
        require(balances[msg.sender] >= _value && _value > 0);
        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(_value);
        Transfer(msg.sender, _to, _value);
        return true;
    }
    
    function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
        require(allowed[_from][msg.sender] >= _value && balances[_from] >= _value && _value > 0);
        balances[_from] = balances[_from].sub(_value);
        balances[_to] = balances[_to].add(_value);
        allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
        Transfer(_from, _to, _value);
        return true;
    }
    
    function approve(address _spender, uint256 _value) returns (bool success) {
        //allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_value);
        Approval(msg.sender, _spender, _value);
        return true;
    }
    
    function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
        return allowed[_owner][_spender];
    }
    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 value);
}

我评论了一些东西,看看这是否会减少 gas 需求,但不幸的是不会。您以前遇到过这种情况吗?

谢谢

发布的合约在调用回退函数时执行(在 Remix 中测试)。但是,一旦您取消注释 createTokens()

中的逻辑,它将失败

后备函数的 gas 限制较低 (2300),因此它们的功能非常有限。您不能执行诸如写入存储、调用外部函数或发送以太币之类的操作,因为您会立即达到极限。它应该主要用于使您的合约能够接收以太币并可能记录一个事件。

在您上面发布的示例中,删除后备函数中对 createTokens() 的调用,直接从您的客户端调用该函数。

Documentation on Fallback Functions

示例客户端代码:

const abiDefinition = ...;
const contractAddress = ...;
const account = ...;
const amountInEther = ...;

const contract = web3.eth.contract(abiDefinition);
const contractInstance = contract.at(contractAddress);

const transactionObj = {
  from: account,
  value: web3.toWei(amountInEther, 'ether'),
};

contractInstance.createTokens.sendTransaction(transactionObj, (error, result) = {
  ...
};

另外,作为旁注,您的价值计算不正确。 msg.value 在 Wei 中,而不是以太币。发送 1 个以太币会让你远远超过你的 hardlimit。建议在合同中与魏合作,所以你应该调整你的RATE