2017-09-28 5 views
1
contract FirstContract { 

    function createOtherContract() payable returns(address) { 
     // this function is payable. I want to take this 
     // value and use it when creating an instance of 
     // SecondContract 
    } 
} 

contract SecondContract { 
    function SecondContract() payable { 
     // SecondContract's constructor which is also payable 
    } 

    function acceptEther() payable { 
     // Some function which accepts ether 
    } 
} 

FirstContractは、ユーザーがWebサイトのボタンをクリックするとjsアプリケーションから作成されます。私は2番目の契約のインスタンスを作成し、新しい契約に沿ってエーテルを渡したいと思います。最初の契約からSecondContractのコンストラクタを呼び出して、etherを送信する方法を理解できません。他の契約のコンストラクタを使ってethに契約を送信できますか?

+0

あなたは第二の作成分割することができます契約書にEthを2つの別々の陳述に送る。これらの2行のコードを持つ別々の関数を作成するだけです。この関数を呼び出すと、行が1つ失敗すると、トランザクション全体が元に戻ります。 –

答えて

0

編集:私はこのための解決策を見つけた:

pragma solidity ^0.4.0; 

contract B { 
    function B() payable {} 
} 

contract A { 
    address child; 

    function test() { 
     child = (new B).value(10)(); //construct a new B with 10 wei 
    } 
} 

出典:あなたのコードを使用してhttp://solidity.readthedocs.io/en/develop/frequently-asked-questions.html#how-do-i-initialize-a-contract-with-only-a-specific-amount-of-wei

が、それは次のようになります。

pragma solidity ^0.4.0; 

contract FirstContract { 

    function createOtherContract() payable returns(address) { 
     return (new SecondContract).value(msg.value)(); 
    } 
} 

contract SecondContract { 
    function SecondContract() payable { 
    } 

    function acceptEther() payable { 
     // Some function which accepts ether 
    } 
} 
関連する問題