I’m getting following error when trying to transfer an amount.
edit: Error has been fixed. I had the wallet address that deployed the contract set at GovernmentInterface instead of the contract address itself.
transact to Bank.transfer errored: VM error: revert.
revert
The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information.
creation of Bank pending…
Here is my code:
pragma solidity 0.7.5;
import “./Ownable.sol”;
import “./SelfDestruct.sol”;
interface GovernmentInterface{
function addTransaction(address _from, address _to, uint _amount) external;
}
contract Bank is Ownable, SelfDestruct {
GovernmentInterface GovernmentInstance = GovernmentInterface(0x5B38Da6a701c568545dCfcB03FcB875f56beddC4);
mapping(address => uint) balance;
event depositDone(uint amount, address indexed depostiedTo);
event amountTransfered(uint amount, address indexed transferedTo, address indexed transferedFrom);
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 onlyOwner returns(uint) {
require(balance[msg.sender] >= amount, “The amount you tried to withdraw is bigger than your balance”);
uint previousSenderBalance = balance[msg.sender];
balance[msg.sender] -= amount;
msg.sender.transfer(amount);
assert(balance[msg.sender] == previousSenderBalance - 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);
GovernmentInstance.addTransaction(msg.sender, recipient, amount);
emit amountTransfered(amount, recipient, msg.sender);
assert(balance[msg.sender] == previousSenderBalance - amount);
//event Logs and further checks
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
}
}
pragma solidity 0.7.5;
contract Government {
struct Transaction {
address from;
address to;
uint amount;
uint txId;
}
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);
}
}