From 27e40c95d8693a5e761d5b30d82ace7daf176fa1 Mon Sep 17 00:00:00 2001 From: Siddhartha Tikaigari Date: Wed, 16 Apr 2025 15:23:10 +0530 Subject: [PATCH 1/4] Update simplebank.sol --- simplebank.sol | 1 + 1 file changed, 1 insertion(+) diff --git a/simplebank.sol b/simplebank.sol index 455d932..8c4dea7 100644 --- a/simplebank.sol +++ b/simplebank.sol @@ -13,6 +13,7 @@ contract SimpleBank { // TODO: Require that msg.sender is the owner require(msg.sender == ____, "Not the owner"); _; + } // TODO: Create a constructor that sets the msg.sender as owner From 8f8e78e9b0a2bc77cc40cfaebc4f11559fc092b1 Mon Sep 17 00:00:00 2001 From: Siddhartha Tikaigari Date: Wed, 16 Apr 2025 15:25:39 +0530 Subject: [PATCH 2/4] Create sahi.sol --- sahi.sol | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 sahi.sol diff --git a/sahi.sol b/sahi.sol new file mode 100644 index 0000000..e69de29 From 55c767e7d98a5c0a6f6ae0eeb6f7a6b1365775bc Mon Sep 17 00:00:00 2001 From: Siddhartha Tikaigari Date: Wed, 16 Apr 2025 15:31:20 +0530 Subject: [PATCH 3/4] Update simplebank.sol --- simplebank.sol | 79 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/simplebank.sol b/simplebank.sol index 8c4dea7..0aac4a0 100644 --- a/simplebank.sol +++ b/simplebank.sol @@ -2,57 +2,80 @@ pragma solidity ^0.8.0; contract SimpleBank { - // TODO: Declare a public variable 'owner' of type address - address public ____; + address public owner; - // TODO: Create a mapping 'balances' that maps address to uint - mapping(address => ____) public ____; + mapping(address => uint) public balances; - // Modifier to allow only owner to access certain functions + // Reentrancy guard + bool private locked; + + // Events + event Deposit(address indexed user, uint amount); + event Withdrawal(address indexed user, uint amount); + event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); + + // Modifier: Only owner can access modifier onlyOwner() { - // TODO: Require that msg.sender is the owner - require(msg.sender == ____, "Not the owner"); + require(msg.sender == owner, "Not the owner"); + _; + } + + // Modifier: Prevent reentrant calls + modifier noReentrant() { + require(!locked, "No reentrancy"); + locked = true; _; - + locked = false; } - // TODO: Create a constructor that sets the msg.sender as owner constructor() { - ____ = msg.sender; + owner = msg.sender; + emit OwnershipTransferred(address(0), msg.sender); } - // Function to deposit ether into the contract + // Deposit ether into the bank function deposit() public payable { - // TODO: Increment the sender's balance with msg.value - ____[msg.sender] += ____; + require(msg.value > 0, "Must deposit more than 0"); + balances[msg.sender] += msg.value; + emit Deposit(msg.sender, msg.value); } - // Function to check balance + // Check sender's balance function getBalance() public view returns (uint) { - // TODO: Return the balance of the sender - return ____[msg.sender]; + return balances[msg.sender]; } - // Function to withdraw amount - function withdraw(uint amount) public { - // TODO: Check if sender has enough balance - require(____[msg.sender] >= amount, "Insufficient balance"); + // Withdraw ether safely + function withdraw(uint amount) public noReentrant { + require(balances[msg.sender] >= amount, "Insufficient balance"); + + balances[msg.sender] -= amount; - // TODO: Deduct the amount from sender's balance - ____[msg.sender] -= amount; + (bool success, ) = msg.sender.call{value: amount}(""); + require(success, "Transfer failed"); - // TODO: Transfer the amount to sender - payable(msg.sender).transfer(____); + emit Withdrawal(msg.sender, amount); } - // Owner-only function to check contract's total balance + // Owner can check total contract balance function contractBalance() public view onlyOwner returns (uint) { - // TODO: Return address(this).balance - return ____; + return address(this).balance; } - // Fallback function to receive ether + // Transfer ownership to new address + function transferOwnership(address newOwner) public onlyOwner { + require(newOwner != address(0), "New owner can't be zero address"); + emit OwnershipTransferred(owner, newOwner); + owner = newOwner; + } + + // Receive Ether directly receive() external payable { deposit(); } + + // Fallback function for unexpected calls + fallback() external payable { + deposit(); + } } From 88ad7fa527c4d285524baa2f5047dfcd3a3d3659 Mon Sep 17 00:00:00 2001 From: Siddhartha Tikaigari Date: Wed, 16 Apr 2025 15:33:42 +0530 Subject: [PATCH 4/4] Delete sahi.sol --- sahi.sol | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 sahi.sol diff --git a/sahi.sol b/sahi.sol deleted file mode 100644 index e69de29..0000000