2

When I send to contract address some coins, I want to call other function in fallback function, but I have encountered the followed error.

enter image description here

pragma solidity ^0.4.23;

contract Solitaire {
    uint randomNonce = 0;
    uint[] randomNumArray;

    event Deposit(address user, uint256 amout);

    constructor() public payable{
    }

    function GetNonce() public returns (uint) {
        return randomNonce ++;
    }

    function () public payable{
        uint value = GetNonce() + 20;
        emit Deposit(msg.sender, value);

    }
}

contract CallTest {
    event logSendEvent(address to, uint256 value);
    event depositvalue(address sender, uint256 value);



    function transferEther(address towho) public payable {
        require(address(this).balance > 100000000000000000, "Contract address does not exist enough money.");
        towho.transfer(100000000000000000);
        emit logSendEvent(towho, 100000000000000000);
    }

    function deposit() public payable {
        emit depositvalue(msg.sender, msg.value);
    }

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

    constructor() public payable{
    }
}
  • 1
    Are you calling the fallback function of Solitaire directly? Or are you trying to invoke it via CallTest's transferEther function? If the latter, the issue is that <address>.transfer(...) doesn't forward enough gas to make another function call. – user19510 Sep 2 '18 at 6:14
  • yes, invoke it via CallTest's transferEther function. I have tested in remix , first I my account has enough coin and provide enough gas , the same exception. – yang yang Sep 2 '18 at 7:17
1

Per comments above, the issue is that <address>.transfer() only forwards 2300 gas, which is not enough to make a function call.

You could use towho.call.value(100000000000000000)() instead if you want to forward more gas.

  • redix give me a worning: security/no-call-value: Consider using 'transfer' in place of 'call.value()'. – yang yang Sep 2 '18 at 7:52
  • redix is wrong, please file a bug – William Entriken Oct 28 '18 at 1:10
  • This answer is right, but for the wrong reason. 2300 /is/ enough to make a function call, but it is not enough to make /that/ function call. Because GetNonce is modifying state (which costs 20000g gas). – William Entriken Oct 28 '18 at 1:12

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.