0
0
Blockchain / Solidityprogramming~10 mins

Payable 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 make the function accept Ether payments.

Blockchain / Solidity
function deposit() public [1] {
    // Function body
}
Drag options to blanks, or click blank then click option'
Apure
Bexternal
Cview
Dpayable
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view' or 'pure' which do not allow Ether transfers.
Forgetting to add 'payable' causes the function to reject Ether.
2fill in blank
medium

Complete the code to access the amount of Ether sent to the function.

Blockchain / Solidity
function deposit() public payable {
    uint amount = [1];
}
Drag options to blanks, or click blank then click option'
Amsg.value
Bmsg.sender
Caddress(this).balance
Dtx.gasprice
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'msg.sender' which is the sender's address, not the amount.
Using 'address(this).balance' which is the contract's total balance, not the sent amount.
3fill in blank
hard

Fix the error in the function declaration to allow receiving Ether.

Blockchain / Solidity
function receiveFunds() public [1] {
    // Accept Ether
}
Drag options to blanks, or click blank then click option'
Apure
Bview
Cpayable
Dinternal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view' or 'pure' which prevent Ether transfers.
Omitting 'payable' causes the function to reject Ether.
4fill in blank
hard

Fill both blanks to create a payable fallback function that accepts Ether.

Blockchain / Solidity
fallback() [1] [2] {
    // fallback logic
}
Drag options to blanks, or click blank then click option'
Apayable
Bview
Cpure
Dexternal
Attempts:
3 left
💡 Hint
Common Mistakes
Marking fallback as 'view' or 'pure' which is invalid.
Omitting 'payable' causes fallback to reject Ether.
5fill in blank
hard

Fill all three blanks to create a payable receive function that accepts Ether and updates balance.

Blockchain / Solidity
mapping(address => uint) public balances;

receive() external [1] {
    balances[msg.sender] [2]= [3];
}
Drag options to blanks, or click blank then click option'
Apayable
B+
Cmsg.value
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting 'payable' on the receive function.
Using '=' instead of '+=' which overwrites balance.
Using 'msg.sender' instead of 'msg.value' for amount.