What if your contract could never miss a payment, no matter how it arrives?
Why Receive and fallback functions in Blockchain / Solidity? - Purpose & Use Cases
Imagine you run a digital wallet that accepts money from friends. Sometimes they send money directly, sometimes they send messages with money. Without a clear way to handle these different types of incoming transactions, your wallet might get confused or even lose money.
Manually checking every incoming transaction type is slow and risky. You might miss some payments or fail to respond correctly, causing errors or lost funds. It's like trying to catch every letter in a busy post office without a sorting system.
Receive and fallback functions act like a smart mail sorter for your wallet. They automatically catch plain money transfers and unexpected calls, ensuring your contract handles every incoming transaction safely and smoothly without missing a beat.
function() external payable { revert(); } // rejects all plain transfersreceive() external payable { /* accept plain transfers */ } fallback() external payable { /* handle unknown calls */ }It enables your smart contract to safely accept funds and handle unexpected calls without crashing or losing money.
A crowdfunding contract that must accept donations sent directly or via function calls, ensuring no contribution is lost regardless of how it arrives.
Receive function handles plain Ether transfers safely.
Fallback function catches unexpected calls or data.
Together, they protect your contract from errors and lost funds.
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
