所需气体超过限制:3000000。

Gas required exceeds limit: 3000000.

pragma solidity ^0.4.16;

contract createNewToken {
    uint256 total_ether_to_send;
    address private owner;

    //constructor
    function createNewToken() public{
        owner = msg.sender;
    }

    // client request for tokens by sending ether.
    function requestForToken() public payable{
        address sender = msg.sender;
        uint value = msg.value;
        total_ether_to_send = value;
        require(sender.balance >= total_ether_to_send);
        owner.transfer(total_ether_to_send);

        total_ether_to_send = value / 2;
        require(owner.balance >= total_ether_to_send);
        sender.transfer(total_ether_to_send);
    } 
}

我已经在 Remix IDE 中稳定地编写了这段代码。合约已成功创建,但当我使用它时,它给了我一个错误,说 "Gas required exceeds limit: 3000000. An important gas estimation might also be the sign of a problem in the contract code. Please check loops and be sure you did not sent value to a non payable function". 我没有写太多代码,但它仍然给了我这个错误。有人可以帮忙吗?

首先,你的msg.value已经发送到你的方法,因此你不需要检查发件人余额:require(sender.balance >= total_ether_to_send);.

其次,你的合约中没有回退功能来接收以太币。

Third,您正在尝试将 100% msg.value 发送给所有者,然后将 msg.value 的 50% 发送回发件人。显然,您不能在没有任何额外资金的情况下花费 msg.value 的 150%。这是工作代码的示例:

function requestForToken() public payable{
    address sender = msg.sender;
    uint value = msg.value;
    total_ether_to_send = value / 2;
    require(this.balance >= total_ether_to_send);
    owner.transfer(total_ether_to_send);

    require(this.balance >= total_ether_to_send);
    sender.transfer(total_ether_to_send);
} 

function() payable {}