1
votes
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);
    } 
}

I have written this code in solidity in Remix IDE. The contract was successfully created but when I used it, it gave me an error saying "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". I don't have much code written but its still gave me this error. Can anyone help?

1
This question would better fit in ethereum.stackexchange.comMCCCS

1 Answers

0
votes

First, your msg.value is already sent to your method, hence you don't need to check senders balance: require(sender.balance >= total_ether_to_send);.

Second, you don't have a fallback function in your contract to receive ether.

Third, you are trying to send 100% msg.value to owner and then send 50% of msg.value back to sender. Obviously you cannot spend 150% of msg.value without any additional funds on your contract. Here is example of working code:

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 {}