Receive and fallback functions let a smart contract accept money and handle calls that don't match any other function.
Receive and fallback functions in Blockchain / Solidity
receive() external payable {
// code to handle plain Ether transfers
}
fallback() external payable {
// code to handle calls with data or unknown functions
}The receive function is called when the contract receives Ether with empty calldata.
The fallback function is called when no other function matches or when Ether is sent with data.
receive() external payable {
// Accept Ether and do nothing else
}fallback() external payable {
// Log that fallback was called
emit FallbackCalled(msg.sender, msg.value);
}receive() external payable {
balance += msg.value;
}
fallback() external {
revert("Function does not exist");
}This contract has a receive function that emits an event when Ether is sent without data. The fallback function emits an event when the contract receives calls with unknown functions or data.
pragma solidity ^0.8.0;
contract ReceiveFallbackExample {
event Received(address sender, uint amount);
event FallbackCalled(address sender, uint amount, bytes data);
receive() external payable {
emit Received(msg.sender, msg.value);
}
fallback() external payable {
emit FallbackCalled(msg.sender, msg.value, msg.data);
}
}Only one receive function is allowed per contract.
If receive is not defined, fallback handles all calls including plain Ether transfers.
Both functions must be marked external and payable to accept Ether.
Receive function handles plain Ether transfers with empty data.
Fallback function handles calls with unknown functions or data.
Use these functions to safely accept Ether and handle unexpected calls.
Practice
What is the main purpose of the receive() function in a Solidity contract?
Solution
Step 1: Understand the role of
Thereceive()receive()function is designed to accept Ether sent to the contract without any accompanying data.Step 2: Differentiate from fallback function
The fallback function handles calls with data or unknown function calls, whilereceive()is only for plain Ether transfers.Final Answer:
To handle plain Ether transfers without any data -> Option DQuick Check:
receive() handles plain Ether = C [OK]
- Confusing receive() with fallback()
- Thinking receive() handles calls with data
- Assuming receive() rejects Ether
Which of the following is the correct syntax for a receive() function in Solidity?
?
Solution
Step 1: Recall receive() function syntax
Thereceive()function must be declared without thefunctionkeyword, must beexternalandpayable.Step 2: Check each option
receive() external payable {}matches the correct syntax. Options using thefunctionkeyword are invalid, as are those lackingpayableor usingfallbackinstead ofreceive.Final Answer:
receive() external payable {} -> Option AQuick Check:
receive() syntax requires external payable = B [OK]
- Adding 'function' keyword to receive()
- Omitting 'payable' modifier
- Confusing fallback() with receive()
Consider this Solidity contract snippet:
contract Test {
event Log(string func);
receive() external payable {
emit Log("receive");
}
fallback() external payable {
emit Log("fallback");
}
}What event is emitted when someone sends 1 Ether with empty data?
Solution
Step 1: Identify the call type
Sending Ether with empty data triggers thereceive()function if it exists.Step 2: Check which event is emitted
Thereceive()function emits the event with "receive" string.Final Answer:
"receive" -> Option AQuick Check:
Empty data + Ether triggers receive() = "receive" [OK]
- Assuming fallback() triggers on empty data
- Thinking no event is emitted
- Confusing receive() and fallback() roles
Given this contract snippet, what is the error?
contract Example {
fallback() external {
// code
}
receive() external {
// code
}
}Solution
Step 1: Check payable modifiers
To accept Ether, bothreceive()andfallback()must be markedpayable.Step 2: Analyze given code
Both functions lackpayable, so contract cannot receive Ether via these functions.Final Answer:
Both fallback and receive must be payable to accept Ether -> Option BQuick Check:
Missing payable means no Ether acceptance = A [OK]
- Thinking order of functions matters
- Assuming fallback needs 'function' keyword
- Ignoring payable requirement
You want your contract to accept Ether sent with or without data, and log which function was called. Which is the best way to implement this?
?
Solution
Step 1: Understand Ether transfer scenarios
Ether can be sent with empty data (calls receive) or with data (calls fallback).Step 2: Implement both functions to handle all cases
Implementing bothreceive()andfallback()asexternal payablelets the contract accept Ether in both cases and emit distinct events.Final Answer:
Implement both receive() external payable and fallback() external payable functions, each emitting a distinct event -> Option CQuick Check:
Both functions needed for all Ether types = D [OK]
- Implementing only one function and missing some Ether transfers
- Ignoring fallback for calls with data
- Using regular payable function instead of receive/fallback
Ready to prove it?
Pass the test and this pattern moves unknown
