0
0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style9 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.