0

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);
    }

}
0

transfer only forwards a very small amount of gas (2300). This is not enough to make a second transfer.

I believe using call in Client should work as a replacement for transfer:

require(_subCompany.call.value(amount)());
  • Please don't use comments to ask new questions. – user19510 Jul 23 '18 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.