Ganache-cli: 'out of gas' error when running a payable function on Remix+TestRPC

Created on 13 Dec 2017  路  25Comments  路  Source: trufflesuite/ganache-cli

I'm using Remix connected to TestRPC 6.0.3 and I'm getting an 'out of gas' error when calling the sendEther() function of the following contract with the argument set to the address of one of the accounts created by TestRPC at startup:

pragma solidity ^0.4.18;

contract Test
{

    function sendEther(address toAddr) public payable
    {
        toAddr.transfer(msg.value);
    }
}

I know that an out of gas error usually means that there was some kind of exception in the function. I tried this in geth + Mist Wallet and it worked fine... Also, running this through Remix debugger did not show me at all what the problem was.

Expected Behavior

If the account running the function has enough ether, then it should not throw an 'out of gas' error and the ether value for the transaction should be transferred to the address passed as argument.

Current Behavior

Any payable function always throws an 'out of gas' error when using TestRPC+Remix. It works on geth+Mist Wallet.

My Environment

Most helpful comment

I tried with this:

`pragma solidity ^0.4.19; // solhint-disable-line compiler-fixed

contract WithdrawalTest {

function WithdrawalTest() public payable {

}

function getContractEtherBalance() public constant returns (uint) {
    return this.balance;
}

function withdraw() public returns (bool) {
    uint amount = this.balance;
    if(msg.sender.send(amount)) {
        return true;
    } else {
        return false;
    }
}

}`

In Remix I deployed this contract with a bit of Ether.

In environment "Javascript VM" it works fine, if I pick "Web3 provided" and point it to my local testrpc it fails with "out of gas".

All 25 comments

Same error here

Hi @joaomontenegro and @AugustoL - what happens when you call eth_estimateGas for the transaction in question? Have you tried setting the gas for the transaction manually?

any news ? I have the same error.

@NTTLuke see my previous comment.

I tried with this:

`pragma solidity ^0.4.19; // solhint-disable-line compiler-fixed

contract WithdrawalTest {

function WithdrawalTest() public payable {

}

function getContractEtherBalance() public constant returns (uint) {
    return this.balance;
}

function withdraw() public returns (bool) {
    uint amount = this.balance;
    if(msg.sender.send(amount)) {
        return true;
    } else {
        return false;
    }
}

}`

In Remix I deployed this contract with a bit of Ether.

In environment "Javascript VM" it works fine, if I pick "Web3 provided" and point it to my local testrpc it fails with "out of gas".

@rthijs - what happens when you call eth_estimateGas for the transaction in question? Have you tried setting the gas for the transaction manually?

... maybe one of you will answer me eventually 馃槅

Setting the gas limit does not change anything. Works fine with "Javascript VM", fails when using testrpc. I did not have time to write something in js to call eth_estimateGas and I don't know if I can do that from within Remix but above you can find the exact code to reproduce it.

To me it looks like a bug in testrpc.

Hi @benjamincburns thank you for your answer. Unfortunately I still have the same error.
I did the same things @rthijs did. Same error on testrpc but using Javascript VM it works fine.
I found the same issue reported on truffle repo and on stackexchange.
I think it's a known behavior and it seems they are working on it.
https://github.com/trufflesuite/ganache-cli/issues/398
https://ethereum.stackexchange.com/questions/33070/out-of-gas-error-when-running-a-payable-function-on-remixtestrpc

