What if Gas required in my program exceeds the limit of 3000000?

0 votes

My code written in Remix IDE got created successfully but gives the following error:

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

Do I need to improvise my code? Can anyone help me with this?

Following is my code:

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);
    } 
}
May 29, 2018 in Blockchain by nsv999
• 5,090 points
72 views

1 answer to this question.

0 votes

Going through your code, I noticed that  you don't have a fallback function in your contract to receive ether.

Also, 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); .

See, what you're doing is 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.

Following is an example:

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 {}
answered May 29, 2018 by Perry
• 17,020 points

Related Questions In Blockchain

+1 vote
1 answer

What is the difference between if() and require() statement in solidity??

If() and require() have separate functions and ...READ MORE

answered Apr 18, 2018 in Blockchain by Shashank
• 10,350 points
412 views
0 votes
1 answer

What could be the best term to use for the collection of contracts in a .sol file?

module - don't think so. Because module ...READ MORE

answered Jun 1, 2018 in Blockchain by Shashank
• 10,350 points
36 views
0 votes
1 answer

What is the use of Ethereum's RLPx in Ethereum ecosystem?

So, RLPx is a protocol suite. It ...READ MORE

answered Jun 5, 2018 in Blockchain by Johnathon
• 9,080 points
210 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
157 views
0 votes
1 answer
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 67,600 points
88 views
0 votes
1 answer

In Ethereum Solidity, what is the purpose of the “memory” keyword?

Without the memory keyword, Solidity tries to declare variables ...READ MORE

answered Nov 14, 2018 in Blockchain by Perry
• 17,020 points
43 views