Challenge - 5 Problems
Payable Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Solidity payable function call?
Consider the following Solidity contract snippet. What will be the value of
contractBalance after calling deposit with 1 ether?Blockchain / Solidity
pragma solidity ^0.8.0;
contract Wallet {
uint public contractBalance;
function deposit() public payable {
contractBalance += msg.value;
}
}Attempts:
2 left
💡 Hint
Remember that
msg.value holds the amount of wei sent with the transaction.✗ Incorrect
The deposit function is marked payable, so it can receive ether. The msg.value contains the amount sent in wei. Adding it to contractBalance updates the balance correctly.
❓ Predict Output
intermediate2:00remaining
What error occurs when calling a non-payable function with ether?
Given this Solidity contract, what happens if you call
withdraw and send 1 ether along with the call?Blockchain / Solidity
pragma solidity ^0.8.0;
contract Bank {
function withdraw() public {
// withdraw logic
}
}Attempts:
2 left
💡 Hint
Check if the function is marked payable or not.
✗ Incorrect
Functions not marked payable reject any ether sent with the call. The transaction reverts with an error indicating the function cannot accept ether.
🔧 Debug
advanced2:00remaining
Why does this payable fallback function not receive ether?
Examine the contract below. Why does sending ether to this contract fail to increase its balance?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
fallback() external {
// fallback without payable
}
}Attempts:
2 left
💡 Hint
Check the fallback function signature and payable keyword.
✗ Incorrect
Fallback functions must be marked payable to accept ether. Without payable, any ether sent causes the transaction to revert.
🧠 Conceptual
advanced1:30remaining
Which statement about payable functions is true?
Select the correct statement about payable functions in Solidity.
Attempts:
2 left
💡 Hint
Think about how Solidity controls ether transfers to functions.
✗ Incorrect
In Solidity, only functions marked payable can receive ether. Other functions reject ether and cause the transaction to revert.
❓ Predict Output
expert2:30remaining
What is the contract balance after these calls?
Given the contract below, what is the value of
address(this).balance after these calls?
1. Call deposit with 2 ether
2. Call withdraw with 1 ether
Assume withdraw sends ether back to the caller.Blockchain / Solidity
pragma solidity ^0.8.0;
contract Vault {
function deposit() public payable {
// Accept ether
}
function withdraw(uint amount) public {
payable(msg.sender).transfer(amount);
}
}Attempts:
2 left
💡 Hint
Consider how much ether is sent and withdrawn from the contract balance.
✗ Incorrect
The contract receives 2 ether in deposit. Then it sends 1 ether back in withdraw. So the contract balance is 1 ether.