Complete the code to declare a payable fallback function in Solidity.
fallback() external [1] {}The fallback function must be marked payable to receive Ether.
Complete the code to update the user's balance after withdrawal.
balances[msg.sender] [1] 0;
We assign zero to the user's balance after withdrawal using the '=' operator.
Fix the error in the withdrawal function to prevent reentrancy by updating state before sending Ether.
function withdraw() public {
uint amount = balances[msg.sender];
require(amount > 0, "No balance");
[1];
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "Failed to send Ether");
}Setting the balance to zero before sending Ether prevents reentrancy attacks.
Fill both blanks to create a mapping and a function that checks the balance of an address.
mapping(address => uint) public [1]; function getBalance(address user) public view returns (uint) { return [2][user]; }
The mapping is named balances, and the function returns the balance for the given user.
Fill all three blanks to implement a safe withdrawal pattern using a mutex lock.
bool private [1]; function withdraw() public { require(![2], "Reentrant call"); [3] = true; uint amount = balances[msg.sender]; require(amount > 0, "No balance"); balances[msg.sender] = 0; (bool success, ) = msg.sender.call{value: amount}(""); require(success, "Failed to send Ether"); [3] = false; }
The boolean locked is used as a mutex to prevent reentrancy by blocking repeated calls.