0
0
Blockchain / Solidityprogramming~10 mins

Receive and fallback functions in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a receive function that accepts Ether.

Blockchain / Solidity
receive() external [1] {}
Drag options to blanks, or click blank then click option'
Avirtual
Bexternal
Cpayable
Dview
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add 'payable' causes the function to reject Ether.
Using 'view' or 'virtual' instead of 'payable' is incorrect.
2fill in blank
medium

Complete the code to declare a fallback function that can receive Ether.

Blockchain / Solidity
fallback() external [1] {}
Drag options to blanks, or click blank then click option'
Apayable
Bview
Cpure
Dvirtual
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'payable' causes the fallback function to reject Ether.
Using 'view' or 'pure' is invalid for fallback functions that receive Ether.
3fill in blank
hard

Fix the error in the fallback function declaration to accept Ether.

Blockchain / Solidity
fallback() external [1] {}
Drag options to blanks, or click blank then click option'
Apayable
Bview
Cpure
Dnonpayable
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view' or 'pure' prevents Ether reception.
Forgetting 'payable' causes runtime errors when sending Ether.
4fill in blank
hard

Fill both blanks to create a receive function that logs an event when Ether is received.

Blockchain / Solidity
event Received(address sender, uint amount);

receive() external [1] {
    emit Received(msg.sender, msg.[2]);
}
Drag options to blanks, or click blank then click option'
Apayable
Bvalue
Cgas
Dview
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view' instead of 'payable' prevents receiving Ether.
Using 'msg.gas' instead of 'msg.value' gives wrong data.
5fill in blank
hard

Fill all three blanks to create a fallback function that forwards received Ether to a payable address.

Blockchain / Solidity
fallback() external [1] {
    address payable recipient = payable(msg.sender);
    (bool success, ) = recipient.call{value: msg.[2]([3]);
    require(success, "Transfer failed.");
}
Drag options to blanks, or click blank then click option'
Apayable
Bvalue
C""
Dgas
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'payable' causes the fallback to reject Ether.
Using 'msg.gas' instead of 'msg.value' sends wrong amount.
Omitting the empty string for calldata causes syntax errors.