Created
March 10, 2026 12:54
-
-
Save renso3x/47fc3ebf608e949215e7720f2904d01c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.8.31; | |
| contract Bank { | |
| /** | |
| ACTIVITY 1 — First Payable Function | |
| Instructions: | |
| Create a mapping: | |
| mapping(address => uint) public balances; | |
| Create a payable function deposit() that: | |
| Requires msg.value > 0 | |
| Adds the received amount to balances[msg.sender] | |
| Create a function getBalance() that returns the user’s balance. | |
| */ | |
| event Deposit(address indexed sender, uint amount); | |
| event Withdrawn(address indexed recipient, uint amount); | |
| mapping(address => uint) public balances; | |
| function deposit() external payable { | |
| require(msg.value > 0, "Need some gas!"); | |
| balances[msg.sender] += msg.value; | |
| emit Deposit(msg.sender, msg.value); | |
| } | |
| function getBalance() external view returns(uint) { | |
| return address(this).balance; | |
| } | |
| /** | |
| ACTIVITY 2 — Withdraw Your Funds | |
| Instructions: | |
| Add a withdraw(uint amount) function that: | |
| Requires the sender has enough balance. | |
| Subtracts the amount from their balance first (Effect). | |
| Sends Ether to them using call{value: amount}(""). | |
| Requires success (require(success, "Withdraw failed")). | |
| Emit a Withdrawn(address user, uint amount) event. | |
| */ | |
| function withdraw(uint amount) external { | |
| require(balances[msg.sender] >= amount, "Insufficient balance"); | |
| // Effect | |
| balances[msg.sender] -= amount; | |
| // Interaction | |
| (bool success,) = payable(msg.sender).call{value: amount}(""); | |
| require(success, "Withdraw Failed"); | |
| emit Withdrawn(msg.sender, amount); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment