Ownable.sol:
pragma solidity 0.7.5;
contract Ownable {
address internal owner;
modifier onlyOwner {
require(msg.sender == owner, "Owner Only");
_; //run the function
}
constructor(){
owner = msg.sender;
}
}
Destroyable.sol:
pragma solidity 0.7.5;
import "./Ownable.sol";
contract Destroyable is Ownable {
function destroy () public onlyOwner {
selfdestruct(0x5B38Da6a701c568545dCfcB03FcB875f56beddC4);
}
}
helloworld.sol (Bank):
pragma solidity 0.7.5;
import "Destroyable.sol";
contract Bank is Destroyable {
mapping(address => uint) balance;
event depositDone(uint amount, address indexed depositedTo);
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 payable returns (uint){
require(balance[msg.sender] >=amount, "Balance not sufficient");
uint previousReceiverBalance = balance[msg.sender];
balance[msg.sender] -= amount;
assert(balance[msg.sender] == previousReceiverBalance - amount);
msg.sender.transfer(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);
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
}
}
Seems to work as intended for the practice, but the only additional thing I saw with this is that if any other addresses deposited ether into the contract and then with the owner address select the destroy (selfdestruct) function, it would move all of the funds to the owner address, but then the other addresses would lose that ether.
In theory, say you have the “Owner” and someone from the public named “Bob”. If “Bob” deposited ether into the contract and the “Owner” destroyed it, wouldn’t that mean “Bob” lost his ether?