Hi! So I’ve been using my own Bank contract. Now the contract is much cleaner and is more secure - big thanks @jon_m! Here’s the SafeMath in my Bank contract:
// SPDX-License-Identifier: UNLICENCED
pragma solidity 0.8.9;
import "./Academy-Ownable.sol";
import "./SafeMath.sol";
contract Bank is Ownable {
using SafeMath for uint;
mapping(address => Customer) customers;
address[] customerLog;
function registerAccount() public payable {
require(!customers[msg.sender].registered);
customers[msg.sender] = Customer(msg.value, true);
customerLog.push(msg.sender);
}
function deposit() public payable mRegistered {
customers[msg.sender].balance = customers[msg.sender].balance.add(msg.value);
}
function getBalance() public view mRegistered returns (uint) {
return customers[msg.sender].balance;
}
function transfer(address _to, uint _amount) public mRegistered {
require(customers[msg.sender].balance >= _amount, "Insuffient balance!");
payable(_to).transfer(_amount);
customers[msg.sender].balance = customers[msg.sender].balance.sub(_amount);
}
function withdraw(uint _amount) public {
transfer(msg.sender, _amount);
}
function closeBank() public mOwnerOnly {
for (uint i=0; i<customerLog.length; i++) {
if (customers[customerLog[i]].balance > 0) {
payable(customerLog[i]).transfer(customers[customerLog[i]].balance);
}
}
selfdestruct(payable(owner));
}
modifier mRegistered {
require(customers[msg.sender].registered, "You are not registered!");
_;
}
}
Academy-Ownable.sol
// SPDX-License-Identifier: UNLICENCED
pragma solidity 0.8.9;
contract Ownable {
address public owner;
struct Customer {
uint balance;
bool registered;
}
constructor() {
owner = msg.sender;
}
modifier mOwnerOnly {
require(msg.sender == owner, "You are not the owner");
_;
}
}