2
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
    }
}

当用户单击网站上的按钮时,将从 js 应用程序创建 FirstContract。然后我想创建第二个合约的实例并将以太币传递给新合约。我无法弄清楚如何在发送以太币时从第一个合约中调用 SecondContract 的构造函数。

4

1 回答 1

2

编辑:我找到了解决方案:

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
    }
}
于 2017-09-29T11:10:17.573 回答