1

I'm running into an issue going through a basic example in "Mastering Ethereum"...

I have a Faucet contract (on Ropsten) per the book that looks like...

pragma solidity ^0.5.1; //mastering ethereum uses ^0.4.19;

//our first contract is a faucet!
contract Faucet {

    //give out ether to anyone who asks
    function withdraw(uint withdraw_amount) public {

        //limit withdrawal amount
        require(withdraw_amount <= 100000000000000000);

        //send the amount to the address that requested it
        msg.sender.transfer(withdraw_amount);

    }

    //accept any incoming amount
    function () payable external {} 
}

I can deposit funds using MetaMask but when I use Remix to withdraw I see this message...

"Gas estimation failed
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
gas required exceeds allowance or always failing transaction"

Then if I force Remix to send the transaction anyway, the debugger only gives me:

Debugger: Invalid JSON RPC response: ""

Any ideas how to troubleshoot this further? I have enough ETH in MetaMask to afford the gas so I'm guessing that perhaps the withdraw function I'm using is outdated in some way and needs to return a value?

New contributor
d3wannabe is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1

Make sure that you are providing some withdraw_amount in the Value datafield in Remix IDE. By default it has value as 0.

Consider this answer for your reference.
https://ethereum.stackexchange.com/a/68640/53398

Let me know if the problem still persists.

Edited solution:
I've find out the error you are facing.
When you're running this contract, you might be getting a (fallback) function. It lets you to deposit ether to the account. function () payable external {} statement lets you to do the same. When you call this, it takes the amount to be deposited from Value field present in top right corner of remix IDE. It doesn't means the ether you're having in metamask account. So, you can check this amount using address(this).balance.
You can now withdraw the amount easily.

Here's a little edited contract I've done.

pragma solidity ^0.5.1; //mastering ethereum uses ^0.4.19;

//our first contract is a faucet!
contract Faucet {

    //give out ether to anyone who asks
    function withdraw(uint withdraw_amount) public {

        //limit withdrawal amount
        require(withdraw_amount < msg.sender.balance);

        //send the amount to the address that requested it
        msg.sender.transfer(withdraw_amount);

    }

    //accept any incoming amount
    function () payable external {} 

    //checking balance
    function getBalanceContract() public view returns(uint){
        return address(this).balance;
    }
}

When you first run this code, call getBalanceContract(). It will return 0 ether(not the ether of Metamask account). Now call the fallback by providing some amount in Value field. It will deposit the same amount. You can check the updated amount then.

You can now withdraw the amount(less than the present amount). It will be successfully withdrawn and the remaining amount can be seen again by calling getBalanceContract().

PS. If you want to work on the ethers present in Metmask account, you should probably learn about Web3- Ethereum Javascript API.
Consider this post for the same. https://medium.com/crowdbotics/building-ethereum-dapps-with-meta-mask-9bd0685dfd57

  • Please post the relevant parts of that post here. Answers consisting only of links are not very helpful and are liable to be deleted. – Chenmunka Aug 14 at 9:11
  • @Chenmunka Thanks for letting me know. – Prachi Sharma Aug 14 at 9:43
  • @PrachiSharma - thanks for the suggestion but I still have the same problem after populating the value field – d3wannabe Aug 15 at 0:41
  • I've edited the answer. Please refer this for the solution. – Prachi Sharma Aug 16 at 19:23
0

Apologies all - I had simply failed to fund the faucet correctly before executing the withdraw function (i.e. the initial transfer from MetaMask had not confirmed)

New contributor
d3wannabe is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

d3wannabe is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.