I have the following code, which is malfunctioning, to create a payable contract from another contract.
pragma solidity ^0.4.16;
Contract Factory {
uint contractCount = 0;
mapping(uint => MyContract) public myContracts;
function createContract(uint money) external payable {
require(msg.value >= money);
contractCount++;
// the following line fails
myContracts[contractCount] = new MyContract(money);
}
}
Contract MyContract {
uint money;
function MyContract(uint _money) {
require(msg.value >= _money);
money = _money;
}
}
I am using Remix IDE. I can create an instance of Factory without a problem, however, it fails to create a new MyContract instance when I try createContract(money). I suspect it is because the way to call new MyContract()
is not transferring any value and thus fails the require(msg.value >= _money) in MyContract
constructor.
So how do I create an instance of a payable constructor from a contract?