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的构造函数。
答案 0 :(得分: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
}
}
使用您的代码,它看起来如下所示:
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
}
}