0

I am a student who studies solidity now. I am following the tutorial now to study how to write solidity code on Remix. But I think below code does not have any problem, but it keeps making an error.

pragma solidity ^0.4.24;

contract Receive {
    uint256 public balance;
    function pay() public payable {

    }
}

contract Send {
    Receive public r;

    constructor() public {
        r = new Receive();
    }

    function send() public payable {
        address(r).transfer(msg.value);
    }
}

The error is:

The constructor should be payable if you send value

But if I remove the name of payable fallback function in Receive contract like below:

function () public payable {

}

It works. but why this happens ..? and the tutorial says that payable fallback function can have its name and also the statements in it, too.

  • The fallback function should be nameless (unfortunately; forcing the name send would actually be very much appropriate IMO). – goodvibration Nov 7 '18 at 16:21
  • But, this code works properly on Remix. pastebin.com/LCnf5Bb6 the bid() function is a fallback function, but it has its name. – xiaofo Nov 7 '18 at 16:29
  • And even send() function in Send contract is a fallback function. But it has the name “send”. – xiaofo Nov 7 '18 at 16:37
  • There's some confusion here. The "fallback function" is specifically the (optional) function with no name. send() is not a fallback function because it has a name. But there's no reason there has to be a fallback function. (In fact, I regularly argue against having one.) – smarx Nov 7 '18 at 17:14
  • The issue is that send() calls address(r).transfer(msg.value). This sends a message with no data to Receive, which means it triggers Receive's fallback function. Unfortunately, it doesn't have one, and the default implementation is to revert if ether is sent to a non-existent fallback function. If you want to call the function pay() on Receive, just use r.pay.value(msg.value)(). – smarx Nov 7 '18 at 17:15
0

You don't have a payable fallback function. The function pay would be fine, but you aren't calling it via something like address(r).pay.value(msg.value);. Instead you're just doing address(r).transfer(msg.value), and to do this, the contract you're calling needs to have a payable fallback, i.e. a payable function that has no name.

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.