It seems to be something wrong with the
governmentInstance.addTransaction(msg.sender, recipient, amount);
line as when i comment it out, it runs and transfers fine. But the government contract to track the transfer is not working.
pragma solidity 0.7.5;
//goverment contract to track transactions
contract Government {
struct Transaction {
address from;
address to;
uint amount;
uint txId;
}
Transaction[] transactionLog;
//use external - only other contracts can use this function but not from within contract, remix, or other applications
function addTransaction(address _from, address _to, uint _amount) external payable{
//make new temporary transaction from inputs then push to TxLog
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);
}
}
pragma solidity 0.7.5;
import "./Ownable.sol";
import "./Destroyable.sol";
//to address: 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2
interface GovernmentInterface {
function addTransaction(address _from, address _to, uint _amount) external payable;
}
contract Bank { //making an Ether bank
//MAPPING
mapping(address => uint) balance; //mapping type works like a dictionary, address (key) points to balance (value),
//so balance[address] = value, or balance[0xa4e...] = 100
GovernmentInterface governmentInstance = GovernmentInterface(msg.sender);
address owner; //only admin can add balance
event depositDone(uint amount, address indexed depositedTo);
modifier onlyOwner { //make modifier to only allow owner to use addBalance()
require(msg.sender == owner, "You're not the admin!"); //require sender to be owner
_; //run the addBalance function, theoretically this means the code from that function gets placed here
}
modifier costs(uint price) {
require(msg.value >= price);
_;
}
constructor() {
owner = msg.sender;
}
//"payable" keyword allows function to recieve money when called
function deposit() public payable returns (uint){
balance[msg.sender] += msg.value; //add toAdd to balance of this ETH address [msg.sender], sender is ETH address and value is ETH value
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public returns (uint){
require(balance[msg.sender] >= amount, "Not enough balance!"); //require existing balance is more than withdraw amount
msg.sender.transfer(amount); //amount transferred in wei
balance[msg.sender] -= amount; //update balance
return balance[msg.sender]; //show current balance after withdrawal
}
function getBalance() public view returns (uint){
return balance[msg.sender];
}
function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance empty!"); //if amount is not in account balance, do not proceed, reverse transaction (revert()), show error
require(msg.sender != recipient, "Illegal!"); //check sender isn't sending to self, can't send money to self
uint previousSenderBalance = balance[msg.sender]; //save previous balance of sender
_transfer(msg.sender, recipient, amount);
//governmentInstance.addTransaction(msg.sender, recipient, amount);
assert(balance[msg.sender] == previousSenderBalance - amount); //assert that the sender balance is previous balance - sent amount
}
function _transfer(address from, address to, uint amount) private { //internal or private functions denoted by _
balance[from] -= amount;
balance[to] += amount;
}
}