Assert if the balance is bigger or equal than amount, then subtract the amount from balance[msg.sender]
pragma solidity 0.7.5;
contract Bank {
mapping (address => uint) balance;
address owner;
event depositDone(uint amount, address depositedTo);
event MoneyTransferred(address indexed transferredFrom,address indexed transferredTo, uint amount);
modifier onlyOwner{
require(msg.sender == owner);
_; //run the function
}
constructor(){
owner = msg.sender;
}
function deposit() public payable returns(uint){
balance[msg.sender] +=msg.value;
emit depositDone (msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public returns(uint){
assert(balance[msg.sender] >= amount);
msg.sender.transfer(amount);
balance[msg.sender] -= amount;
return balance[msg.sender];
}
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");
require(msg.sender != recipient, "Don't transfer money to yourself");
uint previousSenderBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
assert(balance[msg.sender] == previousSenderBalance - amount);
emit MoneyTransferred(msg.sender, recipient, amount);
}
function _transfer(address from, address to, uint amount) private{
balance[from] -= amount;
balance[to] += amount;
}
}