it's my first ever solidity contract and i can't figure out why my withdraw function is consuming infinite gas. when i compile the contract it gives a warning.
Gas requirement of function Faucet.withdraw(uint256) high: infinite. If the gas requirement of a function is higher than the block gas limit, it cannot be executed. Please avoid loops in your functions or actions that modify large areas of storage(this includes clearing or copying arrays in storage)"
pragma solidity ^0.5.11;
//Our First Contract is a Faucet
contract Faucet
{
//Deposits ethers
function deposit(uint256 amount) payable public {
require(msg.value == amount);
// nothing to do!
}
//Give out ether to anyone who asks
function withdraw(uint256 withdraw_amount) public
{
if(withdraw_amount <= address(this).balance)
{
//Send the amount to address which requested it
msg.sender.transfer(withdraw_amount);
}
}
}
Note: i have successfully deployed the contract but transactions are failing because they run out of gas. is it due to this warning ?
If you want to send ether to a contract without calling any of its functions, you need to have a fallback function in that contract.
Add this function in the contract:
function () external payable {}
The code looks fine.
I also didn't encounter any problems when actually running your code. Sometimes error messages are not accurate. Maybe you call withdraw with value?
You can use remix to test it.