From b50c53f02f7b43e76c5dade053e0c9fb4d3a0a4c Mon Sep 17 00:00:00 2001 From: Marcus Wentz <52706599+MarcusWentz@users.noreply.github.com> Date: Fri, 21 Jun 2024 22:24:25 -0400 Subject: [PATCH] Create Bank.sol --- Contracts/Bank.sol | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Contracts/Bank.sol diff --git a/Contracts/Bank.sol b/Contracts/Bank.sol new file mode 100644 index 0000000..b4313d3 --- /dev/null +++ b/Contracts/Bank.sol @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.26; + +error etherNotSent(); +error amountTooBig(); + +// Modified from: https://programtheblockchain.com/posts/2018/01/05/writing-a-banking-contract/ +contract Bank { + + mapping(address => uint256) public balanceOf; // Balances, indexed by addresses. + + function deposit() public payable { + //Effects + balanceOf[msg.sender] += msg.value; // Increase the account's balance. + } + + function withdraw(uint256 amount) public { + //Checks + if(amount > balanceOf[msg.sender]) revert amountTooBig(); + //Effects + balanceOf[msg.sender] -= amount; // Decrease the account's balance BEFORE any external address calls. + //Interactions + (bool sent, ) = (msg.sender).call{value: amount}(""); // Externally call the user's address to send Ether. + if(sent == false) revert etherNotSent(); // Revert if msg.sender is not a payable contract of fails in general. + } +}