I want to transfer ether from Child Contract to Parent Contract automatically whenever someone send ether to child.
In fallback function i tried to run the function and it is giving error: VM error: revert. revert The transaction has been reverted to the initial state. Note: The constructor should be payable if you send value. Debug the transaction to get more information.
Please tell me where i am doing wrong, i tried to run it by allocating more gas too, no luck.
Here is the code.
pragma solidity ^0.4.18;
contract ParentCompany{
address public parentAddress;
function ParentCompany() public{
parentAddress = msg.sender;
}
function getB() public view returns(uint){
return this.balance;
}
function() payable {
}
}
// Subsidiary will automatically transfer received ether to Parent.
contract Subsidiary{
address public subsidiaryAddress;
address parentCompany;
function Subsidiary(address _parentCompany) public payable{
subsidiaryAddress = msg.sender;
parentCompany = _parentCompany;
}
function sendToParent() public {
parentCompany.transfer(this.balance);
}
function() payable{
sendToParent();
}
}
// when Fallback function is empty it works fine.
// client will send money to Subsidiary Company
contract Client{
address clientAddress;
function Client() public payable{
clientAddress = msg.sender;
}
function payment(address _subCompany, uint _amount) public{
uint amount = _amount *(10**18);
_subCompany.transfer(amount);
}
}