But my question is : how people can test on local machine with this error ? Could be a prb related to specific versions ? ( because I think you don't have the same prb )
I'm working with the latest ones.

thank u again for your support

it seems they are working on it

@NTTLuke I'm afraid that I'm the "they" in this case.

I'm also afraid that you didn't answer my question. What happens when you call estimateGas?

I did not have time to write something in js to call eth_estimateGas and I don't know if I can do that from within Remix but above you can find the exact code to reproduce it.

You can call estimate gas via web3.

let request = web3.sendTransaction.request(argsObject)
let gasEstimate = await web3.eth.estimateGas(request)

If you're using web3.eth.Contract in 1.0 it's just myContractInstance.methods.myContractFunctionName.estimateGas and in 0.22.x it's myContractInstance.myContractFunctionName.estimateGas

I'm afraid I'm the "they" in this case. (LOL)

not at home now, I'll answer you tonight as soon as I get home and I try. I will do.
Sorry I had lost question.

thanks a lot @benjamincburns

I could not get the estimate gas to work, I did build a quick page to test it and this does work:

solidity:

pragma solidity ^0.4.19; // solhint-disable-line compiler-fixed


contract WithdrawalTest {

    function WithdrawalTest() public payable {

    }

    function getContractEtherBalance() public constant returns (uint) {
        return this.balance;
    }

    function withdraw() public returns (bool) {
        uint amount = this.balance;
        if(msg.sender.send(amount)) {
            return true;
        } else {
            return false;
        }
    }
}

page (make sure to update the contract address):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <script src="./node_modules/web3/dist/web3.min.js"></script>

</head>
<body>
  <div class="container">

    <h1>TestWithdrawal</h1>

    <p>Walletadres: <b><span id="walletAddress">...</span></b>.</p>

    <p>Contract balance: <span id="balanceContract">...</span></p>
    <p>Wallet balance: <span id="balanceWallet">...</span></p>
    <p>Gas estimate: <span id="estimate">...</span></p>

    <p><a href="#" id="estimateGas">estimate gas</a></p>
    <p><a href="#" id="withdraw">withdrawal</a></p>

  </div>

  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>

  <script>

    if (typeof web3 !== 'undefined') {
        web3 = new Web3(web3.currentProvider);
    } else {
        // set the provider you want from Web3.providers
        web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    }

    var WithdrawalContract = web3.eth.contract([
        {
            "constant": true,
            "inputs": [],
            "name": "getContractEtherBalance",
            "outputs": [
                {
                    "name": "",
                    "type": "uint256"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [],
            "name": "withdraw",
            "outputs": [
                {
                    "name": "",
                    "type": "bool"
                }
            ],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "inputs": [],
            "payable": true,
            "stateMutability": "payable",
            "type": "constructor"
        }
    ]);

    var Withdrawal = WithdrawalContract.at('0xed4107cad8febad23a474e6458eb2ee16d210686');

    $(function() {
      web3.eth.defaultAccount = web3.eth.accounts[0];
      $("#walletAddress").html(web3.eth.defaultAccount);
      showBalances();
    });

    $("#estimateGas").click(function() {
      var estimate = 0; //I can't get this to work
      $("#estimate").html(estimate);
    });

    $("#withdraw").click(function() {
      Withdrawal.withdraw();
      showBalances();
    });

    function showBalances() {
      $("#balanceContract").html(web3.eth.getBalance(Withdrawal.address).toString(10)/1000000000000000000);
      $("#balanceWallet").html(web3.eth.getBalance(web3.eth.defaultAccount).toString(10)/1000000000000000000);
    }

  </script>
</body>
</html>

So it's not a bug in testrpc but a bug in Remix. Withdraw works fine with the code posted but when testing in Remix it fails with an out of gas error.

This is the js library for the code in my previous post.

web3.min.js.zip

@joaomontenegro I just tried to reproduce this using Remix, [email protected], and the contract you provided, and it seems to work fine. If you aren't satisfied with this outcome, please follow the advice I give the others below and reopen.

@rthijs you seem to have concluded that you're experiencing a problem with remix. I'm happy to help you troubleshoot this, but to do so I'll need you to please raise a new issue with the full output from ganache-cli --verbose after reproducing the problem (this might be quite long, so you may have to paste it to a new gist). I also strongly recommend updating to the latest beta release and testing against that. You can do this by running npm install -g ganache-cli@beta.

@NTTLuke & @AugustoL, same goes for you both.

Thanks @benjamincburns , I'll do my best to help out but I have a deadline coming up. I'll see what I can do but I don't think I can make adequate time to test untill february.

@stefek99 run on [email protected], still got this error. On rinkiby works fine.

On [email protected] same error here. Works fine on Javascript VM

https://github.com/trufflesuite/ganache-cli/releases/tag/v3.0.0

TestRPC 3.0.0 Breaking Changes: Default gas limit for transactions is now 90000 gas instead of the full block gas limit.

To avoid these new out of gas errors, you can now pass a higher gas limit as a parameter to web3:

web3.eth.sendTransaction({..., gas: 3141592}) // choose your own gas limit suitable for you

Related: https://ethereum.stackexchange.com/questions/44410/remix-ide-transact-to-errored-vm-exception-while-processing-transaction/44411?noredirect=1#comment51865_44411

npm install -g [email protected]

Lessons learned - always run the latest version of the tool!

I used the same command to update ganache-cli but i am getting the same error. I think my ganache is still using old cli. How i will know that my ganache is using the latest cli instead of older one?

@ZeeshanAhmadKhalil ganache-cli is at version @6.7.0, not 7.0.0 (i'm not sure why a v7 was previously published as a beta).

The command you want is npm install -g [email protected].

To check what version is currently installed run ganache-cli --version.

To check the latest version from the command line run npm view ganache-cli version.

@davidmurdoch I installed ganache-cli 6.7.0 ;

ganache-cli --version
Ganache CLI v6.7.0 (ganache-core: 2.8.0)

But it still gives me out of gas error due toaddress(the_instance).transfer(msg.value); whenever I enter 1 wei value and call fallback function.
image

My ganache application version is 2.1.1;
image

@ZeeshanAhmadKhalil Do you mind opening a new issue with reproduction steps?

@davidmurdoch My issue was resolved when i used Ganache-cli 7.0.0 to create accounts `instead ofusing ganache 2.1.1. Actuallyganache-cli --versiontells us version and also creates 10 eth accounts on local host forganache-cli 7.0.0` .

Was this page helpful?
0 / 5 - 0 ratings