Hello, this is my code. It is not possible to withdraw more money than you have, if you try to do it, it gives an error message. It also updates balance. I am very happy about this assignment, that I was able to figure it out on my own and in such a short time. Thank you Filip for a very good course!
pragma solidity 0.7.5;
contract Bank {
mapping(address => uint) balance;
address owner;
event depositDone(uint amount, address depositedTo);
modifier onlyOwner {
require(msg.sender == owner);
_; //run the function
}
constructor(){
owner = msg.sender;
}
function deposit() public payable returns (uint) {
balance[msg.sender] += msg.value; //this is for ourselves to keep track
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public returns (uint){
require(balance[msg.sender] >= amount, "Balance not sufficient"); // checks balance of msg.sender
msg.sender.transfer(amount);
balance[msg.sender] -= amount;
}
function getBalance() public view returns (uint){
return balance[msg.sender];
}
function transfer(address recipient, uint amount) public{
require(balance[msg.sender] >= amount, "Balance not sufficient"); // checks balance of msg.sender
require(msg.sender != recipient, "Don't transfer money to yourself"); // you should not send money to yourself
uint previousSenderBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
assert(balance[msg.sender] == previousSenderBalance - amount);
}
function _transfer(address from, address to, uint amount) private {
balance[from] -=amount;
balance[to] += amount;
}
}