Complete the code to declare a boolean variable named 'locked' initialized to false.
bool [1] = false;The reentrancy guard uses a boolean variable named locked to track if the function is already running.
Complete the modifier to check if the contract is not locked before running the function.
modifier noReentrant() {
require(![1], "Reentrant call");
_;
}The modifier checks that locked is false before allowing the function to run.
Fix the error in the modifier to set the locked state correctly before and after the function runs.
modifier noReentrant() {
require(!locked, "Reentrant call");
locked = [1];
_;
locked = false;
}Set locked = true before running the function to prevent reentrancy.
Fill both blanks to complete the safe withdraw function using the noReentrant modifier and resetting the locked state.
function withdraw(uint amount) public noReentrant {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
(bool success, ) = msg.sender.call{value: amount}([1]);
require(success, "Transfer failed");
locked = [2];
}gas(2300) which limits gas and can cause failures.Use gas() to forward all gas in the call and set locked = false after the transfer to unlock.
Fill all three blanks to create a dictionary comprehension that maps addresses to their balances only if balance is greater than zero.
mapping(address => uint) public balances;
function positiveBalances(address[] memory users) public view returns (mapping(address => uint) memory) {
return {user: balances[user] for user in users if balances[user] [1] 0};
}
modifier noReentrant() {
require(!locked, "Reentrant call");
locked = [2];
_;
locked = [3];
}The comprehension filters balances greater than zero using >. The modifier sets locked = true before and locked = false after the function.