Hey @jon_m — thanks again for all the help above! Sorry for the delay, been heads deep in work the past month but now getting my Academy studies back on track
Made updates to my code, would you be able to check these over to let me know if I’m on the right track on this? I totally saw what you mean know about how the onlyOwner
modifier was on the withdraw function…I was wondering why I could only withdraw as the contract deployer but that’s why I’ve updated everything below, so looking forward to hearing what you think.
Bank.sol
// SPDX-License-Identifier: None
pragma solidity 0.7.5;
import "./Ownable.sol";
import "./Destroyable.sol";
contract Bank is Ownable, Destroyable {
mapping(address => uint) balance;
// define event
event depositDone(uint amount, address indexed depositedTo);
event balanceTransfered(uint amount, address indexed transferedFrom, address transferredTo);
event balanceWithdrawn(address indexed withdrawnFrom);
// for anyone to deposit
function deposit() public payable returns (uint) {
balance[msg.sender] += msg.value; // to internally track who deposited money
// add event
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
// function for folks to withdraw eth to their address
function withdraw(uint amount) public returns (uint) {
require(balance[msg.sender] >= amount); // prevents withdraw of others' money
msg.sender.transfer(amount); // run the transfer function
balance[msg.sender] -= amount; // remove amount from balance before transfer occurs
emit balanceWithdrawn(msg.sender);
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);
// governmentInstance.addTransaction(msg.sender, recipient, amount);
// add event
emit balanceTransfered(amount, msg.sender, recipient);
assert(balance[msg.sender] == previousSenderBalance - amount);
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
}
}
Destroyable.sol
// SPDX-License-Identifier: None
pragma solidity 0.7.5;
import "./Ownable.sol";
contract Destroyable is Ownable {
function destroy() public onlyOwner {
address payable receiver = msg.sender;
selfdestruct(receiver);
}
}
Ownable.sol
// SPDX-License-Identifier: None
pragma solidity 0.7.5;
// way to inhert from contract ownable for owner functionality
contract Ownable {
address internal owner;
// modifier of the owner
modifier onlyOwner {
require(msg.sender == owner);
// run the function
_;
}
// sets the owner as owner of contract (msg.sender)
constructor() {
owner = msg.sender;
}
}