Reentrancy Guard Pattern in Solidity
📖 Scenario: You are building a simple smart contract for a wallet where users can deposit and withdraw Ether. To keep the contract safe, you need to prevent reentrancy attacks, which happen when a malicious contract repeatedly calls the withdraw function before the first call finishes.
🎯 Goal: Build a Solidity contract that uses a reentrancy guard pattern to protect the withdraw function from reentrancy attacks.
📋 What You'll Learn
Create a contract named
SafeWallet with a mapping balances to track user deposits.Add a boolean variable
locked to act as a reentrancy guard.Implement a
deposit function to allow users to send Ether and update their balance.Implement a
withdraw function protected by the reentrancy guard to safely send Ether back to users.Use the
locked variable to prevent reentrant calls in withdraw.💡 Why This Matters
🌍 Real World
Reentrancy attacks have caused major losses in real blockchain projects. Using a reentrancy guard is a simple and effective way to protect smart contracts that send Ether or tokens.
💼 Career
Understanding and implementing security patterns like the reentrancy guard is essential for blockchain developers to write safe smart contracts and avoid vulnerabilities.
Progress0 / 4 steps