Contract-Development

自動將乙太幣從一份合約發送到另一份合約

  • March 7, 2018

每當有人向孩子發送乙太幣時,我想自動將乙太幣從子合約轉移到父合約。

在回退函式中,我嘗試執行該函式,但它給出錯誤:VM 錯誤:恢復。revert 事務已恢復到初始狀態。注意:如果您發送值,則應支付建構子。調試事務以獲取更多資訊。

請告訴我我在哪裡做錯了,我也試圖通過分配更多的氣體來執行它,但沒有運氣。

這是程式碼。

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

}

transfer只轉發非常少量的氣體(2300)。這不足以進行第二次轉移。

我相信使用callinClient應該可以替代transfer

require(_subCompany.call.value(amount)());

引用自:https://ethereum.stackexchange.com/questions/42014