0
0
Blockchain / Solidityprogramming~30 mins

Reentrancy guard pattern in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
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
1
Create the contract and balances mapping
Create a Solidity contract named SafeWallet and inside it, declare a public mapping called balances that maps address to uint.
Blockchain / Solidity
Need a hint?

Use mapping(address => uint) public balances; inside the contract.

2
Add the reentrancy guard variable
Inside the SafeWallet contract, add a private boolean variable named locked and initialize it to false.
Blockchain / Solidity
Need a hint?

Declare bool private locked = false; inside the contract.

3
Add deposit and protected withdraw functions
Add a public payable function deposit that increases the sender's balance by msg.value. Then add a public function withdraw that takes a uint amount parameter. In withdraw, first check that locked is false, then set locked to true. Next, check that the sender has enough balance, reduce their balance by amount, send amount Ether to the sender, and finally set locked back to false.
Blockchain / Solidity
Need a hint?

Use require(!locked) and set locked = true at the start of withdraw, then reset locked = false at the end.

4
Test the contract by depositing and withdrawing
Write a comment line that shows how to deposit 1 Ether by calling deposit with msg.value 1 Ether, and then write a comment line showing how to withdraw 0.5 Ether by calling withdraw with amount 0.5 Ether. Then print a message "Deposit and withdrawal executed safely" using an event or comment (since Solidity does not have print).
Blockchain / Solidity
Need a hint?

Use comments to show example deposit and withdraw calls and a success message.