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 的构造函数。
最佳答案
编辑:我找到了解决方案:
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
}
}
关于ethereum - 我可以通过另一个合约的构造函数将 eth 发送到合约吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46478646/