24

The recent change in Solidity changed the fallback function format from just function() to fallback(), which is pretty nice for beginners to understand what is going on, but I have a question about a suggestion that the compiler gives me when I implement such a fallback.

For example, a piece of code from my project:

pragma solidity ^0.6.1;

contract payment{
    mapping(address => uint) _balance;

    fallback() payable external {
        _balance[msg.sender] += msg.value;
    }
}

Everything goes fine, but the compiler suggests that:

Warning: This contract has a payable fallback function, but no receive ether function.
Consider adding a receive ether function.

What does it mean by a receive ether function? I tried looking it up and many examples I could find is just another fallback function.

I am using version 0.6.1+commit.e6f7d5a4

Baiqing
  • 1,223
  • 2
  • 9
  • 21

2 Answers2

25

As a complement to the accepted answer, here's how you should define the unnamed fallback and receive functions to solve this error:

contract MyContract {

    fallback() external payable {
        // custom function code
    }

    receive() external payable {
        // custom function code
    }
}
Thomas C. G. de Vilhena
  • 13,819
  • 3
  • 50
  • 44
18

According to solidity version 0.6.0, we have a breaking change. The unnamed function commonly referred to as “fallback function” was split up into a new fallback function that is defined using the fallback keyword and a receive ether function defined using the receive keyword. If present, the receive ether function is called whenever the call data is empty (whether or not ether is received). This function is implicitly payable. The new fallback function is called when no other function matches (if the receive ether function does not exist then this includes calls with empty call data). You can make this function payable or not. If it is not payable then transactions not matching any other function which send value will revert. You should only need to implement the new fallback function if you are following an upgrade or proxy pattern.

https://solidity.readthedocs.io/en/v0.6.7/060-breaking-changes.html#semantic-and-syntactic-changes

Fariha Abbasi
  • 1,212
  • 2
  • 13
  • 19