Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

solidity payable constructor from contract

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?

like image 653
tbraden Avatar asked Jul 23 '26 18:07

tbraden


1 Answers

For those of you still looking to solve this. .value has been deprecated and the example using the new syntax would look like this:

myContracts[contractCount] = (new MyContract){value: msg.value}(money);
like image 129
Andras2000 Avatar answered Jul 28 '26 15:07

Andras2000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!