2

我正在研究智能合约并在此处观看此视频: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);
}

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

谢谢

4

1 回答 1

2

如发布的那样,合约在调用回退函数时执行(在 Remix 中测试)。但是,一旦您取消注释中的逻辑,它将失败createTokens()

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

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

回退函数文档

示例客户端代码:

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是在魏,不是以太。发送 1 个以太币会使你远远高于你的hardlimit. 建议在您的合同中与 Wei 合作,因此您应该调整您的RATE.

于 2017-12-24T19:09:17.127 回答