2

I try to transfer Ether from the contract to an address but it gives the error that the transaction is out of gas. I think it's a small problem but I can't find it. I have to specifically use solidity version 0.4.24.

The warning from Remix
The balance of the contract
The parameters I use
The error from MetaMask

I have tried different methods, like:

address.transfer(amount);
address.send(amount);
address.call.value(amount)( );

All methods will give the same out of gas exception. and the send and call method will also give a warning that it's outdated and that I should use the transfer method.

I also tried to adjust the gas and it didn't work, I also tried the needed 2,300 for the transfer listed on the docs.

The code:

pragma solidity 0.4.24;

contract TestContract {

    function() external payable { }

    function payContract() public payable {}

    function paySomeone(address _address, uint256 _amount) external {
         _address.transfer(_amount);
    }

    function getBalance() public view returns (uint256) {
         return address(this).balance;
   }

}

Does anyone know what the problem is here?

Thank you for reading.

2

Not sure what you meant by

I have to specifically use solidity version 0.4.24

Assuming this is so (e.g. policy) ...

First, change ^0.4.24 to 0.4.24 without ^ to confirm you are not compiling with 0.5.x which would necessitate adding the keyword payable to the address declaration.

Second, confirm that contract has funds to send. The transfer draws from the contract's own balance so if the requested amount exceeds the funds on hand, the operation will fail. Your getBalance() function shows the current balance. It will initially be zero, so you have to send some ether to the contract in order to set up the test scenario.

Third, confirm the backing blockchain implementation is compatible with Solidity 0.4.24. The "backing" could be ganache, a private network with geth or a public testnet. In any case, the EVM version is a property of the network and you need a post-Byzantium EVM to run code compiled with 0.4.24 due to changes in the interface specification. A mismatch will cause this sort of trouble.

Hope it helps.

UPDATE (per comments below):

Your contract works fine, so I would be concentrate on testing methodology next.

You'll need more than 2,300 gas. Send much more to cover the rest of the operations (send itself forwards 2,300 gas to the receiver, the stipend, but this is not the budget for the whole shebang.)

In my test, the cost was 22,000 gas.

You generally don't have to specify gas but estimateGas() isn't 100%. Consider specifying 100,000 gas to rule it out.

Here is the contract working in Remix to show that the issue may be client-side, i.e. how are you forming the request and sending it.

enter image description here

Hope it helps.

  • First, I'm compiling with Remix and i set the specific version of the compiler to 0.4.24 so that works. But thank you for the tip of removing the ^ – Geert Braakman Jan 8 at 19:54
  • Second, I know the contract has funds, I send 10 wei to it with the Payable function, then I execute the getBalance() function and it shows there is 10 wei in there. After that i call the function paySomeone() to pay 9 wei, but that one doesn't work. – Geert Braakman Jan 8 at 19:56
  • Third, can you specify this further, i don't get what you mean by post-Byzantium EVM, how can i check if I have it. I run Ganache CLI v6.2.3 (ganache-core: 2.3.1) and everything else has just worked. – Geert Braakman Jan 8 at 20:02
  • I don't think that's the issue. The issue is the EVM protocol was upgraded last year (?) and part of that upgrade is adding expected length to the interface to support strings, dynamic arrays, etc. function sigs. It's important that your tool chain is aware of/expects the same generation of blockchain. In the case of a mismatch, much fails. – Rob Hitchens - B9lab Jan 8 at 20:15
  • 1
    Ok, thank you for your response, I switched from Ganache to the JavaScrip VM and that worked, after that i tried on the Robsten test network and that worked as well. So for me the issue is fixed because I need to have it working on the test network. – Geert Braakman Jan 9 at 8:09

Your Answer

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.