Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Receiving Ether in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Receiving Ether
Contract deployed
↓
Send Ether to contract
↓
Fallback or receive function triggered?
Yes No↓
Accept Ether
↓
Update contract balance
When Ether is sent to a contract, the receive or fallback function runs to accept or reject the Ether, updating the contract's balance if accepted.
Execution Sample
Blockchain / Solidity
contract ReceiveEther {
    receive() external payable {
        // Accept Ether
    }
}
This contract accepts Ether sent to it by implementing the receive() function.
Execution Table
StepActionConditionFunction TriggeredContract Balance ChangeResult
1Deploy contractN/AN/A0 EtherContract ready to receive Ether
2Send 5 Ether to contractEther sent with empty datareceive()+5 EtherEther accepted
3Send 3 Ether with dataEther sent with datafallback()+3 Ether if fallback payableEther accepted if fallback payable, else rejected
4Send 0 Ether callNo Ether sentfallback()0 EtherNo balance change
5Send Ether but no receive or fallback payableNo payable functionNone0 EtherTransaction rejected
💡 Execution stops when Ether is accepted or transaction is rejected due to no payable receive or fallback.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
contract_balance0 Ether5 Ether8 Ether (if fallback payable)8 Ether8 Ether
Key Moments - 3 Insights
Why does the receive() function trigger only when Ether is sent with empty data?
Because receive() is designed to handle plain Ether transfers without data, as shown in execution_table row 2 where Ether sent with empty data triggers receive().
What happens if Ether is sent with data but no payable fallback() exists?
The transaction is rejected, as shown in execution_table row 5 where no payable fallback or receive causes rejection.
Can the fallback() function accept Ether?
Yes, but only if it is marked payable. Otherwise, Ether sent with data will be rejected, as in execution_table rows 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which function is triggered when 5 Ether is sent with empty data?
Afallback()
Breceive()
Cconstructor()
Dno function
💡 Hint
Refer to execution_table row 2 under 'Function Triggered'
At which step does the contract balance increase by 3 Ether if fallback is payable?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check execution_table row 3 for balance change and function triggered
If the fallback function is not payable, what happens when Ether is sent with data?
AEther accepted and balance updated
Breceive() function triggered
CTransaction rejected
DEther returned but balance updated
💡 Hint
See execution_table row 5 for no payable fallback scenario
Concept Snapshot
Receiving Ether in a contract:
- Use receive() external payable for plain Ether transfers
- Use fallback() external payable for calls with data
- Only payable functions accept Ether
- Contract balance updates when Ether accepted
- Transactions revert if no payable receive or fallback
Full Transcript
This visual execution trace shows how a smart contract receives Ether. When the contract is deployed, its balance starts at zero. Sending Ether with empty data triggers the receive() function, which accepts the Ether and increases the contract balance. Sending Ether with data triggers the fallback() function if it is payable; otherwise, the transaction is rejected. Sending calls without Ether triggers fallback() without balance change. If neither receive() nor fallback() is payable, any Ether sent causes rejection. Variables like contract_balance update accordingly after each step. Key moments clarify why receive() triggers only on empty data and the importance of payable fallback. The quiz tests understanding of which function triggers and what happens on payable or non-payable fallback. The snapshot summarizes the rules for receiving Ether in contracts.

Practice

(1/5)
1.

What is the main purpose of the receive function in a Solidity contract?

easy
A. To reject incoming Ether transfers
B. To send Ether from the contract to an external address
C. To initialize contract state variables
D. To accept Ether sent to the contract without any data

Solution

  1. 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.
  2. Step 2: Identify its purpose

    Its purpose is to accept Ether sent directly to the contract without any function call data.
  3. Final Answer:

    To accept Ether sent to the contract without any data -> Option D
  4. Quick Check:

    receive function = accept Ether without data [OK]
Hint: Remember: receive handles plain Ether transfers only [OK]
Common Mistakes:
  • Confusing receive with fallback function
  • Thinking receive sends Ether out
  • Assuming receive initializes variables
2.

Which of the following is the correct syntax for a receive function in Solidity?

?
easy
A. receive() internal payable {}
B. receive() external payable {}
C. receive() external {}
D. receive() public payable {}

Solution

  1. Step 1: Recall receive function requirements

    The receive function must be declared as external and payable to accept Ether without data.
  2. Step 2: Match syntax

    receive() external payable {} matches the required syntax: receive() external payable {}.
  3. Final Answer:

    receive() external payable {} -> Option B
  4. Quick Check:

    receive must be external and payable [OK]
Hint: receive must be external and payable, no public or internal [OK]
Common Mistakes:
  • Using public instead of external
  • Omitting payable keyword
  • Making receive internal or private
3.

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?

medium
A. The contract rejects the Ether and transaction fails
B. The contract accepts Ether but does not emit any event
C. The contract accepts the Ether and emits the Received event
D. The contract calls fallback function instead

Solution

  1. 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.
  2. Step 2: Understand the behavior on Ether transfer

    Sending 1 Ether without data triggers receive, so Ether is accepted and event emitted.
  3. Final Answer:

    The contract accepts the Ether and emits the Received event -> Option C
  4. Quick Check:

    receive payable + emit event = accept and notify [OK]
Hint: Sending Ether without data triggers receive, not fallback [OK]
Common Mistakes:
  • Thinking fallback is called instead
  • Assuming no event is emitted
  • Believing Ether is rejected
4.

Identify the error in this Solidity contract snippet that prevents it from receiving Ether:

contract Example {
    receive() public payable {
        // code
    }
}
medium
A. receive function must be external, not public
B. receive function must not be payable
C. receive function must have a return type
D. receive function must be internal

Solution

  1. Step 1: Check receive function visibility

    The receive function must be declared as external, not public.
  2. Step 2: Confirm payable requirement

    It is correctly marked payable, so no error there.
  3. Final Answer:

    receive function must be external, not public -> Option A
  4. Quick Check:

    receive must be external, not public [OK]
Hint: receive must be external, not public or internal [OK]
Common Mistakes:
  • Using public instead of external
  • Removing payable keyword
  • Adding return type to receive
5.

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?

hard
A. Implement receive() external payable to accept Ether and fallback() external to revert on data
B. Implement only fallback() external payable to accept all Ether
C. Implement receive() external without payable and fallback() external payable
D. Implement receive() external payable and fallback() external payable both accepting Ether

Solution

  1. Step 1: Accept Ether without data using receive

    The receive function accepts Ether sent without data and can update total received.
  2. Step 2: Reject Ether sent with data using fallback

    The fallback function should revert to reject any Ether sent with data.
  3. Final Answer:

    Implement receive() external payable to accept Ether and fallback() external to revert on data -> Option A
  4. Quick Check:

    receive accepts plain Ether, fallback rejects data [OK]
Hint: Use receive for plain Ether, fallback to reject data [OK]
Common Mistakes:
  • Making fallback payable and accepting Ether with data
  • Omitting fallback to reject data
  • Not marking receive payable
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard