Receiving Ether means your smart contract can accept money sent to it. This is important to handle payments or donations.
Receiving Ether in Blockchain / Solidity
receive() external payable {
// code to run when Ether is received
}The receive function is special and runs automatically when Ether is sent without data.
It must be marked external and payable to accept Ether.
receive() external payable {
// Accept Ether without any action
}receive() external payable {
emit Received(msg.sender, msg.value);
}fallback() external payable {
// fallback function to accept Ether with data
}This contract accepts Ether using the receive function and emits an event each time Ether is received. It also has a function to check the contract's balance.
pragma solidity ^0.8.0; contract ReceiveEtherExample { event Received(address sender, uint amount); receive() external payable { emit Received(msg.sender, msg.value); } function getBalance() public view returns (uint) { return address(this).balance; } }
If your contract does not have a receive or payable fallback function, it will reject incoming Ether.
Use msg.value to know how much Ether was sent.
Always mark the receive function as payable to accept Ether.
The receive function lets your contract accept Ether sent without data.
It must be external and payable.
Use events to track received Ether easily.
Practice
What is the main purpose of the receive function in a Solidity contract?
Solution
Step 1: Understand the role of the receive function
The receive function is a special function in Solidity that is triggered when the contract receives Ether without any calldata.Step 2: Identify its purpose
Its purpose is to accept Ether sent directly to the contract without any function call data.Final Answer:
To accept Ether sent to the contract without any data -> Option DQuick Check:
receive function = accept Ether without data [OK]
- Confusing receive with fallback function
- Thinking receive sends Ether out
- Assuming receive initializes variables
Which of the following is the correct syntax for a receive function in Solidity?
?
Solution
Step 1: Recall receive function requirements
The receive function must be declared as external and payable to accept Ether without data.Step 2: Match syntax
receive() external payable {} matches the required syntax:receive() external payable {}.Final Answer:
receive() external payable {} -> Option BQuick Check:
receive must be external and payable [OK]
- Using public instead of external
- Omitting payable keyword
- Making receive internal or private
Consider this Solidity contract snippet:
contract Test {
event Received(address sender, uint amount);
receive() external payable {
emit Received(msg.sender, msg.value);
}
}What happens when someone sends 1 Ether to this contract without calling any function?
Solution
Step 1: Analyze the receive function
The receive function is payable and emits an event with sender and amount when Ether is received without data.Step 2: Understand the behavior on Ether transfer
Sending 1 Ether without data triggers receive, so Ether is accepted and event emitted.Final Answer:
The contract accepts the Ether and emits the Received event -> Option CQuick Check:
receive payable + emit event = accept and notify [OK]
- Thinking fallback is called instead
- Assuming no event is emitted
- Believing Ether is rejected
Identify the error in this Solidity contract snippet that prevents it from receiving Ether:
contract Example {
receive() public payable {
// code
}
}Solution
Step 1: Check receive function visibility
The receive function must be declared as external, not public.Step 2: Confirm payable requirement
It is correctly marked payable, so no error there.Final Answer:
receive function must be external, not public -> Option AQuick Check:
receive must be external, not public [OK]
- Using public instead of external
- Removing payable keyword
- Adding return type to receive
You want to create a contract that tracks the total Ether received and rejects any Ether sent with data. Which combination of functions should you implement?
Solution
Step 1: Accept Ether without data using receive
The receive function accepts Ether sent without data and can update total received.Step 2: Reject Ether sent with data using fallback
The fallback function should revert to reject any Ether sent with data.Final Answer:
Implement receive() external payable to accept Ether and fallback() external to revert on data -> Option AQuick Check:
receive accepts plain Ether, fallback rejects data [OK]
- Making fallback payable and accepting Ether with data
- Omitting fallback to reject data
- Not marking receive payable
Ready to prove it?
Pass the test and this pattern moves unknown
