Hey guys, I have a problem here and I’ve spent hours stairing at the code and can’t for the life of me figure out what’s causing the issue.
For some reason the transfer() function has stopped working and I don’t understand why because it used to work just fine. I can deposit Eth, I can withdraw Eth, but I cannot transfer it. Every time I try I receive an error and the tx is reverted. Anybody know why?
pragma solidity 0.7.5;
import "./Ownable.sol";
import "./Destroyable.sol";
interface GovernmentInterface {
function addTransaction(address _from, address _to, uint _amount) external;
}
contract Bank is Ownable, Destroyable {
GovernmentInterface GovernmentAddress = GovernmentInterface(0xf8e81D47203A594245E36C48e151709F0C19fBe8);
//mapping(keyType => valueType)name;
mapping(address => uint )balance;
event depositDone(uint amount, address indexed depositedTo);
event amountTransferred(address sentFrom, address sentTo, uint amount);
function deposit() public payable {
emit depositDone(msg.value, msg.sender);
balance[msg.sender] += msg.value;
}
function withdraw(uint amount) public onlyOwner {
require(balance[msg.sender] >= amount, "Balance not sufficient");
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 {
emit amountTransferred(msg.sender, recipient, amount);
require(balance[msg.sender] >= amount, "Insufficient funds");
require(msg.sender != recipient, "Funds sent to self");
uint previousSenderBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
GovernmentAddress.addTransaction(msg.sender, recipient, amount);
assert(balance[msg.sender] == previousSenderBalance - amount);
}
function _transfer(address sender, address recipient, uint amount) private {
balance[sender] -= amount;
balance[recipient] += amount;
}
function getContractBalance() public view returns(uint) {
return address(this).balance;
}
}