Fallback function (sol v 0.8)
Fallback()
The
fallback()
function is a special function in Solidity that is used to define a default behavior if no other functions have been called.It is triggered when a contract receives a transaction or message call and no other functions match the given signature.
It's mainly used to enable the contract to receive ETH.
must be marked
external payable
cannot take any parameters
Receive v Fallback v payable
To handle these scenarios when someone sends ETH directly to a contract, and does not call payable functions, we use receive and fallback functions.
If someone sends ETH to a smart contract without passing calldata and there is a
receive
function, thereceive
function is executed.If there is no
receive
function, then thefallback
is executed.If there is no
receive
orfallback
, then the transaction reverts.
receive
is for receiving ether without calling data.
payable function is for receiving ether with data
If you want your contract to receive Ether, you should implement receive.
Using fallback
for receiving Ether is not recommended, since it would catchall, and not fail on interface confusions.
Last updated