Hello,
So I have a question. For some reason, when I add the code into the transfer function to send a copy of the transaction to the government contract, it makes the transfer function no longer work properly. No matter what I do it keeps reverting and showing an error message. I have uploaded a screenshot of the error message and pasted my full code below. Any help would be greatly appreciated, thanks!
Bank Contract
pragma solidity 0.7.5;
interface GovernmentInterface{
function addTransaction(address _from, address _to, uint _amount) external;
}
contract Bank {
GovernmentInterface governmentInstance = GovernmentInterface(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);
mapping(address => uint) balance;
event depositDone(uint toAdd, address 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 returns(uint){
require(amount <= balance[msg.sender], "You can't withdraw more than your posted balance");
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, "Insuficient Balance");
require(msg.sender != recipient, "Cannot Transfer to Yourself");
uint previousSenderBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
governmentInstance.addTransaction(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;
}
}
Government Contract
pragma solidity 0.7.5;
contract Government {
struct Transaction {
address from;
address to;
uint amount;
uint id;
}
Transaction[] transactionLog;
function addTransaction(address _from, address _to, uint _amount) external {
transactionLog.push( Transaction(_from, _to, _amount, transactionLog.length) );
}
function getTransaction(uint _index) public view returns(address, address, uint){
return (transactionLog[_index].from, transactionLog[_index].to, transactionLog[_index].amount);
}
}