-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ceaa33c
commit b50c53f
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
} | ||
} |